DXGL r132 - Code Review

Jump to navigation Jump to search
Repository:DXGL
Revision:r131‎ | r132 | r133 >
Date:21:24, 2 April 2012
Author:admin
Status:new
Tags:
Comment:
Tweak lighting.
Fix DrawIndexedPrimitive
Modified paths:
  • /ddraw/glDirect3DDevice.cpp (modified) (history)
  • /ddraw/glDirect3DDevice.h (modified) (history)
  • /ddraw/glExtensions.cpp (modified) (history)
  • /ddraw/glExtensions.h (modified) (history)
  • /ddraw/glRenderer.cpp (modified) (history)
  • /ddraw/shadergen.cpp (modified) (history)

Diff [purge]

Index: ddraw/glDirect3DDevice.cpp
@@ -388,6 +388,7 @@
389389 for(i = 0; i < 5; i++)
390390 if(VertexType[i+2].data) blendweights++;
391391 shader |= (__int64)blendweights << 46;
 392+ if(renderstate[D3DRENDERSTATE_NORMALIZENORMALS]) shader |= (1i64 << 49);
392393 //TODO: Implement texture stages.
393394 for(i = 0; i < 8; i++)
394395 {
@@ -611,14 +612,22 @@
612613 HRESULT WINAPI glDirect3DDevice7::GetLight(DWORD dwLightIndex, LPD3DLIGHT7 lpLight)
613614 {
614615 if(!this) return DDERR_INVALIDPARAMS;
615 - FIXME("glDirect3DDevice7::GetLight: stub");
616 - ERR(DDERR_GENERIC);
 616+ if(!lpLight) return DDERR_INVALIDPARAMS;
 617+ if(dwLightIndex >= lightsmax) ERR(DDERR_INVALIDOBJECT);
 618+ if(!lights[dwLightIndex]) ERR(DDERR_INVALIDOBJECT);
 619+ lights[dwLightIndex]->GetLight7(lpLight);
 620+ return D3D_OK;
617621 }
618622 HRESULT WINAPI glDirect3DDevice7::GetLightEnable(DWORD dwLightIndex, BOOL* pbEnable)
619623 {
620624 if(!this) return DDERR_INVALIDPARAMS;
621 - FIXME("glDirect3DDevice7::GetLightEnalbe: stub");
622 - ERR(DDERR_GENERIC);
 625+ if(dwLightIndex >= lightsmax) ERR(DDERR_INVALIDOBJECT);
 626+ if(!lights[dwLightIndex]) ERR(DDERR_INVALIDOBJECT);
 627+ if(!pbEnable) return DDERR_INVALIDPARAMS;
 628+ *pbEnable = FALSE;
 629+ for(int i = 0; i < 8; i++)
 630+ if(gllights[i] == dwLightIndex) *pbEnable = TRUE;
 631+ return D3D_OK;
623632 }
624633 HRESULT WINAPI glDirect3DDevice7::GetMaterial(LPD3DMATERIAL7 lpMaterial)
625634 {
@@ -1059,11 +1068,22 @@
10601069 {
10611070 GLfloat worldview[16];
10621071 GLfloat tmp[16];
 1072+
10631073 ZeroMemory(&worldview,sizeof(D3DMATRIX));
10641074 ZeroMemory(&tmp,sizeof(D3DMATRIX));
10651075 __gluMultMatricesf(matWorld,matView,worldview); // Get worldview
10661076 if(__gluInvertMatrixf(worldview,tmp)) // Invert
1067 - memcpy(matNormal,tmp,16*sizeof(GLfloat));
1068 - else memcpy(matNormal,worldview,16*sizeof(GLfloat));
 1077+ {
 1078+ memcpy(matNormal,tmp,3*sizeof(GLfloat));
 1079+ memcpy(matNormal+3,tmp+4,3*sizeof(GLfloat));
 1080+ memcpy(matNormal+6,tmp+8,3*sizeof(GLfloat));
 1081+ }
 1082+ else
 1083+ {
 1084+ memcpy(matNormal,worldview,3*sizeof(GLfloat));
 1085+ memcpy(matNormal+3,worldview+4,3*sizeof(GLfloat));
 1086+ memcpy(matNormal+6,worldview+8,3*sizeof(GLfloat));
 1087+ }
 1088+
10691089 normal_dirty = false;
10701090 }
\ No newline at end of file
Index: ddraw/glDirect3DDevice.h
@@ -124,7 +124,7 @@
125125 GLfloat matWorld[16];
126126 GLfloat matView[16];
127127 GLfloat matProjection[16];
128 - GLfloat matNormal[16];
 128+ GLfloat matNormal[9];
129129 bool normal_dirty;
130130 D3DMATERIAL7 material;
131131 D3DVIEWPORT7 viewport;
Index: ddraw/glExtensions.cpp
@@ -64,6 +64,7 @@
6565 void (APIENTRY *glUniform4f) (GLint location, GLfloat v0, GLfloat v1, GLfloat v2, GLfloat v3) = NULL;
6666 void (APIENTRY *glUniform3fv) (GLint location, GLsizei count, const GLfloat* value) = NULL;
6767 void (APIENTRY *glUniform4fv) (GLint location, GLsizei count, const GLfloat* value) = NULL;
 68+void (APIENTRY *glUniformMatrix3fv) (GLint location, GLsizei count, GLboolean transpose, const GLfloat* value) = NULL;
6869 void (APIENTRY *glUniformMatrix4fv) (GLint location, GLsizei count, GLboolean transpose, const GLfloat* value) = NULL;
6970
7071 void (APIENTRY *glDrawRangeElements) (GLenum mode, GLuint start, GLuint end, GLsizei count, GLenum type, const GLvoid *indices);
@@ -136,6 +137,7 @@
137138 glUniform4f = (PFNGLUNIFORM4FPROC)wglGetProcAddress("glUniform4f");
138139 glUniform3fv = (PFNGLUNIFORM3FVPROC)wglGetProcAddress("glUniform3fv");
139140 glUniform4fv = (PFNGLUNIFORM4FVPROC)wglGetProcAddress("glUniform4fv");
 141+ glUniformMatrix3fv = (PFNGLUNIFORMMATRIX3FVPROC)wglGetProcAddress("glUniformMatrix3fv");
140142 glUniformMatrix4fv = (PFNGLUNIFORMMATRIX4FVPROC)wglGetProcAddress("glUniformMatrix4fv");
141143 glGetAttribLocation = (PFNGLGETATTRIBLOCATIONPROC)wglGetProcAddress("glGetAttribLocation");
142144 glVertexAttribPointer = (PFNGLVERTEXATTRIBPOINTERPROC)wglGetProcAddress("glVertexAttribPointer");
Index: ddraw/glExtensions.h
@@ -84,6 +84,7 @@
8585 GLAPI void (APIENTRY *glUniform4f) (GLint location, GLfloat v0, GLfloat v1, GLfloat v2, GLfloat v3);
8686 GLAPI void (APIENTRY *glUniform3fv) (GLint location, GLsizei count, const GLfloat* value);
8787 GLAPI void (APIENTRY *glUniform4fv) (GLint location, GLsizei count, const GLfloat* value);
 88+GLAPI void (APIENTRY *glUniformMatrix3fv) (GLint location, GLsizei count, GLboolean transpose, const GLfloat* value);
8889 GLAPI void (APIENTRY *glUniformMatrix4fv) (GLint location, GLsizei count, GLboolean transpose, const GLfloat* value);
8990
9091 GLAPI void (APIENTRY *glDrawRangeElements) (GLenum mode, GLuint start, GLuint end, GLsizei count, GLenum type, const GLvoid *indices);
Index: ddraw/glRenderer.cpp
@@ -1076,7 +1076,7 @@
10771077 loc = glGetUniformLocation(prog,"projection");
10781078 glUniformMatrix4fv(loc,1,false,device->matProjection);
10791079 loc = glGetUniformLocation(prog,"normalmat");
1080 - glUniformMatrix4fv(loc,1,true,device->matNormal);
 1080+ glUniformMatrix3fv(loc,1,true,device->matNormal);
10811081 loc = glGetUniformLocation(prog,"material.diffuse");
10821082 glUniform4fv(loc,1,(GLfloat*)&device->material.diffuse);
10831083 loc = glGetUniformLocation(prog,"material.ambient");
@@ -1142,7 +1142,7 @@
11431143 if(device->glDDS7->zbuffer) SetFBO(device->glDDS7->texture,device->glDDS7->zbuffer->texture,device->glDDS7->zbuffer->hasstencil);
11441144 else SetFBO(device->glDDS7->texture,0,false);
11451145 glViewport(device->viewport.dwX,device->viewport.dwY,device->viewport.dwWidth,device->viewport.dwHeight);
1146 - if(indices) glDrawRangeElements(mode,0,indexcount,count,GL_UNSIGNED_SHORT,indices);
 1146+ if(indices) glDrawElements(mode,indexcount,GL_UNSIGNED_SHORT,indices);
11471147 else glDrawArrays(mode,0,count);
11481148 if(device->glDDS7->zbuffer) device->glDDS7->zbuffer->dirty |= 2;
11491149 device->glDDS7->dirty |= 2;
Index: ddraw/shadergen.cpp
@@ -76,6 +76,7 @@
7777 Bit 37 - Enable normals
7878 Bits 38-45 - Light types
7979 Bits 46-48 - Number of blending weights
 80+Bit 49 - Normalize normals
8081 */
8182
8283 /* Bits in Texture Stage ID:
@@ -215,7 +216,7 @@
216217 static const char unif_matrices[] = "uniform mat4 world;\n\
217218 uniform mat4 view;\n\
218219 uniform mat4 projection;\n\
219 -uniform mat4 normalmat;\n";
 220+uniform mat3 normalmat;\n";
220221 static const char unif_material[] = "struct Material\n\
221222 {\n\
222223 vec4 diffuse;\n\
@@ -244,9 +245,10 @@
245246 static const char unif_ambient[] = "uniform vec4 ambientcolor;\n";
246247 static const char unif_tex[] = "uniform sampler2d texX;\n";
247248 // Variables
248 -static const char var_colors[] = "vec4 diffuse;\n\
 249+static const char var_common[] = "vec4 diffuse;\n\
249250 vec4 specular;\n\
250 -vec4 ambient;\n";
 251+vec4 ambient;\n\
 252+vec3 N;";
251253 static const char var_color[] = "vec4 color;\n";
252254 static const char var_xyzw[] = "vec4 xyzw;\n";
253255 // Operations
@@ -253,6 +255,8 @@
254256 static const char op_transform[] = "xyzw = vec4(xyz,1);\n\
255257 vec4 pos = (projection*(view*world))*xyzw;\n\
256258 gl_Position = vec4(pos.x,-pos.y,pos.z,pos.w);\n";
 259+static const char op_normalize[] = "N = normalize(normalmat*nxyz);\n";
 260+static const char op_normalpassthru[] = "N = normalmat*nxyz;\n";
257261 static const char op_passthru[] = "gl_Position = xyzw;\n";
258262 static const char op_resetcolor[] = "diffuse = specular = vec4(0.0);\n\
259263 ambient = ambientcolor / 255.0;\n";
@@ -268,8 +272,7 @@
269273 static const char func_dirlight[] = "void DirLight(in Light light)\n\
270274 {\n\
271275 float NdotHV = 0.0;\n\
272 -vec3 N = normalize(vec3(normalmat*vec4(nxyz,1.0)));\n\
273 -vec3 dir = normalize(light.direction);\n\
 276+vec3 dir = normalize(-light.direction);\n\
274277 ambient += light.ambient;\n\
275278 float NdotL = max(dot(N,dir),0.0);\n\
276279 diffuse += light.diffuse*NdotL;\n\
@@ -277,7 +280,7 @@
278281 {\n\
279282 vec3 eye = (-view[3].xyz / view[3].w);\n\
280283 vec3 P = vec3((view*world)*xyzw);\n\
281 -vec3 L = normalize(light.direction.xyz - P);\n\
 284+vec3 L = normalize(-light.direction.xyz - P);\n\
282285 vec3 V = normalize(eye - P);\n\
283286 NdotHV = max(dot(N,L+V),0.0);\n\
284287 specular += (pow(NdotHV,float(material.power))*light.specular);\n\
@@ -348,7 +351,7 @@
349352 }
350353
351354 // Variables
352 - vsrc->append(var_colors);
 355+ vsrc->append(var_common);
353356 if(!((id>>34)&1)) vsrc->append(var_xyzw);
354357
355358 // Functions
@@ -367,6 +370,8 @@
368371 if((id>>34)&1) vsrc->append(op_passthru);
369372 else vsrc->append(op_transform);
370373 vsrc->append(op_resetcolor);
 374+ if((id>>49)&1) vsrc->append(op_normalize);
 375+ else vsrc->append(op_normalpassthru);
371376 if(numlights)
372377 {
373378 for(i = 0; i < numlights; i++)