home *** CD-ROM | disk | FTP | other *** search
- #include "Model.h"
-
- #include "Q3bspLoader.h"
- #include "ModelLoader.h"
- #include "vectormath.h"
- #include "matrixmath.h"
- #include "Tokenizer.h"
-
- #include "log.h"
-
- Model::Model(const char* filename){
- name="uninitialized";
-
- meshes = NULL;
- numMeshes = 0;
-
- materials = NULL;
- numMaterials = 0;
-
- matrices = NULL;
- numKeyframes = 0;
-
- // loadModel(filename);
- File f(filename, "rt");
- if(f.isOpen()){
- if( !readFromFile(&f) )
- error("(in Model::Model()): readFromFile() returned false.\n\n");
- }else{
- error("(in Model::Model()): Couldn't open file '%s'.\n\n", filename);
- }
-
- calcBSphere();
- calcAABB();
-
- }
-
- Model::~Model(){
- clearModel();
- }
-
- void Model::clearModel(){ // THINKABOUTME: meshes l÷schen??
- }
- /*
- void Model::loadModel(const char* filename){
- Tokenizer t(filename, ".", "");
- char* ext=t.tokv[t.tokc-1];
-
- if(!strcasecmp(ext, "3ds")){
- if(load3DS(filename, this))
- log("Model loaded: '%s' (3ds import).\n", filename);
- else
- error("(in Model::Model()): Couldn't load 3ds-file '%s'.\n\n", filename);
- }else if(!strcasecmp(ext, "ac")){
- if(loadAC3D(filename, this))
-
- log("Model loaded: '%s' (ac3d import).\n", filename);
- else
- error("(in Model::Model()): Couldn't load ac3d-file '%s'.\n\n", filename);
- }else{
- error("(in Model::loadModel()): unkown extension: '%s'. Model not loaded.\n", ext);
- }
- }
-
- void Model::loadModel(const char* filename, bool generateMatrices=true, bool setupLights=false){
- Tokenizer t(filename, ".", "");
- char* ext=t.tokv[t.tokc-1];
-
- if(!strcasecmp(ext, "3ds")){
- if(L3dsLoader::load3DS(filename, this, generateMatrices, setupLights))
- log("Model loaded: '%s' (3ds import).\n", filename);
- else
- error("(in Model::Model()): Couldn't load 3ds-file '%s'.\n\n", filename);
- }else if(!strcasecmp(ext, "ac")){
- if(Ac3dLoader::loadAC3D(filename, this))
- log("Model loaded: '%s' (ac3d import).\n", filename);
- else
- error("(in Model::Model()): Couldn't load ac3d-file '%s'.\n\n", filename);
- }else if(!strcasecmp(ext, "bsp")){
- if(Q3bspLoader::loadBSP(filename, this))
- log("Model loaded: '%s' (q3bsp import).\n", filename);
- else
- error("(in Model::Model()): Couldn't load ac3d-file '%s'.\n\n", filename);
- }else if(!strcasecmp(ext, "model")){
- if(ModelLoader::loadModel(filename, this))
- log("Model loaded: '%s' (model import).\n", filename);
- else
- error("(in Model::Model()): Couldn't load model-file '%s'.\n\n", filename);
- }else{
- error("(in Model::loadModel()): unkown extension: '%s'. Model not loaded.\n", ext);
- }
- }
- */
- int Model::addMesh(Mesh* mesh){
- int i;
- int id, parentId;
- mat4x4_t mat;
-
- numMeshes++;
- Mesh** newM=new Mesh*[numMeshes];
- for(i=0;i<numMeshes-1;i++){
- newM[i]=meshes[i];
- }
- newM[numMeshes-1]=mesh;
- delete[] meshes;
- meshes=newM;
-
- id=numMeshes-1;
- parentId=getMeshId(mesh->parent);
- if(parentId==-1){
- matrixCopy(mesh->transformationMatrix, mat, 4);
- }else{
- matrixMultMatrix(matrices[0][parentId], mesh->transformationMatrix, 4, mat);
- }
- addMatrix(0, id, mat);
-
-
- for(i=0;i<mesh->numChilds;i++){
- addMesh(mesh->childs[i]);
- }
-
- return id;
- }
-
- int Model::addMatrix(int frame, int id, mat4x4_t mat){
- if(frame<0 || frame>=numKeyframes){
- error("(in Model::addMatrix()): frame invalid.\n");
- return -1;
- }
- if(id<0 || id>=numMeshes){
- error("(in Model::addMatrix()): Mesh id invalid.\n");
- return -1;
- }
- matrixCopy(mat, matrices[frame][id], 4);
- return id;
- }
-
- int Model::getMeshId(Mesh* mesh){
- int i;
-
- for(i=0;i<numMeshes;i++){
- if(meshes[i]==mesh){
- return i;
- }
- }
-
- return -1;
- }
-
-
- void Model::calcBSphere(){
-
- }
-
- void Model::calcAABB(){
-
- vectorInit3d(FLT_MAX, FLT_MAX, FLT_MAX, min);
- vectorInit3d(-FLT_MAX, -FLT_MAX, -FLT_MAX, max);
-
- int i,j,k;
- int numFaces=0;
- int numVertices=0;
-
- for(i=0;i<numMeshes;i++){
- for(j=0;j<meshes[i]->numFaces;j++){
- numFaces++;
- for(k=0;k<meshes[i]->faces[j]->numVertices;k++){
- numVertices++;
- vec4_t p;
- vectorInit4d(meshes[i]->faces[j]->vertices[k*3+0], meshes[i]->faces[j]->vertices[k*3+1], meshes[i]->faces[j]->vertices[k*3+2], 1.0f, p);
- matrixMultVector(matrices[0][i], p, 4, p);
- vectorScale3d(1/p[3], p, p);
-
- min[0]=p[0]<min[0] ? p[0] : min[0];
- min[1]=p[1]<min[1] ? p[1] : min[1];
- min[2]=p[2]<min[2] ? p[2] : min[2];
-
- max[0]=p[0]>max[0] ? p[0] : max[0];
- max[1]=p[1]>max[1] ? p[1] : max[1];
- max[2]=p[2]>max[2] ? p[2] : max[2];
- }
- }
- }
-
- if(numFaces==0){
- vectorInit3d(-0.1f,-0.1f,-0.1f,min);
- vectorInit3d( 0.1f, 0.1f, 0.1f,max);
- }
- log("Model has %i vertices forming %i faces in %i meshes (%i keyframes).\n", numVertices, numFaces, numMeshes, numKeyframes);
- // printf("model: %f %f %f; %f %f %f\n", min[0], min[1], min[2], max[0],max[1],max[2]);
- }
-
-
-
- bool Model::readFromFile(File* f){
- char buff[256];
- Tokenizer t(" =\t\n\r\"", "\"");
-
- while(f->readLine(256, buff, true) != -1){
-
- //printf("Model: line: %s\n", buff);
- t.tokenize(buff);
-
- if(t.tokc==0)
- continue;
-
- if(streq(t.tokv[0], "MATERIAL_LIST")){
- readMaterialList(f);
- }else if(streq(t.tokv[0], "KEYFRAME_LIST")){
- readKeyframeList(f);
- }else if(streq(t.tokv[0], "MESH_LIST")){
- readMeshList(f);
- // }else if(streq(t.tokv[0], "MESH")){
- // this->addMesh(new Mesh(f, this));
-
- }else if(streq(t.tokv[0], "{")){
- continue;
- }else if(streq(t.tokv[0], "}")){
- break;//return true;
- }else{
- warn("(in Model::readFromFile() (%s, line %i)): Unknown token '%s'.\n\n", f->filename, f->line, t.tokv[0]);
- }
- }
-
- return true;
-
- }
-
- bool Model::readMaterialList(File* f){
- char buff[256];
- Tokenizer t(" =\t\n\r\"", "\"");
-
- while(f->readLine(256, buff, true) != -1){
-
- //printf("Model: line: %s\n", buff);
- t.tokenize(buff);
-
- if(t.tokc==0)
- continue;
-
- if(streq(t.tokv[0], "NUM_MATERIALS")){
- numMaterials = atoi(t.tokv[1]);
- materials = new Material*[numMaterials];
- }else if( streq(t.tokv[0], "MATERIAL") ){
- int matId = atoi(t.tokv[1]);
- materials[matId] = new Material(f);
-
- }else if(streq(t.tokv[0], "{")){
- continue;
- }else if(streq(t.tokv[0], "}")){
- break;//return true;
- }else{
- warn("(in Model::readMaterialList() (%s, line %i)): Unknown token '%s'.\n\n", f->filename, f->line, t.tokv[0]);
- }
- }
-
- return true;
- }
-
- bool Model::readMeshList(File* f){
- char buff[256];
- Tokenizer t(" =\t\n\r\"", "\"");
-
- while(f->readLine(256, buff, true) != -1){
-
- //printf("Model: line: %s\n", buff);
- t.tokenize(buff);
-
- if(t.tokc==0)
- continue;
-
- if( streq(t.tokv[0], "NUM_MESHES") ){
- numMeshes = atoi(t.tokv[1]);
- meshes = new Mesh*[numMeshes];
- }else if( streq(t.tokv[0], "MESH") ){
- int meshId = atoi(t.tokv[1]);
- meshes[meshId] = new Mesh(f, this);
-
- }else if(streq(t.tokv[0], "{")){
- continue;
- }else if(streq(t.tokv[0], "}")){
- break;//return true;
- }else{
- warn("(in Model::readMeshList() (%s, line %i)): Unknown token '%s'.\n\n", f->filename, f->line, t.tokv[0]);
- }
- }
-
- return true;
- }
-
- bool Model::readKeyframeList(File* f){
- char buff[256];
- Tokenizer t(" =\t\n\r\"", "\"");
-
- while(f->readLine(256, buff, true) != -1){
-
- //printf("Model: line: %s\n", buff);
- t.tokenize(buff);
-
- if(t.tokc==0)
- continue;
-
- if( streq(t.tokv[0], "NUM_KEYFRAMES") ){
- numKeyframes = atoi(t.tokv[1]);
- matrices = new GLfloat**[numKeyframes];
- }else if( streq(t.tokv[0], "KEYFRAME") ){
- int keyframeId = atoi(t.tokv[1]);
- matrices[keyframeId] = readKeyframe(f);
-
- }else if(streq(t.tokv[0], "{")){
- continue;
- }else if(streq(t.tokv[0], "}")){
- break;//return true;
- }else{
- warn("(in Model::readMeshList() (%s, line %i)): Unknown token '%s'.\n\n", f->filename, f->line, t.tokv[0]);
- }
- }
-
- return true;
- }
-
- GLfloat** Model::readKeyframe(File* f){
- char buff[256];
- Tokenizer t(" =\t\n\r\"", "\"");
-
- GLfloat** matrices = new GLfloat*[numMeshes];
-
- while(f->readLine(256, buff, true) != -1){
-
- //printf("Model: line: %s\n", buff);
- t.tokenize(buff);
-
- if(t.tokc==0)
- continue;
-
- if( streq(t.tokv[0], "MATRIX") ){
- int matrixId = atoi(t.tokv[1]);
- matrices[matrixId] = new GLfloat[16];
-
- // if(t.tokc != 18){
- // warn();
- // }
- for(int i=0;i<16;i++){
- matrices[matrixId][i] = (float)atof(t.tokv[2+i]);
- }
- }else if(streq(t.tokv[0], "{")){
- continue;
- }else if(streq(t.tokv[0], "}")){
- break;//return true;
- }else{
- warn("(in Model::readKeyframes() (%s, line %i)): Unknown token '%s'.\n\n", f->filename, f->line, t.tokv[0]);
- }
- }
-
- return matrices;
-
- }
-
- void Model::dump(){
- int i;
-
- printf("--> Model '%s': numMeshes: %i, numKeyframes: %i\n", name, numMeshes, numKeyframes);
-
- for(i=0;i<numMeshes;i++){
- printf(" * Mesh %i:\n", i);
- meshes[i]->dump();
- }
- printf("<-- Model '%s' end.\n\n", name);
- }
-
-