我正在编写 Wavefront .OBJ 网格解析器。虽然我已经为此工作了大约 3 周,但我决定重写其中的大部分内容,因为我正在逐个字符地解析它(我觉得这最终引入了太多的复杂性,并且没有无论如何都不能正常工作)。现在我一直在逐行解析它,我觉得我的结果越来越差。
这是我的输出示例:
( STREAM ):# cube.obj
}
(LINE 2){
( STREAM ):#
}
(LINE 3){
( STREAM ):
}
(LINE 4){
( STREAM ):g cube
}
(LINE 5){
( STREAM ):
}
(LINE 6){
( VERTEX: 1 )'-597028128851671121920.000000 0.000000 -597118763794171953152.000000'
( STREAM ):v 0.0 0.0 0.0
}
(LINE 7){
( VERTEX: 2 )'-597028128851671121920.000000 0.000000 -597118763794171953152.000000'
( STREAM ):v 0.0 0.0 1.0
}
(LINE 8){
( VERTEX: 3 )'-597028128851671121920.000000 0.000000 -597118763794171953152.000000'
( STREAM ):v 0.0 1.0 0.0
}
(LINE 9){
( VERTEX: 4 )'-597028128851671121920.000000 0.000000 -597118763794171953152.000000'
( STREAM ):v 0.0 1.0 1.0
}
(LINE 10){
( VERTEX: 5 )'-597028128851671121920.000000 0.000000 -597118763794171953152.000000'
( STREAM ):v 1.0 0.0 0.0
}
(LINE 11){
( VERTEX: 6 )'-597028128851671121920.000000 0.000000 -597118763794171953152.000000'
( STREAM ):v 1.0 0.0 1.0
}
(LINE 12){
( VERTEX: 7 )'-597028128851671121920.000000 0.000000 -597118763794171953152.000000'
( STREAM ):v 1.0 1.0 0.0
}
(LINE 13){
( VERTEX: 8 )'-597028128851671121920.000000 0.000000 -597118763794171953152.000000'
( STREAM ):v 1.0 1.0 1.0
}
f
为了简单起见,我目前没有在解析索引(以 开头的行)。
到目前为止我尝试过的(对于顶点数据)
strtof( pLine, &pLine )
:这为顶点产生了零current implementation within the following code
:不产生零,但绝对不是存储的实际数据
代码
//...declare lineCount and vertexCount above
// NOTE: BUFF_LEN == 200
while( !feof( filePtr ) )
{
printf( "(LINE %i){\n\n", lineCount );
fgets( line, BUFF_LEN, filePtr );
// append NULL term, since fgets does not
// return one.
line[ BUFF_LEN - 1 ] = '\0';
// evaluate the first character
switch( line[ 0 ] )
{
case 'v':
{
if ( line[ 1 ] == ' ' )
{
// we have a vertex, not a normal
SIMD_VEC3 v;
fscanf( filePtr, " %f %f %f\n", &v[ 0 ], &v[ 1 ], &v[ 2 ] );
printf( "( %i )\'%f %f %f\'\n\n",
vertexCount, v[ 0 ], v[ 1 ], v[ 2 ] );
++vertexCount;
}
}
break;
}
//...printf out the current line parsed from fgets, increment line count
}
我正在使用 C++,但正在寻找一种标准的 C 方法。我也在使用 OpenGL 和 Linux,但我不确定这是否相关。