DXGL r68 - Code Review

Jump to navigation Jump to search
Repository:DXGL
Revision:r67‎ | r68 | r69 >
Date:02:10, 5 January 2012
Author:admin
Status:new
Tags:
Comment:
Add IDirect3DDevice7::LightEnable
Add glDirect3DLight classe (used internally by glDirect3DDevice7 and implements IDirect3DLight for earlier versions)
Modified paths:
  • /ddraw/common.h (modified) (history)
  • /ddraw/ddraw.vcxproj (modified) (history)
  • /ddraw/ddraw.vcxproj.filters (modified) (history)
  • /ddraw/glDirect3DDevice.cpp (modified) (history)
  • /ddraw/glDirect3DDevice.h (modified) (history)
  • /ddraw/glDirect3DLight.cpp (added) (history)
  • /ddraw/glDirect3DLight.h (added) (history)
  • /dxgltest/common.h (modified) (history)

Diff [purge]

Index: ddraw/common.h
@@ -1,5 +1,5 @@
22 // DXGL
3 -// Copyright (C) 2011 William Feely
 3+// Copyright (C) 2011-2012 William Feely
44
55 // This library is free software; you can redistribute it and/or
66 // modify it under the terms of the GNU Lesser General Public
@@ -16,8 +16,8 @@
1717 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1818
1919 #pragma once
20 -#ifndef _STDAFX_H
21 -#define _STDAFX_H
 20+#ifndef _COMMON_H
 21+#define _COMMON_H
2222
2323 #define WIN32_LEAN_AND_MEAN
2424 #include <windows.h>
@@ -25,6 +25,7 @@
2626 #include <MMSystem.h>
2727 #include "include/winedef.h"
2828 #include <ddraw.h>
 29+#define D3D_OVERLOADS
2930 #include "include/d3d.h"
3031 #include "GL/gl.h"
3132 #include "include/GL/glext.h"
@@ -77,4 +78,4 @@
7879 #define ERR(error) return error;
7980 #endif
8081 #include "../cfgmgr/cfgmgr.h"
81 -#endif //_STDAFX_H
 82+#endif //_COMMON_H
Index: ddraw/ddraw.vcxproj
@@ -165,6 +165,7 @@
166166 <ClInclude Include="glClassFactory.h" />
167167 <ClInclude Include="glDirect3D.h" />
168168 <ClInclude Include="glDirect3DDevice.h" />
 169+ <ClInclude Include="glDirect3DLight.h" />
169170 <ClInclude Include="glDirectDraw.h" />
170171 <ClInclude Include="glDirectDrawClipper.h" />
171172 <ClInclude Include="glDirectDrawPalette.h" />
@@ -204,6 +205,7 @@
205206 </ClCompile>
206207 <ClCompile Include="glDirect3D.cpp" />
207208 <ClCompile Include="glDirect3DDevice.cpp" />
 209+ <ClCompile Include="glDirect3DLight.cpp" />
208210 <ClCompile Include="glDirectDraw.cpp" />
209211 <ClCompile Include="glDirectDrawClipper.cpp" />
210212 <ClCompile Include="glDirectDrawPalette.cpp" />
Index: ddraw/ddraw.vcxproj.filters
@@ -83,6 +83,9 @@
8484 <ClInclude Include="glDirect3DDevice.h">
8585 <Filter>Header Files</Filter>
8686 </ClInclude>
 87+ <ClInclude Include="glDirect3DLight.h">
 88+ <Filter>Header Files</Filter>
 89+ </ClInclude>
8790 </ItemGroup>
8891 <ItemGroup>
8992 <ClCompile Include="ddraw.cpp">
@@ -127,6 +130,9 @@
128131 <ClCompile Include="glDirect3DDevice.cpp">
129132 <Filter>Source Files</Filter>
130133 </ClCompile>
 134+ <ClCompile Include="glDirect3DLight.cpp">
 135+ <Filter>Source Files</Filter>
 136+ </ClCompile>
131137 </ItemGroup>
132138 <ItemGroup>
133139 <ResourceCompile Include="ddraw.rc">
Index: ddraw/glDirect3DDevice.cpp
@@ -1,5 +1,5 @@
22 // DXGL
3 -// Copyright (C) 2011 William Feely
 3+// Copyright (C) 2011-2012 William Feely
44
55 // This library is free software; you can redistribute it and/or
66 // modify it under the terms of the GNU Lesser General Public
@@ -19,8 +19,8 @@
2020 #include "glDirect3D.h"
2121 #include "glDirectDrawSurface.h"
2222 #include "glDirect3DDevice.h"
 23+#include "glDirect3DLight.h"
