| 462 | } |
| 463 | |
| 464 | bool MeshBase::Init(uint32 vertices, uint32 triangles, const Array<const void*, FixedAllocation<MODEL_MAX_VB>>& vbData, const void* ibData, bool use16BitIndexBuffer, Array<GPUVertexLayout*, FixedAllocation<MODEL_MAX_VB>> vbLayout) |
| 465 | { |
| 466 | CHECK_RETURN(vbData.HasItems() && vertices, true); |
| 467 | CHECK_RETURN(ibData, true); |
| 468 | CHECK_RETURN(vbLayout.Count() >= vbData.Count(), true); |
| 469 | const uint32 indices = triangles * 3; |
| 470 | if (use16BitIndexBuffer) |
| 471 | { |
| 472 | CHECK_RETURN(indices <= MAX_uint16, true); |
| 473 | } |
| 474 | ASSERT(_model); |
| 475 | GPUBuffer* vertexBuffer0 = nullptr; |
| 476 | GPUBuffer* vertexBuffer1 = nullptr; |
| 477 | GPUBuffer* vertexBuffer2 = nullptr; |
| 478 | GPUBuffer* indexBuffer = nullptr; |
| 479 | #if MODEL_USE_PRECISE_MESH_INTERSECTS |
| 480 | VertexElement positionsElement; |
| 481 | #endif |
| 482 | |
| 483 | // Create GPU buffers |
| 484 | #if GPU_ENABLE_RESOURCE_NAMING |
| 485 | const String modelPath = _model->GetPath(); |
| 486 | #define MESH_BUFFER_NAME(postfix) modelPath + TEXT(postfix) |
| 487 | #else |
| 488 | #define MESH_BUFFER_NAME(postfix) String::Empty |
| 489 | #endif |
| 490 | vertexBuffer0 = GPUDevice::Instance->CreateBuffer(MESH_BUFFER_NAME(".VB0")); |
| 491 | if (vertexBuffer0->Init(GPUBufferDescription::Vertex(vbLayout[0], vertices, vbData[0]))) |
| 492 | goto ERROR_LOAD_END; |
| 493 | if (vbData.Count() >= 2 && vbData[1]) |
| 494 | { |
| 495 | vertexBuffer1 = GPUDevice::Instance->CreateBuffer(MESH_BUFFER_NAME(".VB1")); |
| 496 | if (vertexBuffer1->Init(GPUBufferDescription::Vertex(vbLayout[1], vertices, vbData[1]))) |
| 497 | goto ERROR_LOAD_END; |
| 498 | } |
| 499 | if (vbData.Count() >= 3 && vbData[2]) |
| 500 | { |
| 501 | vertexBuffer2 = GPUDevice::Instance->CreateBuffer(MESH_BUFFER_NAME(".VB2")); |
| 502 | if (vertexBuffer2->Init(GPUBufferDescription::Vertex(vbLayout[2], vertices, vbData[2]))) |
| 503 | goto ERROR_LOAD_END; |
| 504 | } |
| 505 | indexBuffer = GPUDevice::Instance->CreateBuffer(MESH_BUFFER_NAME(".IB")); |
| 506 | if (indexBuffer->Init(GPUBufferDescription::Index(use16BitIndexBuffer ? sizeof(uint16) : sizeof(uint32), indices, ibData))) |
| 507 | goto ERROR_LOAD_END; |
| 508 | |
| 509 | // Init collision proxy |
| 510 | #if MODEL_USE_PRECISE_MESH_INTERSECTS |
| 511 | positionsElement = vbLayout[0]->FindElement(VertexElement::Types::Position); |
| 512 | if (use16BitIndexBuffer) |
| 513 | _collisionProxy.Init<uint16>(vertices, triangles, (const Float3*)vbData[0], (const uint16*)ibData, vertexBuffer0->GetStride(), positionsElement.Format); |
| 514 | else |
| 515 | _collisionProxy.Init<uint32>(vertices, triangles, (const Float3*)vbData[0], (const uint32*)ibData, vertexBuffer0->GetStride(), positionsElement.Format); |
| 516 | #endif |
| 517 | |
| 518 | // Free old buffers |
| 519 | SAFE_DELETE_GPU_RESOURCE(_vertexBuffers[0]); |
| 520 | SAFE_DELETE_GPU_RESOURCE(_vertexBuffers[1]); |
| 521 | SAFE_DELETE_GPU_RESOURCE(_vertexBuffers[2]); |
no test coverage detected