void FMD3Model::BuildVertexBuffer()
{
	if (mVBuf == nullptr)
	{
		LoadGeometry();

		unsigned int vbufsize = 0;
		unsigned int ibufsize = 0;

		for (int i = 0; i < numSurfaces; i++)
		{
			MD3Surface * surf = &surfaces[i];
			vbufsize += numFrames * surf->numVertices;
			ibufsize += 3 * surf->numTriangles;
		}

		mVBuf = new FModelVertexBuffer(true, numFrames == 1);
		FModelVertex *vertptr = mVBuf->LockVertexBuffer(vbufsize);
		unsigned int *indxptr = mVBuf->LockIndexBuffer(ibufsize);

		assert(vertptr != nullptr && indxptr != nullptr);

		unsigned int vindex = 0, iindex = 0;

		for (int i = 0; i < numSurfaces; i++)
		{
			MD3Surface * surf = &surfaces[i];

			surf->vindex = vindex;
			surf->iindex = iindex;
			for (int j = 0; j < numFrames * surf->numVertices; j++)
			{
				MD3Vertex* vert = surf->vertices + j;

				FModelVertex *bvert = &vertptr[vindex++];

				int tc = j % surf->numVertices;
				bvert->Set(vert->x, vert->z, vert->y, surf->texcoords[tc].s, surf->texcoords[tc].t);
				bvert->SetNormal(vert->nx, vert->nz, vert->ny);
			}

			for (int k = 0; k < surf->numTriangles; k++)
			{
				for (int l = 0; l < 3; l++)
				{
					indxptr[iindex++] = surf->tris[k].VertIndex[l];
				}
			}
			surf->UnloadGeometry();
		}
		mVBuf->UnlockVertexBuffer();
		mVBuf->UnlockIndexBuffer();
	}
}
Пример #2
0
void FUE1Model::BuildVertexBuffer( FModelRenderer *renderer )
{
	if (GetVertexBuffer(renderer))
		return;
	if ( !mDataLoaded )
		LoadGeometry();
	int vsize = 0;
	for ( int i=0; i<numGroups; i++ )
		vsize += groups[i].numPolys*3;
	vsize *= numFrames;
	auto vbuf = renderer->CreateVertexBuffer(false,numFrames==1);
	SetVertexBuffer(renderer, vbuf);
	FModelVertex *vptr = vbuf->LockVertexBuffer(vsize);
	int vidx = 0;
	for ( int i=0; i<numFrames; i++ )
	{
		for ( int j=0; j<numGroups; j++ )
		{
			for ( int k=0; k<groups[j].numPolys; k++ )
			{
				for ( int l=0; l<3; l++ )
				{
					UE1Vertex V = verts[polys[groups[j].P[k]].V[l]+i*numVerts];
					FVector2 C = polys[groups[j].P[k]].C[l];
					FModelVertex *vert = &vptr[vidx++];
					vert->Set(V.Pos.X,V.Pos.Y,V.Pos.Z,C.X,C.Y);
					if ( groups[j].type&PT_Curvy )	// use facet normal
					{
						vert->SetNormal(polys[groups[j].P[k]].Normals[i].X,
							polys[groups[j].P[k]].Normals[i].Y,
							polys[groups[j].P[k]].Normals[i].Z);
					}
					else vert->SetNormal(V.Normal.X,V.Normal.Y,V.Normal.Z);
				}
			}
		}
	}
	vbuf->UnlockVertexBuffer();
}