]> git.mxchange.org Git - flightgear.git/blobdiff - Scenery/obj.c
C++ - ifing the code a bit.
[flightgear.git] / Scenery / obj.c
index b7e5c83bc37b8a01179752ce4829f52580ff80a5..0c1374b2dfd51164aa74d41cf4529b726da968f6 100644 (file)
@@ -1,4 +1,5 @@
-/**************************************************************************
+/* -*- Mode: C++ -*-
+ *
  * obj.c -- routines to handle WaveFront .obj format files.
  *
  * Written by Curtis Olson, started October 1997.
@@ -24,7 +25,9 @@
  **************************************************************************/
 
 
-#ifdef WIN32
+#include <config.h>
+
+#ifdef HAVE_WINDOWS_H
 #  include <windows.h>
 #endif
 
 #include <GL/glut.h>
 #include <XGL/xgl.h>
 
-#include <Main/fg_debug.h>
+#include <Debug/fg_debug.h>
+#include <Include/fg_constants.h>
 #include <Math/mat3.h>
+#include <Math/fg_random.h>
 #include <Scenery/obj.h>
 #include <Scenery/scenery.h>
-
+#include <zlib/zlib.h>
 
 
 #define MAXNODES 100000
@@ -63,31 +68,70 @@ void calc_normal(double p1[3], double p2[3], double p3[3], double normal[3])
 }
 
 
