]> git.mxchange.org Git - flightgear.git/blobdiff - Tri2obj/tri2obj.c
Fixed problem with averaged vertex normals.
[flightgear.git] / Tri2obj / tri2obj.c
index d0c84870a535e288f8438b0b40d1b800b145502f..ef1007e6754d41b90cca0a613b414d9cfc42d15d 100644 (file)
@@ -135,10 +135,10 @@ void triload(char *basename) {
     for ( i = 1; i <= nodecount; i++ ) {
        fscanf(node, "%d %lf %lf %lf %d\n", &junk1, 
               &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]); */
        nodes[i] = geod_to_cart(n);
-       printf("%d %.2f %.2f %.2f\n", 
-              junk1, nodes[i].x, nodes[i].y, nodes[i].z);
+       /* printf("%d %.2f %.2f %.2f\n", 
+              junk1, nodes[i].x, nodes[i].y, nodes[i].z); */
     }
 
     fclose(node);
@@ -171,7 +171,7 @@ void triload(char *basename) {
 /* dump in WaveFront .obj format */
 void dump_obj(char *basename) {
     char objname[256];
-    double n1[3], n2[3], n3[3];
+    double n1[3], n2[3], n3[3], norm[3], temp;
     FILE *obj;
     int i, t1, t2, t3, count;
 
@@ -183,11 +183,13 @@ void dump_obj(char *basename) {
     obj = fopen(objname, "w");
 
     /* dump vertices */
+    printf("  writing vertices\n");
     for ( i = 1; i <= nodecount; i++ ) {
        fprintf(obj, "v %.2f %.2f %.2f\n", 
                nodes[i].x, nodes[i].y, nodes[i].z);
     }
 
+    printf("  calculating and writing normals\n");
     /* calculate and generate normals */
     for ( i = 1; i <= nodecount; i++ ) {
        find_tris(i, &t1, &t2, &t3);
@@ -212,13 +214,17 @@ void dump_obj(char *basename) {
            count = 3;
        }
 
-       fprintf(obj, "vn %.4f %.4f %.4f\n", 
-               ( n1[0] + n2[0] + n3[0] ) / (double)count,
-               ( n1[1] + n2[1] + n3[1] ) / (double)count,
-               ( n1[2] + n2[2] + n3[2] ) / (double)count );
+       norm[0] = ( n1[0] + n2[0] + n3[0] ) / (double)count;
+       norm[1] = ( n1[1] + n2[1] + n3[1] ) / (double)count;
+       norm[2] = ( n1[2] + n2[2] + n3[2] ) / (double)count;
+
+       MAT3_NORMALIZE_VEC(norm, temp);
+       
+       fprintf(obj, "vn %.4f %.4f %.4f\n", norm[0], norm[1], norm[2]);
     }
 
     /* dump faces */
+    printf("  writing faces\n");
     for ( i = 1; i <= tricount; i++ ) {
        fprintf(obj, "f %d//%d %d//%d %d//%d\n", 
                tris[i][0], tris[i][0], 
@@ -245,12 +251,18 @@ int main(int argc, char **argv) {
 
 
 /* $Log$
-/* Revision 1.2  1997/11/14 00:29:13  curt
-/* 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
-/* renderer.  Precalculate normals for each node as average of the normals
-/* of each containing polygon so Garoude shading is now supportable.
+/* Revision 1.4  1997/12/02 13:13:32  curt
+/* Fixed problem with averaged vertex normals.
 /*
+ * Revision 1.3  1997/11/15 18:05:05  curt
+ * minor tweaks ...
+ *
+ * Revision 1.2  1997/11/14 00:29:13  curt
+ * 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
+ * renderer.  Precalculate normals for each node as average of the normals
+ * of each containing polygon so Garoude shading is now supportable.
+ *
  * Revision 1.1  1997/10/29 23:05:15  curt
  * Initial revision.
  *