Platform_Win32_3D_D3D9.cpp
Go to the documentation of this file.00001 //*** Platform_Win32_3D_D3D9.cpp *** 00002 00003 #include "Platform_Win32_3D_D3D9.h" 00004 #include "Debug.h" 00005 #include "StandardLibrary.h" 00006 00007 #define WIN32_LEAN_AND_MEAN 00008 #define VC_EXTRALEAN 00009 #include <d3d9.h> 00010 00011 #include "Platform_Win32_3D_D3D9_VertexBuffer.h" 00012 #include "Platform_Win32_3D_D3D9_IndexBuffer.h" 00013 #include "Platform_Win32_3D_D3D9_Texture.h" 00014 00015 //*** Constructor *** 00016 00017 Platform_Win32_3D_D3D9::Platform_Win32_3D_D3D9(struct HWND__* windowHandle, bool fullscreen, int screenWidth, int screenHeight): 00018 windowHandle_(windowHandle), 00019 fullscreen_(fullscreen), 00020 screenWidth_(screenWidth), 00021 screenHeight_(screenHeight), 00022 d3dDLL_(0), 00023 direct3D_(0), 00024 device_(0) 00025 { 00026 DebugPrint(("Creating Platform_Win32_3D_D3D9...\n\tfullscreen:%d\n\tscreenWidth:%d\n\tscreenHeight:%d\n",fullscreen,screenWidth,screenHeight)); 00027 } 00028 00029 00030 //*** Setup *** 00031 00032 bool Platform_Win32_3D_D3D9::Setup() 00033 { 00034 00035 // Create D3D object 00036 d3dDLL_=LoadLibrary("d3d9.dll"); 00037 if (!d3dDLL_) 00038 { 00039 DebugPrint(("Couldn't find d3d9.dll\n")); 00040 return false; 00041 } 00042 00043 // Create DirectDraw object 00044 typedef IDirect3D9 * (WINAPI *Direct3DCreate9Definition)( unsigned int SDKVersion ); 00045 Direct3DCreate9Definition Direct3DCreate9=(Direct3DCreate9Definition)GetProcAddress((HMODULE)d3dDLL_, "Direct3DCreate9"); 00046 if (Direct3DCreate9==0) 00047 { 00048 DebugPrint(("Failed to find function entry point in d3d9.dll\n")); 00049 return false; 00050 } 00051 00052 direct3D_=Direct3DCreate9(D3D_SDK_VERSION); 00053 if (!direct3D_) 00054 { 00055 DebugPrint(("Couldn't create Direct3D object\n")); 00056 return false; 00057 } 00058 00059 // Set antialias quality 00060 D3DMULTISAMPLE_TYPE multiSampleType=D3DMULTISAMPLE_NONE; 00061 DWORD multiSampleQuality=0; 00062 00063 // Check for antialiasing 00064 /* if( SUCCEEDED(direct3D_->CheckDeviceMultiSampleType( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL , D3DFMT_A8R8G8B8, !fullscreen_, D3DMULTISAMPLE_2_SAMPLES, &multiSampleQuality) ) ) 00065 { 00066 multiSampleType=D3DMULTISAMPLE_2_SAMPLES; 00067 } 00068 else 00069 { 00070 multiSampleQuality=0; 00071 } 00072 */ 00073 // Get present parameters 00074 D3DPRESENT_PARAMETERS d3dpp; 00075 if (fullscreen_) 00076 { 00077 // SetWindowPos(windowHandle_,HWND_TOPMOST,0,0,0,0,SWP_NOSIZE | SWP_NOMOVE | SWP_NOREDRAW | SWP_NOACTIVATE | SWP_NOCOPYBITS | SWP_NOSENDCHANGING); 00078 // SetWindowLong(windowHandle_,GWL_STYLE,WS_POPUPWINDOW|WS_VISIBLE); 00079 ZeroMemory( &d3dpp, sizeof(d3dpp) ); 00080 00081 d3dpp.BackBufferWidth=screenWidth_; 00082 d3dpp.BackBufferHeight=screenHeight_; 00083 d3dpp.BackBufferFormat=D3DFMT_A8R8G8B8; 00084 d3dpp.BackBufferCount=2; 00085 d3dpp.MultiSampleType=multiSampleType; 00086 d3dpp.MultiSampleQuality=multiSampleQuality; 00087 d3dpp.SwapEffect=D3DSWAPEFFECT_FLIP; 00088 d3dpp.hDeviceWindow=windowHandle_; 00089 d3dpp.Windowed=FALSE; 00090 d3dpp.EnableAutoDepthStencil=TRUE; 00091 d3dpp.AutoDepthStencilFormat=D3DFMT_D24S8; 00092 d3dpp.Flags=0; 00093 d3dpp.FullScreen_RefreshRateInHz=D3DPRESENT_RATE_DEFAULT; 00094 d3dpp.PresentationInterval=D3DPRESENT_INTERVAL_ONE; 00095 } 00096 else 00097 { 00098 ZeroMemory( &d3dpp, sizeof(d3dpp) ); 00099 d3dpp.Windowed = TRUE; 00100 d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; 00101 d3dpp.BackBufferFormat=D3DFMT_A8R8G8B8; 00102 d3dpp.EnableAutoDepthStencil = TRUE; 00103 d3dpp.BackBufferCount=1; 00104 d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8; 00105 d3dpp.MultiSampleType=multiSampleType; 00106 d3dpp.MultiSampleQuality=multiSampleQuality; 00107 d3dpp.FullScreen_RefreshRateInHz=D3DPRESENT_RATE_DEFAULT; 00108 d3dpp.PresentationInterval=D3DPRESENT_INTERVAL_ONE; 00109 } 00110 00111 // Set device adapter to use 00112 UINT AdapterToUse=D3DADAPTER_DEFAULT; 00113 D3DDEVTYPE DeviceType=D3DDEVTYPE_HAL; 00114 00115 // Look for 'NVIDIA NVPerfHUD' adapter 00116 // If it is present, override default settings 00117 for (UINT Adapter=0;Adapter<direct3D_->GetAdapterCount();Adapter++) 00118 { 00119 D3DADAPTER_IDENTIFIER9 Identifier; 00120 HRESULT res=direct3D_->GetAdapterIdentifier(Adapter,0,&Identifier); 00121 if (res==S_OK && strcmp(Identifier.Description,"NVIDIA NVPerfHUD")==0) 00122 { 00123 AdapterToUse=Adapter; 00124 DeviceType=D3DDEVTYPE_REF; 00125 break; 00126 } 00127 } 00128 00129 // Create the D3DDevice 00130 HRESULT ret=direct3D_->CreateDevice(AdapterToUse,DeviceType,windowHandle_,D3DCREATE_PUREDEVICE | D3DCREATE_HARDWARE_VERTEXPROCESSING,&d3dpp,&device_); 00131 if (FAILED(ret) || !device_) 00132 { 00133 DebugPrint(("Couldn't create Direct3D Device\n")); 00134 return false; 00135 } 00136 00137 00138 // Turn on antialiasing 00139 if (multiSampleType!=D3DMULTISAMPLE_NONE) 00140 { 00141 device_->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS,TRUE); 00142 } 00143 00144 device_->SetRenderState(D3DRS_CULLMODE,D3DCULL_CCW); 00145 // device_->SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE); 00146 device_->SetRenderState(D3DRS_LIGHTING,TRUE); 00147 // device_->SetRenderState(D3DRS_NORMALIZENORMALS,TRUE); 00148 // device_->SetRenderState(D3DRS_SPECULARENABLE,TRUE); 00149 // device_->SetRenderState(D3DRS_LOCALVIEWER,TRUE); 00150 device_->SetRenderState(D3DRS_COLORVERTEX,TRUE); 00151 00152 device_->SetRenderState(D3DRS_ALPHATESTENABLE,TRUE); 00153 device_->SetRenderState(D3DRS_ALPHAFUNC,D3DCMP_GREATEREQUAL); 00154 device_->SetRenderState(D3DRS_ALPHAREF,1); 00155 device_->SetRenderState(D3DRS_ALPHABLENDENABLE,TRUE); 00156 device_->SetRenderState(D3DRS_SRCBLEND,D3DBLEND_SRCALPHA); 00157 device_->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_INVSRCALPHA); 00158 00159 device_->SetRenderState(D3DRS_AMBIENTMATERIALSOURCE,D3DMCS_MATERIAL); 00160 device_->SetRenderState(D3DRS_DIFFUSEMATERIALSOURCE,D3DMCS_MATERIAL); 00161 device_->SetRenderState(D3DRS_SPECULARMATERIALSOURCE,D3DMCS_MATERIAL); 00162 device_->SetRenderState(D3DRS_EMISSIVEMATERIALSOURCE,D3DMCS_MATERIAL); 00163 00164 device_->SetRenderState(D3DRS_AMBIENTMATERIALSOURCE,D3DMCS_COLOR1); 00165 device_->SetRenderState(D3DRS_DIFFUSEMATERIALSOURCE,D3DMCS_COLOR1); 00166 device_->SetRenderState(D3DRS_SPECULARMATERIALSOURCE,D3DMCS_COLOR1); 00167 // device_->SetRenderState(D3DRS_EMISSIVEMATERIALSOURCE,D3DMCS_MATERIAL); 00168 00169 device_->SetTextureStageState(0,D3DTSS_COLOROP,D3DTOP_MODULATE); 00170 device_->SetTextureStageState(0,D3DTSS_COLORARG1,D3DTA_TEXTURE); 00171 device_->SetTextureStageState(0,D3DTSS_COLORARG2,D3DTA_DIFFUSE); 00172 00173 // device_->SetTextureStageState(1,D3DTSS_COLOROP,D3DTOP_SUBTRACT); 00174 // device_->SetTextureStageState(1,D3DTSS_COLORARG1,D3DTA_CURRENT); 00175 // device_->SetTextureStageState(1,D3DTSS_COLORARG2,D3DTA_TEXTURE); 00176 00177 device_->SetTextureStageState(0,D3DTSS_ALPHAOP,D3DTOP_MODULATE); 00178 device_->SetTextureStageState(0,D3DTSS_ALPHAARG1,D3DTA_TEXTURE); 00179 device_->SetTextureStageState(0,D3DTSS_ALPHAARG2,D3DTA_DIFFUSE); 00180 00181 device_->SetSamplerState(0,D3DSAMP_MINFILTER,D3DTEXF_LINEAR); 00182 device_->SetSamplerState(0,D3DSAMP_MAGFILTER,D3DTEXF_LINEAR); 00183 device_->SetSamplerState(0,D3DSAMP_MIPFILTER,D3DTEXF_LINEAR); 00184 00185 device_->SetSamplerState(1,D3DSAMP_MINFILTER,D3DTEXF_LINEAR); 00186 device_->SetSamplerState(1,D3DSAMP_MAGFILTER,D3DTEXF_LINEAR); 00187 device_->SetSamplerState(1,D3DSAMP_MIPFILTER,D3DTEXF_LINEAR); 00188 00189 // Enable fog 00190 /* 00191 unsigned int color=0; 00192 float Start = 0.0f; // For linear mode 00193 float End = 17.0f; 00194 device_->SetRenderState(D3DRS_FOGENABLE,TRUE); 00195 device_->SetRenderState(D3DRS_FOGVERTEXMODE,D3DFOG_LINEAR); 00196 device_->SetRenderState(D3DRS_FOGCOLOR, color); 00197 device_->SetRenderState(D3DRS_FOGSTART, *static_cast<DWORD*>(&Start)); 00198 device_->SetRenderState(D3DRS_FOGEND, *static_cast<DWORD*>(&End)); 00199 */ 00200 00201 // Setup default material 00202 D3DMATERIAL9 mtrl; 00203 ZeroMemory( &mtrl, sizeof(mtrl) ); 00204 mtrl.Diffuse.r = mtrl.Ambient.r = 1.0f; 00205 mtrl.Diffuse.g = mtrl.Ambient.g = 1.0f; 00206 mtrl.Diffuse.b = mtrl.Ambient.b = 1.0f; 00207 mtrl.Diffuse.a = mtrl.Ambient.a = 1.0f; 00208 mtrl.Power=4; 00209 mtrl.Specular.r=1.0f; 00210 mtrl.Specular.g=1.0f; 00211 mtrl.Specular.b=1.0f; 00212 mtrl.Specular.a=1.0f; 00213 device_->SetMaterial( &mtrl ); 00214 00215 00216 lightCount_=8; 00217 00218 /* 00219 D3DLIGHT9 light; 00220 memset(&light,0,sizeof(light)); 00221 00222 light.Type = D3DLIGHT_DIRECTIONAL; 00223 00224 D3DCOLORVALUE d3dcolor; 00225 d3dcolor.r=1; 00226 d3dcolor.g=1; 00227 d3dcolor.b=1; 00228 d3dcolor.a=1; 00229 00230 light.Diffuse=d3dcolor; 00231 light.Specular=d3dcolor; 00232 00233 light.Direction.x=0; 00234 light.Direction.y=0; 00235 light.Direction.z=-1; 00236 00237 device_->SetLight(0,&light); 00238 00239 device_->LightEnable(0,TRUE); 00240 */ 00241 00242 00243 DebugPrint(("...Platform_Win32_3D_D3D9 created\n")); 00244 return true; 00245 } 00246 00247 00248 //*** Destructor *** 00249 00250 Platform_Win32_3D_D3D9::~Platform_Win32_3D_D3D9() 00251 { 00252 DebugPrint(("Destroying Platform_Win32_3D_D3D9...\n")); 00253 00254 if (device_) 00255 { 00256 device_->Release(); 00257 } 00258 00259 if (direct3D_) 00260 { 00261 direct3D_->Release(); 00262 } 00263 00264 if (d3dDLL_) 00265 { 00266 FreeLibrary((HMODULE)d3dDLL_); 00267 } 00268 00269 if (fullscreen_) 00270 { 00271 // SetWindowLong(windowHandle_,GWL_STYLE,WS_OVERLAPPEDWINDOW | WS_VISIBLE ); 00272 } 00273 00274 // SetWindowPos(windowHandle_,HWND_NOTOPMOST,0,0,0,0,SWP_NOSIZE | SWP_NOMOVE | SWP_NOREDRAW | SWP_NOACTIVATE | SWP_NOCOPYBITS | SWP_NOSENDCHANGING); 00275 00276 00277 DebugPrint(("... Platform_Win32_3D_D3D9 destroyed\n")); 00278 } 00279 00280 00281 //*** IsCompressedTextureFormatOk *** 00282 /* 00283 bool Platform_Win32_3D_D3D9::IsCompressedTextureFormatOk( D3DFORMAT TextureFormat, D3DFORMAT AdapterFormat ) 00284 { 00285 HRESULT hr = device_->CheckDeviceFormat( D3DADAPTER_DEFAULT, 00286 D3DDEVTYPE_HAL, 00287 AdapterFormat, 00288 0, 00289 D3DRTYPE_TEXTURE, 00290 TextureFormat); 00291 00292 return SUCCEEDED( hr ); 00293 } 00294 */ 00295 00296 00297 //*** BeginScene *** 00298 00299 void Platform_Win32_3D_D3D9::BeginScene(unsigned int color, float z, unsigned int stencil) 00300 { 00301 device_->Clear( 0, 0, D3DCLEAR_TARGET|D3DCLEAR_STENCIL|D3DCLEAR_ZBUFFER,color, z, stencil ); 00302 device_->BeginScene(); 00303 } 00304 00305 00306 //*** EndScene *** 00307 00308 void Platform_Win32_3D_D3D9::EndScene() 00309 { 00310 device_->EndScene(); 00311 device_->Present(0, 0, 0, 0 ); 00312 } 00313 00314 00315 //*** CreateVertexBuffer *** 00316 00317 Platform_3D_VertexBuffer* Platform_Win32_3D_D3D9::CreateVertexBuffer(int vertexFormat, int vertexCount, bool dynamic) 00318 { 00319 return new Platform_Win32_3D_D3D9_VertexBuffer(device_, vertexFormat, vertexCount, dynamic); 00320 } 00321 00322 00323 //*** CreateIndexBuffer *** 00324 00325 Platform_3D_IndexBuffer* Platform_Win32_3D_D3D9::CreateIndexBuffer(int indexCount, bool dynamic) 00326 { 00327 return new Platform_Win32_3D_D3D9_IndexBuffer(device_, indexCount, dynamic); 00328 } 00329 00330 00331 //*** CreateTexture *** 00332 00333 Platform_3D_Texture* Platform_Win32_3D_D3D9::CreateTexture(void* data, int size) 00334 { 00335 return new Platform_Win32_3D_D3D9_Texture(device_, data, size); 00336 } 00337 00338 00339 //*** CreateTexture *** 00340 00341 Platform_3D_Texture* Platform_Win32_3D_D3D9::CreateTexture(int width, int height, void* data) 00342 { 00343 return new Platform_Win32_3D_D3D9_Texture(device_, width, height, data); 00344 } 00345 00346 00347 //*** Render *** 00348 00349 void Platform_Win32_3D_D3D9::Render(int startVertex, int vertexCount, int startIndex, int indexCount) 00350 { 00351 device_->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,startVertex,0,vertexCount,startIndex,indexCount/3); 00352 } 00353 00354 00355 //*** Render *** 00356 00357 void Platform_Win32_3D_D3D9::Render(int startVertex, int vertexCount) 00358 { 00359 device_->DrawPrimitive(D3DPT_TRIANGLELIST,startVertex,vertexCount/3); 00360 } 00361 00362 00363 //*** RenderStrip *** 00364 00365 void Platform_Win32_3D_D3D9::RenderStrip(int startVertex, int vertexCount, int startIndex, int indexCount) 00366 { 00367 device_->DrawIndexedPrimitive(D3DPT_TRIANGLESTRIP,startVertex,0,vertexCount,startIndex,indexCount-2); 00368 } 00369 00370 00371 //*** RenderStrip *** 00372 00373 void Platform_Win32_3D_D3D9::RenderStrip(int startVertex, int vertexCount) 00374 { 00375 device_->DrawPrimitive(D3DPT_TRIANGLESTRIP,startVertex, vertexCount-2); 00376 } 00377 00378 00379 //*** RenderFan *** 00380 00381 void Platform_Win32_3D_D3D9::RenderFan(int startVertex, int vertexCount, int startIndex, int indexCount) 00382 { 00383 device_->DrawIndexedPrimitive(D3DPT_TRIANGLEFAN,startVertex,0,vertexCount,startIndex,indexCount/3); 00384 } 00385 00386 00387 //*** RenderFan *** 00388 00389 void Platform_Win32_3D_D3D9::RenderFan(int startVertex, int vertexCount) 00390 { 00391 device_->DrawPrimitive(D3DPT_TRIANGLEFAN,startVertex,vertexCount/3); 00392 } 00393 00394 00395 //*** SetWorldMatrix *** 00396 00397 void Platform_Win32_3D_D3D9::SetWorldMatrix(const float worldMatrix[16]) 00398 { 00399 device_->SetTransform(D3DTS_WORLD,reinterpret_cast<const D3DMATRIX*>(worldMatrix)); 00400 } 00401 00402 00403 //*** SetViewMatrix *** 00404 00405 void Platform_Win32_3D_D3D9::SetViewMatrix(const float viewMatrix[16]) 00406 { 00407 device_->SetTransform(D3DTS_VIEW,reinterpret_cast<const D3DMATRIX*>(viewMatrix)); 00408 } 00409 00410 00411 //*** SetProjectionMatrix *** 00412 00413 void Platform_Win32_3D_D3D9::SetProjectionMatrix(const float projectionMatrix[16]) 00414 { 00415 device_->SetTransform(D3DTS_PROJECTION,reinterpret_cast<const D3DMATRIX*>(projectionMatrix)); 00416 } 00417 00418 00419 //*** GetLightCount *** 00420 00421 int Platform_Win32_3D_D3D9::GetLightCount() 00422 { 00423 return lightCount_; 00424 } 00425 00426 00427 //*** EnableDirectionalLight *** 00428 00429 void Platform_Win32_3D_D3D9::EnableDirectionalLight(int lightIndex, float colorR, float colorG, float colorB, float directionX, float directionY, float directionZ) 00430 { 00431 Assert(lightIndex>=0 && lightIndex<lightCount_,"Light index out of range"); 00432 if (lightIndex<0 || lightIndex>=lightCount_) 00433 { 00434 return; 00435 } 00436 00437 D3DLIGHT9 light; 00438 memset(&light,0,sizeof(light)); 00439 00440 light.Type = D3DLIGHT_DIRECTIONAL; 00441 00442 D3DCOLORVALUE d3dcolor; 00443 d3dcolor.r=colorR; 00444 d3dcolor.g=colorG; 00445 d3dcolor.b=colorB; 00446 d3dcolor.a=1; 00447 00448 light.Diffuse=d3dcolor; 00449 light.Specular=d3dcolor; 00450 00451 light.Direction.x=directionX; 00452 light.Direction.y=directionY; 00453 light.Direction.z=directionZ; 00454 00455 device_->SetLight(lightIndex,&light); 00456 00457 device_->LightEnable(lightIndex,TRUE); 00458 } 00459 00460 00461 //*** DisableLight *** 00462 00463 void Platform_Win32_3D_D3D9::DisableLight(int lightIndex) 00464 { 00465 Assert(lightIndex>=0 && lightIndex<lightCount_,"Light index out of range"); 00466 if (lightIndex<0 || lightIndex>=lightCount_) 00467 { 00468 return; 00469 } 00470 00471 device_->LightEnable(lightIndex,FALSE); 00472 } 00473 00474 00475 //*** SetAmbientLight *** 00476 00477 void Platform_Win32_3D_D3D9::SetAmbientLight(unsigned int color) 00478 { 00479 device_->SetRenderState(D3DRS_AMBIENT,color); 00480 } 00481 00482 00483 //*** EnableLighting *** 00484 00485 void Platform_Win32_3D_D3D9::EnableLighting(bool enabled) 00486 { 00487 if (enabled) 00488 { 00489 device_->SetRenderState(D3DRS_LIGHTING,TRUE); 00490 } 00491 else 00492 { 00493 device_->SetRenderState(D3DRS_LIGHTING,FALSE); 00494 } 00495 } 00496 00497 00498 //*** EnableZRead *** 00499 00500 void Platform_Win32_3D_D3D9::EnableZRead(bool enabled) 00501 { 00502 if (enabled) 00503 { 00504 device_->SetRenderState(D3DRS_ZENABLE,TRUE); 00505 } 00506 else 00507 { 00508 device_->SetRenderState(D3DRS_ZENABLE,FALSE); 00509 } 00510 } 00511 00512 00513 //*** EnableZWrite *** 00514 00515 void Platform_Win32_3D_D3D9::EnableZWrite(bool enabled) 00516 { 00517 if (enabled) 00518 { 00519 device_->SetRenderState(D3DRS_ZWRITEENABLE,TRUE); 00520 } 00521 else 00522 { 00523 device_->SetRenderState(D3DRS_ZWRITEENABLE,FALSE); 00524 } 00525 } 00526
Reproduction/republishing of any material on this site without permission is strictly prohibited.