+#define FG_TEX_CONSTANT 128.0
+
+float calc_lon(double x, double y, double z) {
+    float tmp;
+    tmp = (RAD_TO_DEG*atan2(y, x)) * FG_TEX_CONSTANT;
+
+    // printf("lon = %.2f\n", (float)tmp);
+    return (float)tmp;
+}
+
+
+float calc_lat(double x, double y, double z) {
+    float tmp;
+
+    tmp = (90.0 - RAD_TO_DEG*atan2( sqrt(x*x + y*y), z )) * FG_TEX_CONSTANT;
+
+    // printf("lat = %.2f\n", (float)tmp);
+    return (float)tmp;
+}
+
+
 /* Load a .obj file and generate the GL call list */
 GLint fgObjLoad(char *path, struct fgCartesianPoint *ref, double *radius) {
-    char line[256], winding_str[256];
+    char gzpath[256], line[256], winding_str[256];
     double approx_normal[3], normal[3], scale;
     double x, y, z, xmax, xmin, ymax, ymin, zmax, zmin;
+    GLfloat sgenparams[] = { 1.0, 0.0, 0.0, 0.0 };
     GLint tile;
-    FILE *f;
+    gzFile f;
     int first, ncount, vncount, n1, n2, n3, n4;
-    static int use_vertex_norms = 0;
+    static int use_per_vertex_norms = 1;
     int winding;
     int last1, last2, odd;
 
-    if ( (f = fopen(path, "r")) == NULL ) {
-       fgPrintf(FG_TERRAIN, FG_ALERT, "Cannot open file: %s\n", path);
-       return(-1);
+    // First try "path.gz"
+    strcpy(gzpath, path);
+    strcat(gzpath, ".gz");
+    if ( (f = gzopen(gzpath, "r")) == NULL ) {
+       // Next try "path"
+       if ( (f = gzopen(path, "r")) == NULL ) {
+           fgPrintf(FG_TERRAIN, FG_ALERT, "Cannot open file: %s\n", path);
+           return(-1);
+       }
     }
 
     tile = xglGenLists(1);
     xglNewList(tile, GL_COMPILE);
 
+    /*
+    xglTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
+    xglTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
+    xglTexGenfv(GL_S, GL_OBJECT_PLANE, sgenparams);
+    xglTexGenfv(GL_T, GL_OBJECT_PLANE, sgenparams);
+    // xglTexGenfv(GL_S, GL_SPHERE_MAP, 0);
+    // xglTexGenfv(GL_T, GL_SPHERE_MAP, 0);
+    xglEnable(GL_TEXTURE_GEN_S);
+    xglEnable(GL_TEXTURE_GEN_T);
+    */
+
     first = 1;
     ncount = 1;
     vncount = 1;
 
-    while ( fgets(line, 250, f) != NULL ) {
+    while ( gzgets(f, line, 250) != NULL ) {
        if ( line[0] == '#' ) {
            /* comment -- ignore */
        } else if ( line[0] == '\n' ) {
@@ -119,14 +163,6 @@ GLint fgObjLoad(char *path, struct fgCartesianPoint *ref, double *radius) {
                if ( z < zmin ) zmin = z;
                if ( z > zmax ) zmax = z;               
 
-               /* reference point is the "center" */
-               /* this is overkill to calculate it everytime we get a
-                 * new node, but it's hard to know with the .obj
-                 * format when we are done with vertices */
-               ref->x = (xmin + xmax) / 2;
-               ref->y = (ymin + ymax) / 2;
-               ref->z = (zmin + zmax) / 2;
-
                ncount++;
            } else {
                fgPrintf( FG_TERRAIN, FG_EXIT, 
@@ -187,21 +223,21 @@ GLint fgObjLoad(char *path, struct fgCartesianPoint *ref, double *radius) {
                scale = 1.0;
            }
 
-           if ( use_vertex_norms ) {
+           if ( use_per_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);
+               xglTexCoord2f(calc_lon(nodes[n1][0], nodes[n1][1], nodes[n1][2]), calc_lat(nodes[n1][0], nodes[n1][1], nodes[n1][2]));
+               xglVertex3d(nodes[n1][0], nodes[n1][1], nodes[n1][2]);
 
                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);
+               xglTexCoord2f(calc_lon(nodes[n2][0], nodes[n2][1], nodes[n2][2]), calc_lat(nodes[n2][0], nodes[n2][1], nodes[n2][2]));
+               xglVertex3d(nodes[n2][0], nodes[n2][1], nodes[n2][2]);
 
                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);
+               xglTexCoord2f(calc_lon(nodes[n3][0], nodes[n3][1], nodes[n3][2]), calc_lat(nodes[n3][0], nodes[n3][1], nodes[n3][2]));
+               xglVertex3d(nodes[n3][0], nodes[n3][1], nodes[n3][2]);
            } else {
                if ( odd ) {
                    calc_normal(nodes[n1], nodes[n2], nodes[n3], approx_normal);
@@ -211,12 +247,12 @@ GLint fgObjLoad(char *path, struct fgCartesianPoint *ref, double *radius) {
                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);
+               xglTexCoord2f(calc_lon(nodes[n1][0], nodes[n1][1], nodes[n1][2]), calc_lat(nodes[n1][0], nodes[n1][1], nodes[n1][2]));
+               xglVertex3d(nodes[n1][0], nodes[n1][1], nodes[n1][2]);
+               xglTexCoord2f(calc_lon(nodes[n2][0], nodes[n2][1], nodes[n2][2]), calc_lat(nodes[n2][0], nodes[n2][1], nodes[n2][2]));
+               xglVertex3d(nodes[n2][0], nodes[n2][1], nodes[n2][2]);
+               xglTexCoord2f(calc_lon(nodes[n3][0], nodes[n3][1], nodes[n3][2]), calc_lat(nodes[n3][0], nodes[n3][1], nodes[n3][2]));
+               xglVertex3d(nodes[n3][0], nodes[n3][1], nodes[n3][2]);
            }
 
            odd = 1 - odd;
@@ -224,15 +260,15 @@ GLint fgObjLoad(char *path, struct fgCartesianPoint *ref, double *radius) {
            last2 = n3;
 
            if ( n4 > 0 ) {
-               if ( use_vertex_norms ) {
+               if ( use_per_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);
+               xglTexCoord2f(calc_lon(nodes[n4][0], nodes[n4][1], nodes[n4][2]), calc_lat(nodes[n4][0], nodes[n4][1], nodes[n4][2]));
+               xglVertex3d(nodes[n4][0], nodes[n4][1], nodes[n4][2]);
 
                odd = 1 - odd;
                last1 = n3;
@@ -254,16 +290,16 @@ GLint fgObjLoad(char *path, struct fgCartesianPoint *ref, double *radius) {
            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);
+           xglTexCoord2f(calc_lon(nodes[n1][0], nodes[n1][1], nodes[n1][2]), calc_lat(nodes[n1][0], nodes[n1][1], nodes[n1][2]));
+           xglVertex3d(nodes[n1][0], nodes[n1][1], nodes[n1][2]);
 
             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);
-
+           xglTexCoord2f(calc_lon(nodes[n2][0], nodes[n2][1], nodes[n2][2]), calc_lat(nodes[n2][0], nodes[n2][1], nodes[n2][2]));
+           xglVertex3d(nodes[n2][0], nodes[n2][1], nodes[n2][2]);
+               
             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);
+           xglTexCoord2f(calc_lon(nodes[n3][0], nodes[n3][1], nodes[n3][2]), calc_lat(nodes[n3][0], nodes[n3][1], nodes[n3][2]));
+           xglVertex3d(nodes[n3][0], nodes[n3][1], nodes[n3][2]);
        } else if ( line[0] == 'q' ) {
            /* continue a triangle strip */
            n1 = n2 = 0;
@@ -273,7 +309,7 @@ GLint fgObjLoad(char *path, struct fgCartesianPoint *ref, double *radius) {
            sscanf(line, "q %d %d\n", &n1, &n2);
            /* fgPrintf( FG_TERRAIN, FG_DEBUG, "read %d %d\n", n1, n2); */
 
-           if ( use_vertex_norms ) {
+           if ( use_per_vertex_norms ) {
                MAT3_SCALE_VEC(normal, normals[n1], scale);
                xglNormal3dv(normal);
            } else {
@@ -288,9 +324,9 @@ GLint fgObjLoad(char *path, struct fgCartesianPoint *ref, double *radius) {
                xglNormal3dv(normal);
            }
 
-           xglVertex3d(nodes[n1][0] - ref->x, nodes[n1][1] - ref->y, 
-                       nodes[n1][2] - ref->z);
-           
+           xglTexCoord2f(calc_lon(nodes[n1][0], nodes[n1][1], nodes[n1][2]), calc_lat(nodes[n1][0], nodes[n1][1], nodes[n1][2]));
+           xglVertex3d(nodes[n1][0], nodes[n1][1], nodes[n1][2]);
+    
            odd = 1 - odd;
            last1 = last2;
            last2 = n1;
@@ -298,7 +334,7 @@ GLint fgObjLoad(char *path, struct fgCartesianPoint *ref, double *radius) {
            if ( n2 > 0 ) {
                /* fgPrintf( FG_TERRAIN, FG_DEBUG, " (cont)\n"); */
 
-               if ( use_vertex_norms ) {
+               if ( use_per_vertex_norms ) {
                    MAT3_SCALE_VEC(normal, normals[n2], scale);
                    xglNormal3dv(normal);
                } else {
@@ -313,8 +349,8 @@ GLint fgObjLoad(char *path, struct fgCartesianPoint *ref, double *radius) {
                    xglNormal3dv(normal);
                }
 
-               xglVertex3d(nodes[n2][0] - ref->x, nodes[n2][1] - ref->y, 
-                           nodes[n2][2] - ref->z);
+               xglTexCoord2f(calc_lon(nodes[n2][0], nodes[n2][1], nodes[n2][2]), calc_lat(nodes[n2][0], nodes[n2][1], nodes[n2][2]));
+               xglVertex3d(nodes[n2][0], nodes[n2][1], nodes[n2][2]);
 
                odd = 1 -odd;
                last1 = last2;
@@ -333,30 +369,53 @@ GLint fgObjLoad(char *path, struct fgCartesianPoint *ref, double *radius) {
     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]);
+        xglVertex3d(nodes[i][0],
+                   nodes[i][1] ,
+                   nodes[i][2]);
+       xglVertex3d(nodes[i][0] + 500*normals[i][0],
+                   nodes[i][1] + 500*normals[i][1],
+                   nodes[i][2] + 500*normals[i][2]);
     } 
     xglEnd();
     */
 
+    // xglDisable(GL_TEXTURE_GEN_S);
+    // xglDisable(GL_TEXTURE_GEN_T);
+
     xglFrontFace ( GL_CCW );
 
     xglEndList();
 
-    fclose(f);
+    gzclose(f);
+
+    /* reference point is the "center" */
+    ref->x = (xmin + xmax) / 2.0;
+    ref->y = (ymin + ymax) / 2.0;
+    ref->z = (zmin + zmax) / 2.0;
 
     return(tile);
 }
 
 
 /* $Log$
-/* Revision 1.23  1998/02/09 15:07:52  curt
-/* Minor tweaks.
+/* Revision 1.28  1998/04/22 13:22:44  curt
+/* C++ - ifing the code a bit.
 /*
+ * Revision 1.27  1998/04/18 04:13:17  curt
+ * Added zlib on the fly decompression support for loading scenery objects.
+ *
+ * Revision 1.26  1998/04/03 22:11:36  curt
+ * Converting to Gnu autoconf system.
+ *
+ * Revision 1.25  1998/03/14 00:30:50  curt
+ * Beginning initial terrain texturing experiments.
+ *
+ * Revision 1.24  1998/02/09 21:30:18  curt
+ * Fixed a nagging problem with terrain tiles not "quite" matching up perfectly.
+ *
+ * Revision 1.23  1998/02/09 15:07:52  curt
+ * Minor tweaks.
+ *
  * Revision 1.22  1998/02/01 03:39:54  curt
  * Minor tweaks.
  *