]> git.mxchange.org Git - flightgear.git/blobdiff - Scenery/obj.c
Merged in make system changes from Bob Kuehne <rpk@sgi.com>
[flightgear.git] / Scenery / obj.c
index ea92cffa28422105ad255d0a62cfc9cb868598aa..c480a6f03042b281d1be48d30c85d36139511540 100644 (file)
 #endif
 
 #include <stdio.h>
+#include <string.h>
 #include <GL/glut.h>
+#include <XGL/xgl.h>
 
-#include "obj.h"
+#include <Scenery/obj.h>
+#include <Scenery/scenery.h>
+
+#include <Math/mat3.h>
 
-#include "../constants.h"
-#include "../types.h"
-#include "../Math/fg_geodesy.h"
-#include "../Math/polar.h"
 
 
 #define MAXNODES 100000
 
 float nodes[MAXNODES][3];
+float normals[MAXNODES][3];
 
 
-/* convert a geodetic point lon(arcsec), lat(arcsec), elev(meter) to
- * a cartesian point */
-struct fgCartesianPoint geod_to_cart(float geod[3]) {
-    struct fgCartesianPoint p;
-    double gc_lon, gc_lat, sl_radius;
-
-    printf("A geodetic point is (%.2f, %.2f, %.2f)\n", 
-          geod[0], geod[1], geod[2]);
+/* given three points defining a triangle, calculate the normal */
+void calc_normal(float p1[3], float p2[3], float p3[3], double normal[3])
+{
+    double v1[3], v2[3];
+    float temp;
 
-    gc_lon = geod[0]*ARCSEC_TO_RAD;
-    fgGeodToGeoc(geod[1]*ARCSEC_TO_RAD, geod[2], &sl_radius, &gc_lat);
+    v1[0] = p2[0] - p1[0]; v1[1] = p2[1] - p1[1]; v1[2] = p2[2] - p1[2];
+    v2[0] = p3[0] - p1[0]; v2[1] = p3[1] - p1[1]; v2[2] = p3[2] - p1[2];
 
-    printf("A geocentric point is (%.2f, %.2f, %.2f)\n", gc_lon, 
-          gc_lat, sl_radius+geod[2]);
+    MAT3cross_product(normal, v1, v2);
+    MAT3_NORMALIZE_VEC(normal,temp);
 
-    p = fgPolarToCart(gc_lon, gc_lat, sl_radius+geod[2]);
-    
-    printf("A cart point is (%.8f, %.8f, %.8f)\n", p.x, p.y, p.z);
-
-    return(p);
+    /* printf("  Normal = %.2f %.2f %.2f\n", normal[0], normal[1], normal[2]);*/
 }
 
 
 /* Load a .obj file and generate the GL call list */
-GLint fgObjLoad(char *path) {
-    char line[256];
-    static GLfloat color[4] = { 0.5, 0.5, 0.25, 1.0 };
-    struct fgCartesianPoint p1, p2, p3;
+GLint fgObjLoad(char *path, struct fgCartesianPoint *ref) {
+    char line[256], winding_str[256];
+    double v1[3], v2[3], approx_normal[3], normal[3], dot_prod, scale, temp;
     GLint area;
     FILE *f;
-    int first, ncount, n1, n2, n3;
+    int first, ncount, vncount, n1, n2, n3, n4;
+    static int use_vertex_norms = 1;
+    int i, winding;
+    int last1, last2, odd;
 
     if ( (f = fopen(path, "r")) == NULL ) {
        printf("Cannot open file: %s\n", path);
-       exit(-1);
+       /* exit(-1); */
+       return(0);
     }
 
-    area = glGenLists(1);
-    glNewList(area, GL_COMPILE);
-
-    /* glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color ); */
-    glColor3fv(color);
-
-    glBegin(GL_TRIANGLE_STRIP);
+    area = xglGenLists(1);
+    xglNewList(area, GL_COMPILE);
 
     first = 1;
     ncount = 1;
+    vncount = 1;
 
     while ( fgets(line, 250, f) != NULL ) {
        if ( line[0] == '#' ) {
            /* comment -- ignore */
-       } else if ( line[0] == 'v' ) {
+       } else if ( line[0] == '\n' ) {
+           /* empty line -- ignore */
+       } else if ( strncmp(line, "v ", 2) == 0 ) {
            /* node (vertex) */
            if ( ncount < MAXNODES ) {
-               printf("vertex = %s", line);
+               /* printf("vertex = %s", line); */
                sscanf(line, "v %f %f %f\n", 
                       &nodes[ncount][0], &nodes[ncount][1], &nodes[ncount][2]);
+               if ( ncount == 1 ) {
+                   /* first node becomes the reference point */
+                   ref->x = nodes[ncount][0];
+                   ref->y = nodes[ncount][1];
+                   ref->z = nodes[ncount][2];
+                   /* scenery.center = ref; */
+               }
                ncount++;
            } else {
                printf("Read too many nodes ... dying :-(\n");
                exit(-1);
            }
+       } else if ( strncmp(line, "vn ", 3) == 0 ) {
+           /* vertex normal */
+           if ( vncount < MAXNODES ) {
+               /* printf("vertex normal = %s", line); */
+               sscanf(line, "vn %f %f %f\n", 
+                      &normals[vncount][0], &normals[vncount][1], 
+                      &normals[vncount][2]);
+               vncount++;
+           } else {
+               printf("Read too many vertex normals ... dying :-(\n");
+               exit(-1);
+           }
+       } else if ( strncmp(line, "winding ", 8) == 0 ) {
+           sscanf(line+8, "%s", winding_str);
+           printf("    WINDING = %s\n", winding_str);
+
+           /* can't call xglFrontFace() between xglBegin() & xglEnd() */
+           xglEnd();
+           first = 1;
+
+           if ( strcmp(winding_str, "cw") == 0 ) {
+               xglFrontFace( GL_CW );
+               winding = 0;
+           } else {
+               glFrontFace ( GL_CCW );
+               winding = 1;
+           }
        } else if ( line[0] == 't' ) {
            /* start a new triangle strip */
 
+           n1 = n2 = n3 = n4 = 0;
+
            if ( !first ) {
                /* close out the previous structure and start the next */
+               xglEnd();
+           } else {
                first = 0;
-               glEnd();
-               glBegin(GL_TRIANGLE_STRIP);
            }
 
-           printf("new tri strip = %s", line);
-           sscanf(line, "t %d %d %d\n", &n1, &n2, &n3);
+           /* printf("    new tri strip = %s", line); */
+           sscanf(line, "t %d %d %d %d\n", &n1, &n2, &n3, &n4);
+
+           /* printf("(t) = "); */
+
+           xglBegin(GL_TRIANGLE_STRIP);
+
+           if ( winding ) {
+               odd = 1; 
+               scale = 1.0;
+           } else {
+               odd = 0;
+               scale = 1.0;
+           }
+
+           if ( use_vertex_norms ) {
+               MAT3_SCALE_VEC(normal, normals[n1], scale);
+               xglNormal3dv(normal);
+               xglVertex3d(nodes[n1][0] - ref->x, nodes[n1][1] - ref->y, 
+                           nodes[n1][2] - ref->z);
+
+               MAT3_SCALE_VEC(normal, normals[n2], scale);
+               xglNormal3dv(normal);
+               xglVertex3d(nodes[n2][0] - ref->x, nodes[n2][1] - ref->y, 
+                           nodes[n2][2] - ref->z);
+
+               MAT3_SCALE_VEC(normal, normals[n3], scale);
+               xglNormal3dv(normal);
+               xglVertex3d(nodes[n3][0] - ref->x, nodes[n3][1] - ref->y, 
+                           nodes[n3][2] - ref->z);
+           } else {
+               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);
+               xglNormal3dv(normal);
+
+               xglVertex3d(nodes[n1][0] - ref->x, nodes[n1][1] - ref->y, 
+                           nodes[n1][2] - ref->z);
+               xglVertex3d(nodes[n2][0] - ref->x, nodes[n2][1] - ref->y, 
+                           nodes[n2][2] - ref->z);
+               xglVertex3d(nodes[n3][0] - ref->x, nodes[n3][1] - ref->y, 
+                           nodes[n3][2] - ref->z);
+           }
+
+           odd = 1 - odd;
+           last1 = n2;
+           last2 = n3;
+
+           if ( n4 > 0 ) {
+               if ( use_vertex_norms ) {
+                   MAT3_SCALE_VEC(normal, normals[n4], scale);
+               } else {
+                   calc_normal(nodes[n3], nodes[n2], nodes[n4], approx_normal);
+                   MAT3_SCALE_VEC(normal, approx_normal, scale);
+               }
+               xglNormal3dv(normal);
+               xglVertex3d(nodes[n4][0] - ref->x, nodes[n4][1] - ref->y, 
+                           nodes[n4][2] - ref->z);
+
+               odd = 1 - odd;
+               last1 = n3;
+               last2 = n4;
+           }
+       } else if ( line[0] == 'f' ) {
+           /* unoptimized face */
+
+           if ( !first ) {
+               /* close out the previous structure and start the next */
+               xglEnd();
+           } else {
+               first = 0;
+           }
 
-           p1 = geod_to_cart(nodes[n1]);
-           p2 = geod_to_cart(nodes[n2]);
-           p3 = geod_to_cart(nodes[n3]);
+           xglBegin(GL_TRIANGLES);
+
+           /* printf("new triangle = %s", line);*/
+           sscanf(line, "f %d %d %d\n", &n1, &n2, &n3);
+
+            xglNormal3d(normals[n1][0], normals[n1][1], normals[n1][2]);
+           xglVertex3d(nodes[n1][0] - ref->x, nodes[n1][1] - ref->y, 
+                       nodes[n1][2] - ref->z);
+
+            xglNormal3d(normals[n2][0], normals[n2][1], normals[n2][2]);
+           xglVertex3d(nodes[n2][0] - ref->x, nodes[n2][1] - ref->y, 
+                       nodes[n2][2] - ref->z);
+
+            xglNormal3d(normals[n3][0], normals[n3][1], normals[n3][2]);
+           xglVertex3d(nodes[n3][0] - ref->x, nodes[n3][1] - ref->y, 
+                       nodes[n3][2] - ref->z);
        } else if ( line[0] == 'q' ) {
            /* continue a triangle strip */
            n1 = n2 = 0;
 
-           printf("continued tri strip = %s", line);
+           /* printf("continued tri strip = %s ", line); */
            sscanf(line, "q %d %d\n", &n1, &n2);
-           printf("read %d %d\n", n1, n2);
+           /* printf("read %d %d\n", n1, n2); */
+
+           if ( use_vertex_norms ) {
+               MAT3_SCALE_VEC(normal, normals[n1], scale);
+               xglNormal3dv(normal);
+           } else {
+               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);
+           }
+
+           xglVertex3d(nodes[n1][0] - ref->x, nodes[n1][1] - ref->y, 
+                       nodes[n1][2] - ref->z);
+           
+           odd = 1 - odd;
+           last1 = last2;
+           last2 = n1;
+
+           if ( n2 > 0 ) {
+               /* printf(" (cont)\n"); */
+
+               if ( use_vertex_norms ) {
+                   MAT3_SCALE_VEC(normal, normals[n2], scale);
+                   xglNormal3dv(normal);
+               } else {
+                   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);
+               }
+
+               xglVertex3d(nodes[n2][0] - ref->x, nodes[n2][1] - ref->y, 
+                           nodes[n2][2] - ref->z);
+
+               odd = 1 -odd;
+               last1 = last2;
+               last2 = n2;
+           }
        } else {
            printf("Unknown line in %s = %s\n", path, line);
        }
     }
 
