1
0
Fork 0

C++-ification.

This commit is contained in:
curt 1998-10-19 19:33:31 +00:00
parent b0d67bf6b9
commit 5321d533d6

View file

@ -1,34 +1,33 @@
/* tri2obj.c -- read in a .ele/.node file pair generated by the triangle // tri2obj.cxx -- read in a .ele/.node file pair generated by the triangle
* program and output a simple Wavefront .obj file. // program and output a simple Wavefront .obj file.
* //
* Written by Curtis Olson, started October 1997. // Written by Curtis Olson, started October 1997.
* //
* Copyright (C) 1997 Curtis L. Olson - curt@infoplane.com // Copyright (C) 1997 Curtis L. Olson - curt@infoplane.com
* //
* This program is free software; you can redistribute it and/or modify // This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by // it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or // the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version. // (at your option) any later version.
* //
* This program is distributed in the hope that it will be useful, // This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of // but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. // GNU General Public License for more details.
* //
* You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software // along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
* //
* $Id$ // $Id$
* (Log is kept at end of this file) // (Log is kept at end of this file)
*/
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> /* for atoi() */ #include <stdlib.h> // for atoi()
#include <string.h> #include <string.h>
#include <sys/stat.h> /* for stat() */ #include <sys/stat.h> // for stat()
#include <unistd.h> /* for stat() */ #include <unistd.h> // for stat()
#include "tri2obj.hxx" #include "tri2obj.hxx"
@ -53,7 +52,7 @@ fgBUCKET ne_index, nw_index, sw_index, se_index;
fgBUCKET north_index, south_index, east_index, west_index; fgBUCKET north_index, south_index, east_index, west_index;
/* given three points defining a triangle, calculate the normal */ // given three points defining a triangle, calculate the normal
void calc_normal(Point3D p1, Point3D p2, void calc_normal(Point3D p1, Point3D p2,
Point3D p3, double normal[3]) Point3D p3, double normal[3])
{ {
@ -66,11 +65,11 @@ void calc_normal(Point3D p1, Point3D p2,
MAT3cross_product(normal, v1, v2); MAT3cross_product(normal, v1, v2);
MAT3_NORMALIZE_VEC(normal, temp); MAT3_NORMALIZE_VEC(normal, temp);
/* printf(" Normal = %.2f %.2f %.2f\n", normal[0], normal[1], normal[2]); */ // printf(" Normal = %.2f %.2f %.2f\n", normal[0], normal[1], normal[2]);
} }
/* return the index of all triangles containing the specified node */ // return the index of all triangles containing the specified node
void find_tris(int n, int *t1, int *t2, int *t3, int *t4, int *t5) { void find_tris(int n, int *t1, int *t2, int *t3, int *t4, int *t5) {
int i; int i;
@ -96,7 +95,7 @@ void find_tris(int n, int *t1, int *t2, int *t3, int *t4, int *t5) {
} }
/* return the file base name ( foo/bar/file.ext = file.ext ) */ // return the file base name ( foo/bar/file.ext = file.ext )
void extract_file(char *in, char *base) { void extract_file(char *in, char *base) {
int len, i; int len, i;
@ -112,7 +111,7 @@ void extract_file(char *in, char *base) {
} }
/* return the file path name ( foo/bar/file.ext = foo/bar ) */ // return the file path name ( foo/bar/file.ext = foo/bar )
void extract_path(char *in, char *base) { void extract_path(char *in, char *base) {
int len, i; int len, i;
@ -128,7 +127,7 @@ void extract_path(char *in, char *base) {
} }
/* check if a file exists */ // check if a file exists
int file_exists(char *file) { int file_exists(char *file) {
struct stat stat_buf; struct stat stat_buf;
int result; int result;
@ -138,18 +137,18 @@ int file_exists(char *file) {
result = stat(file, &stat_buf); result = stat(file, &stat_buf);
if ( result != 0 ) { if ( result != 0 ) {
/* stat failed, no file */ // stat failed, no file
printf("not found.\n"); printf("not found.\n");
return(0); return(0);
} else { } else {
/* stat succeeded, file exists */ // stat succeeded, file exists
printf("exists.\n"); printf("exists.\n");
return(1); return(1);
} }
} }
/* check to see if a shared object exists */ // check to see if a shared object exists
int shared_object_exists(char *basepath, char *ext, char *file) { int shared_object_exists(char *basepath, char *ext, char *file) {
char scene_path[256]; char scene_path[256];
long int index; long int index;
@ -335,7 +334,7 @@ int shared_object_exists(char *basepath, char *ext, char *file) {
} }
/* given a file pointer, read all the vn (normals from it) */ // given a file pointer, read all the vn (normals from it)
void read_normals(FILE *fp) { void read_normals(FILE *fp) {
char line[256]; char line[256];
@ -356,28 +355,28 @@ void read_normals(FILE *fp) {
} }
/* my custom file opening routine ... don't open if a shared edge or // my custom file opening routine ... don't open if a shared edge or
* vertex alread exists */ // vertex alread exists
FILE *my_open(char *basename, char *basepath, char *ext) { FILE *my_open(char *basename, char *basepath, char *ext) {
FILE *fp; FILE *fp;
char filename[256]; char filename[256];
/* check if a shared object already exists */ // check if a shared object already exists
if ( shared_object_exists(basepath, ext, filename) ) { if ( shared_object_exists(basepath, ext, filename) ) {
/* not an actual file open error, but we've already got the // not an actual file open error, but we've already got the
* shared edge, so we don't want to create another one */ // shared edge, so we don't want to create another one
fp = fopen(filename, "r"); fp = fopen(filename, "r");
printf("Opening %s\n", filename); printf("Opening %s\n", filename);
return(fp); return(fp);
} else { } else {
/* open the file */ // open the file
printf("not opening\n"); printf("not opening\n");
return(NULL); return(NULL);
} }
} }
/* Initialize a new mesh structure */ // Initialize a new mesh structure
void triload(char *basename, char *basepath) { void triload(char *basename, char *basepath) {
char nodename[256], elename[256]; char nodename[256], elename[256];
double n[3]; double n[3];
@ -442,11 +441,11 @@ void triload(char *basename, char *basepath) {
for ( i = 1; i <= nodecount; i++ ) { for ( i = 1; i <= nodecount; i++ ) {
fscanf(node, "%d %lf %lf %lf %d\n", &junk1, fscanf(node, "%d %lf %lf %lf %d\n", &junk1,
&n[0], &n[1], &n[2], &junk2); &n[0], &n[1], &n[2], &junk2);
/* printf("%d %.2f %.2f %.2f\n", junk1, n[0], n[1], n[2]); */ // printf("%d %.2f %.2f %.2f\n", junk1, n[0], n[1], n[2]);
p = Point3D(n[0], n[1], n[2]); p = Point3D(n[0], n[1], n[2]);
nodes[i] = fgGeodToCart(p); nodes[i] = fgGeodToCart(p);
/* printf("%d %.2f %.2f %.2f\n", // printf("%d %.2f %.2f %.2f\n",
junk1, nodes[i].x, nodes[i].y, nodes[i].z); */ // junk1, nodes[i].x, nodes[i].y, nodes[i].z);
} }
fclose(node); fclose(node);
@ -469,14 +468,14 @@ void triload(char *basename, char *basepath) {
for ( i = 1; i <= tricount; i++ ) { for ( i = 1; i <= tricount; i++ ) {
fscanf(ele, "%d %d %d %d\n", &junk1, fscanf(ele, "%d %d %d %d\n", &junk1,
&tris[i][0], &tris[i][1], &tris[i][2]); &tris[i][0], &tris[i][1], &tris[i][2]);
/* printf("%d %d %d %d\n", junk1, tris[i][0], tris[i][1], tris[i][2]);*/ // printf("%d %d %d %d\n", junk1, tris[i][0], tris[i][1], tris[i][2]);*/
} }
fclose(ele); fclose(ele);
} }
/* dump in WaveFront .obj format */ // dump in WaveFront .obj format
void dump_obj(char *basename) { void dump_obj(char *basename) {
char objname[256]; char objname[256];
double n1[3], n2[3], n3[3], n4[3], n5[3], norm[3], temp; double n1[3], n2[3], n3[3], n4[3], n5[3], norm[3], temp;
@ -491,7 +490,7 @@ void dump_obj(char *basename) {
obj = fopen(objname, "w"); obj = fopen(objname, "w");
/* dump vertices */ // dump vertices
printf(" writing vertices\n"); printf(" writing vertices\n");
for ( i = 1; i <= nodecount; i++ ) { for ( i = 1; i <= nodecount; i++ ) {
x = nodes[i].x(); x = nodes[i].x();
@ -503,16 +502,16 @@ void dump_obj(char *basename) {
printf(" calculating and writing normals\n"); printf(" calculating and writing normals\n");
printf(" First %d normals taken from shared files.\n", normalcount); printf(" First %d normals taken from shared files.\n", normalcount);
/* calculate and generate normals */ // calculate and generate normals
for ( i = 1; i <= nodecount; i++ ) { for ( i = 1; i <= nodecount; i++ ) {
if ( i <= normalcount ) { if ( i <= normalcount ) {
/* use precalculated (shared) normal */ // use precalculated (shared) normal
norm[0] = normals[i-1][0]; norm[0] = normals[i-1][0];
norm[1] = normals[i-1][1]; norm[1] = normals[i-1][1];
norm[2] = normals[i-1][2]; norm[2] = normals[i-1][2];
} else { } else {
/* printf("Finding normal\n"); */ // printf("Finding normal\n");
find_tris(i, &t1, &t2, &t3, &t4, &t5); find_tris(i, &t1, &t2, &t3, &t4, &t5);
@ -550,24 +549,24 @@ void dump_obj(char *basename) {
count = 5; count = 5;
} }
/* printf(" norm[2] = %.2f %.2f %.2f\n", n1[2], n2[2], n3[2]); */ // printf(" norm[2] = %.2f %.2f %.2f\n", n1[2], n2[2], n3[2]);
norm[0] = ( n1[0] + n2[0] + n3[0] + n4[0] + n5[0] ) / (double)count; norm[0] = ( n1[0] + n2[0] + n3[0] + n4[0] + n5[0] ) / (double)count;
norm[1] = ( n1[1] + n2[1] + n3[1] + n4[1] + n5[1] ) / (double)count; norm[1] = ( n1[1] + n2[1] + n3[1] + n4[1] + n5[1] ) / (double)count;
norm[2] = ( n1[2] + n2[2] + n3[2] + n4[2] + n5[2] ) / (double)count; norm[2] = ( n1[2] + n2[2] + n3[2] + n4[2] + n5[2] ) / (double)count;
/* printf(" count = %d\n", count); */ // printf(" count = %d\n", count);
/* printf(" Ave. normal = %.4f %.4f %.4f\n", // printf(" Ave. normal = %.4f %.4f %.4f\n",
norm[0], norm[1], norm[2]);*/ // norm[0], norm[1], norm[2]);*/
MAT3_NORMALIZE_VEC(norm, temp); MAT3_NORMALIZE_VEC(norm, temp);
/* printf(" Normalized ave. normal = %.4f %.4f %.4f\n", */ // printf(" Normalized ave. normal = %.4f %.4f %.4f\n",
/* norm[0], norm[1], norm[2]); */ // norm[0], norm[1], norm[2]);
} }
/* printf("%d vn %.4f %.4f %.4f\n", i, norm[0], norm[1], norm[2]); */ // printf("%d vn %.4f %.4f %.4f\n", i, norm[0], norm[1], norm[2]);
fprintf(obj, "vn %.4f %.4f %.4f\n", norm[0], norm[1], norm[2]); fprintf(obj, "vn %.4f %.4f %.4f\n", norm[0], norm[1], norm[2]);
} }
/* dump faces */ // dump faces
printf(" writing faces\n"); printf(" writing faces\n");
for ( i = 1; i <= tricount; i++ ) { for ( i = 1; i <= tricount; i++ ) {
fprintf(obj, "f %d %d %d\n", tris[i][0], tris[i][1], tris[i][2]); fprintf(obj, "f %d %d %d\n", tris[i][0], tris[i][1], tris[i][2]);
@ -583,13 +582,13 @@ int main(int argc, char **argv) {
strcpy(basename, argv[1]); strcpy(basename, argv[1]);
/* find the base path of the file */ // find the base path of the file
extract_path(basename, basepath); extract_path(basename, basepath);
extract_path(basepath, basepath); extract_path(basepath, basepath);
extract_path(basepath, basepath); extract_path(basepath, basepath);
printf("%s\n", basepath); printf("%s\n", basepath);
/* find the index of the current file */ // find the index of the current file
extract_file(basename, temp); extract_file(basename, temp);
len = strlen(temp); len = strlen(temp);
if ( len >= 2 ) { if ( len >= 2 ) {
@ -601,7 +600,7 @@ int main(int argc, char **argv) {
printf("bucket = %d %d %d %d\n", printf("bucket = %d %d %d %d\n",
my_index.lon, my_index.lat, my_index.x, my_index.y); my_index.lon, my_index.lat, my_index.x, my_index.y);
/* generate the indexes of the neighbors */ // generate the indexes of the neighbors
fgBucketOffset(&my_index, &ne_index, 1, 1); fgBucketOffset(&my_index, &ne_index, 1, 1);
fgBucketOffset(&my_index, &nw_index, -1, 1); fgBucketOffset(&my_index, &nw_index, -1, 1);
fgBucketOffset(&my_index, &se_index, 1, -1); fgBucketOffset(&my_index, &se_index, 1, -1);
@ -612,75 +611,78 @@ int main(int argc, char **argv) {
fgBucketOffset(&my_index, &east_index, 1, 0); fgBucketOffset(&my_index, &east_index, 1, 0);
fgBucketOffset(&my_index, &west_index, -1, 0); fgBucketOffset(&my_index, &west_index, -1, 0);
/* load the input data files */ // load the input data files
triload(basename, basepath); triload(basename, basepath);
/* dump in WaveFront .obj format */ // dump in WaveFront .obj format
dump_obj(basename); dump_obj(basename);
return(0); return(0);
} }
/* $Log$ // $Log$
/* Revision 1.2 1998/10/18 01:17:29 curt // Revision 1.3 1998/10/19 19:33:31 curt
/* Point3D tweaks. // C++-ification.
/* //
* Revision 1.1 1998/07/08 14:54:53 curt // Revision 1.2 1998/10/18 01:17:29 curt
* renamed *.[ch] to *.[ch]xx // Point3D tweaks.
* //
* Revision 1.17 1998/07/04 00:56:40 curt // Revision 1.1 1998/07/08 14:54:53 curt
* typedef'd struct fgBUCKET. // renamed *.[ch] to *.[ch]xx
* //
* Revision 1.16 1998/05/23 15:20:41 curt // Revision 1.17 1998/07/04 00:56:40 curt
* Output more digits after the decimal place. // typedef'd struct fgBUCKET.
* //
* Revision 1.15 1998/05/02 01:54:39 curt // Revision 1.16 1998/05/23 15:20:41 curt
* Converting to polar3d.h routines. // Output more digits after the decimal place.
* //
* Revision 1.14 1998/04/18 04:01:32 curt // Revision 1.15 1998/05/02 01:54:39 curt
* Now use libMath rather than having local copies of math routines. // Converting to polar3d.h routines.
* //
* Revision 1.13 1998/04/14 02:26:11 curt // Revision 1.14 1998/04/18 04:01:32 curt
* Code reorganizations. Added a Lib/ directory for more general libraries. // Now use libMath rather than having local copies of math routines.
* //
* Revision 1.12 1998/04/08 23:22:18 curt // Revision 1.13 1998/04/14 02:26:11 curt
* Adopted Gnu automake/autoconf system. // Code reorganizations. Added a Lib/ directory for more general libraries.
* //
* Revision 1.11 1998/03/03 16:01:00 curt // Revision 1.12 1998/04/08 23:22:18 curt
* More c++ compile tweaks. // Adopted Gnu automake/autoconf system.
* //
* Revision 1.10 1998/01/31 00:41:27 curt // Revision 1.11 1998/03/03 16:01:00 curt
* Made a few changes converting floats to doubles. // More c++ compile tweaks.
* //
* Revision 1.9 1998/01/27 18:37:04 curt // Revision 1.10 1998/01/31 00:41:27 curt
* Lots of updates to get back in sync with changes made over in .../Src/ // Made a few changes converting floats to doubles.
* //
* Revision 1.8 1998/01/17 01:25:39 curt // Revision 1.9 1998/01/27 18:37:04 curt
* Added support for shared normals. // Lots of updates to get back in sync with changes made over in .../Src/
* //
* Revision 1.7 1998/01/12 02:42:00 curt // Revision 1.8 1998/01/17 01:25:39 curt
* Average up to five triangles per vertex instead of three. // Added support for shared normals.
* //
* Revision 1.6 1998/01/09 23:03:15 curt // Revision 1.7 1998/01/12 02:42:00 curt
* Restructured to split 1deg x 1deg dem's into 64 subsections. // Average up to five triangles per vertex instead of three.
* //
* Revision 1.5 1997/12/08 19:17:50 curt // Revision 1.6 1998/01/09 23:03:15 curt
* Fixed a type in the normal generation code. // Restructured to split 1deg x 1deg dem's into 64 subsections.
* //
* Revision 1.4 1997/12/02 13:13:32 curt // Revision 1.5 1997/12/08 19:17:50 curt
* Fixed problem with averaged vertex normals. // Fixed a type in the normal generation code.
* //
* Revision 1.3 1997/11/15 18:05:05 curt // Revision 1.4 1997/12/02 13:13:32 curt
* minor tweaks ... // Fixed problem with averaged vertex normals.
* //
* Revision 1.2 1997/11/14 00:29:13 curt // Revision 1.3 1997/11/15 18:05:05 curt
* Transform scenery coordinates at this point in pipeline when scenery is // minor tweaks ...
* being translated to .obj format, not when it is being loaded into the end //
* renderer. Precalculate normals for each node as average of the normals // Revision 1.2 1997/11/14 00:29:13 curt
* of each containing polygon so Garoude shading is now supportable. // Transform scenery coordinates at this point in pipeline when scenery is
* // being translated to .obj format, not when it is being loaded into the end
* Revision 1.1 1997/10/29 23:05:15 curt // renderer. Precalculate normals for each node as average of the normals
* Initial revision. // of each containing polygon so Garoude shading is now supportable.
* //
*/ // Revision 1.1 1997/10/29 23:05:15 curt
// Initial revision.
//