Text archives Help
- From: abe@sci.utah.edu
- To: manta@sci.utah.edu
- Subject: [MANTA] r740 - branches/itanium2/StandAlone
- Date: Thu, 1 Dec 2005 00:37:39 -0700 (MST)
Author: abe
Date: Thu Dec 1 00:37:38 2005
New Revision: 740
Modified:
branches/itanium2/StandAlone/v3c1_tools.cc
Log:
Added support for extracting vertex normals from .obj files.
Fixed bug which caused a crash if a missing material was referenced. Added
-scale option to resize an input mesh before writting the v3c1 file.
M v3c1_tools.cc
Modified: branches/itanium2/StandAlone/v3c1_tools.cc
==============================================================================
--- branches/itanium2/StandAlone/v3c1_tools.cc (original)
+++ branches/itanium2/StandAlone/v3c1_tools.cc Thu Dec 1 00:37:38 2005
@@ -43,7 +43,9 @@
void v3c1_clip( const char *file_name, const Point &plane_point, const
Vector &plane_normal );
FILE *out_file = 0;
+FILE *normal_out_file = 0;
int total_faces = 0;
+bool use_normals = false;
// Output file name.
char *output_file_name = 0;
@@ -117,6 +119,7 @@
// Objects containing global results
BBox bounds;
+float input_scale[3] = { 1.0f, 1.0f, 1.0f };
class FaceColorMap {
private:
@@ -180,6 +183,9 @@
else if (arg == "-obj") {
command = CONVERT_OBJ;
}
+ else if (arg == "-normals") {
+ use_normals = true;
+ }
else if (arg == "-m") {
command = CONVERT_M;
}
@@ -188,6 +194,11 @@
default_color[1] = atof(argv[++i]);
default_color[2] = atof(argv[++i]);
}
+ else if (arg == "-scale") {
+ input_scale[0] = atof(argv[++i]);
+ input_scale[1] = atof(argv[++i]);
+ input_scale[2] = atof(argv[++i]);
+ }
else if (arg == "-out") {
output_file_name = argv[++i];
}
@@ -225,10 +236,17 @@
// Check that both were found.
if ((command == NONE) || (file_list_begin == 0)) {
printf( "Usage: v2v3c1 -vn3 -in <tanker paths...> -out
<outfile.v3c1>\n"
- " v2v3c1 -obj -in
<file1.obj ...> [-out <outfile.v3c1>]\n -- Outfile optional uses group names
otherwise.\n"
- " v2v3c1 -m -in <file1.m ...> -color <r> <g> <b> [-out
<outfile.v3c1>]\n -- Hughes Hoppe .m file format conversion.\n"
- " v2v3c1 -info <infile.v3c1 ...> -- Computes
bounds and number of triangles.\n"
- " v2v3c1 -clip <px>
<py> <pz> <nx> <ny> <nz> -- Apply a clipping plane to the input files. \n");
+ " v2v3c1 -obj -in
<file1.obj ...> [-out <outfile.v3c1>]\n"
+ " Related options:\n"
+ " -scale <x> <y> <z> -- Scale the model before
writting\n"
+ " -normals -- Output a vertex normal file.\n"
+ " Outfile optional uses group names otherwise.\n"
+ " v2v3c1 -m -in <file1.m ...> -color <r> <g> <b> [-out
<outfile.v3c1>]\n"
+ " Hughes Hoppe .m file format conversion.\n"
+ " v2v3c1 -info <infile.v3c1 ...>\n"
+ " Computes bounds and number of triangles, colors.\n"
+ " v2v3c1 -clip <px>
<py> <pz> <nx> <ny> <nz>\n"
+ " Apply a clipping plane to the input files given a
point and normal.\n");
return 1;
}
@@ -264,6 +282,16 @@
perror( "Could not open output file." );
return 1;
}
+
+ // Determine normal filename.
+ if (use_normals) {
+ char file_name[128];
+ sprintf( file_name, "%s.nor", output_file_name );
+ if ((normal_out_file = fopen( file_name, "w" )) == 0) {
+ std::cout << "Error could not open normal output file: "
+ << file_name << std::endl;
+ }
+ }
}
for (int i=file_list_begin;i<file_list_end;++i) {
@@ -273,6 +301,10 @@
// Close the output file.
if (out_file) {
fclose( out_file );
+
+ // Check to see if the normal file needs to be closed.
+ if (normal_out_file)
+ fclose( normal_out_file );
}
// Output the bounds.
@@ -427,6 +459,11 @@
// Check to make sure none of the vertex coords are too large.
bool bad_triangle = false;
for (int j=0;j<3;++j) {
+
+ out_buffer[i].vertex0[j] *= input_scale[0];
+ out_buffer[i].vertex1[j] *= input_scale[1];
+ out_buffer[i].vertex2[j] *= input_scale[2];
+
if (fabs(out_buffer[i].vertex0[j]) > 1000.0f) {
bad_triangle = true;
}
@@ -469,9 +506,14 @@
// glmReverseWinding( model );
FILE *out;
+ FILE *normal_out = 0;
int group_num = 0;
- v3c1_face out_face;
-
+
+ // Output structures
+ v3c1_face out_face;
+ float normal[3][3] = { {0, 0, 0}, {1, 1, 1}, {2, 2, 2} };
+
+
// Iterate over the groups.
GLMgroup *group = model->groups;
while (group) {
@@ -487,12 +529,23 @@
printf( "Error could not open output file:
%s\n", file_name );
return;
}
-
+
+ // Determine if it is necessary to open a normal file.
+ if (use_normals) {
+ char normal_file_name[128];
+ sprintf( normal_file_name, "%s.nor", file_name );
+ if ((normal_out = fopen( normal_file_name, "w" )) == 0) {
+ std::cout << "Error could not open normal output file: "
+ << normal_file_name << std::endl;
+ }
+ }
+
printf( "Group: %d File: %s ", group_num, file_name );
}
else {
// Use a single output file.
- out = out_file;
+ out = out_file;
+ normal_out = normal_out_file;
printf( "Group: %d\n", group_num );
}
@@ -500,33 +553,76 @@
// Output all of the triangles in the group.
for (int i=0;i<group->numtriangles;++i) {
int tri_index = group->triangles[i];
-
+
+
/////////////////////////////////////////////////////////////////////////
// Copy out three vertices.
for (int v=0;v<3;++v) {
int vertex_index =
model->triangles[tri_index].vindices[v];
- out_face.vertex(v)[0] =
model->vertices[(3*vertex_index)];
- out_face.vertex(v)[1] =
model->vertices[(3*vertex_index)+1];
- out_face.vertex(v)[2] =
model->vertices[(3*vertex_index)+2];
+ out_face.vertex(v)[0] =
model->vertices[(3*vertex_index)] * input_scale[0];
+ out_face.vertex(v)[1] =
model->vertices[(3*vertex_index)+1] * input_scale[1];
+ out_face.vertex(v)[2] =
model->vertices[(3*vertex_index)+2] * input_scale[2];
// Bound the vertex.
+ bounds.extendByPoint( Point( out_face.vertex(v)[0],
+ out_face.vertex(v)[1],
+ out_face.vertex(v)[2] ) );
+
+
///////////////////////////////////////////////////////////////////////
+ // Do we need to copy out normals?
+ if (normal_out) {
+ int normal_index = model->triangles[tri_index].nindices[v];
+ if (normal_index < model->numnormals) {
+ float *f = model->normals+(normal_index*3);
+
+ // Copy out the normal.
+ normal[v][0] = f[0];
+ normal[v][1] = f[1];
+ normal[v][2] = f[2];
+ }
+ else {
+ std::cout << "Invalid normal " << normal_index
+ << " specified for face: " << tri_index
+ << std::endl;
+ }
+ }
}
- // Copy out the color.
+
/////////////////////////////////////////////////////////////////////////
+ // Specify the face color.
int mtl_index = group->material;
- for (int c=0; c<3;++c) {
- out_face.color[c] =
model->materials[mtl_index].diffuse[c];
- }
-
+ if (mtl_index < model->nummaterials) {
+ // Copy out the color.
+ for (int c=0; c<3;++c) {
+ out_face.color[c] = model->materials[mtl_index].diffuse[c];
+ }
+ }
+ else {
+ // Use the default color.
+ for (int c=0; c<3;++c) {
+ out_face.color[c] = default_color[c];
+ }
+ }
+
+
/////////////////////////////////////////////////////////////////////////
// Output.
fwrite( &out_face, sizeof( v3c1_face ), 1, out );
+
+ if (normal_out) {
+ fwrite( &normal, sizeof(float)*9, 1, normal_out );
+ }
}
// Close the file if we are using one per group.
if (out_file == 0) {
fclose( out );
+
+ // Check to see if the normal file should be closed.
+ if (normal_out)
+ fclose( normal_out );
}
printf( "Faces: %d\n", group->numtriangles );
+
total_faces += group->numtriangles;
// Proceed to next group.
@@ -584,9 +680,9 @@
&index, &v[0], &v[1], &v[2] );
// Copy the coordinates to the vertex list.
- vertex_list[index-1].v[0] = v[0];
- vertex_list[index-1].v[1] = v[1];
- vertex_list[index-1].v[2] = v[2];
+ vertex_list[index-1].v[0] = v[0] * input_scale[0];
+ vertex_list[index-1].v[1] = v[1] * input_scale[1];
+ vertex_list[index-1].v[2] = v[2] * input_scale[2];
bounds.extendByPoint( Point( v[0], v[1], v[2] ) );
}
- [MANTA] r740 - branches/itanium2/StandAlone, abe, 12/01/2005
Archive powered by MHonArc 2.6.16.