DXGL r215 - Code Review

Jump to navigation Jump to search
Repository:DXGL
Revision:r214‎ | r215 | r216 >
Date:18:06, 15 July 2012
Author:admin
Status:new
Tags:
Comment:
Move matrices to gl_ModelViewMartix and gl_ProjectionMatrix
Move material to gl_FrontMaterial.
Modified paths:
  • /ddraw/glDirect3DDevice.cpp (modified) (history)
  • /ddraw/glDirect3DDevice.h (modified) (history)
  • /ddraw/glRenderer.cpp (modified) (history)
  • /ddraw/glutil.cpp (modified) (history)
  • /ddraw/glutil.h (modified) (history)
  • /ddraw/shadergen.cpp (modified) (history)

Diff [purge]

Index: ddraw/glDirect3DDevice.cpp
@@ -228,13 +228,13 @@
229229 __gluMakeIdentityf(matWorld);
230230 __gluMakeIdentityf(matView);
231231 __gluMakeIdentityf(matProjection);
232 - __gluMakeIdentityf(matNormal);
233232 texstages[0] = texstagedefault0;
234233 texstages[1] = texstages[2] = texstages[3] = texstages[4] =
235234 texstages[5] = texstages[6] = texstages[7] = texstagedefault1;
236235 refcount = 1;
237236 inscene = false;
238 - normal_dirty = false;
 237+ modelview_dirty = false;
 238+ projection_dirty = false;
