+ int x, startx = span->startx, endx = span->endx;
+ float Color_Ambient[4], Color_Diffuse[4], Color_Specular[4], Color_Glow[4], Color_Pants[4], Color_Shirt[4], LightColor[4];
+ float CubeVectordata[4];
+ float CubeVectorslope[4];
+ float LightVectordata[4];
+ float LightVectorslope[4];
+ float EyeVectordata[4];
+ float EyeVectorslope[4];
+ float z;
+ float diffusetex[4];
+ float glosstex[4];
+ float surfacenormal[4];
+ float lightnormal[4];
+ float eyenormal[4];
+ float specularnormal[4];
+ float diffuse;
+ float specular;
+ float SpecularPower;
+ float CubeVector[4];
+ float attenuation;
+ int d[4];
+ Color_Glow[2] = thread->uniform4f[DPSOFTRAST_UNIFORM_Color_Glow*4+0];
+ Color_Glow[1] = thread->uniform4f[DPSOFTRAST_UNIFORM_Color_Glow*4+1];
+ Color_Glow[0] = thread->uniform4f[DPSOFTRAST_UNIFORM_Color_Glow*4+2];
+ Color_Glow[3] = 0.0f;
+ Color_Ambient[2] = thread->uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+0];
+ Color_Ambient[1] = thread->uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+1];
+ Color_Ambient[0] = thread->uniform4f[DPSOFTRAST_UNIFORM_Color_Ambient*4+2];
+ Color_Ambient[3] = thread->uniform4f[DPSOFTRAST_UNIFORM_Alpha*4+0];
+ Color_Diffuse[2] = thread->uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+0];
+ Color_Diffuse[1] = thread->uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+1];
+ Color_Diffuse[0] = thread->uniform4f[DPSOFTRAST_UNIFORM_Color_Diffuse*4+2];
+ Color_Diffuse[3] = 0.0f;
+ Color_Specular[2] = thread->uniform4f[DPSOFTRAST_UNIFORM_Color_Specular*4+0];
+ Color_Specular[1] = thread->uniform4f[DPSOFTRAST_UNIFORM_Color_Specular*4+1];
+ Color_Specular[0] = thread->uniform4f[DPSOFTRAST_UNIFORM_Color_Specular*4+2];
+ Color_Specular[3] = 0.0f;
+ Color_Pants[2] = thread->uniform4f[DPSOFTRAST_UNIFORM_Color_Pants*4+0];
+ Color_Pants[1] = thread->uniform4f[DPSOFTRAST_UNIFORM_Color_Pants*4+1];
+ Color_Pants[0] = thread->uniform4f[DPSOFTRAST_UNIFORM_Color_Pants*4+2];
+ Color_Pants[3] = 0.0f;
+ Color_Shirt[2] = thread->uniform4f[DPSOFTRAST_UNIFORM_Color_Shirt*4+0];
+ Color_Shirt[1] = thread->uniform4f[DPSOFTRAST_UNIFORM_Color_Shirt*4+1];
+ Color_Shirt[0] = thread->uniform4f[DPSOFTRAST_UNIFORM_Color_Shirt*4+2];
+ Color_Shirt[3] = 0.0f;
+ LightColor[2] = thread->uniform4f[DPSOFTRAST_UNIFORM_LightColor*4+0];
+ LightColor[1] = thread->uniform4f[DPSOFTRAST_UNIFORM_LightColor*4+1];
+ LightColor[0] = thread->uniform4f[DPSOFTRAST_UNIFORM_LightColor*4+2];
+ LightColor[3] = 0.0f;
+ SpecularPower = thread->uniform4f[DPSOFTRAST_UNIFORM_SpecularPower*4+0] * (1.0f / 255.0f);
+ DPSOFTRAST_CALCATTRIB4F(triangle, span, LightVectordata, LightVectorslope, DPSOFTRAST_ARRAY_TEXCOORD1);
+ DPSOFTRAST_CALCATTRIB4F(triangle, span, EyeVectordata, EyeVectorslope, DPSOFTRAST_ARRAY_TEXCOORD2);
+ DPSOFTRAST_CALCATTRIB4F(triangle, span, CubeVectordata, CubeVectorslope, DPSOFTRAST_ARRAY_TEXCOORD3);
+ DPSOFTRAST_Draw_Span_Begin(thread, triangle, span, buffer_z);
+ memset(buffer_FragColorbgra8 + startx*4, 0, (endx-startx)*4); // clear first, because we skip writing black pixels, and there are a LOT of them...
+ DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(thread, triangle, span, buffer_texture_colorbgra8, GL20TU_COLOR, DPSOFTRAST_ARRAY_TEXCOORD0, buffer_z);
+ if (thread->shader_permutation & SHADERPERMUTATION_COLORMAPPING)
+ {
+ DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(thread, triangle, span, buffer_texture_pantsbgra8, GL20TU_PANTS, DPSOFTRAST_ARRAY_TEXCOORD0, buffer_z);
+ DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(thread, triangle, span, buffer_texture_shirtbgra8, GL20TU_SHIRT, DPSOFTRAST_ARRAY_TEXCOORD0, buffer_z);
+ }
+ if (thread->shader_permutation & SHADERPERMUTATION_CUBEFILTER)
+ DPSOFTRAST_Draw_Span_TextureCubeVaryingBGRA8(triangle, span, buffer_texture_cubebgra8, GL20TU_CUBE, DPSOFTRAST_ARRAY_TEXCOORD3, buffer_z);
+ if (thread->shader_permutation & SHADERPERMUTATION_SPECULAR)
+ {
+ DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(thread, triangle, span, buffer_texture_normalbgra8, GL20TU_NORMAL, DPSOFTRAST_ARRAY_TEXCOORD0, buffer_z);
+ DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(thread, triangle, span, buffer_texture_glossbgra8, GL20TU_GLOSS, DPSOFTRAST_ARRAY_TEXCOORD0, buffer_z);
+ for (x = startx;x < endx;x++)
+ {
+ z = buffer_z[x];
+ CubeVector[0] = (CubeVectordata[0] + CubeVectorslope[0]*x) * z;
+ CubeVector[1] = (CubeVectordata[1] + CubeVectorslope[1]*x) * z;
+ CubeVector[2] = (CubeVectordata[2] + CubeVectorslope[2]*x) * z;
+ attenuation = 1.0f - DPSOFTRAST_Vector3LengthSquared(CubeVector);
+ if (attenuation < 0.01f)
+ continue;
+ if (thread->shader_permutation & SHADERPERMUTATION_SHADOWMAP2D)
+ {
+ attenuation *= DPSOFTRAST_SampleShadowmap(CubeVector);
+ if (attenuation < 0.01f)
+ continue;
+ }
+
+ diffusetex[0] = buffer_texture_colorbgra8[x*4+0];
+ diffusetex[1] = buffer_texture_colorbgra8[x*4+1];
+ diffusetex[2] = buffer_texture_colorbgra8[x*4+2];
+ diffusetex[3] = buffer_texture_colorbgra8[x*4+3];
+ if (thread->shader_permutation & SHADERPERMUTATION_COLORMAPPING)
+ {
+ diffusetex[0] += buffer_texture_pantsbgra8[x*4+0] * Color_Pants[0] + buffer_texture_shirtbgra8[x*4+0] * Color_Shirt[0];
+ diffusetex[1] += buffer_texture_pantsbgra8[x*4+1] * Color_Pants[1] + buffer_texture_shirtbgra8[x*4+1] * Color_Shirt[1];
+ diffusetex[2] += buffer_texture_pantsbgra8[x*4+2] * Color_Pants[2] + buffer_texture_shirtbgra8[x*4+2] * Color_Shirt[2];
+ diffusetex[3] += buffer_texture_pantsbgra8[x*4+3] * Color_Pants[3] + buffer_texture_shirtbgra8[x*4+3] * Color_Shirt[3];
+ }
+ glosstex[0] = buffer_texture_glossbgra8[x*4+0];
+ glosstex[1] = buffer_texture_glossbgra8[x*4+1];
+ glosstex[2] = buffer_texture_glossbgra8[x*4+2];
+ glosstex[3] = buffer_texture_glossbgra8[x*4+3];
+ surfacenormal[0] = buffer_texture_normalbgra8[x*4+2] * (1.0f / 128.0f) - 1.0f;
+ surfacenormal[1] = buffer_texture_normalbgra8[x*4+1] * (1.0f / 128.0f) - 1.0f;
+ surfacenormal[2] = buffer_texture_normalbgra8[x*4+0] * (1.0f / 128.0f) - 1.0f;
+ DPSOFTRAST_Vector3Normalize(surfacenormal);
+
+ lightnormal[0] = (LightVectordata[0] + LightVectorslope[0]*x) * z;
+ lightnormal[1] = (LightVectordata[1] + LightVectorslope[1]*x) * z;
+ lightnormal[2] = (LightVectordata[2] + LightVectorslope[2]*x) * z;
+ DPSOFTRAST_Vector3Normalize(lightnormal);
+
+ eyenormal[0] = (EyeVectordata[0] + EyeVectorslope[0]*x) * z;
+ eyenormal[1] = (EyeVectordata[1] + EyeVectorslope[1]*x) * z;
+ eyenormal[2] = (EyeVectordata[2] + EyeVectorslope[2]*x) * z;
+ DPSOFTRAST_Vector3Normalize(eyenormal);
+
+ specularnormal[0] = lightnormal[0] + eyenormal[0];
+ specularnormal[1] = lightnormal[1] + eyenormal[1];
+ specularnormal[2] = lightnormal[2] + eyenormal[2];
+ DPSOFTRAST_Vector3Normalize(specularnormal);
+
+ diffuse = DPSOFTRAST_Vector3Dot(surfacenormal, lightnormal);if (diffuse < 0.0f) diffuse = 0.0f;
+ specular = DPSOFTRAST_Vector3Dot(surfacenormal, specularnormal);if (specular < 0.0f) specular = 0.0f;
+ specular = pow(specular, SpecularPower * glosstex[3]);
+ if (thread->shader_permutation & SHADERPERMUTATION_CUBEFILTER)
+ {
+ // scale down the attenuation to account for the cubefilter multiplying everything by 255
+ attenuation *= (1.0f / 255.0f);
+ d[0] = (int)((diffusetex[0] * (Color_Ambient[0] + Color_Diffuse[0] * diffuse) + glosstex[0] * Color_Specular[0] * specular) * LightColor[0] * buffer_texture_cubebgra8[x*4+0] * attenuation);if (d[0] > 255) d[0] = 255;
+ d[1] = (int)((diffusetex[1] * (Color_Ambient[1] + Color_Diffuse[1] * diffuse) + glosstex[1] * Color_Specular[1] * specular) * LightColor[1] * buffer_texture_cubebgra8[x*4+1] * attenuation);if (d[1] > 255) d[1] = 255;
+ d[2] = (int)((diffusetex[2] * (Color_Ambient[2] + Color_Diffuse[2] * diffuse) + glosstex[2] * Color_Specular[2] * specular) * LightColor[2] * buffer_texture_cubebgra8[x*4+2] * attenuation);if (d[2] > 255) d[2] = 255;
+ d[3] = (int)( diffusetex[3] );if (d[3] > 255) d[3] = 255;
+ }
+ else
+ {
+ d[0] = (int)((diffusetex[0] * (Color_Ambient[0] + Color_Diffuse[0] * diffuse) + glosstex[0] * Color_Specular[0] * specular) * LightColor[0] * attenuation);if (d[0] > 255) d[0] = 255;
+ d[1] = (int)((diffusetex[1] * (Color_Ambient[1] + Color_Diffuse[1] * diffuse) + glosstex[1] * Color_Specular[1] * specular) * LightColor[1] * attenuation);if (d[1] > 255) d[1] = 255;
+ d[2] = (int)((diffusetex[2] * (Color_Ambient[2] + Color_Diffuse[2] * diffuse) + glosstex[2] * Color_Specular[2] * specular) * LightColor[2] * attenuation);if (d[2] > 255) d[2] = 255;
+ d[3] = (int)( diffusetex[3] );if (d[3] > 255) d[3] = 255;
+ }
+ buffer_FragColorbgra8[x*4+0] = d[0];
+ buffer_FragColorbgra8[x*4+1] = d[1];
+ buffer_FragColorbgra8[x*4+2] = d[2];
+ buffer_FragColorbgra8[x*4+3] = d[3];
+ }
+ }
+ else if (thread->shader_permutation & SHADERPERMUTATION_DIFFUSE)
+ {
+ DPSOFTRAST_Draw_Span_Texture2DVaryingBGRA8(thread, triangle, span, buffer_texture_normalbgra8, GL20TU_NORMAL, DPSOFTRAST_ARRAY_TEXCOORD0, buffer_z);
+ for (x = startx;x < endx;x++)
+ {
+ z = buffer_z[x];
+ CubeVector[0] = (CubeVectordata[0] + CubeVectorslope[0]*x) * z;
+ CubeVector[1] = (CubeVectordata[1] + CubeVectorslope[1]*x) * z;
+ CubeVector[2] = (CubeVectordata[2] + CubeVectorslope[2]*x) * z;
+ attenuation = 1.0f - DPSOFTRAST_Vector3LengthSquared(CubeVector);
+ if (attenuation < 0.01f)
+ continue;
+ if (thread->shader_permutation & SHADERPERMUTATION_SHADOWMAP2D)
+ {
+ attenuation *= DPSOFTRAST_SampleShadowmap(CubeVector);
+ if (attenuation < 0.01f)
+ continue;
+ }
+
+ diffusetex[0] = buffer_texture_colorbgra8[x*4+0];
+ diffusetex[1] = buffer_texture_colorbgra8[x*4+1];
+ diffusetex[2] = buffer_texture_colorbgra8[x*4+2];
+ diffusetex[3] = buffer_texture_colorbgra8[x*4+3];
+ if (thread->shader_permutation & SHADERPERMUTATION_COLORMAPPING)
+ {
+ diffusetex[0] += buffer_texture_pantsbgra8[x*4+0] * Color_Pants[0] + buffer_texture_shirtbgra8[x*4+0] * Color_Shirt[0];
+ diffusetex[1] += buffer_texture_pantsbgra8[x*4+1] * Color_Pants[1] + buffer_texture_shirtbgra8[x*4+1] * Color_Shirt[1];
+ diffusetex[2] += buffer_texture_pantsbgra8[x*4+2] * Color_Pants[2] + buffer_texture_shirtbgra8[x*4+2] * Color_Shirt[2];
+ diffusetex[3] += buffer_texture_pantsbgra8[x*4+3] * Color_Pants[3] + buffer_texture_shirtbgra8[x*4+3] * Color_Shirt[3];
+ }
+ surfacenormal[0] = buffer_texture_normalbgra8[x*4+2] * (1.0f / 128.0f) - 1.0f;
+ surfacenormal[1] = buffer_texture_normalbgra8[x*4+1] * (1.0f / 128.0f) - 1.0f;
+ surfacenormal[2] = buffer_texture_normalbgra8[x*4+0] * (1.0f / 128.0f) - 1.0f;
+ DPSOFTRAST_Vector3Normalize(surfacenormal);
+
+ lightnormal[0] = (LightVectordata[0] + LightVectorslope[0]*x) * z;
+ lightnormal[1] = (LightVectordata[1] + LightVectorslope[1]*x) * z;
+ lightnormal[2] = (LightVectordata[2] + LightVectorslope[2]*x) * z;
+ DPSOFTRAST_Vector3Normalize(lightnormal);
+
+ diffuse = DPSOFTRAST_Vector3Dot(surfacenormal, lightnormal);if (diffuse < 0.0f) diffuse = 0.0f;
+ if (thread->shader_permutation & SHADERPERMUTATION_CUBEFILTER)
+ {
+ // scale down the attenuation to account for the cubefilter multiplying everything by 255
+ attenuation *= (1.0f / 255.0f);
+ d[0] = (int)((diffusetex[0] * (Color_Ambient[0] + Color_Diffuse[0] * diffuse)) * LightColor[0] * buffer_texture_cubebgra8[x*4+0] * attenuation);if (d[0] > 255) d[0] = 255;
+ d[1] = (int)((diffusetex[1] * (Color_Ambient[1] + Color_Diffuse[1] * diffuse)) * LightColor[1] * buffer_texture_cubebgra8[x*4+1] * attenuation);if (d[1] > 255) d[1] = 255;
+ d[2] = (int)((diffusetex[2] * (Color_Ambient[2] + Color_Diffuse[2] * diffuse)) * LightColor[2] * buffer_texture_cubebgra8[x*4+2] * attenuation);if (d[2] > 255) d[2] = 255;
+ d[3] = (int)( diffusetex[3] );if (d[3] > 255) d[3] = 255;
+ }
+ else
+ {
+ d[0] = (int)((diffusetex[0] * (Color_Ambient[0] + Color_Diffuse[0] * diffuse)) * LightColor[0] * attenuation);if (d[0] > 255) d[0] = 255;
+ d[1] = (int)((diffusetex[1] * (Color_Ambient[1] + Color_Diffuse[1] * diffuse)) * LightColor[1] * attenuation);if (d[1] > 255) d[1] = 255;
+ d[2] = (int)((diffusetex[2] * (Color_Ambient[2] + Color_Diffuse[2] * diffuse)) * LightColor[2] * attenuation);if (d[2] > 255) d[2] = 255;
+ d[3] = (int)( diffusetex[3] );if (d[3] > 255) d[3] = 255;
+ }
+ buffer_FragColorbgra8[x*4+0] = d[0];
+ buffer_FragColorbgra8[x*4+1] = d[1];
+ buffer_FragColorbgra8[x*4+2] = d[2];
+ buffer_FragColorbgra8[x*4+3] = d[3];
+ }
+ }
+ else
+ {
+ for (x = startx;x < endx;x++)
+ {
+ z = buffer_z[x];
+ CubeVector[0] = (CubeVectordata[0] + CubeVectorslope[0]*x) * z;
+ CubeVector[1] = (CubeVectordata[1] + CubeVectorslope[1]*x) * z;
+ CubeVector[2] = (CubeVectordata[2] + CubeVectorslope[2]*x) * z;
+ attenuation = 1.0f - DPSOFTRAST_Vector3LengthSquared(CubeVector);
+ if (attenuation < 0.01f)
+ continue;
+ if (thread->shader_permutation & SHADERPERMUTATION_SHADOWMAP2D)
+ {
+ attenuation *= DPSOFTRAST_SampleShadowmap(CubeVector);
+ if (attenuation < 0.01f)
+ continue;
+ }
+
+ diffusetex[0] = buffer_texture_colorbgra8[x*4+0];
+ diffusetex[1] = buffer_texture_colorbgra8[x*4+1];
+ diffusetex[2] = buffer_texture_colorbgra8[x*4+2];
+ diffusetex[3] = buffer_texture_colorbgra8[x*4+3];
+ if (thread->shader_permutation & SHADERPERMUTATION_COLORMAPPING)
+ {
+ diffusetex[0] += buffer_texture_pantsbgra8[x*4+0] * Color_Pants[0] + buffer_texture_shirtbgra8[x*4+0] * Color_Shirt[0];
+ diffusetex[1] += buffer_texture_pantsbgra8[x*4+1] * Color_Pants[1] + buffer_texture_shirtbgra8[x*4+1] * Color_Shirt[1];
+ diffusetex[2] += buffer_texture_pantsbgra8[x*4+2] * Color_Pants[2] + buffer_texture_shirtbgra8[x*4+2] * Color_Shirt[2];
+ diffusetex[3] += buffer_texture_pantsbgra8[x*4+3] * Color_Pants[3] + buffer_texture_shirtbgra8[x*4+3] * Color_Shirt[3];
+ }
+ if (thread->shader_permutation & SHADERPERMUTATION_CUBEFILTER)
+ {
+ // scale down the attenuation to account for the cubefilter multiplying everything by 255
+ attenuation *= (1.0f / 255.0f);
+ d[0] = (int)((diffusetex[0] * (Color_Ambient[0])) * LightColor[0] * buffer_texture_cubebgra8[x*4+0] * attenuation);if (d[0] > 255) d[0] = 255;
+ d[1] = (int)((diffusetex[1] * (Color_Ambient[1])) * LightColor[1] * buffer_texture_cubebgra8[x*4+1] * attenuation);if (d[1] > 255) d[1] = 255;
+ d[2] = (int)((diffusetex[2] * (Color_Ambient[2])) * LightColor[2] * buffer_texture_cubebgra8[x*4+2] * attenuation);if (d[2] > 255) d[2] = 255;
+ d[3] = (int)( diffusetex[3] );if (d[3] > 255) d[3] = 255;
+ }
+ else
+ {
+ d[0] = (int)((diffusetex[0] * (Color_Ambient[0])) * LightColor[0] * attenuation);if (d[0] > 255) d[0] = 255;
+ d[1] = (int)((diffusetex[1] * (Color_Ambient[1])) * LightColor[1] * attenuation);if (d[1] > 255) d[1] = 255;
+ d[2] = (int)((diffusetex[2] * (Color_Ambient[2])) * LightColor[2] * attenuation);if (d[2] > 255) d[2] = 255;
+ d[3] = (int)( diffusetex[3] );if (d[3] > 255) d[3] = 255;
+ }
+ buffer_FragColorbgra8[x*4+0] = d[0];
+ buffer_FragColorbgra8[x*4+1] = d[1];
+ buffer_FragColorbgra8[x*4+2] = d[2];
+ buffer_FragColorbgra8[x*4+3] = d[3];
+ }
+ }
+ DPSOFTRAST_Draw_Span_FinishBGRA8(thread, triangle, span, buffer_FragColorbgra8);
+#endif