Index: ddraw/glDirect3DDevice.cpp |
— | — | @@ -228,13 +228,13 @@ |
229 | 229 | __gluMakeIdentityf(matWorld);
|
230 | 230 | __gluMakeIdentityf(matView);
|
231 | 231 | __gluMakeIdentityf(matProjection);
|
232 | | - __gluMakeIdentityf(matNormal);
|
233 | 232 | texstages[0] = texstagedefault0;
|
234 | 233 | texstages[1] = texstages[2] = texstages[3] = texstages[4] =
|
235 | 234 | texstages[5] = texstages[6] = texstages[7] = texstagedefault1;
|
236 | 235 | refcount = 1;
|
237 | 236 | inscene = false;
|
238 | | - normal_dirty = false;
|
| 237 | + modelview_dirty = false;
|
| 238 | + projection_dirty = false;
|
239 | 239 | this->glD3D7 = glD3D7;
|
240 | 240 | glD3D7->AddRef();
|
241 | 241 | this->glDDS7 = glDDS7;
|
— | — | @@ -1197,14 +1197,15 @@ |
1198 | 1198 | {
|
1199 | 1199 | case D3DTRANSFORMSTATE_WORLD:
|
1200 | 1200 | memcpy(&matWorld,lpD3DMatrix,sizeof(D3DMATRIX));
|
1201 | | - normal_dirty = true;
|
| 1201 | + modelview_dirty = true;
|
1202 | 1202 | return D3D_OK;
|
1203 | 1203 | case D3DTRANSFORMSTATE_VIEW:
|
1204 | 1204 | memcpy(&matView,lpD3DMatrix,sizeof(D3DMATRIX));
|
1205 | | - normal_dirty = true;
|
| 1205 | + modelview_dirty = true;
|
1206 | 1206 | return D3D_OK;
|
1207 | 1207 | case D3DTRANSFORMSTATE_PROJECTION:
|
1208 | 1208 | memcpy(&matProjection,lpD3DMatrix,sizeof(D3DMATRIX));
|
| 1209 | + projection_dirty = true;
|
1209 | 1210 | return D3D_OK;
|
1210 | 1211 | default:
|
1211 | 1212 | ERR(DDERR_INVALIDPARAMS);
|
— | — | @@ -1263,30 +1264,6 @@ |
1264 | 1265 | return D3D_OK;
|
1265 | 1266 | }
|
1266 | 1267 |
|
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 | | -
|
1291 | 1268 | void glDirect3DDevice7::SetDepthComp()
|
1292 | 1269 | {
|
1293 | 1270 | switch(renderstate[D3DRENDERSTATE_ZFUNC])
|
Index: ddraw/glDirect3DDevice.h |
— | — | @@ -141,12 +141,9 @@ |
142 | 142 | HRESULT SetLightState(D3DLIGHTSTATETYPE dwLightStateType, DWORD dwLightState);
|
143 | 143 | HRESULT GetStats(LPD3DSTATS lpD3DStats);
|
144 | 144 | __int64 SelectShader(GLVERTEX *VertexType);
|
145 | | - void UpdateNormalMatrix();
|
146 | 145 | GLfloat matWorld[16];
|
147 | 146 | GLfloat matView[16];
|
148 | 147 | GLfloat matProjection[16];
|
149 | | - GLfloat matNormal[9];
|
150 | | - bool normal_dirty;
|
151 | 148 | D3DMATERIAL7 material;
|
152 | 149 | D3DVIEWPORT7 viewport;
|
153 | 150 | glDirect3DLight **lights;
|
— | — | @@ -158,7 +155,8 @@ |
159 | 156 | glDirect3DMaterial3 **materials;
|
160 | 157 | glDirect3DMaterial3 *currentmaterial;
|
161 | 158 | int materialcount;
|
162 | | -
|
| 159 | + bool modelview_dirty;
|
| 160 | + bool projection_dirty;
|
163 | 161 | private:
|
164 | 162 | HRESULT fvftoglvertex(DWORD dwVertexTypeDesc,LPDWORD vertptr);
|
165 | 163 | ULONG refcount;
|
Index: ddraw/glRenderer.cpp |
— | — | @@ -792,6 +792,7 @@ |
793 | 793 | glViewport(0,0,width,height);
|
794 | 794 | DepthWrite(true);
|
795 | 795 | DepthTest(false);
|
| 796 | + MatrixMode(GL_MODELVIEW);
|
796 | 797 | glDisable(GL_DEPTH_TEST);
|
797 | 798 | SetDepthComp(GL_LESS);
|
798 | 799 | const GLubyte *glver = glGetString(GL_VERSION);
|
— | — | @@ -1213,7 +1214,13 @@ |
1214 | 1215 | GLfloat ambient[] = {0.0,0.0,0.0,0.0};
|
1215 | 1216 | if(zbuffer) DepthTest(true);
|
1216 | 1217 | 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);
|
1218 | 1225 | }
|
1219 | 1226 |
|
1220 | 1227 | void glRenderer::_Clear(glDirectDrawSurface7 *target, DWORD dwCount, LPD3DRECT lpRects, DWORD dwFlags, DWORD dwColor, D3DVALUE dvZ, DWORD dwStencil)
|
— | — | @@ -1401,17 +1408,12 @@ |
1402 | 1409 |
|
1403 | 1410 | }
|
1404 | 1411 | }
|
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);
|
1410 | 1414 |
|
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 | +
|
1416 | 1418 | int lightindex = 0;
|
1417 | 1419 | char lightname[] = "lightX.xxxxxxxxxxxxxxxx";
|
1418 | 1420 | for(i = 0; i < 8; i++)
|
Index: ddraw/glutil.cpp |
— | — | @@ -29,6 +29,12 @@ |
30 | 30 | GLint scissory = 0;
|
31 | 31 | GLsizei scissorwidth = 0;
|
32 | 32 | 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;
|
33 | 39 | bool scissorenabled = false;
|
34 | 40 | bool stencil = false;
|
35 | 41 | GLint texlevel = 0;
|
— | — | @@ -211,4 +217,50 @@ |
212 | 218 | scissorheight = height;
|
213 | 219 | glScissor(x,y,width,height);
|
214 | 220 | }
|
| 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 | + }
|
215 | 267 | } |
\ No newline at end of file |
Index: ddraw/glutil.h |
— | — | @@ -33,5 +33,8 @@ |
34 | 34 | void DepthWrite(bool enabled);
|
35 | 35 | void DepthTest(bool enabled);
|
36 | 36 | 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);
|
37 | 40 |
|
38 | 41 | #endif //_GLUTIL_H |
\ No newline at end of file |
Index: ddraw/shadergen.cpp |
— | — | @@ -237,19 +237,6 @@ |
238 | 238 | static const char conv_str[] = "vec4 strqX = vec4(strX[0],strX[1],strX[2],1);\n";
|
239 | 239 | static const char attr_strq[] = "attribute vec4 strqX;\n";
|
240 | 240 | // 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";
|
254 | 241 | static const char lightstruct[] = "struct Light\n\
|
255 | 242 | {\n\
|
256 | 243 | vec4 diffuse;\n\
|
— | — | @@ -280,10 +267,10 @@ |
281 | 268 | static const char var_xyzw[] = "vec4 xyzw;\n";
|
282 | 269 | // Operations
|
283 | 270 | 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\
|
285 | 272 | 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";
|
288 | 275 | 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";
|
289 | 276 | static const char op_resetcolor[] = "diffuse = specular = vec4(0.0);\n\
|
290 | 277 | ambient = ambientcolor / 255.0;\n";
|
— | — | @@ -290,9 +277,9 @@ |
291 | 278 | static const char op_dirlight[] = "DirLight(lightX);\n";
|
292 | 279 | static const char op_pointlight[] = "PointLight(lightX);\n";
|
293 | 280 | 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";
|
297 | 284 | static const char op_colorvert[] = "gl_FrontColor = rgba0.bgra;\n";
|
298 | 285 | static const char op_color2vert[] = "gl_FrontSecondaryColor = rgba1.bgra;\n";
|
299 | 286 | static const char op_colorfragout[] = "gl_FragColor = vec4(color,alpha);\n";
|
— | — | @@ -313,20 +300,20 @@ |
314 | 301 | ambient += light.ambient;\n\
|
315 | 302 | float NdotL = max(dot(N,dir),0.0);\n\
|
316 | 303 | 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\
|
318 | 305 | {\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\
|
321 | 308 | vec3 L = normalize(-light.direction.xyz - P);\n\
|
322 | 309 | vec3 V = normalize(eye - P);\n\
|
323 | 310 | 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\
|
325 | 312 | ambient += light.ambient;\n\
|
326 | 313 | }\n\
|
327 | 314 | }\n";
|
328 | 315 | static const char func_pointlight[] = "void PointLight(in Light light)\n\
|
329 | 316 | {\n\
|
330 | | -vec4 pos = ((view*world)*xyzw);\n\
|
| 317 | +vec4 pos = gl_ModelViewMatrix*xyzw;\n\
|
331 | 318 | vec3 pos3 = pos.xyz / pos.w;\n\
|
332 | 319 | vec3 V = light.position - pos3;\n\
|
333 | 320 | float d = length(V);\n\
|
— | — | @@ -336,7 +323,7 @@ |
337 | 324 | float NdotHV = max(0.0,dot(N,normalize(V+vec3(0.0,0.0,1.0))));\n\
|
338 | 325 | float pf;\n\
|
339 | 326 | 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\
|
341 | 328 | else pf = 0.0;\n\
|
342 | 329 | diffuse += light.diffuse*NdotV*attenuation;\n\
|
343 | 330 | ambient += light.ambient;\n\
|
— | — | @@ -344,7 +331,7 @@ |
345 | 332 | }\n";
|
346 | 333 | static const char func_spotlight[] = "void SpotLight(in Light light)\n\
|
347 | 334 | {\n\
|
348 | | -vec4 pos = ((view*world)*xyzw);\n\
|
| 335 | +vec4 pos = (gl_ModelViewMatrix*xyzw);\n\
|
349 | 336 | vec3 pos3 = pos.xyz / pos.w;\n\
|
350 | 337 | vec3 V = light.position - pos3;\n\
|
351 | 338 | float d = length(V);\n\
|
— | — | @@ -354,7 +341,7 @@ |
355 | 342 | float NdotHV = max(0.0,dot(N,normalize(V+vec3(0.0,0.0,1.0))));\n\
|
356 | 343 | float pf;\n\
|
357 | 344 | 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\
|
359 | 346 | else pf = 0.0;\n\
|
360 | 347 | float spotangle = dot(-V,normalize(light.direction));\n\
|
361 | 348 | if(spotangle < cos(light.phi * (180.0/3.14159265)))\n\
|
— | — | @@ -451,8 +438,6 @@ |
452 | 439 | }
|
453 | 440 |
|
454 | 441 | // Uniforms
|
455 | | - vsrc->append(unif_matrices); // Material
|
456 | | - vsrc->append(unif_material);
|
457 | 442 | vsrc->append(unif_ambient);
|
458 | 443 | if((id>>50)&1) vsrc->append(unif_size);
|
459 | 444 | numlights = (id>>18)&7;
|
— | — | @@ -896,16 +881,7 @@ |
897 | 882 | genshaders[index].shader.attribs[i+34] = glGetAttribLocation(genshaders[index].shader.prog,attrSTRQ);
|
898 | 883 | }
|
899 | 884 | // 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");
|
904 | 885 | // 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");
|
910 | 886 | char uniflight[] = "lightX. ";
|
911 | 887 | for(int i = 0; i < 8; i++)
|
912 | 888 | {
|