DXGL r113 - Code Review

Jump to navigation Jump to search
Repository:DXGL
Revision:r112‎ | r113 | r114 >
Date:23:23, 4 March 2012
Author:admin
Status:new
Tags:
Comment:
Get directional lights working
Modified paths:
  • /ddraw/glDirect3DDevice.cpp (modified) (history)
  • /ddraw/glDirect3DDevice.h (modified) (history)
  • /ddraw/glRenderer.cpp (modified) (history)
  • /ddraw/matrix.cpp (modified) (history)
  • /ddraw/matrix.h (modified) (history)
  • /ddraw/shadergen.cpp (modified) (history)

Diff [purge]

Index: ddraw/glDirect3DDevice.cpp
@@ -636,7 +636,7 @@
637637 GLfloat tmp[16];
638638 ZeroMemory(&worldview,sizeof(D3DMATRIX));
639639 ZeroMemory(&tmp,sizeof(D3DMATRIX));
640 - __gluMultMatricesf(matWorld,matView,worldview); // Get worldview
 640+ __gluMultMatricesf(matView,matWorld,worldview); // Get worldview
641641 if(__gluInvertMatrixf(worldview,tmp)) // Invert
642642 memcpy(matNormal,tmp,16*sizeof(GLfloat));
643643 else memcpy(matNormal,worldview,16*sizeof(GLfloat));
Index: ddraw/glDirect3DDevice.h
@@ -98,11 +98,11 @@
9999 glDirect3DLight **lights;
100100 int gllights[8];
101101 glDirectDrawSurface7 *glDDS7;
 102+ DWORD renderstate[153];
102103
103104 private:
104105 glDirect3D7 *glD3D7;
105106 ULONG refcount;
106 - DWORD renderstate[153];
107107 GLuint gltextures[8];
108108 DWORD lightsmax;
109109 bool inscene;
Index: ddraw/glRenderer.cpp
@@ -26,6 +26,7 @@
2727 #include "ddraw.h"
2828 #include "scalers.h"
2929 #include "shadergen.h"
 30+#include "matrix.h"
3031
3132 WNDCLASSEXA wndclass;
3233 bool wndclasscreated = false;
@@ -1033,6 +1034,7 @@
10341035 void glRenderer::_DrawIndexedPrimitive(glDirect3DDevice7 *device, D3DPRIMITIVETYPE d3dptPrimitiveType, DWORD dwVertexTypeDesc,
10351036 LPVOID lpvVertices, DWORD dwVertexCount, LPWORD lpwIndices, DWORD dwIndexCount, DWORD dwFlags)
10361037 {
 1038+ GLfloat tmpmat[16];
10371039 bool transformed;
10381040 int normalptr = 0;
10391041 int colorptr[2] = {0,0};
@@ -1165,11 +1167,14 @@
11661168 }
11671169 if(device->normal_dirty) device->UpdateNormalMatrix();
11681170 GLint loc = glGetUniformLocation(prog,"world");
1169 - glUniformMatrix4fv(loc,1,false,device->matWorld);
 1171+ viewLHtoRH(tmpmat,device->matWorld);
 1172+ glUniformMatrix4fv(loc,1,false,tmpmat);
11701173 loc = glGetUniformLocation(prog,"view");
1171 - glUniformMatrix4fv(loc,1,false,device->matView);
 1174+ viewLHtoRH(tmpmat,device->matView);
 1175+ glUniformMatrix4fv(loc,1,false,tmpmat);
11721176 loc = glGetUniformLocation(prog,"projection");
1173 - glUniformMatrix4fv(loc,1,false,device->matProjection);
 1177+ prjLHtoRH(tmpmat,device->matProjection);
 1178+ glUniformMatrix4fv(loc,1,false,tmpmat);
11741179 loc = glGetUniformLocation(prog,"normalmat");
11751180 glUniformMatrix4fv(loc,1,true,device->matNormal);
11761181 loc = glGetUniformLocation(prog,"material.diffuse");
@@ -1182,6 +1187,10 @@
11831188 glUniform4fv(loc,1,(GLfloat*)&device->material.emissive);
11841189 loc = glGetUniformLocation(prog,"material.power");
11851190 glUniform1f(loc,device->material.power);
 1191+ loc = glGetUniformLocation(prog,"ambientcolor");
 1192+ DWORD ambient = device->renderstate[D3DRENDERSTATE_AMBIENT];
 1193+ glUniform4f(loc,RGBA_GETRED(ambient),RGBA_GETGREEN(ambient),
 1194+ RGBA_GETBLUE(ambient),RGBA_GETALPHA(ambient));
