1
0
Fork 0

Alas, I have made non-backwardsly compatible changes to the scenery file

format.  Thus I have had to make the corresponding changes here in the
  file loader.
Things that do not correspond the the .obj format are placed in comments.
This commit is contained in:
curt 1999-03-27 05:36:03 +00:00
parent b722f8bb63
commit 35e2cf2307

View file

@ -141,22 +141,16 @@ int fgObjLoad( const string& path, fgTILE *t) {
// Attempt to open "path.gz" or "path" // Attempt to open "path.gz" or "path"
fg_gzifstream in( path ); fg_gzifstream in( path );
if ( ! in ) if ( ! in ) {
{ FG_LOG( FG_TERRAIN, FG_ALERT, "Cannot open file: " << path );
// Attempt to open "path.obj" or "path.obj.gz" return 0;
in.open( path + ".obj" );
if ( ! in )
{
FG_LOG( FG_TERRAIN, FG_ALERT, "Cannot open file: " << path );
return 0;
}
} }
shading = current_options.get_shading(); shading = current_options.get_shading();
in_fragment = 0; in_fragment = 0;
t->ncount = 1; t->ncount = 0;
vncount = 1; vncount = 0;
t->bounding_radius = 0.0; t->bounding_radius = 0.0;
nodes = t->nodes; nodes = t->nodes;
center = t->center; center = t->center;
@ -165,353 +159,354 @@ int fgObjLoad( const string& path, fgTILE *t) {
stopwatch.start(); stopwatch.start();
// ignore initial comments and blank lines. (priming the pump) // ignore initial comments and blank lines. (priming the pump)
in >> skipcomment; // in >> skipcomment;
string line;
while ( ! in.eof() )
{
while ( ! in.eof() ) {
string token; string token;
in >> token; char c;
// printf("token = %s\n", token.c_str() ); in >> skipws;
if ( token == "gbs" ) if ( in.get( c ) && c == '#' ) {
{ // process a comment line
// reference point (center offset)
in >> t->center >> t->bounding_radius;
center = t->center;
}
else if ( token == "bs" )
{
// reference point (center offset)
in >> fragment.center;
in >> fragment.bounding_radius;
}
else if ( token == "vn" )
{
// vertex normal
if ( vncount < MAX_NODES ) {
in >> normals[vncount][0]
>> normals[vncount][1]
>> normals[vncount][2];
vncount++;
} else {
FG_LOG( FG_TERRAIN, FG_ALERT,
"Read too many vertex normals ... dying :-(" );
exit(-1);
}
}
else if ( token[0] == 'v' )
{
// node (vertex)
if ( t->ncount < MAX_NODES ) {
in >> t->nodes[t->ncount][0]
>> t->nodes[t->ncount][1]
>> t->nodes[t->ncount][2];
t->ncount++;
} else {
FG_LOG( FG_TERRAIN, FG_ALERT,
"Read too many nodes ... dying :-(");
exit(-1);
}
}
else if ( token == "usemtl" )
{
// material property specification
// this also signals the start of a new fragment // getline( in, line );
if ( in_fragment ) { // cout << "comment = " << line << endl;
// close out the previous structure and start the next
xglEnd();
xglEndList();
// printf("xglEnd(); xglEndList();\n");
// update fragment in >> token;
fragment.display_list = display_list;
// push this fragment onto the tile's object list if ( token == "gbs" ) {
t->fragment_list.push_back(fragment); // reference point (center offset)
} else { in >> t->center >> t->bounding_radius;
in_fragment = 1; center = t->center;
} // cout << "center = " << center
// << " radius = " << t->bounding_radius << endl;
} else if ( token == "bs" ) {
// reference point (center offset)
in >> fragment.center;
in >> fragment.bounding_radius;
// printf("start of fragment (usemtl)\n"); // cout << "center = " << fragment.center
// << " radius = " << fragment.bounding_radius << endl;
} else if ( token == "usemtl" ) {
// material property specification
display_list = xglGenLists(1); // this also signals the start of a new fragment
xglNewList(display_list, GL_COMPILE); if ( in_fragment ) {
// printf("xglGenLists(); xglNewList();\n"); // close out the previous structure and start the next
in_faces = 0; xglEnd();
xglEndList();
// printf("xglEnd(); xglEndList();\n");
// reset the existing face list // update fragment
// printf("cleaning a fragment with %d faces\n", fragment.display_list = display_list;
// fragment.faces.size());
fragment.init();
// scan the material line // push this fragment onto the tile's object list
string material; t->fragment_list.push_back(fragment);
in >> material;
fragment.tile_ptr = t;
// find this material in the properties list
if ( ! material_mgr.find( material, fragment.material_ptr )) {
FG_LOG( FG_TERRAIN, FG_ALERT,
"Ack! unknown usemtl name = " << material
<< " in " << path );
}
// initialize the fragment transformation matrix
/*
for ( i = 0; i < 16; i++ ) {
fragment.matrix[i] = 0.0;
}
fragment.matrix[0] = fragment.matrix[5] =
fragment.matrix[10] = fragment.matrix[15] = 1.0;
*/
} else if ( token[0] == 't' ) {
// start a new triangle strip
n1 = n2 = n3 = n4 = 0;
// fgPrintf( FG_TERRAIN, FG_DEBUG, " new tri strip = %s", line);
in >> n1 >> n2 >> n3;
fragment.add_face(n1, n2, n3);
// fgPrintf( FG_TERRAIN, FG_DEBUG, "(t) = ");
xglBegin(GL_TRIANGLE_STRIP);
// printf("xglBegin(tristrip) %d %d %d\n", n1, n2, n3);
odd = 1;
scale = 1.0;
if ( shading ) {
// Shading model is "GL_SMOOTH" so use precalculated
// (averaged) normals
MAT3_SCALE_VEC(normal, normals[n1], scale);
xglNormal3dv(normal);
pp = calc_tex_coords(nodes[n1], center);
xglTexCoord2f(pp.lon(), pp.lat());
// xglVertex3d(t->nodes[n1][0],t->nodes[n1][1],t->nodes[n1][2]);
xglVertex3dv(nodes[n1]);
MAT3_SCALE_VEC(normal, normals[n2], scale);
xglNormal3dv(normal);
pp = calc_tex_coords(nodes[n2], center);
xglTexCoord2f(pp.lon(), pp.lat());
//xglVertex3d(t->nodes[n2][0],t->nodes[n2][1],t->nodes[n2][2]);
xglVertex3dv(nodes[n2]);
MAT3_SCALE_VEC(normal, normals[n3], scale);
xglNormal3dv(normal);
pp = calc_tex_coords(nodes[n3], center);
xglTexCoord2f(pp.lon(), pp.lat());
// xglVertex3d(t->nodes[n3][0],t->nodes[n3][1],t->nodes[n3][2]);
xglVertex3dv(nodes[n3]);
} else {
// Shading model is "GL_FLAT" so calculate per face
// normals on the fly.
if ( odd ) {
calc_normal(nodes[n1], nodes[n2],
nodes[n3], approx_normal);
} else { } else {
calc_normal(nodes[n2], nodes[n1], in_fragment = 1;
nodes[n3], approx_normal);
} }
MAT3_SCALE_VEC(normal, approx_normal, scale);
xglNormal3dv(normal);
pp = calc_tex_coords(nodes[n1], center); // printf("start of fragment (usemtl)\n");
xglTexCoord2f(pp.lon(), pp.lat());
// xglVertex3d(t->nodes[n1][0],t->nodes[n1][1],t->nodes[n1][2]);
xglVertex3dv(nodes[n1]);
pp = calc_tex_coords(nodes[n2], center); display_list = xglGenLists(1);
xglTexCoord2f(pp.lon(), pp.lat()); xglNewList(display_list, GL_COMPILE);
// xglVertex3d(t->nodes[n2][0],t->nodes[n2][1],t->nodes[n2][2]); // printf("xglGenLists(); xglNewList();\n");
xglVertex3dv(nodes[n2]); in_faces = 0;
pp = calc_tex_coords(nodes[n3], center); // reset the existing face list
xglTexCoord2f(pp.lon(), pp.lat()); // printf("cleaning a fragment with %d faces\n",
// xglVertex3d(t->nodes[n3][0],t->nodes[n3][1],t->nodes[n3][2]); // fragment.faces.size());
xglVertex3dv(nodes[n3]); fragment.init();
}
// printf("some normals, texcoords, and vertices\n"); // scan the material line
string material;
odd = 1 - odd; in >> material;
last1 = n2; fragment.tile_ptr = t;
last2 = n3;
// find this material in the properties list
// There can be three or four values if ( ! material_mgr.find( material, fragment.material_ptr )) {
char c; FG_LOG( FG_TERRAIN, FG_ALERT,
while ( in.get(c) ) "Ack! unknown usemtl name = " << material
{ << " in " << path );
if ( c == '\n' )
break; // only the one
if ( isdigit(c) )
{
in.putback(c);
in >> n4;
break;
} }
}
// initialize the fragment transformation matrix
/*
for ( i = 0; i < 16; i++ ) {
fragment.matrix[i] = 0.0;
}
fragment.matrix[0] = fragment.matrix[5] =
fragment.matrix[10] = fragment.matrix[15] = 1.0;
*/
} else {
// unknown comment, just gobble the input untill the
// end of line
if ( n4 > 0 ) { in >> skipeol;
fragment.add_face(n3, n2, n4); }
} else {
in.putback( c );
in >> token;
// cout << "token = " << token << endl;
if ( token == "vn" ) {
// vertex normal
if ( vncount < MAX_NODES ) {
in >> normals[vncount][0]
>> normals[vncount][1]
>> normals[vncount][2];
vncount++;
} else {
FG_LOG( FG_TERRAIN, FG_ALERT,
"Read too many vertex normals ... dying :-(" );
exit(-1);
}
} else if ( token[0] == 'v' ) {
// node (vertex)
if ( t->ncount < MAX_NODES ) {
in >> t->nodes[t->ncount][0]
>> t->nodes[t->ncount][1]
>> t->nodes[t->ncount][2];
t->ncount++;
} else {
FG_LOG( FG_TERRAIN, FG_ALERT,
"Read too many nodes ... dying :-(");
exit(-1);
}
} else if ( token[0] == 't' ) {
// start a new triangle strip
n1 = n2 = n3 = n4 = 0;
// fgPrintf( FG_TERRAIN, FG_DEBUG,
// " new tri strip = %s", line);
in >> n1 >> n2 >> n3;
fragment.add_face(n1, n2, n3);
// fgPrintf( FG_TERRAIN, FG_DEBUG, "(t) = ");
xglBegin(GL_TRIANGLE_STRIP);
// printf("xglBegin(tristrip) %d %d %d\n", n1, n2, n3);
odd = 1;
scale = 1.0;
if ( shading ) { if ( shading ) {
// Shading model is "GL_SMOOTH" // Shading model is "GL_SMOOTH" so use precalculated
MAT3_SCALE_VEC(normal, normals[n4], scale); // (averaged) normals
MAT3_SCALE_VEC(normal, normals[n1], scale);
xglNormal3dv(normal);
pp = calc_tex_coords(nodes[n1], center);
xglTexCoord2f(pp.lon(), pp.lat());
xglVertex3dv(nodes[n1]);
MAT3_SCALE_VEC(normal, normals[n2], scale);
xglNormal3dv(normal);
pp = calc_tex_coords(nodes[n2], center);
xglTexCoord2f(pp.lon(), pp.lat());
xglVertex3dv(nodes[n2]);
MAT3_SCALE_VEC(normal, normals[n3], scale);
xglNormal3dv(normal);
pp = calc_tex_coords(nodes[n3], center);
xglTexCoord2f(pp.lon(), pp.lat());
xglVertex3dv(nodes[n3]);
} else { } else {
// Shading model is "GL_FLAT" // Shading model is "GL_FLAT" so calculate per face
calc_normal(nodes[n3], nodes[n2], nodes[n4], // normals on the fly.
approx_normal); if ( odd ) {
calc_normal(nodes[n1], nodes[n2],
nodes[n3], approx_normal);
} else {
calc_normal(nodes[n2], nodes[n1],
nodes[n3], approx_normal);
}
MAT3_SCALE_VEC(normal, approx_normal, scale); MAT3_SCALE_VEC(normal, approx_normal, scale);
xglNormal3dv(normal);
pp = calc_tex_coords(nodes[n1], center);
xglTexCoord2f(pp.lon(), pp.lat());
xglVertex3dv(nodes[n1]);
pp = calc_tex_coords(nodes[n2], center);
xglTexCoord2f(pp.lon(), pp.lat());
xglVertex3dv(nodes[n2]);
pp = calc_tex_coords(nodes[n3], center);
xglTexCoord2f(pp.lon(), pp.lat());
xglVertex3dv(nodes[n3]);
} }
xglNormal3dv(normal); // printf("some normals, texcoords, and vertices\n");
pp = calc_tex_coords(nodes[n4], center);
xglTexCoord2f(pp.lon(), pp.lat());
// xglVertex3d(t->nodes[n4][0],t->nodes[n4][1],t->nodes[n4][2]);
xglVertex3dv(nodes[n4]);
odd = 1 - odd; odd = 1 - odd;
last1 = n3; last1 = n2;
last2 = n4; last2 = n3;
// printf("a normal, texcoord, and vertex (4th)\n");
}
} else if ( token[0] == 'f' ) {
// unoptimized face
if ( !in_faces ) { // There can be three or four values
xglBegin(GL_TRIANGLES); char c;
// printf("xglBegin(triangles)\n"); while ( in.get(c) ) {
in_faces = 1; if ( c == '\n' ) {
} break; // only the one
}
if ( isdigit(c) ){
in.putback(c);
in >> n4;
break;
}
}
// fgPrintf( FG_TERRAIN, FG_DEBUG, "new triangle = %s", line);*/ if ( n4 > 0 ) {
in >> n1 >> n2 >> n3; fragment.add_face(n3, n2, n4);
fragment.add_face(n1, n2, n3);
// xglNormal3d(normals[n1][0], normals[n1][1], normals[n1][2]); if ( shading ) {
xglNormal3dv(normals[n1]); // Shading model is "GL_SMOOTH"
pp = calc_tex_coords(nodes[n1], center); MAT3_SCALE_VEC(normal, normals[n4], scale);
xglTexCoord2f(pp.lon(), pp.lat()); } else {
// xglVertex3d(t->nodes[n1][0], t->nodes[n1][1], t->nodes[n1][2]); // Shading model is "GL_FLAT"
xglVertex3dv(nodes[n1]); calc_normal(nodes[n3], nodes[n2], nodes[n4],
approx_normal);
MAT3_SCALE_VEC(normal, approx_normal, scale);
}
xglNormal3dv(normal);
pp = calc_tex_coords(nodes[n4], center);
xglTexCoord2f(pp.lon(), pp.lat());
xglVertex3dv(nodes[n4]);
odd = 1 - odd;
last1 = n3;
last2 = n4;
// printf("a normal, texcoord, and vertex (4th)\n");
}
} else if ( token[0] == 'f' ) {
// unoptimized face
// xglNormal3d(normals[n2][0], normals[n2][1], normals[n2][2]); if ( !in_faces ) {
xglNormal3dv(normals[n2]); xglBegin(GL_TRIANGLES);
pp = calc_tex_coords(nodes[n2], center); // printf("xglBegin(triangles)\n");
xglTexCoord2f(pp.lon(), pp.lat()); in_faces = 1;
// xglVertex3d(t->nodes[n2][0], t->nodes[n2][1], t->nodes[n2][2]); }
xglVertex3dv(nodes[n2]);
// fgPrintf( FG_TERRAIN, FG_DEBUG, "new triangle = %s", line);*/
in >> n1 >> n2 >> n3;
fragment.add_face(n1, n2, n3);
// xglNormal3d(normals[n1][0], normals[n1][1], normals[n1][2]);
xglNormal3dv(normals[n1]);
pp = calc_tex_coords(nodes[n1], center);
xglTexCoord2f(pp.lon(), pp.lat());
xglVertex3dv(nodes[n1]);
xglNormal3dv(normals[n2]);
pp = calc_tex_coords(nodes[n2], center);
xglTexCoord2f(pp.lon(), pp.lat());
xglVertex3dv(nodes[n2]);
// xglNormal3d(normals[n3][0], normals[n3][1], normals[n3][2]); xglNormal3dv(normals[n3]);
xglNormal3dv(normals[n3]); pp = calc_tex_coords(nodes[n3], center);
pp = calc_tex_coords(nodes[n3], center); xglTexCoord2f(pp.lon(), pp.lat());
xglTexCoord2f(pp.lon(), pp.lat()); xglVertex3dv(nodes[n3]);
// xglVertex3d(t->nodes[n3][0], t->nodes[n3][1], t->nodes[n3][2]); // printf("some normals, texcoords, and vertices (tris)\n");
xglVertex3dv(nodes[n3]); } else if ( token[0] == 'q' ) {
// printf("some normals, texcoords, and vertices (tris)\n"); // continue a triangle strip
} else if ( token[0] == 'q' ) { n1 = n2 = 0;
// continue a triangle strip
n1 = n2 = 0;
// fgPrintf( FG_TERRAIN, FG_DEBUG, "continued tri strip = %s ", // fgPrintf( FG_TERRAIN, FG_DEBUG, "continued tri strip = %s ",
// line); // line);
in >> n1; in >> n1;
// There can be one or two values // There can be one or two values
char c; char c;
while ( in.get(c) ) while ( in.get(c) ) {
{ if ( c == '\n' ) {
if ( c == '\n' ) break; // only the one
break; // only the one }
if ( isdigit(c) ) if ( isdigit(c) ) {
{ in.putback(c);
in.putback(c); in >> n2;
in >> n2; break;
break; }
} }
} // fgPrintf( FG_TERRAIN, FG_DEBUG, "read %d %d\n", n1, n2);
// fgPrintf( FG_TERRAIN, FG_DEBUG, "read %d %d\n", n1, n2);
if ( odd ) {
fragment.add_face(last1, last2, n1);
} else {
fragment.add_face(last2, last1, n1);
}
if ( shading ) {
// Shading model is "GL_SMOOTH"
MAT3_SCALE_VEC(normal, normals[n1], scale);
} else {
// Shading model is "GL_FLAT"
if ( odd ) {
calc_normal(nodes[last1], nodes[last2], nodes[n1],
approx_normal);
} else {
calc_normal(nodes[last2], nodes[last1], nodes[n1],
approx_normal);
}
MAT3_SCALE_VEC(normal, approx_normal, scale);
}
xglNormal3dv(normal);
pp = calc_tex_coords(nodes[n1], center);
xglTexCoord2f(pp.lon(), pp.lat());
// xglVertex3d(t->nodes[n1][0], t->nodes[n1][1], t->nodes[n1][2]);
xglVertex3dv(nodes[n1]);
// printf("a normal, texcoord, and vertex (4th)\n");
odd = 1 - odd;
last1 = last2;
last2 = n1;
if ( n2 > 0 ) {
// fgPrintf( FG_TERRAIN, FG_DEBUG, " (cont)\n");
if ( odd ) { if ( odd ) {
fragment.add_face(last1, last2, n2); fragment.add_face(last1, last2, n1);
} else { } else {
fragment.add_face(last2, last1, n2); fragment.add_face(last2, last1, n1);
} }
if ( shading ) { if ( shading ) {
// Shading model is "GL_SMOOTH" // Shading model is "GL_SMOOTH"
MAT3_SCALE_VEC(normal, normals[n2], scale); MAT3_SCALE_VEC(normal, normals[n1], scale);
} else { } else {
// Shading model is "GL_FLAT" // Shading model is "GL_FLAT"
if ( odd ) { if ( odd ) {
calc_normal(nodes[last1], nodes[last2], calc_normal(nodes[last1], nodes[last2], nodes[n1],
nodes[n2], approx_normal); approx_normal);
} else { } else {
calc_normal(nodes[last2], nodes[last1], calc_normal(nodes[last2], nodes[last1], nodes[n1],
nodes[n2], approx_normal); approx_normal);
} }
MAT3_SCALE_VEC(normal, approx_normal, scale); MAT3_SCALE_VEC(normal, approx_normal, scale);
} }
xglNormal3dv(normal); xglNormal3dv(normal);
pp = calc_tex_coords(nodes[n2], center); pp = calc_tex_coords(nodes[n1], center);
xglTexCoord2f(pp.lon(), pp.lat()); xglTexCoord2f(pp.lon(), pp.lat());
// xglVertex3d(t->nodes[n2][0],t->nodes[n2][1],t->nodes[n2][2]); xglVertex3dv(nodes[n1]);
xglVertex3dv(nodes[n2]);
// printf("a normal, texcoord, and vertex (4th)\n"); // printf("a normal, texcoord, and vertex (4th)\n");
odd = 1 -odd; odd = 1 - odd;
last1 = last2; last1 = last2;
last2 = n2; last2 = n1;
}
} else {
FG_LOG( FG_TERRAIN, FG_WARN, "Unknown token in "
<< path << " = " << token );
}
// eat comments and blank lines before start of while loop so if ( n2 > 0 ) {
// if we are done with useful input it is noticed before hand. // fgPrintf( FG_TERRAIN, FG_DEBUG, " (cont)\n");
in >> skipcomment;
if ( odd ) {
fragment.add_face(last1, last2, n2);
} else {
fragment.add_face(last2, last1, n2);
}
if ( shading ) {
// Shading model is "GL_SMOOTH"
MAT3_SCALE_VEC(normal, normals[n2], scale);
} else {
// Shading model is "GL_FLAT"
if ( odd ) {
calc_normal(nodes[last1], nodes[last2],
nodes[n2], approx_normal);
} else {
calc_normal(nodes[last2], nodes[last1],
nodes[n2], approx_normal);
}
MAT3_SCALE_VEC(normal, approx_normal, scale);
}
xglNormal3dv(normal);
pp = calc_tex_coords(nodes[n2], center);
xglTexCoord2f(pp.lon(), pp.lat());
xglVertex3dv(nodes[n2]);
// printf("a normal, texcoord, and vertex (4th)\n");
odd = 1 -odd;
last1 = last2;
last2 = n2;
}
} else {
FG_LOG( FG_TERRAIN, FG_WARN, "Unknown token in "
<< path << " = " << token );
}
// eat white space before start of while loop so if we are
// done with useful input it is noticed before hand.
in >> skipws;
}
} }
if ( in_fragment ) { if ( in_fragment ) {
@ -519,7 +514,7 @@ int fgObjLoad( const string& path, fgTILE *t) {
xglEnd(); xglEnd();
xglEndList(); xglEndList();
// printf("xglEnd(); xglEndList();\n"); // printf("xglEnd(); xglEndList();\n");
// update fragment // update fragment
fragment.display_list = display_list; fragment.display_list = display_list;
@ -527,33 +522,37 @@ int fgObjLoad( const string& path, fgTILE *t) {
t->fragment_list.push_back(fragment); t->fragment_list.push_back(fragment);
} }
#if 0
// Draw normal vectors (for visually verifying normals) // Draw normal vectors (for visually verifying normals)
/*
xglBegin(GL_LINES); xglBegin(GL_LINES);
xglColor3f(0.0, 0.0, 0.0); xglColor3f(0.0, 0.0, 0.0);
for ( i = 0; i < t->ncount; i++ ) { for ( i = 0; i < t->ncount; i++ ) {
xglVertex3d(t->nodes[i][0], xglVertex3d(t->nodes[i][0],
t->nodes[i][1] , t->nodes[i][1] ,
t->nodes[i][2]); t->nodes[i][2]);
xglVertex3d(t->nodes[i][0] + 500*normals[i][0], xglVertex3d(t->nodes[i][0] + 500*normals[i][0],
t->nodes[i][1] + 500*normals[i][1], t->nodes[i][1] + 500*normals[i][1],
t->nodes[i][2] + 500*normals[i][2]); t->nodes[i][2] + 500*normals[i][2]);
} }
xglEnd(); xglEnd();
*/ #endif
stopwatch.stop(); stopwatch.stop();
FG_LOG( FG_TERRAIN, FG_INFO, FG_LOG( FG_TERRAIN, FG_INFO,
"Loaded " << path << " in " "Loaded " << path << " in "
<< stopwatch.elapsedSeconds() << " seconds" ); << stopwatch.elapsedSeconds() << " seconds" );
// printf("end of tile\n"); return 1;
return(1);
} }
// $Log$ // $Log$
// Revision 1.13 1999/03/27 05:36:03 curt
// Alas, I have made non-backwardsly compatible changes to the scenery file
// format. Thus I have had to make the corresponding changes here in the
// file loader.
// Things that do not correspond the the .obj format are placed in comments.
//
// Revision 1.12 1999/03/02 01:03:25 curt // Revision 1.12 1999/03/02 01:03:25 curt
// Tweaks for building with native SGI compilers. // Tweaks for building with native SGI compilers.
// //