-    glEnd();
-    glEndList();
+    xglEnd();
+
+    /* Draw normal vectors (for visually verifying normals)*/
+    /*
+    xglBegin(GL_LINES);
+    xglColor3f(0.0, 0.0, 0.0);
+    for ( i = 0; i < ncount; i++ ) {
+        xglVertex3d(nodes[i][0] - ref->x,
+                   nodes[i][1] - ref->y,
+                   nodes[i][2] - ref->z);
+       xglVertex3d(nodes[i][0] - ref->x + 500*normals[i][0],
+                   nodes[i][1] - ref->y + 500*normals[i][1],
+                   nodes[i][2] - ref->z + 500*normals[i][2]);
+    } 
+    xglEnd();
+    */
+
+    xglFrontFace ( GL_CCW );
+
+    xglEndList();
 
     fclose(f);
 
@@ -144,7 +328,67 @@ GLint fgObjLoad(char *path) {
 
 
 /* $Log$
-/* Revision 1.1  1997/10/28 21:14:54  curt
-/* Initial revision.
+/* Revision 1.18  1998/01/19 19:27:16  curt
+/* Merged in make system changes from Bob Kuehne <rpk@sgi.com>
+/* This should simplify things tremendously.
 /*
+ * Revision 1.17  1998/01/13 00:23:10  curt
+ * Initial changes to support loading and management of scenery tiles.  Note,
+ * there's still a fair amount of work left to be done.
+ *
+ * Revision 1.16  1997/12/30 23:09:40  curt
+ * Worked on winding problem without luck, so back to calling glFrontFace()
+ * 3 times for each scenery area.
+ *
+ * Revision 1.15  1997/12/30 20:47:51  curt
+ * Integrated new event manager with subsystem initializations.
+ *
+ * Revision 1.14  1997/12/30 01:38:46  curt
+ * Switched back to per vertex normals and smooth shading for terrain.
+ *
+ * Revision 1.13  1997/12/18 23:32:36  curt
+ * First stab at sky dome actually starting to look reasonable. :-)
+ *
+ * Revision 1.12  1997/12/17 23:13:47  curt
+ * Began working on rendering the sky.
+ *
+ * Revision 1.11  1997/12/15 23:55:01  curt
+ * Add xgl wrappers for debugging.
+ * Generate terrain normals on the fly.
+ *
+ * Revision 1.10  1997/12/12 21:41:28  curt
+ * More light/material property tweaking ... still a ways off.
+ *
+ * Revision 1.9  1997/12/12 19:52:57  curt
+ * Working on lightling and material properties.
+ *
+ * Revision 1.8  1997/12/10 01:19:51  curt
+ * Tweaks for verion 0.15 release.
+ *
+ * Revision 1.7  1997/12/08 22:51:17  curt
+ * Enhanced to handle ccw and cw tri-stripe winding.  This is a temporary
+ * admission of defeat.  I will eventually go back and get all the stripes
+ * wound the same way (ccw).
+ *
+ * Revision 1.6  1997/11/25 19:25:35  curt
+ * Changes to integrate Durk's moon/sun code updates + clean up.
+ *
+ * Revision 1.5  1997/11/15 18:16:39  curt
+ * minor tweaks.
+ *
+ * Revision 1.4  1997/11/14 00:26:49  curt
+ * Transform scenery coordinates earlier in pipeline when scenery is being
+ * created, not when it is being loaded.  Precalculate normals for each node
+ * as average of the normals of each containing polygon so Garoude shading is
+ * now supportable.
+ *
+ * Revision 1.3  1997/10/31 04:49:12  curt
+ * Tweaking vertex orders.
+ *
+ * Revision 1.2  1997/10/30 12:38:45  curt
+ * Working on new scenery subsystem.
+ *
+ * Revision 1.1  1997/10/28 21:14:54  curt
+ * Initial revision.
+ *
  */