239239 this->glD3D7 = glD3D7;
240240 glD3D7->AddRef();
241241 this->glDDS7 = glDDS7;
@@ -1197,14 +1197,15 @@
11981198 {
11991199 case D3DTRANSFORMSTATE_WORLD:
12001200 memcpy(&matWorld,lpD3DMatrix,sizeof(D3DMATRIX));
1201 - normal_dirty = true;
 1201+ modelview_dirty = true;
12021202 return D3D_OK;
12031203 case D3DTRANSFORMSTATE_VIEW:
12041204 memcpy(&matView,lpD3DMatrix,sizeof(D3DMATRIX));
1205 - normal_dirty = true;
 1205+ modelview_dirty = true;
12061206 return D3D_OK;
12071207 case D3DTRANSFORMSTATE_PROJECTION:
12081208 memcpy(&matProjection,lpD3DMatrix,sizeof(D3DMATRIX));
 1209+ projection_dirty = true;
12091210 return D3D_OK;
12101211 default:
12111212 ERR(DDERR_INVALIDPARAMS);
@@ -1263,30 +1264,6 @@
12641265 return D3D_OK;
12651266 }
12661267
1267 -void glDirect3DDevice7::UpdateNormalMatrix()
1268 -{
1269 - GLfloat worldview[16];
1270 - GLfloat tmp[16];
1271 -
1272 - ZeroMemory(&worldview,sizeof(D3DMATRIX));
1273 - ZeroMemory(&tmp,sizeof(D3DMATRIX));
1274 - __gluMultMatricesf(matWorld,matView,worldview); // Get worldview
1275 - if(__gluInvertMatrixf(worldview,tmp)) // Invert
1276 - {
1277 - memcpy(matNormal,tmp,3*sizeof(GLfloat));
1278 - memcpy(matNormal+3,tmp+4,3*sizeof(GLfloat));
1279 - memcpy(matNormal+6,tmp+8,3*sizeof(GLfloat));
1280 - }
1281 - else
1282 - {
1283 - memcpy(matNormal,worldview,3*sizeof(GLfloat));
1284 - memcpy(matNormal+3,worldview+4,3*sizeof(GLfloat));
1285 - memcpy(matNormal+6,worldview+8,3*sizeof(GLfloat));
1286 - }
1287 -
1288 - normal_dirty = false;
1289 -}
1290 -
12911268 void glDirect3DDevice7::SetDepthComp()
12921269 {
12931270 switch(renderstate[D3DRENDERSTATE_ZFUNC])
Index: ddraw/glDirect3DDevice.h
@@ -141,12 +141,9 @@
142142 HRESULT SetLightState(D3DLIGHTSTATETYPE dwLightStateType, DWORD dwLightState);
143143 HRESULT GetStats(LPD3DSTATS lpD3DStats);
144144 __int64 SelectShader(GLVERTEX *VertexType);
145 - void UpdateNormalMatrix();
146145 GLfloat matWorld[16];
147146 GLfloat matView[16];
148147 GLfloat matProjection[16];
149 - GLfloat matNormal[9];
150 - bool normal_dirty;
151148 D3DMATERIAL7 material;
152149 D3DVIEWPORT7 viewport;
153150 glDirect3DLight **lights;
@@ -158,7 +155,8 @@
159156 glDirect3DMaterial3 **materials;
160157 glDirect3DMaterial3 *currentmaterial;
161158 int materialcount;
162 -
 159+ bool modelview_dirty;
 160+ bool projection_dirty;
163161 private:
164162 HRESULT fvftoglvertex(DWORD dwVertexTypeDesc,LPDWORD vertptr);
165163 ULONG refcount;
Index: ddraw/glRenderer.cpp
@@ -792,6 +792,7 @@
793793 glViewport(0,0,width,height);
794794 DepthWrite(true);
795795 DepthTest(false);
 796+ MatrixMode(GL_MODELVIEW);
796797 glDisable(GL_DEPTH_TEST);
797798 SetDepthComp(GL_LESS);
798799 const GLubyte *glver = glGetString(GL_VERSION);
@@ -1213,7 +1214,13 @@
12141215 GLfloat ambient[] = {0.0,0.0,0.0,0.0};
12151216 if(zbuffer) DepthTest(true);
12161217 SetDepthComp(GL_LEQUAL);
1217 - glDisable(GL_DITHER);
 1218+ GLfloat identity[16];
 1219+ __gluMakeIdentityf(identity);
 1220+ SetMatrix(GL_MODELVIEW,identity,identity,NULL);
 1221+ SetMatrix(GL_PROJECTION,identity,NULL,NULL);
 1222+ GLfloat one[4] = {1,1,1,1};
 1223+ GLfloat zero[4] = {0,0,0,1};
 1224+ SetMaterial(one,one,zero,zero,0);
12181225 }
12191226
12201227 void glRenderer::_Clear(glDirectDrawSurface7 *target, DWORD dwCount, LPD3DRECT lpRects, DWORD dwFlags, DWORD dwColor, D3DVALUE dvZ, DWORD dwStencil)
@@ -1401,17 +1408,12 @@
14021409
14031410 }
14041411 }
1405 - if(device->normal_dirty) device->UpdateNormalMatrix();
1406 - if(prog.uniforms[0] != -1) glUniformMatrix4fv(prog.uniforms[0],1,false,device->matWorld);
1407 - if(prog.uniforms[1] != -1) glUniformMatrix4fv(prog.uniforms[1],1,false,device->matView);
1408 - if(prog.uniforms[2] != -1) glUniformMatrix4fv(prog.uniforms[2],1,false,device->matProjection);
1409 - if(prog.uniforms[3] != -1) glUniformMatrix3fv(prog.uniforms[3],1,true,device->matNormal);
 1412+ if(device->modelview_dirty) SetMatrix(GL_MODELVIEW,device->matView,device->matWorld,&device->modelview_dirty);
 1413+ if(device->projection_dirty) SetMatrix(GL_PROJECTION,device->matProjection,NULL,&device->projection_dirty);
14101414
1411 - if(prog.uniforms[15] != -1) glUniform4fv(prog.uniforms[15],1,(GLfloat*)&device->material.ambient);
1412 - if(prog.uniforms[16] != -1) glUniform4fv(prog.uniforms[16],1,(GLfloat*)&device->material.diffuse);
1413 - if(prog.uniforms[17] != -1) glUniform4fv(prog.uniforms[17],1,(GLfloat*)&device->material.specular);
1414 - if(prog.uniforms[18] != -1) glUniform4fv(prog.uniforms[18],1,(GLfloat*)&device->material.emissive);
1415 - if(prog.uniforms[19] != -1) glUniform1f(prog.uniforms[19],device->material.power);
 1415+ SetMaterial((GLfloat*)&device->material.ambient,(GLfloat*)&device->material.diffuse,(GLfloat*)&device->material.specular,
 1416+ (GLfloat*)&device->material.emissive,device->material.power);
 1417+
