Platform_Win32_3D_D3D9_VertexBuffer.cpp
Go to the documentation of this file.00001 //*** Platform_Win32_3D_D3D9_VertexBuffer.cpp *** 00002 00003 #include "Platform_Win32_3D_D3D9_VertexBuffer.h" 00004 #include "Debug.h" 00005 00006 #define WIN32_LEAN_AND_MEAN 00007 #define VC_EXTRALEAN 00008 #include <d3d9.h> 00009 00010 //*** Constructor *** 00011 00012 Platform_Win32_3D_D3D9_VertexBuffer::Platform_Win32_3D_D3D9_VertexBuffer(IDirect3DDevice9* device, int vertexFormat, int vertexCount, bool dynamic): 00013 device_(device), 00014 vertexFormat_(vertexFormat), 00015 vertexCount_(vertexCount ), 00016 dynamic_(dynamic), 00017 vertexSize_(0), 00018 d3dFVF_(0), 00019 d3dVertexBuffer_(0), 00020 lockedData_(0) 00021 { 00022 Assert(ValidateVertexFormat(vertexFormat),"Invalid vertex format"); 00023 if (!ValidateVertexFormat(vertexFormat)) 00024 { 00025 return; 00026 } 00027 d3dFVF_=CalculateFVF(vertexFormat); 00028 vertexSize_=CalculateSize(vertexFormat); 00029 DWORD usage=0; 00030 D3DPOOL pool=D3DPOOL_DEFAULT; 00031 if (dynamic) 00032 { 00033 usage=D3DUSAGE_DYNAMIC|D3DUSAGE_WRITEONLY; 00034 pool=D3DPOOL_DEFAULT; 00035 } 00036 else 00037 { 00038 usage=D3DUSAGE_WRITEONLY; 00039 pool=D3DPOOL_MANAGED; 00040 } 00041 HRESULT hr=device->CreateVertexBuffer(vertexSize_*vertexCount_,usage,d3dFVF_,pool,&d3dVertexBuffer_,0); 00042 Assert(SUCCEEDED(hr),"Failed to create vertex buffer"); 00043 } 00044 00045 00046 //*** Destructor *** 00047 00048 Platform_Win32_3D_D3D9_VertexBuffer::~Platform_Win32_3D_D3D9_VertexBuffer() 00049 { 00050 d3dVertexBuffer_->Release(); 00051 } 00052 00053 00054 //*** Lock *** 00055 00056 void Platform_Win32_3D_D3D9_VertexBuffer::Lock(int startVertex, int vertexCount) 00057 { 00058 Assert(!lockedData_,"Vertex buffer is aready locked"); 00059 if (lockedData_) 00060 { 00061 return; 00062 } 00063 00064 DWORD flags=0; 00065 if (dynamic_) 00066 { 00067 flags=D3DLOCK_DISCARD/*|D3DLOCK_NOOVERWRITE*/; 00068 } 00069 HRESULT hr=d3dVertexBuffer_->Lock(0,0,reinterpret_cast<void**>(&lockedData_),flags); 00070 Assert(SUCCEEDED(hr),"Failed to lock vertex buffer"); 00071 } 00072 00073 00074 //*** Unlock *** 00075 00076 void Platform_Win32_3D_D3D9_VertexBuffer::Unlock() 00077 { 00078 Assert(lockedData_,"Vertex buffer is not locked"); 00079 if (!lockedData_) 00080 { 00081 return; 00082 } 00083 00084 HRESULT hr=d3dVertexBuffer_->Unlock(); 00085 Assert(SUCCEEDED(hr),"Failed to unlock vertex buffer"); 00086 lockedData_=0; 00087 } 00088 00089 00090 //*** GetPositionData *** 00091 00092 float* Platform_Win32_3D_D3D9_VertexBuffer::GetPositionData() 00093 { 00094 Assert(lockedData_,"Vertex buffer is not locked"); 00095 Assert(vertexFormat_ & VertexFormat_Position,"Component does not exist in vertexbuffer"); 00096 if (!lockedData_ || !(vertexFormat_ & VertexFormat_Position)) 00097 { 00098 return 0; 00099 } 00100 00101 return reinterpret_cast<float*>(lockedData_); 00102 } 00103 00104 00105 //*** GetPositionStride *** 00106 00107 int Platform_Win32_3D_D3D9_VertexBuffer::GetPositionStride() 00108 { 00109 Assert(lockedData_,"Vertex buffer is not locked"); 00110 if (!lockedData_) 00111 { 00112 return 0; 00113 } 00114 00115 return vertexSize_; 00116 } 00117 00118 00119 //*** GetScreenPositionData *** 00120 00121 float* Platform_Win32_3D_D3D9_VertexBuffer::GetScreenPositionData() 00122 { 00123 Assert(lockedData_,"Vertex buffer is not locked"); 00124 Assert(vertexFormat_ & VertexFormat_ScreenPosition,"Component does not exist in vertexbuffer"); 00125 if (!lockedData_ || !(vertexFormat_ & VertexFormat_ScreenPosition)) 00126 { 00127 return 0; 00128 } 00129 00130 return reinterpret_cast<float*>(lockedData_); 00131 } 00132 00133 00134 //*** GetScreenPositionStride *** 00135 00136 int Platform_Win32_3D_D3D9_VertexBuffer::GetScreenPositionStride() 00137 { 00138 Assert(lockedData_,"Vertex buffer is not locked"); 00139 if (!lockedData_) 00140 { 00141 return 0; 00142 } 00143 00144 return vertexSize_; 00145 } 00146 00147 00148 //*** GetNormalData *** 00149 00150 float* Platform_Win32_3D_D3D9_VertexBuffer::GetNormalData() 00151 { 00152 Assert(lockedData_,"Vertex buffer is not locked"); 00153 Assert(vertexFormat_ & VertexFormat_Normal,"Component does not exist in vertexbuffer"); 00154 if (!lockedData_ || !(vertexFormat_ & VertexFormat_Normal)) 00155 { 00156 return 0; 00157 } 00158 00159 unsigned char* data=lockedData_; 00160 if (vertexFormat_ & VertexFormat_Position) 00161 { 00162 data+=sizeof(float)*3; 00163 } 00164 if (vertexFormat_ & VertexFormat_ScreenPosition) 00165 { 00166 data+=sizeof(float)*4; 00167 } 00168 return reinterpret_cast<float*>(data); 00169 } 00170 00171 00172 //*** GetNormalStride *** 00173 00174 int Platform_Win32_3D_D3D9_VertexBuffer::GetNormalStride() 00175 { 00176 Assert(lockedData_,"Vertex buffer is not locked"); 00177 if (!lockedData_) 00178 { 00179 return 0; 00180 } 00181 00182 return vertexSize_; 00183 } 00184 00185 00186 //*** GetDiffuseData *** 00187 00188 unsigned int* Platform_Win32_3D_D3D9_VertexBuffer::GetDiffuseData() 00189 { 00190 Assert(lockedData_,"Vertex buffer is not locked"); 00191 Assert(vertexFormat_ & VertexFormat_Diffuse,"Component does not exist in vertexbuffer"); 00192 if (!lockedData_ || !(vertexFormat_ & VertexFormat_Diffuse)) 00193 { 00194 return 0; 00195 } 00196 00197 unsigned char* data=lockedData_; 00198 if (vertexFormat_ & VertexFormat_Position) 00199 { 00200 data+=sizeof(float)*3; 00201 } 00202 if (vertexFormat_ & VertexFormat_ScreenPosition) 00203 { 00204 data+=sizeof(float)*4; 00205 } 00206 if (vertexFormat_ & VertexFormat_Normal) 00207 { 00208 data+=sizeof(float)*3; 00209 } 00210 return reinterpret_cast<unsigned int*>(data); 00211 } 00212 00213 00214 //*** GetDiffuseStride *** 00215 00216 int Platform_Win32_3D_D3D9_VertexBuffer::GetDiffuseStride() 00217 { 00218 Assert(lockedData_,"Vertex buffer is not locked"); 00219 if (!lockedData_) 00220 { 00221 return 0; 00222 } 00223 00224 return vertexSize_; 00225 } 00226 00227 00228 //*** GetSpecularData *** 00229 00230 unsigned int* Platform_Win32_3D_D3D9_VertexBuffer::GetSpecularData() 00231 { 00232 Assert(lockedData_,"Vertex buffer is not locked"); 00233 Assert(vertexFormat_ & VertexFormat_Specular,"Component does not exist in vertexbuffer"); 00234 if (!lockedData_ || !(vertexFormat_ & VertexFormat_Specular)) 00235 { 00236 return 0; 00237 } 00238 00239 unsigned char* data=lockedData_; 00240 if (vertexFormat_ & VertexFormat_Position) 00241 { 00242 data+=sizeof(float)*3; 00243 } 00244 if (vertexFormat_ & VertexFormat_ScreenPosition) 00245 { 00246 data+=sizeof(float)*4; 00247 } 00248 if (vertexFormat_ & VertexFormat_Normal) 00249 { 00250 data+=sizeof(float)*3; 00251 } 00252 if (vertexFormat_ & VertexFormat_Diffuse) 00253 { 00254 data+=sizeof(unsigned int); 00255 } 00256 return reinterpret_cast<unsigned int*>(data); 00257 } 00258 00259 00260 //*** GetSpecularStride *** 00261 00262 int Platform_Win32_3D_D3D9_VertexBuffer::GetSpecularStride() 00263 { 00264 Assert(lockedData_,"Vertex buffer is not locked"); 00265 if (!lockedData_) 00266 { 00267 return 0; 00268 } 00269 00270 return vertexSize_; 00271 } 00272 00273 00274 //*** GetUVData *** 00275 00276 float* Platform_Win32_3D_D3D9_VertexBuffer::GetUVData(int textureSet) 00277 { 00278 Assert(lockedData_,"Vertex buffer is not locked"); 00279 Assert(GetNumberOfTextureSets(vertexFormat_)>0,"Component does not exist in vertexbuffer"); 00280 Assert(textureSet>=0 && textureSet<GetNumberOfTextureSets(vertexFormat_),"Invalid texture set index"); 00281 if (!lockedData_ || GetNumberOfTextureSets(vertexFormat_)<=0 || textureSet<0 || textureSet>=GetNumberOfTextureSets(vertexFormat_)) 00282 { 00283 return 0; 00284 } 00285 00286 unsigned char* data=lockedData_; 00287 if (vertexFormat_ & VertexFormat_Position) 00288 { 00289 data+=sizeof(float)*3; 00290 } 00291 if (vertexFormat_ & VertexFormat_ScreenPosition) 00292 { 00293 data+=sizeof(float)*4; 00294 } 00295 if (vertexFormat_ & VertexFormat_Normal) 00296 { 00297 data+=sizeof(float)*3; 00298 } 00299 if (vertexFormat_ & VertexFormat_Diffuse) 00300 { 00301 data+=sizeof(unsigned int); 00302 } 00303 if (vertexFormat_ & VertexFormat_Specular) 00304 { 00305 data+=sizeof(unsigned int); 00306 } 00307 data+=sizeof(float)*2*textureSet; 00308 return reinterpret_cast<float*>(data); 00309 } 00310 00311 00312 //*** GetUVStride *** 00313 00314 int Platform_Win32_3D_D3D9_VertexBuffer::GetUVStride(int textureSet) 00315 { 00316 Assert(lockedData_,"Vertex buffer is not locked"); 00317 if (!lockedData_) 00318 { 00319 return 0; 00320 } 00321 00322 return vertexSize_; 00323 } 00324 00325 00326 //*** Bind *** 00327 00328 void Platform_Win32_3D_D3D9_VertexBuffer::Bind() 00329 { 00330 Assert(!lockedData_,"Vertex buffer is locked"); 00331 if (lockedData_) 00332 { 00333 return; 00334 } 00335 00336 device_->SetFVF(d3dFVF_); 00337 device_->SetStreamSource(0,d3dVertexBuffer_,0,vertexSize_); 00338 } 00339 00340 00341 00342 //*** ValidateVertexFormat *** 00343 00344 bool Platform_Win32_3D_D3D9_VertexBuffer::ValidateVertexFormat(int vertexFormat) 00345 { 00346 int texturesetFlags=0; 00347 00348 if (vertexFormat & VertexFormat_1TextureSets) 00349 { 00350 texturesetFlags++; 00351 } 00352 00353 if (vertexFormat & VertexFormat_2TextureSets) 00354 { 00355 texturesetFlags++; 00356 } 00357 00358 if (vertexFormat & VertexFormat_3TextureSets) 00359 { 00360 texturesetFlags++; 00361 } 00362 00363 if (vertexFormat & VertexFormat_4TextureSets) 00364 { 00365 texturesetFlags++; 00366 } 00367 00368 if (vertexFormat & VertexFormat_5TextureSets) 00369 { 00370 texturesetFlags++; 00371 } 00372 00373 if (vertexFormat & VertexFormat_6TextureSets) 00374 { 00375 texturesetFlags++; 00376 } 00377 00378 if (vertexFormat & VertexFormat_7TextureSets) 00379 { 00380 texturesetFlags++; 00381 } 00382 00383 if (vertexFormat & VertexFormat_8TextureSets) 00384 { 00385 texturesetFlags++; 00386 } 00387 00388 if (texturesetFlags>1) 00389 { 00390 return false; 00391 } 00392 00393 if (!(vertexFormat & VertexFormat_Position) && !(vertexFormat & VertexFormat_ScreenPosition)) 00394 { 00395 return false; 00396 } 00397 00398 if ((vertexFormat & VertexFormat_Position) && (vertexFormat & VertexFormat_ScreenPosition)) 00399 { 00400 return false; 00401 } 00402 00403 if ((vertexFormat & VertexFormat_ScreenPosition) && (vertexFormat & VertexFormat_Normal)) 00404 { 00405 return false; 00406 } 00407 00408 return true; 00409 } 00410 00411 00412 //*** CalculateFVF *** 00413 00414 unsigned long Platform_Win32_3D_D3D9_VertexBuffer::CalculateFVF(int vertexFormat) 00415 { 00416 unsigned FVF=0; 00417 00418 if (vertexFormat & VertexFormat_Position) 00419 { 00420 FVF|=D3DFVF_XYZ; 00421 } 00422 00423 if (vertexFormat & VertexFormat_ScreenPosition) 00424 { 00425 FVF|=D3DFVF_XYZRHW; 00426 } 00427 00428 if (vertexFormat & VertexFormat_Normal) 00429 { 00430 FVF|=D3DFVF_NORMAL; 00431 } 00432 00433 if (vertexFormat & VertexFormat_Diffuse) 00434 { 00435 FVF|=D3DFVF_DIFFUSE; 00436 } 00437 00438 if (vertexFormat & VertexFormat_Specular) 00439 { 00440 FVF|=D3DFVF_SPECULAR; 00441 } 00442 00443 if (vertexFormat & VertexFormat_1TextureSets) 00444 { 00445 FVF|=D3DFVF_TEX1; 00446 } 00447 00448 if (vertexFormat & VertexFormat_2TextureSets) 00449 { 00450 FVF|=D3DFVF_TEX2; 00451 } 00452 00453 if (vertexFormat & VertexFormat_3TextureSets) 00454 { 00455 FVF|=D3DFVF_TEX3; 00456 } 00457 00458 if (vertexFormat & VertexFormat_4TextureSets) 00459 { 00460 FVF|=D3DFVF_TEX4; 00461 } 00462 00463 if (vertexFormat & VertexFormat_5TextureSets) 00464 { 00465 FVF|=D3DFVF_TEX5; 00466 } 00467 00468 if (vertexFormat & VertexFormat_6TextureSets) 00469 { 00470 FVF|=D3DFVF_TEX6; 00471 } 00472 00473 if (vertexFormat & VertexFormat_7TextureSets) 00474 { 00475 FVF|=D3DFVF_TEX7; 00476 } 00477 00478 if (vertexFormat & VertexFormat_8TextureSets) 00479 { 00480 FVF|=D3DFVF_TEX8; 00481 } 00482 00483 return FVF; 00484 } 00485 00486 00487 //*** CalculateSize *** 00488 00489 int Platform_Win32_3D_D3D9_VertexBuffer::CalculateSize(int vertexFormat) 00490 { 00491 int size=0; 00492 00493 00494 if (vertexFormat & VertexFormat_Position) 00495 { 00496 size+=sizeof(float)*3; 00497 } 00498 00499 if (vertexFormat & VertexFormat_ScreenPosition) 00500 { 00501 size+=sizeof(float)*4; 00502 } 00503 00504 if (vertexFormat & VertexFormat_Normal) 00505 { 00506 size+=sizeof(float)*3; 00507 } 00508 00509 if (vertexFormat & VertexFormat_Diffuse) 00510 { 00511 size+=sizeof(unsigned int); 00512 } 00513 00514 if (vertexFormat & VertexFormat_Specular) 00515 { 00516 size+=sizeof(unsigned int); 00517 } 00518 00519 if (vertexFormat & VertexFormat_1TextureSets) 00520 { 00521 size+=sizeof(float)*2; 00522 } 00523 00524 if (vertexFormat & VertexFormat_2TextureSets) 00525 { 00526 size+=sizeof(float)*2*2; 00527 } 00528 00529 if (vertexFormat & VertexFormat_3TextureSets) 00530 { 00531 size+=sizeof(float)*2*3; 00532 } 00533 00534 if (vertexFormat & VertexFormat_4TextureSets) 00535 { 00536 size+=sizeof(float)*2*4; 00537 } 00538 00539 if (vertexFormat & VertexFormat_5TextureSets) 00540 { 00541 size+=sizeof(float)*2*5; 00542 } 00543 00544 if (vertexFormat & VertexFormat_6TextureSets) 00545 { 00546 size+=sizeof(float)*2*6; 00547 } 00548 00549 if (vertexFormat & VertexFormat_7TextureSets) 00550 { 00551 size+=sizeof(float)*2*7; 00552 } 00553 00554 if (vertexFormat & VertexFormat_8TextureSets) 00555 { 00556 size+=sizeof(float)*2*8; 00557 } 00558 00559 return size; 00560 } 00561 00562 00563 //*** GetNumberOfTextureSets *** 00564 00565 int Platform_Win32_3D_D3D9_VertexBuffer::GetNumberOfTextureSets(int vertexFormat) 00566 { 00567 if (vertexFormat & VertexFormat_1TextureSets) 00568 { 00569 return 1; 00570 } 00571 00572 if (vertexFormat & VertexFormat_2TextureSets) 00573 { 00574 return 2; 00575 } 00576 00577 if (vertexFormat & VertexFormat_3TextureSets) 00578 { 00579 return 3; 00580 } 00581 00582 if (vertexFormat & VertexFormat_4TextureSets) 00583 { 00584 return 4; 00585 } 00586 00587 if (vertexFormat & VertexFormat_5TextureSets) 00588 { 00589 return 5; 00590 } 00591 00592 if (vertexFormat & VertexFormat_6TextureSets) 00593 { 00594 return 6; 00595 } 00596 00597 if (vertexFormat & VertexFormat_7TextureSets) 00598 { 00599 return 7; 00600 } 00601 00602 if (vertexFormat & VertexFormat_8TextureSets) 00603 { 00604 return 8; 00605 } 00606 00607 return 0; 00608 }
Reproduction/republishing of any material on this site without permission is strictly prohibited.
