DXGL r330 - Code Review

Jump to navigation Jump to search
Repository:DXGL
Revision:r329‎ | r330 | r331 >
Date:21:20, 18 February 2013
Author:admin
Status:new
Tags:
Comment:
Add point lights to software T&L.
Fix ambient calculation in point lights in vertex shader.
Modified paths:
  • /ddraw/glDirect3DDevice.cpp (modified) (history)
  • /ddraw/shadergen.cpp (modified) (history)

Diff [purge]

Index: ddraw/glDirect3DDevice.cpp
@@ -2080,6 +2080,11 @@
20812081 return (a[0]*b[0])+(a[1]*b[1])+(a[2]*b[2]);
20822082 }
20832083
 2084+inline float len3(float in[3])
 2085+{
 2086+ return sqrt((in[0]*in[0])+(in[1]*in[1])+(in[2]*in[2]));
 2087+}
 2088+
20842089 INT glDirect3DDevice7::TransformAndLight(D3DTLVERTEX **output, DWORD *outsize, D3DVERTEX *input, WORD start, WORD dest, DWORD count, D3DRECT *extents)
20852090 {
20862091 D3DVALUE dir[3];
@@ -2094,7 +2099,11 @@
20952100 D3DCOLORVALUE color1;
20962101 D3DCOLORVALUE color2;
20972102 D3DVALUE NdotHV;
 2103+ D3DVALUE NdotV;
20982104 D3DVALUE NdotL;
 2105+ D3DVALUE length;
 2106+ D3DVALUE attenuation;
 2107+ D3DVALUE pf;
20992108 in[3] = 1.0f;
21002109 if(transform_dirty) UpdateTransform();
21012110 if(*outsize < (dest+count)*sizeof(D3DTLVERTEX))
@@ -2124,6 +2133,7 @@
21252134 {
21262135 if(gllights[l] != -1)
21272136 {
 2137+ AddD3DCV(&ambient,&lights[gllights[l]]->light.dcvAmbient);
21282138 switch(lights[gllights[l]]->light.dltType)
21292139 {
21302140 case D3DLIGHT_DIRECTIONAL:
@@ -2130,7 +2140,6 @@
21312141 NdotHV = 0;
21322142 memcpy(dir,&lights[gllights[l]]->light.dvDirection,3*sizeof(D3DVALUE));
21332143 normalize(dir);
2134 - AddD3DCV(&ambient,&lights[gllights[l]]->light.dcvAmbient);
21352144 NdotL = max(dot3((float*)&input[i+start].dvNX,(float*)&dir),0.0f);
21362145 color1 = lights[gllights[l]]->light.dcvDiffuse;
21372146 MulD3DCVFloat(&color1,NdotL);
@@ -2138,7 +2147,7 @@
21392148 if((NdotL > 0.0) && (material.dvPower != 0.0))
21402149 {
21412150 __gluMultMatrixVecf(matWorld,&input[i+start].dvX,P);
2142 - memcpy(L ,&lights[gllights[l]]->light.dvDirection,3*sizeof(D3DVALUE));
 2151+ memcpy(L,&lights[gllights[l]]->light.dvDirection,3*sizeof(D3DVALUE));
21432152 NegativeVec3(L);
21442153 SubVec3(L,P);
21452154 normalize(L);
@@ -2153,6 +2162,27 @@
21542163 }
21552164 break;
21562165 case D3DLIGHT_POINT:
 2166+ __gluMultMatrixVecf(matWorld,&input[i+start].dvX,P);
 2167+ memcpy(V,&lights[gllights[l]]->light.dvPosition,3*sizeof(D3DVALUE));
 2168+ SubVec3(V,P);
 2169+ length = len3(V);
 2170+ if((length > lights[gllights[l]]->light.dvRange) && (lights[gllights[l]]->light.dvRange != 0.0)) continue;
 2171+ normalize(V);
 2172+ attenuation = 1.0/(lights[gllights[l]]->light.dvAttenuation0+(length*lights[gllights[l]]->light.dvAttenuation1)
 2173+ +((length*length)*lights[gllights[l]]->light.dvAttenuation2));
 2174+ NdotV = max(0.0,dot3((float*)&input[i+start].dvNX,V));
 2175+ AddVec3(V,eye);
 2176+ normalize(V);
 2177+ NdotHV = max(0.0,dot3((float*)&input[i+start].dvNX,V));
 2178+ if(NdotV == 0.0) pf = 0.0;
 2179+ else if(material.dvPower != 0.0) pf = pow(NdotHV,material.dvPower);
 2180+ else pf = 0.0;
 2181+ color1 = lights[gllights[l]]->light.dcvDiffuse;
 2182+ MulD3DCVFloat(&color1,NdotV*attenuation);
 2183+ AddD3DCV(&diffuse,&color1);
 2184+ color1 = lights[gllights[l]]->light.dcvSpecular;
 2185+ MulD3DCVFloat(&color1,pf*attenuation);
 2186+ AddD3DCV(&specular,&color1);
21572187 break;
21582188 case D3DLIGHT_SPOT:
21592189 break;
Index: ddraw/shadergen.cpp
@@ -339,6 +339,7 @@
340340 }\n";
341341 static const char func_pointlight[] = "void PointLight(in Light light)\n\
342342 {\n\
 343+ambient += light.ambient;\n\
343344 vec4 pos = matWorld*xyzw;\n\
344345 vec3 pos3 = pos.xyz / pos.w;\n\
345346 vec3 V = light.position - pos3;\n\
@@ -353,7 +354,6 @@
354355 else if(gl_FrontMaterial.shininess > 0.0) pf = pow(NdotHV,gl_FrontMaterial.shininess);\n\
355356 else pf = 0.0;\n\
356357 diffuse += light.diffuse*NdotV*attenuation;\n\
357 -ambient += light.ambient;\n\
358358 specular += light.specular*pf*attenuation;\n\
359359 }\n";
360360 static const char func_spotlight[] = "void SpotLight(in Light light)\n\