1998-10-19 19:33:31 +00:00
|
|
|
// tri2obj.cxx -- read in a .ele/.node file pair generated by the triangle
|
|
|
|
// program and output a simple Wavefront .obj file.
|
|
|
|
//
|
|
|
|
// Written by Curtis Olson, started October 1997.
|
|
|
|
//
|
|
|
|
// Copyright (C) 1997 Curtis L. Olson - curt@infoplane.com
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
// the Free Software Foundation; either version 2 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
//
|
|
|
|
// $Id$
|
|
|
|
// (Log is kept at end of this file)
|
1997-10-29 23:05:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
1998-10-19 19:33:31 +00:00
|
|
|
#include <stdlib.h> // for atoi()
|
1997-10-29 23:05:14 +00:00
|
|
|
#include <string.h>
|
1998-10-19 19:33:31 +00:00
|
|
|
#include <sys/stat.h> // for stat()
|
|
|
|
#include <unistd.h> // for stat()
|
1997-10-29 23:05:14 +00:00
|
|
|
|
1998-07-08 14:54:52 +00:00
|
|
|
#include "tri2obj.hxx"
|
1997-10-29 23:05:14 +00:00
|
|
|
|
1998-01-27 18:36:53 +00:00
|
|
|
#include <Include/fg_constants.h>
|
1998-04-14 02:25:54 +00:00
|
|
|
#include <Bucket/bucketutils.h>
|
1998-04-08 23:19:34 +00:00
|
|
|
|
1998-10-18 01:17:09 +00:00
|
|
|
#include <Math/fg_geodesy.hxx>
|
1998-04-18 04:01:01 +00:00
|
|
|
#include <Math/mat3.h>
|
1998-07-08 14:54:52 +00:00
|
|
|
#include <Math/polar3d.hxx>
|
1997-11-14 00:29:13 +00:00
|
|
|
|
1997-10-29 23:05:14 +00:00
|
|
|
|
|
|
|
int nodecount, tricount;
|
1998-01-17 01:25:39 +00:00
|
|
|
int normalcount = 0;
|
1998-10-18 01:17:09 +00:00
|
|
|
static Point3D nodes[MAX_NODES];
|
1998-03-03 16:00:52 +00:00
|
|
|
static int tris[MAX_TRIS][3];
|
1997-10-29 23:05:14 +00:00
|
|
|
|
1998-03-03 16:00:52 +00:00
|
|
|
static double normals[MAX_NODES][3];
|
1998-01-17 01:25:39 +00:00
|
|
|
|
1998-07-04 00:55:39 +00:00
|
|
|
fgBUCKET my_index;
|
|
|
|
fgBUCKET ne_index, nw_index, sw_index, se_index;
|
|
|
|
fgBUCKET north_index, south_index, east_index, west_index;
|
1997-10-29 23:05:14 +00:00
|
|
|
|
1997-11-14 00:29:13 +00:00
|
|
|
|
1998-10-19 19:33:31 +00:00
|
|
|
// given three points defining a triangle, calculate the normal
|
1998-10-18 01:17:09 +00:00
|
|
|
void calc_normal(Point3D p1, Point3D p2,
|
|
|
|
Point3D p3, double normal[3])
|
1997-11-14 00:29:13 +00:00
|
|
|
{
|
|
|
|
double v1[3], v2[3];
|
1998-01-31 00:41:19 +00:00
|
|
|
double temp;
|
1997-11-14 00:29:13 +00:00
|
|
|
|
1998-10-18 01:17:09 +00:00
|
|
|
v1[0] = p2.x() - p1.x(); v1[1] = p2.y() - p1.y(); v1[2] = p2.z() - p1.z();
|
|
|
|
v2[0] = p3.x() - p1.x(); v2[1] = p3.y() - p1.y(); v2[2] = p3.z() - p1.z();
|
1997-11-14 00:29:13 +00:00
|
|
|
|
|
|
|
MAT3cross_product(normal, v1, v2);
|
1998-01-31 00:41:19 +00:00
|
|
|
MAT3_NORMALIZE_VEC(normal, temp);
|
1997-11-14 00:29:13 +00:00
|
|
|
|
1998-10-19 19:33:31 +00:00
|
|
|
// printf(" Normal = %.2f %.2f %.2f\n", normal[0], normal[1], normal[2]);
|
1997-11-14 00:29:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-10-19 19:33:31 +00:00
|
|
|
// return the index of all triangles containing the specified node
|
1998-01-12 02:42:00 +00:00
|
|
|
void find_tris(int n, int *t1, int *t2, int *t3, int *t4, int *t5) {
|
1997-11-14 00:29:13 +00:00
|
|
|
int i;
|
|
|
|
|
1998-01-12 02:42:00 +00:00
|
|
|
*t1 = *t2 = *t3 = *t4 = *t5 = 0;
|
1997-11-14 00:29:13 +00:00
|
|
|
|
|
|
|
i = 1;
|
|
|
|
while ( i <= tricount ) {
|
|
|
|
if ( (n == tris[i][0]) || (n == tris[i][1]) || (n == tris[i][2]) ) {
|
|
|
|
if ( *t1 == 0 ) {
|
|
|
|
*t1 = i;
|
|
|
|
} else if ( *t2 == 0 ) {
|
|
|
|
*t2 = i;
|
1998-01-12 02:42:00 +00:00
|
|
|
} else if ( *t3 == 0 ) {
|
1997-11-14 00:29:13 +00:00
|
|
|
*t3 = i;
|
1998-01-12 02:42:00 +00:00
|
|
|
} else if ( *t4 == 0 ) {
|
|
|
|
*t4 = i;
|
|
|
|
} else {
|
|
|
|
*t5 = i;
|
1997-11-14 00:29:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-10-19 19:33:31 +00:00
|
|
|
// return the file base name ( foo/bar/file.ext = file.ext )
|
1998-01-17 01:25:39 +00:00
|
|
|
void extract_file(char *in, char *base) {
|
|
|
|
int len, i;
|
|
|
|
|
|
|
|
len = strlen(in);
|
|
|
|
|
|
|
|
i = len - 1;
|
|
|
|
while ( (i >= 0) && (in[i] != '/') ) {
|
|
|
|
i--;
|
|
|
|
}
|
|
|
|
|
|
|
|
in += (i + 1);
|
|
|
|
strcpy(base, in);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-10-19 19:33:31 +00:00
|
|
|
// return the file path name ( foo/bar/file.ext = foo/bar )
|
1998-01-17 01:25:39 +00:00
|
|
|
void extract_path(char *in, char *base) {
|
|
|
|
int len, i;
|
|
|
|
|
|
|
|
len = strlen(in);
|
|
|
|
strcpy(base, in);
|
|
|
|
|
|
|
|
i = len - 1;
|
|
|
|
while ( (i >= 0) && (in[i] != '/') ) {
|
|
|
|
i--;
|
|
|
|
}
|
|
|
|
|
|
|
|
base[i] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-10-19 19:33:31 +00:00
|
|
|
// check if a file exists
|
1998-01-17 01:25:39 +00:00
|
|
|
int file_exists(char *file) {
|
|
|
|
struct stat stat_buf;
|
|
|
|
int result;
|
|
|
|
|
|
|
|
printf("checking %s ... ", file);
|
|
|
|
|
|
|
|
result = stat(file, &stat_buf);
|
|
|
|
|
|
|
|
if ( result != 0 ) {
|
1998-10-19 19:33:31 +00:00
|
|
|
// stat failed, no file
|
1998-01-17 01:25:39 +00:00
|
|
|
printf("not found.\n");
|
|
|
|
return(0);
|
|
|
|
} else {
|
1998-10-19 19:33:31 +00:00
|
|
|
// stat succeeded, file exists
|
1998-01-17 01:25:39 +00:00
|
|
|
printf("exists.\n");
|
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-10-19 19:33:31 +00:00
|
|
|
// check to see if a shared object exists
|
1998-01-17 01:25:39 +00:00
|
|
|
int shared_object_exists(char *basepath, char *ext, char *file) {
|
|
|
|
char scene_path[256];
|
|
|
|
long int index;
|
|
|
|
|
|
|
|
if ( strcmp(ext, ".sw") == 0 ) {
|
1998-01-27 18:36:53 +00:00
|
|
|
fgBucketGenBasePath(&my_index, scene_path);
|
|
|
|
index = fgBucketGenIndex(&my_index);
|
1998-01-17 01:25:39 +00:00
|
|
|
sprintf(file, "%s/%s/%ld.1.sw", basepath, scene_path, index);
|
|
|
|
if ( file_exists(file) ) {
|
|
|
|
return(1);
|
|
|
|
}
|
1998-01-27 18:36:53 +00:00
|
|
|
fgBucketGenBasePath(&west_index, scene_path);
|
|
|
|
index = fgBucketGenIndex(&west_index);
|
1998-01-17 01:25:39 +00:00
|
|
|
sprintf(file, "%s/%s/%ld.1.se", basepath, scene_path, index);
|
|
|
|
if ( file_exists(file) ) {
|
|
|
|
return(1);
|
|
|
|
}
|
1998-01-27 18:36:53 +00:00
|
|
|
fgBucketGenBasePath(&sw_index, scene_path);
|
|
|
|
index = fgBucketGenIndex(&sw_index);
|
1998-01-17 01:25:39 +00:00
|
|
|
sprintf(file, "%s/%s/%ld.1.ne", basepath, scene_path, index);
|
|
|
|
if ( file_exists(file) ) {
|
|
|
|
return(1);
|
|
|
|
}
|
1998-01-27 18:36:53 +00:00
|
|
|
fgBucketGenBasePath(&south_index, scene_path);
|
|
|
|
index = fgBucketGenIndex(&south_index);
|
1998-01-17 01:25:39 +00:00
|
|
|
sprintf(file, "%s/%s/%ld.1.nw", basepath, scene_path, index);
|
|
|
|
if ( file_exists(file) ) {
|
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( strcmp(ext, ".se") == 0 ) {
|
1998-01-27 18:36:53 +00:00
|
|
|
fgBucketGenBasePath(&my_index, scene_path);
|
|
|
|
index = fgBucketGenIndex(&my_index);
|
1998-01-17 01:25:39 +00:00
|
|
|
sprintf(file, "%s/%s/%ld.1.se", basepath, scene_path, index);
|
|
|
|
if ( file_exists(file) ) {
|
|
|
|
return(1);
|
|
|
|
}
|
1998-01-27 18:36:53 +00:00
|
|
|
fgBucketGenBasePath(&east_index, scene_path);
|
|
|
|
index = fgBucketGenIndex(&east_index);
|
1998-01-17 01:25:39 +00:00
|
|
|
sprintf(file, "%s/%s/%ld.1.sw", basepath, scene_path, index);
|
|
|
|
if ( file_exists(file) ) {
|
|
|
|
return(1);
|
|
|
|
}
|
1998-01-27 18:36:53 +00:00
|
|
|
fgBucketGenBasePath(&se_index, scene_path);
|
|
|
|
index = fgBucketGenIndex(&se_index);
|
1998-01-17 01:25:39 +00:00
|
|
|
sprintf(file, "%s/%s/%ld.1.nw", basepath, scene_path, index);
|
|
|
|
if ( file_exists(file) ) {
|
|
|
|
return(1);
|
|
|
|
}
|
1998-01-27 18:36:53 +00:00
|
|
|
fgBucketGenBasePath(&south_index, scene_path);
|
|
|
|
index = fgBucketGenIndex(&south_index);
|
1998-01-17 01:25:39 +00:00
|
|
|
sprintf(file, "%s/%s/%ld.1.ne", basepath, scene_path, index);
|
|
|
|
if ( file_exists(file) ) {
|
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( strcmp(ext, ".ne") == 0 ) {
|
1998-01-27 18:36:53 +00:00
|
|
|
fgBucketGenBasePath(&my_index, scene_path);
|
|
|
|
index = fgBucketGenIndex(&my_index);
|
1998-01-17 01:25:39 +00:00
|
|
|
sprintf(file, "%s/%s/%ld.1.ne", basepath, scene_path, index);
|
|
|
|
if ( file_exists(file) ) {
|
|
|
|
return(1);
|
|
|
|
}
|
1998-01-27 18:36:53 +00:00
|
|
|
fgBucketGenBasePath(&east_index, scene_path);
|
|
|
|
index = fgBucketGenIndex(&east_index);
|
1998-01-17 01:25:39 +00:00
|
|
|
sprintf(file, "%s/%s/%ld.1.nw", basepath, scene_path, index);
|
|
|
|
if ( file_exists(file) ) {
|
|
|
|
return(1);
|
|
|
|
}
|
1998-01-27 18:36:53 +00:00
|
|
|
fgBucketGenBasePath(&ne_index, scene_path);
|
|
|
|
index = fgBucketGenIndex(&ne_index);
|
1998-01-17 01:25:39 +00:00
|
|
|
sprintf(file, "%s/%s/%ld.1.sw", basepath, scene_path, index);
|
|
|
|
if ( file_exists(file) ) {
|
|
|
|
return(1);
|
|
|
|
}
|
1998-01-27 18:36:53 +00:00
|
|
|
fgBucketGenBasePath(&north_index, scene_path);
|
|
|
|
index = fgBucketGenIndex(&north_index);
|
1998-01-17 01:25:39 +00:00
|
|
|
sprintf(file, "%s/%s/%ld.1.se", basepath, scene_path, index);
|
|
|
|
if ( file_exists(file) ) {
|
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( strcmp(ext, ".nw") == 0 ) {
|
1998-01-27 18:36:53 +00:00
|
|
|
fgBucketGenBasePath(&my_index, scene_path);
|
|
|
|
index = fgBucketGenIndex(&my_index);
|
1998-01-17 01:25:39 +00:00
|
|
|
sprintf(file, "%s/%s/%ld.1.nw", basepath, scene_path, index);
|
|
|
|
if ( file_exists(file) ) {
|
|
|
|
return(1);
|
|
|
|
}
|
1998-01-27 18:36:53 +00:00
|
|
|
fgBucketGenBasePath(&west_index, scene_path);
|
|
|
|
index = fgBucketGenIndex(&west_index);
|
1998-01-17 01:25:39 +00:00
|
|
|
sprintf(file, "%s/%s/%ld.1.ne", basepath, scene_path, index);
|
|
|
|
if ( file_exists(file) ) {
|
|
|
|
return(1);
|
|
|
|
}
|
1998-01-27 18:36:53 +00:00
|
|
|
fgBucketGenBasePath(&nw_index, scene_path);
|
|
|
|
index = fgBucketGenIndex(&nw_index);
|
1998-01-17 01:25:39 +00:00
|
|
|
sprintf(file, "%s/%s/%ld.1.se", basepath, scene_path, index);
|
|
|
|
if ( file_exists(file) ) {
|
|
|
|
return(1);
|
|
|
|
}
|
1998-01-27 18:36:53 +00:00
|
|
|
fgBucketGenBasePath(&north_index, scene_path);
|
|
|
|
index = fgBucketGenIndex(&north_index);
|
1998-01-17 01:25:39 +00:00
|
|
|
sprintf(file, "%s/%s/%ld.1.sw", basepath, scene_path, index);
|
|
|
|
if ( file_exists(file) ) {
|
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( strcmp(ext, ".south") == 0 ) {
|
1998-01-27 18:36:53 +00:00
|
|
|
fgBucketGenBasePath(&my_index, scene_path);
|
|
|
|
index = fgBucketGenIndex(&my_index);
|
1998-01-17 01:25:39 +00:00
|
|
|
sprintf(file, "%s/%s/%ld.1.south", basepath, scene_path, index);
|
|
|
|
if ( file_exists(file) ) {
|
|
|
|
return(1);
|
|
|
|
}
|
1998-01-27 18:36:53 +00:00
|
|
|
fgBucketGenBasePath(&south_index, scene_path);
|
|
|
|
index = fgBucketGenIndex(&south_index);
|
1998-01-17 01:25:39 +00:00
|
|
|
sprintf(file, "%s/%s/%ld.1.north", basepath, scene_path, index);
|
|
|
|
if ( file_exists(file) ) {
|
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( strcmp(ext, ".north") == 0 ) {
|
1998-01-27 18:36:53 +00:00
|
|
|
fgBucketGenBasePath(&my_index, scene_path);
|
|
|
|
index = fgBucketGenIndex(&my_index);
|
1998-01-17 01:25:39 +00:00
|
|
|
sprintf(file, "%s/%s/%ld.1.north", basepath, scene_path, index);
|
|
|
|
if ( file_exists(file) ) {
|
|
|
|
return(1);
|
|
|
|
}
|
1998-01-27 18:36:53 +00:00
|
|
|
fgBucketGenBasePath(&north_index, scene_path);
|
|
|
|
index = fgBucketGenIndex(&north_index);
|
1998-01-17 01:25:39 +00:00
|
|
|
sprintf(file, "%s/%s/%ld.1.south", basepath, scene_path, index);
|
|
|
|
if ( file_exists(file) ) {
|
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( strcmp(ext, ".west") == 0 ) {
|
1998-01-27 18:36:53 +00:00
|
|
|
fgBucketGenBasePath(&my_index, scene_path);
|
|
|
|
index = fgBucketGenIndex(&my_index);
|
1998-01-17 01:25:39 +00:00
|
|
|
sprintf(file, "%s/%s/%ld.1.west", basepath, scene_path, index);
|
|
|
|
if ( file_exists(file) ) {
|
|
|
|
return(1);
|
|
|
|
}
|
1998-01-27 18:36:53 +00:00
|
|
|
fgBucketGenBasePath(&west_index, scene_path);
|
|
|
|
index = fgBucketGenIndex(&west_index);
|
1998-01-17 01:25:39 +00:00
|
|
|
sprintf(file, "%s/%s/%ld.1.east", basepath, scene_path, index);
|
|
|
|
if ( file_exists(file) ) {
|
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( strcmp(ext, ".east") == 0 ) {
|
1998-01-27 18:36:53 +00:00
|
|
|
fgBucketGenBasePath(&my_index, scene_path);
|
|
|
|
index = fgBucketGenIndex(&my_index);
|
1998-01-17 01:25:39 +00:00
|
|
|
sprintf(file, "%s/%s/%ld.1.east", basepath, scene_path, index);
|
|
|
|
if ( file_exists(file) ) {
|
|
|
|
return(1);
|
|
|
|
}
|
1998-01-27 18:36:53 +00:00
|
|
|
fgBucketGenBasePath(&east_index, scene_path);
|
|
|
|
index = fgBucketGenIndex(&east_index);
|
1998-01-17 01:25:39 +00:00
|
|
|
sprintf(file, "%s/%s/%ld.1.west", basepath, scene_path, index);
|
|
|
|
if ( file_exists(file) ) {
|
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( strcmp(ext, ".body") == 0 ) {
|
1998-01-27 18:36:53 +00:00
|
|
|
fgBucketGenBasePath(&my_index, scene_path);
|
|
|
|
index = fgBucketGenIndex(&my_index);
|
1998-01-17 01:25:39 +00:00
|
|
|
sprintf(file, "%s/%s/%ld.1.body", basepath, scene_path, index);
|
|
|
|
if ( file_exists(file) ) {
|
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-10-19 19:33:31 +00:00
|
|
|
// given a file pointer, read all the vn (normals from it)
|
1998-01-17 01:25:39 +00:00
|
|
|
void read_normals(FILE *fp) {
|
|
|
|
char line[256];
|
|
|
|
|
|
|
|
while ( fgets(line, 250, fp) != NULL ) {
|
|
|
|
if ( strncmp(line, "vn ", 3) == 0 ) {
|
1998-01-31 00:41:19 +00:00
|
|
|
sscanf( line, "vn %lf %lf %lf\n",
|
|
|
|
&normals[normalcount][0],
|
|
|
|
&normals[normalcount][1],
|
|
|
|
&normals[normalcount][2] );
|
1998-01-17 01:25:39 +00:00
|
|
|
/*
|
|
|
|
printf("read_normals(%d) %.2f %.2f %.2f %s", normalcount,
|
|
|
|
normals[normalcount][0], normals[normalcount][1],
|
|
|
|
normals[normalcount][2], line);
|
|
|
|
*/
|
|
|
|
normalcount++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-10-19 19:33:31 +00:00
|
|
|
// my custom file opening routine ... don't open if a shared edge or
|
|
|
|
// vertex alread exists
|
1998-01-17 01:25:39 +00:00
|
|
|
FILE *my_open(char *basename, char *basepath, char *ext) {
|
|
|
|
FILE *fp;
|
|
|
|
char filename[256];
|
|
|
|
|
1998-10-19 19:33:31 +00:00
|
|
|
// check if a shared object already exists
|
1998-01-17 01:25:39 +00:00
|
|
|
if ( shared_object_exists(basepath, ext, filename) ) {
|
1998-10-19 19:33:31 +00:00
|
|
|
// not an actual file open error, but we've already got the
|
|
|
|
// shared edge, so we don't want to create another one
|
1998-01-17 01:25:39 +00:00
|
|
|
fp = fopen(filename, "r");
|
|
|
|
printf("Opening %s\n", filename);
|
|
|
|
return(fp);
|
|
|
|
} else {
|
1998-10-19 19:33:31 +00:00
|
|
|
// open the file
|
1998-01-17 01:25:39 +00:00
|
|
|
printf("not opening\n");
|
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-10-19 19:33:31 +00:00
|
|
|
// Initialize a new mesh structure
|
1998-01-17 01:25:39 +00:00
|
|
|
void triload(char *basename, char *basepath) {
|
1997-10-29 23:05:14 +00:00
|
|
|
char nodename[256], elename[256];
|
1997-11-14 00:29:13 +00:00
|
|
|
double n[3];
|
1998-10-18 01:17:09 +00:00
|
|
|
Point3D p;
|
1998-01-17 01:25:39 +00:00
|
|
|
FILE *ne, *nw, *se, *sw, *north, *south, *east, *west;
|
1997-10-29 23:05:14 +00:00
|
|
|
FILE *node, *ele;
|
|
|
|
int dim, junk1, junk2;
|
|
|
|
int i;
|
|
|
|
|
1998-01-17 01:25:39 +00:00
|
|
|
ne = my_open(basename, basepath, ".ne");
|
|
|
|
read_normals(ne);
|
|
|
|
fclose(ne);
|
|
|
|
|
|
|
|
nw = my_open(basename, basepath, ".nw");
|
|
|
|
read_normals(nw);
|
|
|
|
fclose(nw);
|
|
|
|
|
|
|
|
se = my_open(basename, basepath, ".se");
|
|
|
|
read_normals(se);
|
|
|
|
fclose(se);
|
|
|
|
|
|
|
|
sw = my_open(basename, basepath, ".sw");
|
|
|
|
read_normals(sw);
|
|
|
|
fclose(sw);
|
|
|
|
|
|
|
|
north = my_open(basename, basepath, ".north");
|
|
|
|
read_normals(north);
|
|
|
|
fclose(north);
|
|
|
|
|
|
|
|
south = my_open(basename, basepath, ".south");
|
|
|
|
read_normals(south);
|
|
|
|
fclose(south);
|
|
|
|
|
|
|
|
east = my_open(basename, basepath, ".east");
|
|
|
|
read_normals(east);
|
|
|
|
fclose(east);
|
|
|
|
|
|
|
|
west = my_open(basename, basepath, ".west");
|
|
|
|
read_normals(west);
|
|
|
|
fclose(west);
|
|
|
|
|
1997-10-29 23:05:14 +00:00
|
|
|
strcpy(nodename, basename);
|
|
|
|
strcat(nodename, ".node");
|
|
|
|
strcpy(elename, basename);
|
|
|
|
strcat(elename, ".ele");
|
|
|
|
|
|
|
|
printf("Loading node file: %s ...\n", nodename);
|
|
|
|
if ( (node = fopen(nodename, "r")) == NULL ) {
|
|
|
|
printf("Cannot open file '%s'\n", nodename);
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
fscanf(node, "%d %d %d %d", &nodecount, &dim, &junk1, &junk2);
|
|
|
|
|
|
|
|
if ( nodecount > MAX_NODES - 1 ) {
|
|
|
|
printf("Error, too many nodes, need to increase array size\n");
|
|
|
|
exit(-1);
|
|
|
|
} else {
|
|
|
|
printf(" Expecting %d nodes\n", nodecount);
|
|
|
|
}
|
|
|
|
|
|
|
|
for ( i = 1; i <= nodecount; i++ ) {
|
|
|
|
fscanf(node, "%d %lf %lf %lf %d\n", &junk1,
|
1997-11-14 00:29:13 +00:00
|
|
|
&n[0], &n[1], &n[2], &junk2);
|
1998-10-19 19:33:31 +00:00
|
|
|
// printf("%d %.2f %.2f %.2f\n", junk1, n[0], n[1], n[2]);
|
1998-10-20 15:52:46 +00:00
|
|
|
p = Point3D( n[0] * ARCSEC_TO_RAD,
|
|
|
|
n[1] * ARCSEC_TO_RAD,
|
|
|
|
n[2] );
|
1998-10-18 01:17:09 +00:00
|
|
|
nodes[i] = fgGeodToCart(p);
|
1998-10-19 19:33:31 +00:00
|
|
|
// printf("%d %.2f %.2f %.2f\n",
|
|
|
|
// junk1, nodes[i].x, nodes[i].y, nodes[i].z);
|
1997-10-29 23:05:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fclose(node);
|
|
|
|
|
|
|
|
printf("Loading element file: %s ...\n", elename);
|
|
|
|
if ( (ele = fopen(elename, "r")) == NULL ) {
|
|
|
|
printf("Cannot open file '%s'\n", elename);
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
fscanf(ele, "%d %d %d", &tricount, &junk1, &junk2);
|
|
|
|
|
|
|
|
if ( tricount > MAX_TRIS - 1 ) {
|
|
|
|
printf("Error, too many elements, need to increase array size\n");
|
|
|
|
exit(-1);
|
|
|
|
} else {
|
|
|
|
printf(" Expecting %d elements\n", tricount);
|
|
|
|
}
|
|
|
|
|
|
|
|
for ( i = 1; i <= tricount; i++ ) {
|
|
|
|
fscanf(ele, "%d %d %d %d\n", &junk1,
|
|
|
|
&tris[i][0], &tris[i][1], &tris[i][2]);
|
1998-10-19 19:33:31 +00:00
|
|
|
// printf("%d %d %d %d\n", junk1, tris[i][0], tris[i][1], tris[i][2]);*/
|
1997-10-29 23:05:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fclose(ele);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-10-19 19:33:31 +00:00
|
|
|
// dump in WaveFront .obj format
|
1997-10-29 23:05:14 +00:00
|
|
|
void dump_obj(char *basename) {
|
|
|
|
char objname[256];
|
1998-01-12 02:42:00 +00:00
|
|
|
double n1[3], n2[3], n3[3], n4[3], n5[3], norm[3], temp;
|
1997-10-29 23:05:14 +00:00
|
|
|
FILE *obj;
|
1998-01-12 02:42:00 +00:00
|
|
|
int i, t1, t2, t3, t4, t5, count;
|
1998-10-18 01:17:09 +00:00
|
|
|
double x, y, z;
|
1997-10-29 23:05:14 +00:00
|
|
|
|
|
|
|
strcpy(objname, basename);
|
|
|
|
strcat(objname, ".obj");
|
|
|
|
|
|
|
|
printf("Dumping to file: %s ...\n", objname);
|
|
|
|
|
|
|
|
obj = fopen(objname, "w");
|
|
|
|
|
1998-10-19 19:33:31 +00:00
|
|
|
// dump vertices
|
1997-11-15 18:05:05 +00:00
|
|
|
printf(" writing vertices\n");
|
1997-10-29 23:05:14 +00:00
|
|
|
for ( i = 1; i <= nodecount; i++ ) {
|
1998-10-18 01:17:09 +00:00
|
|
|
x = nodes[i].x();
|
|
|
|
y = nodes[i].y();
|
|
|
|
z = nodes[i].z();
|
|
|
|
fprintf(obj, "v %.6f %.6f %.6f\n", x, y, z);
|
1997-11-14 00:29:13 +00:00
|
|
|
}
|
|
|
|
|
1997-11-15 18:05:05 +00:00
|
|
|
printf(" calculating and writing normals\n");
|
1998-01-17 01:25:39 +00:00
|
|
|
printf(" First %d normals taken from shared files.\n", normalcount);
|
|
|
|
|
1998-10-19 19:33:31 +00:00
|
|
|
// calculate and generate normals
|
1997-11-14 00:29:13 +00:00
|
|
|
for ( i = 1; i <= nodecount; i++ ) {
|
|
|
|
|
1998-01-17 01:25:39 +00:00
|
|
|
if ( i <= normalcount ) {
|
1998-10-19 19:33:31 +00:00
|
|
|
// use precalculated (shared) normal
|
1998-01-17 01:25:39 +00:00
|
|
|
norm[0] = normals[i-1][0];
|
|
|
|
norm[1] = normals[i-1][1];
|
|
|
|
norm[2] = normals[i-1][2];
|
|
|
|
} else {
|
1998-10-19 19:33:31 +00:00
|
|
|
// printf("Finding normal\n");
|
1998-01-17 01:25:39 +00:00
|
|
|
|
|
|
|
find_tris(i, &t1, &t2, &t3, &t4, &t5);
|
|
|
|
|
|
|
|
n1[0] = n1[1] = n1[2] = 0.0;
|
|
|
|
n2[0] = n2[1] = n2[2] = 0.0;
|
|
|
|
n3[0] = n3[1] = n3[2] = 0.0;
|
|
|
|
n4[0] = n4[1] = n4[2] = 0.0;
|
|
|
|
n5[0] = n5[1] = n5[2] = 0.0;
|
|
|
|
|
|
|
|
count = 1;
|
|
|
|
calc_normal(nodes[tris[t1][0]], nodes[tris[t1][1]],
|
|
|
|
nodes[tris[t1][2]], n1);
|
|
|
|
|
|
|
|
if ( t2 > 0 ) {
|
|
|
|
calc_normal(nodes[tris[t2][0]], nodes[tris[t2][1]],
|
|
|
|
nodes[tris[t2][2]], n2);
|
|
|
|
count = 2;
|
|
|
|
}
|
1997-11-14 00:29:13 +00:00
|
|
|
|
1998-01-17 01:25:39 +00:00
|
|
|
if ( t3 > 0 ) {
|
|
|
|
calc_normal(nodes[tris[t3][0]], nodes[tris[t3][1]],
|
|
|
|
nodes[tris[t3][2]], n3);
|
|
|
|
count = 3;
|
|
|
|
}
|
1997-11-14 00:29:13 +00:00
|
|
|
|
1998-01-17 01:25:39 +00:00
|
|
|
if ( t4 > 0 ) {
|
|
|
|
calc_normal(nodes[tris[t4][0]], nodes[tris[t4][1]],
|
|
|
|
nodes[tris[t4][2]], n4);
|
|
|
|
count = 4;
|
|
|
|
}
|
1998-01-12 02:42:00 +00:00
|
|
|
|
1998-01-17 01:25:39 +00:00
|
|
|
if ( t5 > 0 ) {
|
|
|
|
calc_normal(nodes[tris[t5][0]], nodes[tris[t5][1]],
|
|
|
|
nodes[tris[t5][2]], n5);
|
|
|
|
count = 5;
|
|
|
|
}
|
1998-01-12 02:42:00 +00:00
|
|
|
|
1998-10-19 19:33:31 +00:00
|
|
|
// printf(" norm[2] = %.2f %.2f %.2f\n", n1[2], n2[2], n3[2]);
|
1997-12-08 19:17:50 +00:00
|
|
|
|
1998-01-17 01:25:39 +00:00
|
|
|
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[2] = ( n1[2] + n2[2] + n3[2] + n4[2] + n5[2] ) / (double)count;
|
1997-12-02 13:13:32 +00:00
|
|
|
|
1998-10-19 19:33:31 +00:00
|
|
|
// printf(" count = %d\n", count);
|
|
|
|
// printf(" Ave. normal = %.4f %.4f %.4f\n",
|
|
|
|
// norm[0], norm[1], norm[2]);*/
|
1998-01-17 01:25:39 +00:00
|
|
|
MAT3_NORMALIZE_VEC(norm, temp);
|
1998-10-19 19:33:31 +00:00
|
|
|
// printf(" Normalized ave. normal = %.4f %.4f %.4f\n",
|
|
|
|
// norm[0], norm[1], norm[2]);
|
1998-01-17 01:25:39 +00:00
|
|
|
}
|
1998-10-19 19:33:31 +00:00
|
|
|
// printf("%d vn %.4f %.4f %.4f\n", i, norm[0], norm[1], norm[2]);
|
1997-12-02 13:13:32 +00:00
|
|
|
fprintf(obj, "vn %.4f %.4f %.4f\n", norm[0], norm[1], norm[2]);
|
1997-10-29 23:05:14 +00:00
|
|
|
}
|
|
|
|
|
1998-10-19 19:33:31 +00:00
|
|
|
// dump faces
|
1997-11-15 18:05:05 +00:00
|
|
|
printf(" writing faces\n");
|
1997-10-29 23:05:14 +00:00
|
|
|
for ( i = 1; i <= tricount; i++ ) {
|
1998-01-09 23:03:05 +00:00
|
|
|
fprintf(obj, "f %d %d %d\n", tris[i][0], tris[i][1], tris[i][2]);
|
1997-10-29 23:05:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fclose(obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
1998-01-17 01:25:39 +00:00
|
|
|
char basename[256], basepath[256], temp[256];
|
|
|
|
long int tmp_index;
|
|
|
|
int len;
|
1997-10-29 23:05:14 +00:00
|
|
|
|
|
|
|
strcpy(basename, argv[1]);
|
|
|
|
|
1998-10-19 19:33:31 +00:00
|
|
|
// find the base path of the file
|
1998-01-17 01:25:39 +00:00
|
|
|
extract_path(basename, basepath);
|
|
|
|
extract_path(basepath, basepath);
|
|
|
|
extract_path(basepath, basepath);
|
|
|
|
printf("%s\n", basepath);
|
|
|
|
|
1998-10-19 19:33:31 +00:00
|
|
|
// find the index of the current file
|
1998-01-17 01:25:39 +00:00
|
|
|
extract_file(basename, temp);
|
|
|
|
len = strlen(temp);
|
|
|
|
if ( len >= 2 ) {
|
|
|
|
temp[len-2] = '\0';
|
|
|
|
}
|
|
|
|
tmp_index = atoi(temp);
|
|
|
|
printf("%ld\n", tmp_index);
|
1998-01-27 18:36:53 +00:00
|
|
|
fgBucketParseIndex(tmp_index, &my_index);
|
1998-01-17 01:25:39 +00:00
|
|
|
|
|
|
|
printf("bucket = %d %d %d %d\n",
|
|
|
|
my_index.lon, my_index.lat, my_index.x, my_index.y);
|
1998-10-19 19:33:31 +00:00
|
|
|
// generate the indexes of the neighbors
|
1998-01-27 18:36:53 +00:00
|
|
|
fgBucketOffset(&my_index, &ne_index, 1, 1);
|
|
|
|
fgBucketOffset(&my_index, &nw_index, -1, 1);
|
|
|
|
fgBucketOffset(&my_index, &se_index, 1, -1);
|
|
|
|
fgBucketOffset(&my_index, &sw_index, -1, -1);
|
1998-01-17 01:25:39 +00:00
|
|
|
|
1998-01-27 18:36:53 +00:00
|
|
|
fgBucketOffset(&my_index, &north_index, 0, 1);
|
|
|
|
fgBucketOffset(&my_index, &south_index, 0, -1);
|
|
|
|
fgBucketOffset(&my_index, &east_index, 1, 0);
|
|
|
|
fgBucketOffset(&my_index, &west_index, -1, 0);
|
1998-01-17 01:25:39 +00:00
|
|
|
|
1998-10-19 19:33:31 +00:00
|
|
|
// load the input data files
|
1998-01-17 01:25:39 +00:00
|
|
|
triload(basename, basepath);
|
1997-10-29 23:05:14 +00:00
|
|
|
|
1998-10-19 19:33:31 +00:00
|
|
|
// dump in WaveFront .obj format
|
1997-10-29 23:05:14 +00:00
|
|
|
dump_obj(basename);
|
|
|
|
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-10-19 19:33:31 +00:00
|
|
|
// $Log$
|
1998-10-20 15:52:46 +00:00
|
|
|
// Revision 1.4 1998/10/20 15:52:46 curt
|
|
|
|
// Fixed a units conversion bug introduced when converting to Point3D class.
|
|
|
|
//
|
1998-10-19 19:33:31 +00:00
|
|
|
// Revision 1.3 1998/10/19 19:33:31 curt
|
|
|
|
// C++-ification.
|
|
|
|
//
|
|
|
|
// Revision 1.2 1998/10/18 01:17:29 curt
|
|
|
|
// Point3D tweaks.
|
|
|
|
//
|
|
|
|
// Revision 1.1 1998/07/08 14:54:53 curt
|
|
|
|
// renamed *.[ch] to *.[ch]xx
|
|
|
|
//
|
|
|
|
// Revision 1.17 1998/07/04 00:56:40 curt
|
|
|
|
// typedef'd struct fgBUCKET.
|
|
|
|
//
|
|
|
|
// Revision 1.16 1998/05/23 15:20:41 curt
|
|
|
|
// Output more digits after the decimal place.
|
|
|
|
//
|
|
|
|
// Revision 1.15 1998/05/02 01:54:39 curt
|
|
|
|
// Converting to polar3d.h routines.
|
|
|
|
//
|
|
|
|
// Revision 1.14 1998/04/18 04:01:32 curt
|
|
|
|
// Now use libMath rather than having local copies of math routines.
|
|
|
|
//
|
|
|
|
// Revision 1.13 1998/04/14 02:26:11 curt
|
|
|
|
// Code reorganizations. Added a Lib/ directory for more general libraries.
|
|
|
|
//
|
|
|
|
// Revision 1.12 1998/04/08 23:22:18 curt
|
|
|
|
// Adopted Gnu automake/autoconf system.
|
|
|
|
//
|
|
|
|
// Revision 1.11 1998/03/03 16:01:00 curt
|
|
|
|
// More c++ compile tweaks.
|
|
|
|
//
|
|
|
|
// Revision 1.10 1998/01/31 00:41:27 curt
|
|
|
|
// Made a few changes converting floats to doubles.
|
|
|
|
//
|
|
|
|
// Revision 1.9 1998/01/27 18:37:04 curt
|
|
|
|
// Lots of updates to get back in sync with changes made over in .../Src/
|
|
|
|
//
|
|
|
|
// Revision 1.8 1998/01/17 01:25:39 curt
|
|
|
|
// Added support for shared normals.
|
|
|
|
//
|
|
|
|
// Revision 1.7 1998/01/12 02:42:00 curt
|
|
|
|
// Average up to five triangles per vertex instead of three.
|
|
|
|
//
|
|
|
|
// Revision 1.6 1998/01/09 23:03:15 curt
|
|
|
|
// Restructured to split 1deg x 1deg dem's into 64 subsections.
|
|
|
|
//
|
|
|
|
// Revision 1.5 1997/12/08 19:17:50 curt
|
|
|
|
// Fixed a type in the normal generation code.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
|