1
0
Fork 0

Code now calculates a center reference points and outputs everything

relative to that.  This is useful in the rendering engine to keep everything
close to (0, 0, 0) where we can avoid many GLfloat precision problems.
This commit is contained in:
curt 1998-04-27 03:33:11 +00:00
parent 41515d1bae
commit 9cc98181fa

View file

@ -35,6 +35,8 @@
/* what do ya' know, here's some global variables */ /* what do ya' know, here's some global variables */
static double nodes[MAXNODES][3]; static double nodes[MAXNODES][3];
static double normals[MAXNODES][3]; static double normals[MAXNODES][3];
static int faces[MAXNODES][3];
int ncount, vncount, fcount;
static int ccw_list[MAXNODES]; static int ccw_list[MAXNODES];
int ccw_list_ptr; int ccw_list_ptr;
@ -44,6 +46,8 @@ int cw_list_ptr;
FILE *in, *out; FILE *in, *out;
double refx, refy, refz;
/* some simple list routines */ /* some simple list routines */
@ -67,6 +71,42 @@ void list_add(int *list, int *list_ptr, int node) {
} }
/* dump nodes */
void dump_nodes( void ) {
int i;
fprintf(out, "\n");
for ( i = 0; i < ncount; i++ ) {
fprintf(out, "v %.4lf %.4lf %.4lf\n",
nodes[i][0] - refx, nodes[i][1] - refy, nodes[i][2] - refz);
}
}
/* dump normals */
void dump_normals( void ) {
int i;
fprintf(out, "\n");
for ( i = 0; i < vncount; i++ ) {
fprintf(out, "vn %.4lf %.4lf %.4lf\n",
normals[i][0], normals[i][1], normals[i][2]);
}
}
/* dump faces */
void dump_faces( void ) {
int i;
fprintf(out, "\n");
for ( i = 0; i < fcount; i++ ) {
fprintf(out, "f %d %d %d\n",
faces[i][0], faces[i][1], faces[i][2]);
}
}
/* dump list */ /* dump list */
void dump_list(int *list, int list_ptr) { void dump_list(int *list, int list_ptr) {
int i; int i;
@ -142,7 +182,8 @@ double check_cur_face(int n1, int n2, int n3) {
void obj_fix(char *infile, char *outfile) { void obj_fix(char *infile, char *outfile) {
char line[256]; char line[256];
double dot_prod; double dot_prod;
int first, ncount, vncount, n1, n2, n3, n4; int first, n1, n2, n3, n4;
double x, y, z, xmax, xmin, ymax, ymin, zmax, zmin;
int is_ccw; int is_ccw;
if ( (in = fopen(infile, "r")) == NULL ) { if ( (in = fopen(infile, "r")) == NULL ) {
@ -159,8 +200,9 @@ void obj_fix(char *infile, char *outfile) {
list_init(&cw_list_ptr); list_init(&cw_list_ptr);
first = 1; first = 1;
ncount = 1; ncount = 0;
vncount = 1; vncount = 0;
fcount = 0;
printf("Reading file: %s\n", infile); printf("Reading file: %s\n", infile);
@ -169,16 +211,37 @@ void obj_fix(char *infile, char *outfile) {
/* pass along the comments verbatim */ /* pass along the comments verbatim */
fprintf(out, "%s", line); fprintf(out, "%s", line);
} else if ( strlen(line) <= 1 ) { } else if ( strlen(line) <= 1 ) {
/* pass along empty lines */ /* don't pass along empty lines */
fprintf(out, "%s", line); // fprintf(out, "%s", line);
} else if ( strncmp(line, "v ", 2) == 0 ) { } else if ( strncmp(line, "v ", 2) == 0 ) {
/* save vertex to memory and output to file */ /* save vertex to memory and output to file */
if ( ncount < MAXNODES ) { if ( ncount < MAXNODES ) {
/* printf("vertex = %s", line); */ /* printf("vertex = %s", line); */
sscanf(line, "v %lf %lf %lf\n", sscanf(line, "v %lf %lf %lf\n", &x, &y, &z);
&nodes[ncount][0], &nodes[ncount][1], &nodes[ncount][2]); nodes[ncount][0] = x;
fprintf(out, "v %.2f %.2f %.2f\n", nodes[ncount][1] = y;
nodes[ncount][0], nodes[ncount][1], nodes[ncount][2]); nodes[ncount][2] = z;
/* first time through set min's and max'es */
if ( ncount == 1 ) {
xmin = x;
xmax = x;
ymin = y;
ymax = y;
zmin = z;
zmax = z;
}
/* keep track of min/max vertex values */
if ( x < xmin ) xmin = x;
if ( x > xmax ) xmax = x;
if ( y < ymin ) ymin = y;
if ( y > ymax ) ymax = y;
if ( z < zmin ) zmin = z;
if ( z > zmax ) zmax = z;
// fprintf(out, "v %.2f %.2f %.2f\n",
// nodes[ncount][0], nodes[ncount][1], nodes[ncount][2]);
ncount++; ncount++;
} else { } else {
printf("Read too many nodes ... dying :-(\n"); printf("Read too many nodes ... dying :-(\n");
@ -191,8 +254,8 @@ void obj_fix(char *infile, char *outfile) {
sscanf(line, "vn %lf %lf %lf\n", sscanf(line, "vn %lf %lf %lf\n",
&normals[vncount][0], &normals[vncount][1], &normals[vncount][0], &normals[vncount][1],
&normals[vncount][2]); &normals[vncount][2]);
fprintf(out, "vn %.4f %.4f %.4f\n", normals[vncount][0], // fprintf(out, "vn %.4f %.4f %.4f\n", normals[vncount][0],
normals[vncount][1], normals[vncount][2]); // normals[vncount][1], normals[vncount][2]);
vncount++; vncount++;
} else { } else {
printf("Read too many vertex normals ... dying :-(\n"); printf("Read too many vertex normals ... dying :-(\n");
@ -252,8 +315,20 @@ void obj_fix(char *infile, char *outfile) {
} }
} }
} else if ( line[0] == 'f' ) { } else if ( line[0] == 'f' ) {
/* pass along the unoptimized faces verbatim */ if ( fcount < MAXNODES ) {
fprintf(out, "%s", line); /* pass along the unoptimized faces verbatim */
sscanf(line, "f %d %d %d\n", &n1, &n2, &n3);
faces[fcount][0] = n1;
faces[fcount][1] = n2;
faces[fcount][2] = n3;
fcount++;
} else {
printf("Read too many unoptimized faces ... dying :-(\n");
exit(-1);
}
// fprintf(out, "%s", line);
} else if ( line[0] == 'q' ) { } else if ( line[0] == 'q' ) {
/* continue a triangle strip */ /* continue a triangle strip */
n1 = n2 = 0; n1 = n2 = 0;
@ -279,6 +354,18 @@ void obj_fix(char *infile, char *outfile) {
} }
} }
/* reference point is the "center" */
refx = (xmin + xmax) / 2.0;
refy = (ymin + ymax) / 2.0;
refz = (zmin + zmax) / 2.0;
fprintf(out, "\n");
fprintf(out, "ref %.2f %.2f %.2f\n", refx, refy, refz);
dump_nodes();
dump_normals();
dump_faces();
fprintf(out, "winding ccw\n"); fprintf(out, "winding ccw\n");
dump_list(ccw_list, ccw_list_ptr); dump_list(ccw_list, ccw_list_ptr);
@ -291,9 +378,14 @@ void obj_fix(char *infile, char *outfile) {
/* $Log$ /* $Log$
/* Revision 1.9 1998/04/18 04:01:03 curt /* Revision 1.10 1998/04/27 03:33:11 curt
/* Now use libMath rather than having local copies of math routines. /* Code now calculates a center reference points and outputs everything
/* relative to that. This is useful in the rendering engine to keep everything
/* close to (0, 0, 0) where we can avoid many GLfloat precision problems.
/* /*
* Revision 1.9 1998/04/18 04:01:03 curt
* Now use libMath rather than having local copies of math routines.
*
* Revision 1.8 1998/04/08 23:19:37 curt * Revision 1.8 1998/04/08 23:19:37 curt
* Adopted Gnu automake/autoconf system. * Adopted Gnu automake/autoconf system.
* *