* Construct the vertex buffer for this model * * @param renderer A pointer to the model renderer. Used to allocate the vertex buffer. */
| 358 | * @param renderer A pointer to the model renderer. Used to allocate the vertex buffer. |
| 359 | */ |
| 360 | void FOBJModel::BuildVertexBuffer(FModelRenderer *renderer) |
| 361 | { |
| 362 | if (GetVertexBuffer(renderer->GetType())) |
| 363 | { |
| 364 | return; |
| 365 | } |
| 366 | |
| 367 | unsigned int vbufsize = 0; |
| 368 | |
| 369 | for (size_t i = 0; i < surfaces.Size(); i++) |
| 370 | { |
| 371 | ConstructSurfaceTris(surfaces[i]); |
| 372 | surfaces[i].vbStart = vbufsize; |
| 373 | vbufsize += surfaces[i].numTris * 3; |
| 374 | } |
| 375 | // Initialize/populate vertFaces |
| 376 | if (hasMissingNormals && hasSmoothGroups) |
| 377 | { |
| 378 | AddVertFaces(); |
| 379 | } |
| 380 | |
| 381 | auto vbuf = renderer->CreateVertexBuffer(false,true); |
| 382 | SetVertexBuffer(renderer->GetType(), vbuf); |
| 383 | |
| 384 | FModelVertex *vertptr = vbuf->LockVertexBuffer(vbufsize); |
| 385 | |
| 386 | for (unsigned int i = 0; i < surfaces.Size(); i++) |
| 387 | { |
| 388 | for (unsigned int j = 0; j < surfaces[i].numTris; j++) |
| 389 | { |
| 390 | for (size_t side = 0; side < 3; side++) |
| 391 | { |
| 392 | FModelVertex *mdv = vertptr + |
| 393 | side + j * 3 + // Current surface and previous triangles |
| 394 | surfaces[i].vbStart; // Previous surfaces |
| 395 | |
| 396 | OBJFaceSide &curSide = surfaces[i].tris[j].sides[2 - side]; |
| 397 | |
| 398 | int vidx = curSide.vertref; |
| 399 | int uvidx = (curSide.uvref >= 0 && (unsigned int)curSide.uvref < uvs.Size()) ? curSide.uvref : 0; |
| 400 | int nidx = curSide.normref; |
| 401 | |
| 402 | FVector3 curVvec = RealignVector(verts[vidx]); |
| 403 | FVector2 curUvec = FixUV(uvs[uvidx]); |
| 404 | FVector3 nvec; |
| 405 | |
| 406 | mdv->Set(curVvec.X, curVvec.Y, curVvec.Z, curUvec.X, curUvec.Y); |
| 407 | |
| 408 | if (nidx >= 0 && (unsigned int)nidx < norms.Size()) |
| 409 | { |
| 410 | nvec = RealignVector(norms[nidx]); |
| 411 | } |
| 412 | else |
| 413 | { |
| 414 | if (surfaces[i].tris[j].smoothGroup == 0) |
| 415 | { |
| 416 | nvec = CalculateNormalFlat(i, j); |
| 417 | } |
nothing calls this directly
no test coverage detected