brender-v1.1.2/TOOLS/LOADASC.C
2022-05-04 18:14:23 -07:00

281 lines
5.4 KiB
C

/*
* Copyright (c) 1993-1995 Argonaut Technologies Limited. All rights reserved.
*
* $Id: loadasc.c 1.2 1995/02/22 22:15:37 sam Exp $
* $Locker: $
*
* Loader for 3D-Studio .ASC format
*
* $BC<"make -f geoconv.wtc %s.obj;">
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <argstd.h>
#include <error.h>
#define FILE_LOG 0
#include "brender.h"
#include "fmt.h"
//#include "shortcut.h"
static char rscid[] = "$Id: loadasc.c 1.2 1995/02/22 22:15:37 sam Exp $";
/*
* Various keywords than can appear in 3D-Studio .ASC file
*/
#define K_NOP 0
#define K_AMBIENT_LIGHT 1
#define K_NAMED_OBJECT 2
#define K_TRI_MESH 3
#define K_VERTEX 4
#define K_FACE 5
#define K_SMOOTHING 6
#define K_MATERIAL 7
#define K_DIRECT_LIGHT 8
#define K_POSITION 9
#define K_LIGHT_COLOR 10
#define K_CAMERA 11
#define K_TARGET 12
#define K_BANK_ANGLE 13
#define K_EOF 255
static struct {
char *word;
short length;
short token;
#define K(word,token) {word,sizeof(word)-1,token,},
} Keywords[] = {
K("Ambient light color:", K_AMBIENT_LIGHT)
K("Named object: ", K_NAMED_OBJECT)
K("Tri-mesh,", K_TRI_MESH)
K("Vertex list:", K_NOP)
K("Vertex", K_VERTEX)
K("Face list:", K_NOP)
K("Face", K_FACE)
K("Smoothing:", K_SMOOTHING)
K("Material:", K_MATERIAL)
K("Direct light", K_DIRECT_LIGHT)
K("Position:", K_POSITION)
K("Light color:", K_LIGHT_COLOR)
K("Camera ", K_CAMERA)
K("Target ", K_TARGET)
K("Bank angle: ", K_BANK_ANGLE)
};
#undef K
#define MAX_LINE 512
#define MAX_NAME 64
#define TABLE_SIZE 16
static br_model *LoadModels;
static br_material *LoadMaterials;
static br_camera *LoadCameras;
static br_light *LoadLights;
static br_actor *LoadActors;
/*
* Load a 3D-Studio .ASC file and produce -
*
* Models
*
* TODO:
* Actors
* Materials
* Cameras
* Lights
*/
int BR_PUBLIC_ENTRY BrFmtASCLoad(char *name,
br_model **mtable, int max_models)
{
FILE *fh;
char line[MAX_LINE];
char object_name[MAX_NAME+1] = "";
char material_name[MAX_NAME+1] = "";
char *line_tail;
int token,cvert,cface,vnum,i;
float vx,vy,vz,vu,vv;
int fnum,f,fa,fb,fc,fab,fbc,fca;
char ch,*cp;
br_model *mp = NULL;
int nmodels = 0;
/*
* Open input file
*/
fh = FOPEN(name,"r");
/*
* Read lines from input file
*/
for(;;) {
/*
* Pull next line and look at the front to see what it contains:
*/
if(fgets(line,MAX_LINE,fh) == NULL) {
token = K_EOF;
} else {
token = K_NOP;
for(i=0; i< ASIZE(Keywords); i++) {
if(!strnicmp(Keywords[i].word,line,Keywords[i].length)) {
token = Keywords[i].token;
line_tail = line+Keywords[i].length;
break;
}
}
}
/*
* Perform action for current line
*/
switch(token) {
case K_NAMED_OBJECT:
/*
* Extract name
*/
sscanf(line_tail,"\"%64[^\"]",object_name);
#if FILE_LOG
fprintf(stderr,"NAMED_OBJECT: <%s>\n",object_name);
#endif
break;
case K_MATERIAL:
sscanf(line_tail,"\"%64[^\"]",material_name);
#if FILE_LOG
fprintf(stderr,"MATERIAL: <%s>\n",material_name);
#endif
/*
* make sure a face has been read
*/
if(cface > 0)
mp->faces[cface-1].material = BrMaterialFind(material_name);
break;
case K_TRI_MESH:
/*
* Fail if no enough space in output table
*/
if(nmodels >= max_models)
return nmodels;
/*
* Allocate a new model and buffers
*/
NEW_PTR(mp);
if(strlen(object_name) != 0) {
mp->identifier = MALLOC(strlen(object_name)+1);
strcpy(mp->identifier,object_name);
}
sscanf(line_tail," Vertices: %d Faces: %d",&mp->nvertices,&mp->nfaces);
#if FILE_LOG
fprintf(stderr,"TRIMESH: %d %d\n",mp->nvertices,mp->nfaces);
#endif
if (mp->nvertices)
NEW_PTR_N(mp->vertices,mp->nvertices);
if (mp->nfaces)
NEW_PTR_N(mp->faces,mp->nfaces);
cvert = 0;
cface = 0;
/*
* Record model in output table
*/
mtable[nmodels++] = mp;
break;
case K_VERTEX:
/*
* Add vertex to current mesh
*/
vx = vy = vz = vu = vv = 0.0;
sscanf(line_tail,"%d: X: %f Y: %f Z: %f U: %f V: %f",&vnum,&vx,&vy,&vz,&vu,&vv);
#if FILE_LOG
fprintf(stderr,"VERTEX: %d\n",vnum);
#endif
/*
* Only process if things are consistent
*/
if(cvert != vnum || cvert >= mp->nvertices)
break;
mp->vertices[cvert].p.v[0] = BrFloatToScalar(vx);
mp->vertices[cvert].p.v[1] = BrFloatToScalar(vy);
mp->vertices[cvert].p.v[2] = BrFloatToScalar(vz);
mp->vertices[cvert].map.v[0] = BrFloatToScalar(vu);
mp->vertices[cvert].map.v[1] = BrFloatToScalar(vv);
cvert++;
break;
case K_FACE:
/*
* Add face to current mesh
*/
sscanf(line_tail,"%d: A:%d B:%d C:%d AB:%d BC:%d CA:%d",
&fnum,&fa,&fb,&fc,&fab,&fbc,&fca);
#if FILE_LOG
fprintf(stderr,"FACE: %d\n",fnum);
#endif
/*
* Only process if things are consistent
*/
if(cface != fnum || cface >= mp->nfaces)
break;
mp->faces[cface].vertices[0] = fa;
mp->faces[cface].vertices[1] = fb;
mp->faces[cface].vertices[2] = fc;
mp->faces[cface].material = 0; /* default material */
cface++;
break;
case K_SMOOTHING:
/*
* Add smoothing group to last face
*/
sscanf(line_tail,"%d",&i);
#if FILE_LOG
fprintf(stderr,"SMOOTHING: %d\n",i);
#endif
if(cface > 0)
mp->faces[cface-1].smoothing = i;
break;
case K_EOF:
#if FILE_LOG
fprintf(stderr,"EOF:\n");
#endif
FCLOSE(fh);
return nmodels;
break;
/*
* Ignore everything else
*/
case K_NOP:
default:
;
}
}
}