home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
DOS/V Power Report 1997 March
/
VPR9703A.ISO
/
VPR_DATA
/
DOGA
/
SOURCES
/
POLYEDIT.LZH
/
MODEL
/
FRAME.C
< prev
next >
Wrap
C/C++ Source or Header
|
1996-07-31
|
8KB
|
377 lines
/*
* フレームファイル入力
*
* Copyright T.Kobayashi 1994.7.17
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <io.h>
#include <assert.h>
#include "lib.h"
#include "reader.h"
#include "poly.h"
#include "view.h"
#include "vector.h"
#define MAXOBJECT 64
static char objname[MAXOBJECT][F_NAME_LEN];
static Matrix objmat[MAXOBJECT];
static char *initialdir;
static char searchdir[F_NAME_LEN];
extern char ExecPath[];
static int SearchFile(char *path, char *file)
{
char *p;
if (access(file, 0) == 0) {
strcpy(path, file);
return TRUE;
}
strcpy(path, ExecPath);
strcat(path, file);
if (access(path, 0) == 0) {
return TRUE;
}
if (strlen(file) > 2) {
path[0] = file[0];
path[1] = file[1];
path[2] = '\\';
strcpy(path+3, file);
if (access(path, 0) == 0) {
return TRUE;
}
strcpy(path, ExecPath);
p = path + strlen(path);
p[0] = file[0];
p[1] = file[1];
p[2] = '\\';
strcpy(p+3, file);
if (access(path, 0) == 0) {
return TRUE;
}
strcpy(path, "parts\\");
p = path + strlen(path);
p[0] = file[0];
p[1] = file[1];
p[2] = '\\';
strcpy(p+3, file);
if (access(path, 0) == 0) {
return TRUE;
}
strcpy(path, ExecPath);
strcat(path, "parts\\");
p = path + strlen(path);
p[0] = file[0];
p[1] = file[1];
p[2] = '\\';
strcpy(p+3, file);
if (access(path, 0) == 0) {
return TRUE;
}
strcpy(path, "..\\");
strcat(path, "parts\\");
p = path + strlen(path);
p[0] = file[0];
p[1] = file[1];
p[2] = '\\';
strcpy(p+3, file);
if (access(path, 0) == 0) {
return TRUE;
}
strcpy(path, "..\\..\\");
strcat(path, "parts\\");
p = path + strlen(path);
p[0] = file[0];
p[1] = file[1];
p[2] = '\\';
strcpy(p+3, file);
if (access(path, 0) == 0) {
return TRUE;
}
strcpy(path, "..\\..\\..\\");
strcat(path, "parts\\");
p = path + strlen(path);
p[0] = file[0];
p[1] = file[1];
p[2] = '\\';
strcpy(p+3, file);
if (access(path, 0) == 0) {
return TRUE;
}
}
if (searchdir[0] != '\0') {
strcpy(path, searchdir);
p = path + strlen(path);
strcat(path, file);
if (access(path, 0) == 0) {
return TRUE;
}
p[0] = file[0];
p[1] = file[1];
p[2] = '\\';
strcpy(p+3, file);
if (access(path, 0) == 0) {
return TRUE;
}
strcpy(p, "parts\\");
p = path + strlen(path);
p[0] = file[0];
p[1] = file[1];
p[2] = '\\';
strcpy(p+3, file);
if (access(path, 0) == 0) {
return TRUE;
}
}
strcpy(path, file);
return FALSE;
}
int FrmReadFile( char *file, char *dir, int mode )
{
int i, j, objs, atrno, objno, type ;
int rdata[8];
int x,y,z;
int pvx=0, pvy=0, pvz=0;
int coe[3];
int reverseflag;
char atrname[64], frmname[64], *p, *q ;
char filename[F_NAME_LEN];
Matrix matit;
int imat[5][3], imatit[5][3] ;
if (dir != NULL) {
strcpy(searchdir, dir);
i = strlen(searchdir);
if (i > 0 && searchdir[i-1] != '\\') {
searchdir[i] = '\\';
searchdir[i+1] = '\0';
}
} else {
strcpy(searchdir, file);
if ((p = strrchr(searchdir, '\\')) == NULL) {
p = searchdir;
}
if ((q = strrchr(p, '/')) == NULL) {
q = p;
}
if (*q == '\\' || *q == '/') {
p[1] = '\0';
}
}
if (FrmReadOpen( file ) == FALSE) {
return FALSE;
}
if ((p = strrchr(file, '\\')) == NULL) {
p = file;
}
if ((q = strrchr(p, '/')) == NULL) {
q = p;
}
if (*q == '\\' || *q == '/') {
q++;
}
strcpy(frmname, q);
if ((p = strchr(frmname, '.')) != NULL) {
*p = '\0';
}
if (mode != FILE_APPEND) {
objno = ObjAppend( frmname );
} else {
objno = ObjSearchOrAppend( frmname );
}
strcpy( ObjData[objno].fname, file);
i = strlen(file);
if (i > 4 && file[i-4] == '.') {
strcpy( ObjData[objno].fname + i-4, ".suf");
}
objs = 0;
while ( FrmReadObj(objname[objs], objmat[objs])) {
strcat(objname[objs], ".suf");
if (objs < MAXOBJECT-1) {
objs++;
}
}
FrmReadClose();
for (j = 0; j < objs; j++) {
float delta, r;
SearchFile(filename, objname[j]);
if (ObjReadOpen(filename) == FALSE) {
continue;
}
MatCopy(matit, objmat[j]);
MatInv(matit);
MatTra(matit);
MatToInt( imat, objmat[j]);
MatToInt( imatit, matit );
delta = (float)0.0 ;
for( i = 0 ; i < 3 ; i ++ )
{
delta = delta
+ objmat[j][0+i][0] * objmat[j][(1+i)%3][1] * objmat[j][(2+i)%3][2]
- objmat[j][0+i][2] * objmat[j][(1+i)%3][1] * objmat[j][(2+i)%3][0] ;
}
reverseflag = (delta < 0.0);
while( ObjReadName( filename ) && !ReaderError())
{
CurrentPoly->obj = (short)objno ;
while( ObjReadPoly( atrname, &type ) && !ReaderError())
{
atrno = AttrSearchOrAppend( atrname );
if (atrno < 0) return -1;
CurrentPoly->type = (short)type ;
CurrentPoly->atr = (short)atrno ;
for( i = 0 ; ObjReadVertexInt( rdata ) && !ReaderError(); i++ )
{
x = rdata[0] * imat[0][0]
+ rdata[1] * imat[1][0]
+ rdata[2] * imat[2][0]
+ imat[4][0] ;
y = rdata[0] * imat[0][1]
+ rdata[1] * imat[1][1]
+ rdata[2] * imat[2][1]
+ imat[4][1] ;
z = rdata[0] * imat[0][2]
+ rdata[1] * imat[1][2]
+ rdata[2] * imat[2][2]
+ imat[4][2] ;
if (x > 0) {
x = imat[3][0] + ( ( x + 32767) >> 16 );
} else {
x = imat[3][0] - ( (-x + 32767) >> 16 );
}
if (y > 0) {
y = imat[3][1] + ( ( y + 32767) >> 16 );
} else {
y = imat[3][1] - ( (-y + 32767) >> 16 );
}
if (z > 0) {
z = imat[3][2] + ( ( z + 32767) >> 16 );
} else {
z = imat[3][2] - ( (-z + 32767) >> 16 );
}
CurrentPoly->ver[i].x = (short)x;
CurrentPoly->ver[i].y = (short)y;
CurrentPoly->ver[i].z = (short)z;
if ( type & POLY_SHADE )
{
if ( i > 0 )
{
if ( pvx * rdata[3] + pvy * rdata[4] + pvz * rdata[5] < 0 )
{
rdata[3] = -rdata[3];
rdata[4] = -rdata[4];
rdata[5] = -rdata[5];
}
}
x = rdata[3] * imatit[0][0]
+ rdata[4] * imatit[1][0]
+ rdata[5] * imatit[2][0];
y = rdata[3] * imatit[0][1]
+ rdata[4] * imatit[1][1]
+ rdata[5] * imatit[2][1];
z = rdata[3] * imatit[0][2]
+ rdata[4] * imatit[1][2]
+ rdata[5] * imatit[2][2];
if ( i > 0 )
{
if ( pvx * (x/256) + pvy * (y/256) + pvz * (z/256) < 0 )
{
x = -x;
y = -y;
z = -z;
}
}
r = sqrt( (float)x*(float)x + (float)y*(float)y + (float)z*(float)z );
if (r > 0.0) r = 256.0 / r;
pvx = CurrentPoly->ver[i].vx = (short)(r * x);
pvy = CurrentPoly->ver[i].vy = (short)(r * y);
pvz = CurrentPoly->ver[i].vz = (short)(r * z);
}
CurrentPoly->ver[i].u = (short)rdata[6] ;
CurrentPoly->ver[i].v = (short)rdata[7] ;
}
CurrentPoly->vers = (short)i ;
CurrentPoly->select = TRUE ;
Coefficent(coe, CurrentPoly->ver, CurrentPoly->vers);
if ( coe[0] * CurrentPoly->ver[0].vx
+ coe[1] * CurrentPoly->ver[0].vy
+ coe[2] * CurrentPoly->ver[0].vz < 0) {
for( i = 1 ; i < CurrentPoly->vers; i++) {
Vertex *ver;
ver = &(CurrentPoly->ver[i]);
if ( coe[0] * ver->vx + coe[1] * ver->vy + coe[2] * ver->vz > 0) {
ver->vx = -ver->vx;
ver->vy = -ver->vy;
ver->vz = -ver->vz;
}
}
} else {
for( i = 1 ; i < CurrentPoly->vers; i++) {
Vertex *ver;
ver = &(CurrentPoly->ver[i]);
if ( coe[0] * ver->vx + coe[1] * ver->vy + coe[2] * ver->vz < 0) {
ver->vx = -ver->vx;
ver->vy = -ver->vy;
ver->vz = -ver->vz;
}
}
}
/* Selects ++ ;*/
if (reverseflag) {
PolyInvVertex();
#if 0
for (i = 0; i < CurrentPoly->vers; i++) {
CurrentPoly->ver[i].vx = -CurrentPoly->ver[i].vx;
CurrentPoly->ver[i].vy = -CurrentPoly->ver[i].vy;
CurrentPoly->ver[i].vz = -CurrentPoly->ver[i].vz;
}
#endif
}
PolyAppend();
}
ObjData[objno].edit = FALSE ;
}
ObjReadClose();
if (ReaderError()) {
return -1;
}
}
return objno ;
}