11861195 int lightindex = 0;
11871196 char lightname[] = "lightX.xxxxxxxxxxxxxxxx";
11881197 for(i = 0; i < 8; i++)
Index: ddraw/matrix.cpp
@@ -46,10 +46,34 @@
4747 * other dealings in this Software without prior written authorization from
4848 * Silicon Graphics, Inc.
4949 */
50 -
 50+// Portions of this file are from the dxglwrap project, and are licensed under
 51+// the following terms:
 52+/*
 53+ *
 54+ * dxglwrapper 0.08 - Sept. 12, 2002
 55+ * For conditions of distribution and use, see copyright notice in d3dprj.h
 56+ * Copyright (c) 1991-2002 realtech VR
 57+ *
 58+ * Copyright (c) 2002 realtech VR
 59+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
 60+ * and associated documentation files (the "Software"), to deal in the Software without
 61+ * restriction, including without limitation the rights to use, copy, modify, merge, publish,
 62+ * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
 63+ * Software is furnished to do so, subject to the following conditions:
 64+ * The above copyright notice and this permission notice shall be included in all copies or
 65+ * substantial portions of the Software.
 66+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 67+ * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 68+ * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
 69+ * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 70+ * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 71+ * DEALINGS IN THE SOFTWARE.
 72+ *
 73+ */
5174 #include "common.h"
5275 #include "matrix.h"
5376
 77+// From project.c:
5478 /*
5579 ** Invert 4x4 matrix.
5680 ** Contributed by David Moore (See Mesa bug #6748)
@@ -128,3 +152,39 @@
129153 m[2+4*0] = 0; m[2+4*1] = 0; m[2+4*2] = 1; m[2+4*3] = 0;
130154 m[3+4*0] = 0; m[3+4*1] = 0; m[3+4*2] = 0; m[3+4*3] = 1;
131155 }
 156+
 157+
 158+// from dxglwrap:
 159+/*
 160+
 161+ This is one basic difference between OpenGL and Direct3D.
 162+ In both coordinate systems, the X and Y axis are as normal.
 163+ The Y positive axis going upwards and the X positive axis going to the right.
 164+
 165+ However, the two systems differ in their Z-axis implementation.
 166+ A Left-Handed System, which Direct3D uses, is one where the Z-axis is positive INTO the screen.
 167+ Take your Left Hand, face the palm upwards with your fingers pointing in the direction of the positive x-axis (to the right), and curl your fingers in the direction of the Y-positive axis (up). Now if you stick your thumb STRAIGHT (like you were giving a sideways "thumbs-up" sign), your thumb is now pointing in the direction of the positive Z-axis.
 168+
 169+ OpenGL, on the other hand, uses a Right-Handed system, where the positive Z-axis is coming OUT of the screen.
 170+ Again, to remember this, take your right hand, point your palm upwards and your fingers pointing in the direction of the positive x-axis
 171+ (to the right). Now curl your fingers up to the direction of the positive y-axis (up), and point your thumb out. It is now pointing in the direction of the positive Z-axis (out)..
 172+
 173+ Note: no doc exists how to do the conversion
 174+*/
 175+
 176+
 177+void prjLHtoRH(float *d, const float *s) // Exclusive function
 178+{
 179+ d[0] = -s[0]; d[1] = -s[1]; d[2] = -s[2]; d[3] = -s[3];
 180+ d[4] = s[4]; d[5] = s[5]; d[6] = s[6]; d[7] = s[7];
 181+ d[8] = -s[8]; d[9] = -s[9]; d[10] = -s[10]; d[11] = -s[11];
 182+ d[12] = s[12]; d[13] = s[13]; d[14] = s[14]; d[15] = s[15];
 183+}
 184+
 185+void viewLHtoRH(float *d, const float *s)
 186+{
 187+ d[0] = -s[0]; d[1] = s[1]; d[2] = -s[2]; d[3] = s[3];
 188+ d[4] = -s[4]; d[5] = s[5]; d[6] = -s[6]; d[7] = s[7];
 189+ d[8] = -s[8]; d[9] = s[9]; d[10] = -s[10]; d[11] = s[11];
 190+ d[12] = -s[12]; d[13] = s[13]; d[14] = -s[14]; d[15] = s[15];
 191+}
