admin管理员组文章数量:1122833
I wrote up a simple OBJ parser, it doesn't really parse what you'd call conventional OBJ (vn ->n because of the switch statement) but I need it for some other stuff and OBJ parsing is just a test so I don't have to write all vertex coordinates myself:
void M_LoadOBJ( char* filename ) {
FILE* pLMFFile;
lmf_file_t* pModel;
char pLine[128];
BYTE bByte, bPosition;
INT placeholder;
printf( "Loading %s...\n", filename );
pLMFFile = fopen( filename, "rb" );
if( !pLMFFile ) {
MessageBoxA( NULL, "Couldn't open the OBJ", "Error", MB_ICONERROR );
exit( -1 );
}
pModel = (lmf_file_t*)malloc( sizeof(lmf_file_t) );
pModel->vertexes = NULL; pModel->normals = NULL; pModel->faces = NULL;
bByte = 0; bPosition = 0; placeholder = 0;
pModel->numvertexes = 0; pModel->numnormals = 0; pModel->numfaces = 0;
memset( pLine, 0, 128 );
while( !feof(pLMFFile) ) {
if( !fread( &bByte, 1, 1, pLMFFile ) ) break;
if( bByte == '\r' ) fread( &bByte, 1, 1, pLMFFile );
if( bByte == '\n' ) {
switch( pLine[0] ) {
case 'v': {
pModel->vertexes = (FLOAT*)realloc( pModel->vertexes, (pModel->numvertexes+3)*4 );
sscanf( pLine, "v %f %f %f", &pModel->vertexes[pModel->numvertexes],
&pModel->vertexes[pModel->numvertexes+1], &pModel->vertexes[pModel->numvertexes+2] );
printf( "Reading vertexes: %f %f %f\n", pModel->vertexes[pModel->numvertexes],
pModel->vertexes[pModel->numvertexes+1], pModel->vertexes[pModel->numvertexes+2] );
pModel->numvertexes+=3;
} break;
case 'n': {
pModel->normals = (FLOAT*)realloc( pModel->normals, (pModel->numnormals+3)*4 );
sscanf( pLine, "n %f %f %f", &pModel->normals[pModel->numnormals],
&pModel->normals[pModel->numnormals+1], &pModel->normals[pModel->numnormals+2] );
pModel->numnormals+=3;
} break;
case 'f': {
pModel->faces = (lmf_face_t*)realloc( pModel->faces, (pModel->numfaces+1)*sizeof(lmf_face_t) );
sscanf( pLine, "f %i/%i/%i %i/%i/%i %i/%i/%i",
&pModel->faces[pModel->numfaces].vertexes[0], &placeholder, &pModel->faces[pModel->numfaces].normals[0],
&pModel->faces[pModel->numfaces].vertexes[1], &placeholder, &pModel->faces[pModel->numfaces].normals[1],
&pModel->faces[pModel->numfaces].vertexes[2], &placeholder, &pModel->faces[pModel->numfaces].normals[2] );
/*printf( "Pline: %s:\n", pLine );
printf( "%i/%i/%i %i/%i/%i %i/%i/%i\n",
pModel->faces[pModel->numfaces].vertexes[0], 0, pModel->faces[pModel->numfaces].normals[0],
pModel->faces[pModel->numfaces].vertexes[1], 0, pModel->faces[pModel->numfaces].normals[1],
pModel->faces[pModel->numfaces].vertexes[2], 0, pModel->faces[pModel->numfaces].normals[2]
);*/
pModel->numfaces++;
} break;
}
memset( pLine, 0, 128 );
bPosition = 0;
continue;
}
pLine[bPosition] = bByte;
bPosition++;
}
printf( "\n--------------------\n" );
printf( "Model finished with %i vertexes\n\n", pModel->numvertexes );
pModelFile = pModel;
}
while( TRUE ) {
SleepEx( 200, FALSE );
// Controls
if( GetAsyncKeyState(65) ) {
glRotatef( 10, 0, 1, 0 );
}
else if( GetAsyncKeyState(68) ) {
glRotatef( -10, 0, 1, 0 );
}
else if( GetAsyncKeyState(87) ) {
glScalef( 1.5, 1.5, 0 );
}
else if( GetAsyncKeyState(83) ) {
glScalef( 0.5, 0.5, 0 );
}
else if( GetAsyncKeyState(VK_LEFT) ) {
glTranslatef( -8, 0, 0 );
}
else if( GetAsyncKeyState(VK_RIGHT) ) {
glTranslatef( 8, 0, 0 );
}
else if( GetAsyncKeyState(VK_UP) ) {
glTranslatef( 0, 8, 0 );
}
else if( GetAsyncKeyState(VK_DOWN) ) {
glTranslatef( 0, -8, 0 );
}
glClear( GL_COLOR_BUFFER_BIT );
glColor3f( 0, 0, 1 );
//glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
//glFrontFace( GL_CW );
glBegin( GL_TRIANGLE_STRIP );
for( INT f = 0; f < mdl->numfaces-2; f+=3 ) {
glNormal3f( mdl->normals[ mdl->faces[f].normals[0]-1 ], mdl->normals[ mdl->faces[f].normals[1]-1 ], mdl->normals[ mdl->faces[f].normals[2]-1 ] );
glVertex3f( mdl->vertexes[ mdl->faces[f].vertexes[0]-1 ], mdl->vertexes[ mdl->faces[f].vertexes[1]-1 ], mdl->vertexes[ mdl->faces[f].vertexes[2]-1 ] );
glNormal3f( mdl->normals[ mdl->faces[f+1].normals[0]-1 ], mdl->normals[ mdl->faces[f+1].normals[1]-1 ], mdl->normals[ mdl->faces[f+1].normals[2]-1 ] );
glVertex3f( mdl->vertexes[ mdl->faces[f+1].vertexes[0]-1 ], mdl->vertexes[ mdl->faces[f+1].vertexes[1]-1 ], mdl->vertexes[ mdl->faces[f+1].vertexes[2]-1 ] );
glNormal3f( mdl->normals[ mdl->faces[f+2].normals[0]-1 ], mdl->normals[ mdl->faces[f+2].normals[1]-1 ], mdl->normals[ mdl->faces[f+2].normals[2]-1 ] );
glVertex3f( mdl->vertexes[ mdl->faces[f+2].vertexes[0]-1 ], mdl->vertexes[ mdl->faces[f+2].vertexes[1]-1 ], mdl->vertexes[ mdl->faces[f+2].vertexes[2]-1 ] );
}
/*for( INT v = 0; v < mdl->numvertexes-3; v+=3 ) {
glVertex3f( mdl->vertexes[v], -mdl->vertexes[v+1], mdl->vertexes[v+2] );
}*/
glEnd();
SwapBuffers( ____hdc );
}
OBJ FILE:
v -10.125000 9.748589 10.442236
v -10.125000 -9.626411 10.442236
v 10.125000 9.748589 10.442236
v 10.125000 -9.626411 10.442236
v 10.125000 9.748589 -9.807764
v 10.125000 -9.626411 -9.807764
v -10.125000 9.748589 -9.807764
v -10.125000 -9.626411 -9.807764
n 0.000000 0.000000 1.000000
n 1.000000 0.000000 0.000000
n 0.000000 0.000000 -1.000000
n -1.000000 0.000000 0.000000
n 0.000000 1.000000 0.000000
n 0.000000 -1.000000 0.000000
f 1/1/1 2/2/1 3/3/1
f 2/2/1 4/4/1 3/3/1
f 3/1/2 4/2/2 5/3/2
f 4/2/2 6/4/2 5/3/2
f 5/1/3 6/2/3 7/3/3
f 6/2/3 8/4/3 7/3/3
f 7/1/4 8/2/4 1/3/4
f 8/2/4 2/4/4 1/3/4
f 7/1/5 1/2/5 5/3/5
f 1/2/5 3/4/5 5/3/5
f 2/1/6 8/2/6 4/3/6
f 8/2/6 6/4/6 4/3/6
The issue is I can't render it for some reason, it just looks wrong:
I tried changing the glBegin modes (triangle strip, triangles)
Tried ignoring face parameters and just sequentially doing glvertex3f on the incremenenting vertex indexes
本文标签: cCan39t render the simplest OBJ properlyStack Overflow
版权声明:本文标题:c - Can't render the simplest OBJ properly - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736311980a1934934.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论