14161418 int lightindex = 0;
14171419 char lightname[] = "lightX.xxxxxxxxxxxxxxxx";
14181420 for(i = 0; i < 8; i++)
Index: ddraw/glutil.cpp
@@ -29,6 +29,12 @@
3030 GLint scissory = 0;
3131 GLsizei scissorwidth = 0;
3232 GLsizei scissorheight = 0;
 33+GLenum matrixmode = GL_MODELVIEW;
 34+GLfloat materialambient[4] = {0,0,0,0};
 35+GLfloat materialdiffuse[4] = {0,0,0,0};
 36+GLfloat materialspecular[4] = {0,0,0,0};
 37+GLfloat materialemission[4] = {0,0,0,0};
 38+GLfloat materialshininess = 0;
3339 bool scissorenabled = false;
3440 bool stencil = false;
3541 GLint texlevel = 0;
@@ -211,4 +217,50 @@
212218 scissorheight = height;
213219 glScissor(x,y,width,height);
214220 }
 221+}
 222+
 223+void MatrixMode(GLenum mode)
 224+{
 225+ if(mode != matrixmode)
 226+ {
 227+ matrixmode = mode;
 228+ glMatrixMode(mode);
 229+ }
 230+}
 231+
 232+void SetMatrix(GLenum mode, GLfloat *mat1, GLfloat *mat2, bool *dirty)
 233+{
 234+ MatrixMode(mode);
 235+ glLoadMatrixf(mat1);
 236+ if(mode == GL_MODELVIEW) glMultMatrixf(mat2);
 237+ if(dirty) *dirty = false;
 238+}
 239+
 240+void SetMaterial(GLfloat ambient[4],GLfloat diffuse[4],GLfloat specular[4],GLfloat emission[4],GLfloat shininess)
 241+{
 242+ if(ambient != materialambient)
 243+ {
 244+ memcpy(materialambient,ambient,4*sizeof(GLfloat));
 245+ glMaterialfv(GL_FRONT,GL_AMBIENT,ambient);
 246+ }
 247+ if(diffuse != materialdiffuse)
 248+ {
 249+ memcpy(materialdiffuse,diffuse,4*sizeof(GLfloat));
 250+ glMaterialfv(GL_FRONT,GL_DIFFUSE,diffuse);
 251+ }
 252+ if(specular != materialspecular)
 253+ {
 254+ memcpy(materialspecular,specular,4*sizeof(GLfloat));
 255+ glMaterialfv(GL_FRONT,GL_SPECULAR,specular);
 256+ }
 257+ if(emission != materialemission)
 258+ {
 259+ memcpy(materialemission,emission,4*sizeof(GLfloat));
 260+ glMaterialfv(GL_FRONT,GL_EMISSION,emission);
 261+ }
 262+ if(shininess != materialshininess)
 263+ {
 264+ materialshininess = shininess;
 265+ glMaterialf(GL_FRONT,GL_SHININESS,shininess);
 266+ }
215267 }
\ No newline at end of file
Index: ddraw/glutil.h
@@ -33,5 +33,8 @@
3434 void DepthWrite(bool enabled);
3535 void DepthTest(bool enabled);
3636 void SetScissor(bool enabled, GLint x, GLint y, GLsizei width, GLsizei height);
 37+void SetMatrix(GLenum mode, GLfloat *mat1, GLfloat *mat2, bool *dirty);
 38+void MatrixMode(GLenum mode);
 39+void SetMaterial(GLfloat ambient[4],GLfloat diffuse[4],GLfloat specular[4],GLfloat emission[4],GLfloat shininess);