Index: ddraw/matrix.h
@@ -46,6 +46,30 @@
4747 * other dealings in this Software without prior written authorization from
4848 * Silicon Graphics, Inc.
4949 */
 50+// Portions of this file are from the dxglwrap project, and are licensed under
 51+// the following terms:
 52+/*
 53+ *
 54+ * dxglwrapper 0.08 - Sept. 12, 2002
 55+ * For conditions of distribution and use, see copyright notice in d3dprj.h
 56+ * Copyright (c) 1991-2002 realtech VR
 57+ *
 58+ * Copyright (c) 2002 realtech VR
 59+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
 60+ * and associated documentation files (the "Software"), to deal in the Software without
 61+ * restriction, including without limitation the rights to use, copy, modify, merge, publish,
 62+ * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
 63+ * Software is furnished to do so, subject to the following conditions:
 64+ * The above copyright notice and this permission notice shall be included in all copies or
 65+ * substantial portions of the Software.
 66+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 67+ * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 68+ * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
 69+ * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 70+ * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 71+ * DEALINGS IN THE SOFTWARE.
 72+ *
 73+ */
5074 #pragma once
5175 #ifndef _MATRIX_H
5276 #define _MATRIX_H
@@ -55,4 +79,7 @@
5680 GLfloat r[16]);
5781 void __gluMakeIdentityf(GLfloat m[16]);
5882
 83+void prjLHtoRH(float *d, const float *s);
 84+void viewLHtoRH(float *d, const float *s);
 85+
5986 #endif //_MATRIX_H
\ No newline at end of file
Index: ddraw/shadergen.cpp
@@ -215,6 +215,7 @@
216216 float phi;\n\
217217 };\n";
218218 static const char unif_light[] = "uniform Light lightX;\n";
 219+static const char unif_ambient[] = "uniform vec4 ambientcolor;\n";
219220 // Variables
220221 static const char var_colors[] = "vec4 diffuse;\n\
221222 vec4 specular;\n\
@@ -225,7 +226,8 @@
226227 static const char op_transform[] = "xyzw = vec4(xyz,1);\n\
227228 gl_Position = (projection*(view*world))*xyzw;\n";
228229 static const char op_passthru[] = "gl_Position = xyzw;\n";
229 -static const char op_resetcolor[] = "diffuse = specular = ambient = vec4(0.0);\n";
 230+static const char op_resetcolor[] = "diffuse = specular = vec4(0.0);\n\
 231+ambient = ambientcolor / 255.0;\n";
230232 static const char op_dirlight[] = "DirLight(lightX);\n";
231233 static const char op_spotlight[] = "SpotLight(lightX);\n";
232234 static const char op_colorout[] = "vec4 color = (material.diffuse * diffuse) + (material.ambient * ambient) + \n\
@@ -238,7 +240,7 @@
239241 static const char func_dirlight[] = "void DirLight(in Light light)\n\
240242 {\n\
241243 float NdotHV = 0.0;\n\
242 -vec3 N = normalize(vec3(normalmat*vec4(nxyz,0.0)));\n\
 244+vec3 N = normalize(vec3(normalmat*vec4(nxyz,1.0)));\n\
243245 vec3 dir = normalize(light.direction);\n\
244246 ambient += light.ambient;\n\
245247 float NdotL = max(dot(N,dir),0.0);\n\
@@ -246,11 +248,12 @@
247249 if(NdotL > 0.0)\n\
248250 {\n\
249251 vec3 eye = (-view[3].xyz / view[3].w);\n\
250 -vec3 P = vec3((world*view)*xyzw);\n\
 252+vec3 P = vec3((view*world)*xyzw);\n\
251253 vec3 L = normalize(light.direction.xyz - P);\n\
252254 vec3 V = normalize(eye - P);\n\
253255 NdotHV = max(dot(N,L+V),0.0);\n\
254 -specular += pow(NdotHV,float(material.power));\n\
 256+specular += (pow(NdotHV,float(material.power))*light.specular);\n\
 257+ambient += light.ambient;\n\
255258 }\n\
256259 }\n";
257260
@@ -303,6 +306,7 @@
304307 // Uniforms
305308 vsrc->append(unif_matrices); // Material
306309 vsrc->append(unif_material);
 310+ vsrc->append(unif_ambient);
307311 numlights = (id>>18)&7;
308312 if(numlights) // Lighting
309313 {