2324
24 -
2525 const DWORD renderstate_default[153] = {0, // 0
2626 NULL, //texturehandle
2727 D3DANTIALIAS_NONE, //antialias
@@ -132,6 +132,10 @@
133133 glEnable(GL_LIGHTING);
134134 glLightModelfv(GL_LIGHT_MODEL_AMBIENT,ambient);
135135 ZeroMemory(&material,sizeof(D3DMATERIAL7));
 136+ lightsmax = 16;
 137+ lights = (glDirect3DLight**) malloc(16*sizeof(glDirect3DLight*));
 138+ ZeroMemory(lights,16*sizeof(glDirect3DLight*));
 139+ memset(gllights,0xff,8*sizeof(int));
136140
137141 }
138142 glDirect3DDevice7::~glDirect3DDevice7()
@@ -140,6 +144,18 @@
141145 glDDS7->Release();
142146 }
143147
 148+int ExpandLightBuffer(glDirect3DLight ***lights, DWORD *maxlights, DWORD newmax)
 149+{
 150+ if(newmax < *maxlights) return 1;
 151+ glDirect3DLight **tmp = (glDirect3DLight**)realloc(*lights,newmax*sizeof(glDirect3DLight*));
 152+ if(!tmp) return 0;
 153+ *lights = tmp;
 154+ for(DWORD i = *maxlights; i < newmax; i++)
 155+ lights[i] = NULL;
 156+ *maxlights = newmax;
 157+ return 1;
 158+}
 159+
144160 HRESULT WINAPI glDirect3DDevice7::QueryInterface(REFIID riid, void** ppvObj)
145161 {
146162 ERR(E_NOINTERFACE);
@@ -333,6 +349,30 @@
334350 }
335351 HRESULT WINAPI glDirect3DDevice7::LightEnable(DWORD dwLightIndex, BOOL bEnable)
336352 {
 353+ int i;
 354+ D3DLIGHT7 light;
 355+ bool foundlight = false;
 356+ if(dwLightIndex >= lightsmax)
 357+ {
 358+ if(!ExpandLightBuffer(&lights,&lightsmax,dwLightIndex-1)) return DDERR_OUTOFMEMORY;
 359+ }
 360+ if(!lights[dwLightIndex]) lights[dwLightIndex] = new glDirect3DLight;
 361+ if(bEnable)
 362+ {
 363+ for(i = 0; i < 8; i++)
 364+ if(gllights[i] == dwLightIndex) return D3D_OK;
 365+ for(i = 0; i < 8; i++)
 366+ {
 367+ if(gllights[i] == -1)
 368+ {
 369+ foundlight = true;
 370+ gllights[i] = dwLightIndex;
 371+ break;
 372+ }
 373+ }
 374+ if(!foundlight) return D3DERR_LIGHT_SET_FAILED;
 375+ lights[dwLightIndex]->SetGLLight(i);
 376+ }
337377 FIXME("glDirect3DDevice7::LightEnable: stub");
338378 ERR(DDERR_GENERIC);
339379 }
Index: ddraw/glDirect3DDevice.h
@@ -1,5 +1,5 @@
22 // DXGL
3 -// Copyright (C) 2011 William Feely
 3+// Copyright (C) 2011-2012 William Feely
44
55 // This library is free software; you can redistribute it and/or
66 // modify it under the terms of the GNU Lesser General Public
@@ -19,6 +19,7 @@
2020 #ifndef __GLDIRECT3DDEVICE_H
2121 #define __GLDIRECT3DDEVICE_H
2222
 23+class glDirect3DLight;
