您可以保留来自.obj
顶点共享的数据。这是从顶点缓冲区加载数据.obj
并为顶点缓冲区生成顶点/面的代码:
int FaceIndex = 0;
int VertexIndex = 1;
int TextureVertexIndex = 1;
char Prefix[3];
float X, Y, Z;
int A1, A2, A3, B1, B2, B3, C1, C2, C3, D1, D2;
while ( !IStream->Eof() )
{
std::string Line = IStream->ReadLine();
int NumRead = sscanf( Line.c_str(), "%2s %f %f %f", Prefix, &X, &Y, &Z );
if ( NumRead < 1 ) continue;
switch ( Prefix[0] )
{
case '#':
continue;
生成单个顶点。这些可以在多个面之间共享:
case 'v':
{
switch ( Prefix[1] )
{
case 0:
if ( NumRead != 4 ) continue;
EmitVertex( VertexIndex++, vec3( X, Y, Z ), -1, -1 );
break;
case 't':
if ( NumRead != 3 ) continue;
EmitTextureVertex( TextureVertexIndex++, vec3( X, 1.0f-Y, 0.0f ) );
break;
case 'n':
if ( NumRead != 4 ) continue;
EmitNormal( VertexIndex, vec3( X, Y, Z ) );
break;
}
}
break;
使用索引生成具有纹理坐标的面:
case 'f':
{
if ( sscanf( Line.c_str(), "%2s %d/%d %d/%d %d/%d %d/%d", Prefix, &A1, &A2, &B1, &B2, &C1, &C2, &D1, &D2 ) == 9 )
{
A3 = B3 = C3 = 0;
EmitTextureFace( FaceIndex, A2, B2, D2 );
EmitFace( FaceIndex++, A1, B1, D1, -1, -1 );
EmitTextureFace( FaceIndex, B2, C2, D2 );
EmitFace( FaceIndex++, B1, C1, D1, -1, -1 );
}
else if ( sscanf( Line.c_str(), "%2s %d/%d/%d %d/%d/%d %d/%d/%d", Prefix, &A1, &A2, &A3, &B1, &B2, &B3, &C1, &C2, &C3 ) == 10 )
{
EmitTextureFace( FaceIndex, A2, B2, C2 );
EmitFace( FaceIndex++, A1, B1, C1, -1, -1 );
}
else if ( sscanf( Line.c_str(), "%2s %d//%d %d//%d %d//%d", Prefix, &A1, &A3, &B1, &B3, &C1, &C3 ) == 7 )
{
A2 = B2 = C2 = 0;
EmitFace( FaceIndex++, A1, B1, C1, -1, -1 );
}
else if ( sscanf( Line.c_str(), "%2s %d/%d %d/%d %d/%d", Prefix, &A1, &A2, &B1, &B2, &C1, &C2 ) == 7 )
{
A3 = B3 = C3 = 0;
EmitTextureFace( FaceIndex, A2, B2, C2 );
EmitFace( FaceIndex++, A1, B1, C1, -1, -1 );
}
}
break;
}
}