3740
3841 #endif //_GLUTIL_H
\ No newline at end of file
Index: ddraw/shadergen.cpp
@@ -237,19 +237,6 @@
238238 static const char conv_str[] = "vec4 strqX = vec4(strX[0],strX[1],strX[2],1);\n";
239239 static const char attr_strq[] = "attribute vec4 strqX;\n";
240240 // Uniforms
241 -static const char unif_matrices[] = "uniform mat4 world;\n\
242 -uniform mat4 view;\n\
243 -uniform mat4 projection;\n\
244 -uniform mat3 normalmat;\n";
245 -static const char unif_material[] = "struct Material\n\
246 -{\n\
247 -vec4 diffuse;\n\
248 -vec4 ambient;\n\
249 -vec4 specular;\n\
250 -vec4 emissive;\n\
251 -float power;\n\
252 -};\n\
253 -uniform Material material;\n";
254241 static const char lightstruct[] = "struct Light\n\
255242 {\n\
256243 vec4 diffuse;\n\
@@ -280,10 +267,10 @@
281268 static const char var_xyzw[] = "vec4 xyzw;\n";
282269 // Operations
283270 static const char op_transform[] = "xyzw = vec4(xyz,1);\n\
284 -vec4 pos = (projection*(view*world))*xyzw;\n\
 271+vec4 pos = gl_ModelViewProjectionMatrix*xyzw;\n\
285272 gl_Position = vec4(pos.x,-pos.y,pos.z,pos.w);\n";
286 -static const char op_normalize[] = "N = normalize(normalmat*nxyz);\n";
287 -static const char op_normalpassthru[] = "N = normalmat*nxyz;\n";
 273+static const char op_normalize[] = "N = normalize(gl_NormalMatrix*nxyz);\n";
 274+static const char op_normalpassthru[] = "N = gl_NormalMatrix*nxyz;\n";
288275 static const char op_passthru[] = "gl_Position = vec4((xyz.x/(width/2.0))-1.0,(xyz.y/(height/2.0))-1.0,xyz.z,1.0/rhw);\n";
289276 static const char op_resetcolor[] = "diffuse = specular = vec4(0.0);\n\
290277 ambient = ambientcolor / 255.0;\n";
@@ -290,9 +277,9 @@
291278 static const char op_dirlight[] = "DirLight(lightX);\n";
292279 static const char op_pointlight[] = "PointLight(lightX);\n";
293280 static const char op_spotlight[] = "SpotLight(lightX);\n";
294 -static const char op_colorout[] = "gl_FrontColor = (material.diffuse * diffuse) + (material.ambient * ambient)\n\
295 -+ (material.specular * specular) + material.emissive;\n\
296 -gl_FrontSecondaryColor = (material.specular * specular);\n";
 281+static const char op_colorout[] = "gl_FrontColor = (gl_FrontMaterial.diffuse * diffuse) + (gl_FrontMaterial.ambient * ambient)\n\
 282++ (gl_FrontMaterial.specular * specular) + gl_FrontMaterial.emission;\n\
 283+gl_FrontSecondaryColor = (gl_FrontMaterial.specular * specular);\n";
297284 static const char op_colorvert[] = "gl_FrontColor = rgba0.bgra;\n";
298285 static const char op_color2vert[] = "gl_FrontSecondaryColor = rgba1.bgra;\n";
299286 static const char op_colorfragout[] = "gl_FragColor = vec4(color,alpha);\n";
@@ -313,20 +300,20 @@
314301 ambient += light.ambient;\n\
315302 float NdotL = max(dot(N,dir),0.0);\n\
316303 diffuse += light.diffuse*NdotL;\n\
317 -if((NdotL > 0.0) && (material.power != 0.0))\n\
 304+if((NdotL > 0.0) && (gl_FrontMaterial.shininess != 0.0))\n\