2324 class glDirectDrawSurface7;
2425 class glDirect3DDevice7 : public IDirect3DDevice7
2526 {
@@ -92,6 +93,9 @@
9394 D3DVIEWPORT7 viewport;
9495 DWORD renderstate[153];
9596 D3DMATERIAL7 material;
 97+ glDirect3DLight **lights;
 98+ int gllights[8];
 99+ DWORD lightsmax;
96100 };
97101
98102 #endif //__GLDIRECT3DDEVICE_H
\ No newline at end of file
Index: ddraw/glDirect3DLight.cpp
@@ -0,0 +1,122 @@
 2+// DXGL
 3+// Copyright (C) 2012 William Feely
 4+
 5+// This library is free software; you can redistribute it and/or
 6+// modify it under the terms of the GNU Lesser General Public
 7+// License as published by the Free Software Foundation; either
 8+// version 2.1 of the License, or (at your option) any later version.
 9+
 10+// This library is distributed in the hope that it will be useful,
 11+// but WITHOUT ANY WARRANTY; without even the implied warranty of
 12+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 13+// Lesser General Public License for more details.
 14+
 15+// You should have received a copy of the GNU Lesser General Public
 16+// License along with this library; if not, write to the Free Software
 17+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 18+
 19+#include "common.h"
 20+#include "glDirect3DLight.h"
 21+#define _USE_MATH_DEFINES
 22+#include <math.h>
 23+
 24+
 25+
 26+glDirect3DLight::glDirect3DLight()
 27+{
 28+ refcount=1;
 29+ gllight = -1;
 30+ ZeroMemory(&light,sizeof(D3DLIGHT7));
 31+ light.dltType = D3DLIGHT_DIRECTIONAL;
 32+ light.dcvAmbient.r = light.dcvAmbient.g = light.dcvAmbient.b = 1.0f;
 33+ light.dvDirection = D3DVECTOR(0,0,1.0);
 34+}
 35+glDirect3DLight::glDirect3DLight(D3DLIGHT7 *light_in)
 36+{
 37+ refcount=1;
 38+ gllight = -1;
 39+ memcpy(&light,light_in,sizeof(D3DLIGHT7));
 40+}
 41+
 42+glDirect3DLight::~glDirect3DLight()
 43+{
 44+ if(gllight != -1)
 45+ glDisable(GL_LIGHT0+gllight);
 46+}
 47+
 48+ULONG WINAPI glDirect3DLight::AddRef()
 49+{
 50+ refcount++;
 51+ return refcount;
 52+}
 53+ULONG WINAPI glDirect3DLight::Release()
 54+{
 55+ ULONG ret;
 56+ refcount--;
 57+ ret = refcount;
 58+ if(refcount == 0) delete this;
 59+ return ret;
 60+}
 61+
 62+HRESULT WINAPI glDirect3DLight::Initialize(LPDIRECT3D lpDirect3D)
 63+{
 64+ return DDERR_ALREADYINITIALIZED;
 65+}
 66+
 67+HRESULT WINAPI glDirect3DLight::QueryInterface(REFIID riid, void** ppvObj)
 68+{
 69+ return E_NOINTERFACE;
 70+}
 71+
 72+void glDirect3DLight::GetLight7(LPD3DLIGHT7 lpLight7)
 73+{
 74+ memcpy(lpLight7,&light,sizeof(D3DLIGHT7));
 75+}
 76+void glDirect3DLight::SetLight7(LPD3DLIGHT7 lpLight7)
 77+{
 78+ memcpy(&light,lpLight7,sizeof(D3DLIGHT7));
 79+}
 80+
 81+HRESULT WINAPI glDirect3DLight::GetLight(LPD3DLIGHT lpLight)
 82+{
 83+ FIXME("glDirect3DLight::GetLight: stub");
 84+ ERR(DDERR_GENERIC);
 85+}
 86+HRESULT WINAPI glDirect3DLight::SetLight(LPD3DLIGHT lpLight)
 87+{
 88+ FIXME("glDirect3DLight::SetLight: stub");
 89+ ERR(DDERR_GENERIC);
 90+}
 91+
 92+void glDirect3DLight::SetGLLight(int gllightin)
 93+{
 94+ const float angle_dx_gl = 90.0f / (float)M_PI;
 95+ if(gllightin != -1)
 96+ {
 97+ if(gllightin != gllight) glDisable(GL_LIGHT0+gllight);
 98+ GLfloat ambient[] = {light.dcvAmbient.r,light.dcvAmbient.g,light.dcvAmbient.b,light.dcvAmbient.a};
 99+ glLightfv(GL_LIGHT0+gllightin,GL_AMBIENT,ambient);
 100+ GLfloat diffuse[] = {light.dcvDiffuse.r,light.dcvDiffuse.g,light.dcvDiffuse.b,light.dcvDiffuse.a};
 101+ glLightfv(GL_LIGHT0+gllightin,GL_DIFFUSE,diffuse);
 102+ GLfloat specular[] = {light.dcvSpecular.r,light.dcvSpecular.g,light.dcvSpecular.b,light.dcvSpecular.a};
 103+ glLightfv(GL_LIGHT0+gllightin,GL_DIFFUSE,specular);
 104+ if(light.dltType == D3DLIGHT_DIRECTIONAL)
 105+ GLfloat position[] = {light.dvPosition.x,light.dvPosition.y,light.dvPosition.z,0.};
 106+ else GLfloat position[] = {light.dvPosition.x,light.dvPosition.y,light.dvPosition.z,1.};
 107+ glLightfv(GL_LIGHT0+gllightin,GL_POSITION,specular);
 108+ GLfloat direction[] = {light.dvDirection.x,light.dvDirection.y,light.dvDirection.z};
 109+ glLightfv(GL_LIGHT0+gllightin,GL_SPOT_DIRECTION,specular);
 110+ if(light.dltType == D3DLIGHT_SPOT)
 111+ glLightf(GL_LIGHT0+gllightin,GL_SPOT_CUTOFF,angle_dx_gl*light.dvPhi);
 112+ else glLightf(GL_LIGHT0+gllightin,GL_SPOT_CUTOFF,180.0);
 113+ glLightf(GL_LIGHT0+gllightin,GL_CONSTANT_ATTENUATION,light.dvAttenuation0);
 114+ glLightf(GL_LIGHT0+gllightin,GL_LINEAR_ATTENUATION,light.dvAttenuation1);
 115+ glLightf(GL_LIGHT0+gllightin,GL_QUADRATIC_ATTENUATION,light.dvAttenuation2);
 116+ glEnable(GL_LIGHT0+gllightin);
 117+ }
 118+ else
 119+ {
 120+ glDisable(GL_LIGHT0+gllightin);
 121+ }
 122+ gllight = gllightin;
 123+}
