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