318305 {\n\
319 -vec3 eye = (-view[3].xyz / view[3].w);\n\
320 -vec3 P = vec3((view*world)*xyzw);\n\
 306+vec3 eye = vec3(0.0,0.0,1.0);\n\
 307+vec3 P = vec3(gl_ModelViewMatrix*xyzw);\n\
321308 vec3 L = normalize(-light.direction.xyz - P);\n\
322309 vec3 V = normalize(eye - P);\n\
323310 NdotHV = max(dot(N,L+V),0.0);\n\
324 -specular += (pow(NdotHV,float(material.power))*light.specular);\n\
 311+specular += (pow(NdotHV,float(gl_FrontMaterial.shininess))*light.specular);\n\
325312 ambient += light.ambient;\n\
326313 }\n\
327314 }\n";
328315 static const char func_pointlight[] = "void PointLight(in Light light)\n\
329316 {\n\
330 -vec4 pos = ((view*world)*xyzw);\n\
 317+vec4 pos = gl_ModelViewMatrix*xyzw;\n\
331318 vec3 pos3 = pos.xyz / pos.w;\n\
332319 vec3 V = light.position - pos3;\n\
333320 float d = length(V);\n\
@@ -336,7 +323,7 @@
337324 float NdotHV = max(0.0,dot(N,normalize(V+vec3(0.0,0.0,1.0))));\n\
338325 float pf;\n\
339326 if(NdotV == 0.0) pf = 0.0;\n\
340 -else if(material.power > 0.0) pf = pow(NdotHV,material.power);\n\
 327+else if(gl_FrontMaterial.shininess > 0.0) pf = pow(NdotHV,gl_FrontMaterial.shininess);\n\
341328 else pf = 0.0;\n\
342329 diffuse += light.diffuse*NdotV*attenuation;\n\
343330 ambient += light.ambient;\n\
@@ -344,7 +331,7 @@
345332 }\n";
346333 static const char func_spotlight[] = "void SpotLight(in Light light)\n\
347334 {\n\
348 -vec4 pos = ((view*world)*xyzw);\n\
 335+vec4 pos = (gl_ModelViewMatrix*xyzw);\n\
349336 vec3 pos3 = pos.xyz / pos.w;\n\
350337 vec3 V = light.position - pos3;\n\
351338 float d = length(V);\n\
@@ -354,7 +341,7 @@
355342 float NdotHV = max(0.0,dot(N,normalize(V+vec3(0.0,0.0,1.0))));\n\
356343 float pf;\n\
357344 if(NdotV == 0.0) pf = 0.0;\n\
358 -else if(material.power > 0.0) pf = pow(NdotHV,material.power);\n\
 345+else if(gl_FrontMaterial.shininess > 0.0) pf = pow(NdotHV,gl_FrontMaterial.shininess);\n\
359346 else pf = 0.0;\n\
360347 float spotangle = dot(-V,normalize(light.direction));\n\
361348 if(spotangle < cos(light.phi * (180.0/3.14159265)))\n\
@@ -451,8 +438,6 @@
452439 }
453440
454441 // Uniforms
455 - vsrc->append(unif_matrices); // Material
456 - vsrc->append(unif_material);
457442 vsrc->append(unif_ambient);
458443 if((id>>50)&1) vsrc->append(unif_size);
459444 numlights = (id>>18)&7;
@@ -896,16 +881,7 @@
897882 genshaders[index].shader.attribs[i+34] = glGetAttribLocation(genshaders[index].shader.prog,attrSTRQ);
898883 }
899884 // Uniforms
900 - genshaders[index].shader.uniforms[0] = glGetUniformLocation(genshaders[index].shader.prog,"world");
901 - genshaders[index].shader.uniforms[1] = glGetUniformLocation(genshaders[index].shader.prog,"view");
902 - genshaders[index].shader.uniforms[2] = glGetUniformLocation(genshaders[index].shader.prog,"projection");
903 - genshaders[index].shader.uniforms[3] = glGetUniformLocation(genshaders[index].shader.prog,"normalmat");
904885 // TODO: 4-14 world1-3 and texture0-7
905 - genshaders[index].shader.uniforms[15] = glGetUniformLocation(genshaders[index].shader.prog,"material.ambient");
906 - genshaders[index].shader.uniforms[16] = glGetUniformLocation(genshaders[index].shader.prog,"material.diffuse");
907 - genshaders[index].shader.uniforms[17] = glGetUniformLocation(genshaders[index].shader.prog,"material.specular");
908 - genshaders[index].shader.uniforms[18] = glGetUniformLocation(genshaders[index].shader.prog,"material.emissive");
909 - genshaders[index].shader.uniforms[19] = glGetUniformLocation(genshaders[index].shader.prog,"material.power");
910886 char uniflight[] = "lightX. ";
911887 for(int i = 0; i < 8; i++)
912888 {