\ No newline at end of file
Index: ddraw/glDirect3DLight.h
@@ -0,0 +1,46 @@
 2+// DXGL
 3+// Copyright (C) 2012 William Feely
 4+
 5+// This library is free software; you can redistribute it and/or
 6+// modify it under the terms of the GNU Lesser General Public
 7+// License as published by the Free Software Foundation; either
 8+// version 2.1 of the License, or (at your option) any later version.
 9+
 10+// This library is distributed in the hope that it will be useful,
 11+// but WITHOUT ANY WARRANTY; without even the implied warranty of
 12+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 13+// Lesser General Public License for more details.
 14+
 15+// You should have received a copy of the GNU Lesser General Public
 16+// License along with this library; if not, write to the Free Software
 17+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 18+
 19+#pragma once
 20+#ifndef __GLDIRECT3DLIGHT_H
 21+#define __GLDIRECT3DLIGHT_H
 22+
 23+class glDirect3DLight : public IDirect3DLight
 24+{
 25+public:
 26+ glDirect3DLight();
 27+ glDirect3DLight(D3DLIGHT7 *light_in);
 28+ virtual ~glDirect3DLight();
 29+
 30+ HRESULT WINAPI QueryInterface(REFIID riid, void** ppvObj);
 31+ ULONG WINAPI AddRef();
 32+ ULONG WINAPI Release();
 33+ HRESULT WINAPI GetLight(LPD3DLIGHT lpLight);
 34+ void GetLight7(LPD3DLIGHT7 lpLight7);
 35+ HRESULT WINAPI Initialize(LPDIRECT3D lpDirect3D);
 36+ HRESULT WINAPI SetLight(LPD3DLIGHT lpLight);
 37+ void SetLight7(LPD3DLIGHT7 lpLight7);
 38+ void SetGLLight(int gllightin);
 39+ void GetGLLight(int *gllightout){*gllightout=gllight;};
 40+ D3DLIGHT7 light;
 41+private:
 42+ ULONG refcount;
 43+ D3DLIGHT2 convert;
 44+ int gllight;
 45+};
 46+
 47+#endif //__GLDIRECT3DLIGHT_H
\ No newline at end of file
Index: dxgltest/common.h
@@ -1,5 +1,5 @@
22 // DXGL
3 -// Copyright (C) 2011 William Feely
 3+// Copyright (C) 2011-2012 William Feely
44
55 // This library is free software; you can redistribute it and/or
66 // modify it under the terms of the GNU Lesser General Public
@@ -16,8 +16,8 @@
1717 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1818
1919 #pragma once
20 -#ifndef _STDAFX_H
21 -#define _STDAFX_H
 20+#ifndef _COMMON_H
 21+#define _COMMON_H
2222
2323 #define _CRT_SECURE_NO_WARNINGS
2424
@@ -60,4 +60,4 @@
6161 // DirectX/DXGL headers
6262 #include <ddraw.h>
6363 extern const unsigned char DefaultPalette[1024];
64 -#endif //_STDAFX_H
 64+#endif //_COMMON_H