home *** CD-ROM | disk | FTP | other *** search
/ Enter 2005 March / ENTER.ISO / files / fwp-0.0.6-win32-installer.exe / Mesh.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2004-12-06  |  19.7 KB  |  741 lines

  1. #include "Mesh.h"
  2.  
  3. //#include "Ac3dLoader.h"
  4. #include "log.h"
  5. #include "vectormath.h"
  6. #include "matrixmath.h"
  7. //#include "light.h"
  8. #include <stdlib.h>
  9.  
  10. #include "Renderer.h"
  11. #include "RendererInfo.h"
  12.  
  13.  
  14. Mesh::Mesh(){
  15.     name="uninitialized";
  16.     parent=NULL;
  17.     childs=NULL;
  18.     numChilds=0;
  19.  
  20.     vertices=NULL;
  21.     colors=NULL;
  22.     secondaryColors=NULL;
  23.     normals=NULL;
  24.     texCoords1=NULL;
  25.     texCoords2=NULL;
  26.     indices=NULL;
  27.     numVertices=0;
  28.     numColors=0;
  29.     numSecondaryColors=0;
  30.     numNormals=0;
  31.     numTexCoords=0;
  32.     numIndices=0;
  33.  
  34.     vertexVBOName = 0;
  35.     colorVBOName = 0;
  36.     secondaryColorVBOName = 0;
  37.     normalVBOName = 0;
  38.     texCoord1VBOName = 0;
  39.     texCoord2VBOName = 0;
  40.     indexVBOName = 0;
  41.  
  42.     faces=NULL;
  43.     materials=NULL;
  44.     numFaces=0;
  45.     numMaterials=0;
  46.  
  47.     matrixCopy(id4x4, transformationMatrix, 4);
  48.  
  49.     renderMode=MESH_RENDER_MODE_DO_NOT_RENDER;
  50.     isTriangulation=false;
  51.     hasTransparentFaces=false;
  52.     hasLightmapsForAllFaces=false;
  53.     hasSecondMappingChannel=false;
  54.  
  55.     //transformationMatrix;
  56.  
  57. //    for(int i=0;i<MESH_MAX_DYNAMIC_LIGHTS;i++){
  58. //        dynamicLightParticleClusters[i]=NULL;
  59. //    }
  60.     
  61. }
  62.  
  63. Mesh::Mesh(File* f, Model* model){
  64.     name="uninitialized";
  65.     parent=NULL;
  66.     childs=NULL;
  67.     numChilds=0;
  68.  
  69.     vertices=NULL;
  70.     colors=NULL;
  71.     secondaryColors=NULL;
  72.     normals=NULL;
  73.     texCoords1=NULL;
  74.     texCoords2=NULL;
  75.     indices=NULL;
  76.     numVertices=0;
  77.     numColors=0;
  78.     numSecondaryColors=0;
  79.     numNormals=0;
  80.     numTexCoords=0;
  81.     numIndices=0;
  82.  
  83.     vertexVBOName = 0;
  84.     colorVBOName = 0;
  85.     secondaryColorVBOName = 0;
  86.     normalVBOName = 0;
  87.     texCoord1VBOName = 0;
  88.     texCoord2VBOName = 0;
  89.     indexVBOName = 0;
  90.  
  91.     faces=NULL;
  92.     materials=NULL;
  93.     numFaces=0;
  94.     numMaterials=0;
  95.  
  96.     matrixCopy(id4x4, transformationMatrix, 4);
  97.  
  98.     renderMode=MESH_RENDER_MODE_DO_NOT_RENDER;
  99.     isTriangulation=false;
  100.     hasTransparentFaces=false;
  101.     hasLightmapsForAllFaces=false;
  102.     hasSecondMappingChannel=false;
  103.  
  104.     //transformationMatrix;
  105.  
  106. //    for(int i=0;i<MESH_MAX_DYNAMIC_LIGHTS;i++){
  107. //        dynamicLightParticleClusters[i]=NULL;
  108. //    }
  109.     
  110.     if( !readFromFile(f, model) )
  111.         error("(in Mesh::Mesh()): readFromFile() returned false.\n\n");
  112.  
  113. }
  114.  
  115. Mesh::~Mesh(){
  116.     clearMesh();
  117. }
  118.  
  119. void Mesh::clearMesh(){
  120.     int i;
  121.  
  122.     if(vertices!=NULL){
  123.         delete[] vertices;
  124.         vertices=NULL;
  125.         numVertices=0;
  126.     }
  127.     if(colors!=NULL){
  128.         delete[] colors;
  129.         colors=NULL;
  130.         numColors=0;
  131.     }
  132.     if(secondaryColors!=NULL){
  133.         delete[] secondaryColors;
  134.         secondaryColors=NULL;
  135.         numSecondaryColors=0;
  136.     }
  137.     if(normals!=NULL){
  138.         delete[] normals;
  139.         normals=NULL;
  140.         numNormals=0;
  141.     }
  142.     if(texCoords1!=NULL || texCoords2!=NULL){    // THINKABOUTME: bisserl gefΣhrlich das zusammenzulegen...
  143.         delete[] texCoords1;
  144.         texCoords1=NULL;
  145.         delete[] texCoords2;
  146.         texCoords2=NULL;
  147.         numTexCoords=0;
  148.     }
  149.     if(indices!=NULL){
  150.         delete[] indices;
  151.         indices=NULL;
  152.         numIndices=0;
  153.     }
  154.  
  155.     freeVBOs();
  156.  
  157. //THINKABOUTME: wirklich alles l÷schen?? - mats k÷nne auch von anderen meshes noch verwendet werden!!
  158.  
  159.     if(materials!=NULL){
  160.         for(i=0;i<numMaterials;i++){
  161. //            delete materials[i];        // siehe oben!!
  162.         }
  163.         delete[] materials;
  164.         materials=NULL;
  165.         numMaterials=0;
  166.     }
  167.  
  168.     if(faces!=NULL){
  169.         for(i=0;i<numFaces;i++){
  170.             delete faces[i];
  171.         }
  172.         delete[] faces;
  173.         faces=NULL;
  174.         numFaces=0;
  175.     }
  176.  
  177.  
  178.     renderMode=MESH_RENDER_MODE_DO_NOT_RENDER;
  179.     isTriangulation=false;
  180.     hasTransparentFaces=false;
  181.     hasLightmapsForAllFaces=false;
  182.  
  183. //    for(i=0;i<MESH_MAX_DYNAMIC_LIGHTS;i++){
  184. //        dynamicLightParticleClusters[i] = NULL;
  185. //    }
  186.     //transformationMatrix;
  187. }
  188.  
  189. int Mesh::addMaterial(Material* mat){
  190.     int i;
  191.  
  192.     for(i=0;i<numMaterials;i++){
  193.         if(materials[i]==mat)
  194.             return i;
  195.     }
  196.  
  197.     numMaterials++;
  198.     Material** newMat=new Material*[numMaterials];
  199.     for(i=0;i<numMaterials-1;i++)
  200.         newMat[i]=materials[i];
  201.     
  202.     newMat[numMaterials-1]=mat;
  203.     delete[] materials;
  204.     materials=newMat;
  205.  
  206.     return numMaterials-1;
  207. }
  208.  
  209.  
  210. int Mesh::addArrayElement(GLfloat v[3], GLfloat c[3], GLfloat n[3], GLfloat t1[2], GLfloat t2[2]){
  211.     int i;
  212.  
  213. //    _ASSERTE(numVertices==numNormals==numTexCoords);
  214.     for(i=0;i<numVertices;i++){
  215.         if(vectorEqual3d(&vertices[i*3], v) && vectorEqual3d(&colors[i*3], c)  && vectorEqual3d(&normals[i*3], n) 
  216.             && vectorEqual2d(&texCoords1[i*2], t1) && vectorEqual2d(&texCoords2[i*2], t2) ){
  217.             return i;
  218.         }
  219.     }
  220.  
  221.     // add vertex
  222.     numVertices++;
  223.     GLfloat* newV=new GLfloat[numVertices*3];
  224.     for(i=0;i<numVertices*3-3;i++){
  225.         newV[i]=vertices[i];
  226.     }
  227.     newV[numVertices*3-3]=v[0];
  228.     newV[numVertices*3-2]=v[1];
  229.     newV[numVertices*3-1]=v[2];
  230.     
  231.     delete[] vertices;
  232.     vertices=newV;
  233.  
  234.     // add color
  235.     numColors++;
  236.     GLfloat* newC=new GLfloat[numColors*3];
  237.     for(i=0;i<numColors*3-3;i++){
  238.         newC[i]=colors[i];
  239.     }
  240.     newC[numColors*3-3]=c[0];
  241.     newC[numColors*3-2]=c[1];
  242.     newC[numColors*3-1]=c[2];
  243.     
  244.     delete[] colors;
  245.     colors=newC;
  246.  
  247.     // add normal
  248.     numNormals++;
  249.     GLfloat* newN=new GLfloat[numNormals*3];
  250.     for(i=0;i<numNormals*3-3;i++){
  251.         newN[i]=normals[i];
  252.     }
  253.     newN[numNormals*3-3]=n[0];
  254.     newN[numNormals*3-2]=n[1];
  255.     newN[numNormals*3-1]=n[2];
  256.     
  257.     delete[] normals;
  258.     normals=newN;
  259.  
  260.     // add texCoord1
  261.     numTexCoords++;
  262.     GLfloat* newT1=new GLfloat[numTexCoords*2];
  263.     for(i=0;i<numTexCoords*2-2;i++){
  264.         newT1[i]=texCoords1[i];
  265.     }
  266.     newT1[numTexCoords*2-2]=t1[0];
  267.     newT1[numTexCoords*2-1]=t1[1];
  268.     
  269.     delete[] texCoords1;
  270.     texCoords1=newT1;
  271.  
  272.     // add texCoord2
  273.     //numTexCoords++;
  274.     GLfloat* newT2=new GLfloat[numTexCoords*2];
  275.     for(i=0;i<numTexCoords*2-2;i++){
  276.         newT2[i]=texCoords2[i];
  277.     }
  278.     newT2[numTexCoords*2-2]=t2[0];
  279.     newT2[numTexCoords*2-1]=t2[1];
  280.     
  281.     delete[] texCoords2;
  282.     texCoords2=newT2;
  283.  
  284.  
  285. //    _ASSERTE(numVertices==numNormals==numTexCoords);
  286.     return numVertices-1;
  287. }
  288.  
  289. int Mesh::addFace(Face *face){
  290.     int i;
  291. /*
  292.     for(i=0;i<numFaces;i++){
  293.         if(faces[i]==face)
  294.             return i;
  295.     }
  296. */
  297.     numFaces++;
  298.     Face** newF=new Face*[numFaces];
  299.     for(i=0;i<numFaces-1;i++){
  300.         newF[i]=faces[i];
  301.     }
  302.     newF[numFaces-1]=face;
  303.  
  304.     
  305.     delete[] faces;
  306.     faces=newF;
  307.  
  308.  
  309.     // add Elements
  310.     face->numIndices=face->numVertices;
  311.     face->indices=new GLuint[face->numIndices];
  312.     for(i=0;i<face->numIndices;i++){
  313.         face->indices[i]=addArrayElement(&face->vertices[i*3], &face->colors[i*3], &face->normals[i*3], &face->texCoords1[i*2], &face->texCoords2[i*2]);
  314.     }
  315.     numIndices+=face->numIndices;
  316.     GLuint* newInd=new GLuint[numIndices];
  317.     for(i=0;i<numIndices-face->numIndices;i++){
  318.         newInd[i]=indices[i];
  319.     }
  320.     for(i=numIndices-face->numIndices;i<numIndices;i++){
  321.         newInd[i]=face->indices[i-(numIndices-face->numIndices)];
  322.     }
  323.     delete[] indices;
  324.     indices=newInd;
  325.  
  326.     addMaterial(face->material);
  327.     face->mesh=this;
  328.  
  329.     return numFaces-1;
  330. }
  331.  
  332. int Mesh::addChild(Mesh* child){
  333.     int i;
  334.  
  335.     numChilds++;
  336.     Mesh** newChilds=new Mesh*[numChilds];
  337.     for(i=0;i<numChilds-1;i++){
  338.         newChilds[i]=childs[i];
  339.     }
  340.     newChilds[numChilds-1]=child;
  341.  
  342.     
  343.     delete[] childs;
  344.     childs=newChilds;
  345.  
  346.     child->parent=this;
  347.  
  348.     return numChilds-1;
  349. }
  350. /*
  351. void Mesh::addDynamicLightParticleCluster(DynamicLightParticleCluster* pc){
  352.     for(int i=0;i<MESH_MAX_DYNAMIC_LIGHTS;i++){
  353.         if(dynamicLightParticleClusters[i]==NULL){
  354.             dynamicLightParticleClusters[i]=pc;
  355.             return;
  356.         }
  357.     }
  358. //    warn("(in Mesh::addDynamicLightmapParticleCluster()): Couldn't add particle cluster.\n\n");
  359. }
  360.  
  361. void Mesh::removeDynamicLightParticleCluster(DynamicLightParticleCluster* pc){
  362.     for(int i=0;i<MESH_MAX_DYNAMIC_LIGHTS;i++){
  363.         if(dynamicLightParticleClusters[i]==pc){
  364.             dynamicLightParticleClusters[i]=NULL;
  365.     //        return;
  366.         }
  367.     }
  368.     //warn("(in Mesh::addDynamicLightmapParticleCluster()): Couldn't add particle cluster.\n\n");
  369. }
  370. */
  371.  
  372. void Mesh::setRenderMode(){
  373.     int i;
  374.     //renderMode=MESH_RENDER_MODE_TRIANGLE_ARRAY;
  375.     //renderMode=MESH_RENDER_MODE_SINGLE_POLYGON;
  376.     //renderMode=MESH_RENDER_MODE_SINGLE_TRIANGLE;
  377.  
  378.     hasTransparentFaces=false;
  379.     isTriangulation=true;
  380.     hasLightmapsForAllFaces=true;
  381.     hasSecondMappingChannel=true;
  382.  
  383.     for(i=0;i<numMaterials;i++){
  384.         if(materials[i]->isTransparent){
  385.             hasTransparentFaces=true;
  386.         }
  387.         if(!materials[i]->hasLightmap){
  388.             hasLightmapsForAllFaces=false;
  389.         }
  390.         if(materials[i]->shader!=NULL && materials[i]->shader->mapChannel2!=NULL){
  391.             hasSecondMappingChannel=true;
  392.         }
  393.     }
  394.  
  395.     for(i=0;i<numFaces;i++){
  396.         if(faces[i]->numVertices!=3)
  397.             isTriangulation=false;
  398. //        if(!(faces[i]->flags & FACE_FLAG_TWO_SIDED))
  399. //            hasOnlyTwoSidedFaces=false;
  400.     }
  401.  
  402.  
  403. //    if(_glGenBuffersARB!=NULL && Renderer::info.var.useVBOs==true)
  404. //        createVBOs();
  405.  
  406.  
  407.     if(numMaterials==1 && !hasTransparentFaces && isTriangulation){
  408.         renderMode=MESH_RENDER_MODE_TRIANGLE_ARRAY;
  409.         return;
  410.     }
  411.     if(isTriangulation){
  412.         renderMode=MESH_RENDER_MODE_SINGLE_TRIANGLE;
  413.         return;
  414.     }
  415.     renderMode=MESH_RENDER_MODE_SINGLE_POLYGON;
  416.  
  417. }
  418.  
  419. void Mesh::createVBOs(){
  420.  
  421.     if(_glGenBuffersARB==NULL || Renderer::info.var.useVBOs==false)
  422.         return;
  423.  
  424.     _glGenBuffersARB( 1, &vertexVBOName );
  425.     _glBindBufferARB( GL_ARRAY_BUFFER_ARB, vertexVBOName );
  426.     _glBufferDataARB( GL_ARRAY_BUFFER_ARB, numVertices*3*sizeof(float), vertices, GL_STATIC_DRAW_ARB );
  427.     _glBindBufferARB( GL_ARRAY_BUFFER_ARB, 0);
  428.  
  429.     _glGenBuffersARB( 1, &colorVBOName );
  430.     _glBindBufferARB( GL_ARRAY_BUFFER_ARB, colorVBOName );
  431.     _glBufferDataARB( GL_ARRAY_BUFFER_ARB, numColors*3*sizeof(float), colors, GL_STATIC_DRAW_ARB );
  432.     _glBindBufferARB( GL_ARRAY_BUFFER_ARB, 0);
  433.  
  434.     _glGenBuffersARB( 1, &secondaryColorVBOName );
  435.     _glBindBufferARB( GL_ARRAY_BUFFER_ARB, secondaryColorVBOName );
  436.     _glBufferDataARB( GL_ARRAY_BUFFER_ARB, numSecondaryColors*3*sizeof(float), colors, GL_STATIC_DRAW_ARB );
  437.     _glBindBufferARB( GL_ARRAY_BUFFER_ARB, 0);
  438.  
  439.     _glGenBuffersARB( 1, &normalVBOName );
  440.     _glBindBufferARB( GL_ARRAY_BUFFER_ARB, normalVBOName );
  441.     _glBufferDataARB( GL_ARRAY_BUFFER_ARB, numNormals*3*sizeof(float), normals, GL_STATIC_DRAW_ARB );
  442.     _glBindBufferARB( GL_ARRAY_BUFFER_ARB, 0);
  443.  
  444.     _glGenBuffersARB( 1, &texCoord1VBOName );
  445.     _glBindBufferARB( GL_ARRAY_BUFFER_ARB, texCoord1VBOName );
  446.     _glBufferDataARB( GL_ARRAY_BUFFER_ARB, numTexCoords*2*sizeof(float), texCoords1, GL_STATIC_DRAW_ARB );
  447.     _glBindBufferARB( GL_ARRAY_BUFFER_ARB, 0);
  448.  
  449.     _glGenBuffersARB( 1, &texCoord2VBOName );
  450.     _glBindBufferARB( GL_ARRAY_BUFFER_ARB, texCoord2VBOName );
  451.     _glBufferDataARB( GL_ARRAY_BUFFER_ARB, numTexCoords*2*sizeof(float), texCoords2, GL_STATIC_DRAW_ARB );
  452.     _glBindBufferARB( GL_ARRAY_BUFFER_ARB, 0);
  453.  
  454. //    _glGenBuffersARB( 1, &indexVBOName );
  455. //    _glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, indexVBOName );
  456. //    _glBufferDataARB( GL_ELEMENT_ARRAY_BUFFER_ARB, numIndices*sizeof(GLuint), indices, GL_STATIC_DRAW_ARB );
  457. //    _glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
  458.  
  459. //    printf("VBO created. (Names: %d, %d, %d, %d, %d)\n", vertexVBOName, colorVBOName, normalVBOName, texCoord1VBOName, texCoord2VBOName);
  460. }
  461.  
  462. void Mesh::freeVBOs(){
  463.     if(vertexVBOName!=0){
  464.         _glBindBufferARB(GL_ARRAY_BUFFER_ARB, vertexVBOName);
  465.         _glBufferDataARB(GL_ARRAY_BUFFER_ARB, 0, NULL, GL_STATIC_DRAW_ARB );
  466.         _glDeleteBuffersARB(1, &vertexVBOName);
  467.         _glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
  468.         vertexVBOName=0;
  469.     }
  470.  
  471.     if(colorVBOName!=0){
  472.         _glBindBufferARB(GL_ARRAY_BUFFER_ARB, colorVBOName);
  473.         _glBufferDataARB(GL_ARRAY_BUFFER_ARB, 0, NULL, GL_STATIC_DRAW_ARB );
  474.         _glDeleteBuffersARB(1, &colorVBOName);
  475.         _glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
  476.         colorVBOName=0;
  477.     }
  478.  
  479.     if(secondaryColorVBOName!=0){
  480.         _glBindBufferARB(GL_ARRAY_BUFFER_ARB, secondaryColorVBOName);
  481.         _glBufferDataARB(GL_ARRAY_BUFFER_ARB, 0, NULL, GL_STATIC_DRAW_ARB );
  482.         _glDeleteBuffersARB(1, &secondaryColorVBOName);
  483.         _glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
  484.         secondaryColorVBOName=0;
  485.     }
  486.  
  487.     if(normalVBOName!=0){
  488.         _glBindBufferARB(GL_ARRAY_BUFFER_ARB, normalVBOName);
  489.         _glBufferDataARB(GL_ARRAY_BUFFER_ARB, 0, NULL, GL_STATIC_DRAW_ARB );
  490.         _glDeleteBuffersARB(1, &normalVBOName);
  491.         _glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
  492.         normalVBOName=0;
  493.     }
  494.  
  495.     if(texCoord1VBOName!=0){
  496.         _glBindBufferARB(GL_ARRAY_BUFFER_ARB, texCoord1VBOName);
  497.         _glBufferDataARB(GL_ARRAY_BUFFER_ARB, 0, NULL, GL_STATIC_DRAW_ARB );
  498.         _glDeleteBuffersARB(1, &texCoord1VBOName);
  499.         _glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
  500.         texCoord1VBOName=0;
  501.     }
  502.  
  503.     if(texCoord2VBOName!=0){
  504.         _glBindBufferARB(GL_ARRAY_BUFFER_ARB, texCoord2VBOName);
  505.         _glBufferDataARB(GL_ARRAY_BUFFER_ARB, 0, NULL, GL_STATIC_DRAW_ARB );
  506.         _glDeleteBuffersARB(1, &texCoord2VBOName);
  507.         _glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
  508.         texCoord2VBOName=0;
  509.     }
  510.  
  511. //    if(indexVBOName!=0){
  512. //        _glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, indexVBOName);
  513. //        _glBufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0, NULL, GL_STATIC_DRAW_ARB );
  514. //        _glDeleteBuffersARB(1, &indexVBOName);
  515. //        _glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
  516. //        indexVBOName=0;
  517. //    }
  518.  
  519. }
  520.  
  521. void Mesh::createSecondaryColors(){
  522.     numSecondaryColors = numVertices;
  523.     secondaryColors = new GLfloat[numSecondaryColors*3];
  524.  
  525.     for(int i=0;i<numSecondaryColors;i++){
  526.         vectorInit3d(0.0f, 0.0f, 0.0f, &secondaryColors[i*3]);
  527.     }
  528. }
  529.  
  530. void Mesh::updateSecondaryColorVBO(){
  531.     if(secondaryColorVBOName == 0 || _glBindBufferARB == NULL)
  532.         return;
  533.  
  534.  
  535.     _glBindBufferARB( GL_ARRAY_BUFFER_ARB, secondaryColorVBOName );
  536.     _glBufferDataARB( GL_ARRAY_BUFFER_ARB, numSecondaryColors*3*sizeof(float), secondaryColors, GL_STATIC_DRAW_ARB );
  537.     _glBindBufferARB( GL_ARRAY_BUFFER_ARB, 0);
  538. }
  539.  
  540. int Mesh::getMaterialId(Material* m){
  541.     for(int i=0;i<numMaterials;i++){
  542.         if(materials[i]==m)
  543.             return i;
  544.     }
  545.     return -1;
  546. }
  547.  
  548. void Mesh::swapFaces(int i, int j){
  549.     Face* tmp=faces[i];
  550.     faces[i]=faces[j];
  551.     faces[j]=tmp;
  552. }
  553.  
  554. void Mesh::sortFacesByMaterial(){
  555.     for(int i=0;i<numFaces;i++){
  556.         for(int j=i+1;j<numFaces;j++){
  557.             if(getMaterialId(faces[i]->material) > getMaterialId(faces[j]->material)){
  558.                 swapFaces(i,j);
  559.             }
  560.         }
  561.     }
  562. /*
  563.     for(int k=0;k<numFaces-1;k++){
  564.         if(getMaterialId(faces[k]->material)>getMaterialId(faces[k+1]->material))
  565.             printf("ACHTUNG: k: %i; matID: %i\n", k, getMaterialId(faces[k]->material));
  566.     }
  567. */
  568. }
  569.  
  570.  
  571. bool Mesh::readFromFile(File* f, Model* model){
  572.     char buff[256];
  573.     Tokenizer t(" =\t\n\r\"", "\"");
  574.  
  575.     while(f->readLine(256, buff, true) != -1){
  576.         
  577.         t.tokenize(buff);
  578.  
  579.         if(t.tokc==0)
  580.             continue;
  581.  
  582.         if(streq(t.tokv[0], "TRANSFORMATION_MATRIX")){
  583. //            sscanf(t.tokv[1], "%f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f"
  584. //                , &m->transformationMatrix[0], &m->transformationMatrix[1], &m->transformationMatrix[2], &m->transformationMatrix[3]
  585. //                , &m->transformationMatrix[4], &m->transformationMatrix[5], &m->transformationMatrix[6], &m->transformationMatrix[7]
  586. //                , &m->transformationMatrix[8], &m->transformationMatrix[9], &m->transformationMatrix[10], &m->transformationMatrix[11]
  587. //                , &m->transformationMatrix[12], &m->transformationMatrix[13], &m->transformationMatrix[14], &m->transformationMatrix[15]
  588. //                );
  589.             //printf("N: %i\n", n);
  590.         }else if(streq(t.tokv[0], "MATERIAL")){
  591.             this->numMaterials = 1;
  592.             this->materials = new Material*[1];
  593.             this->materials[0] = model->materials[atoi(t.tokv[1])];
  594.  
  595. //            printf("MAT: %i, hasTex %i, hasLM %i\n", atoi(t.tokv[1]), m->materials[0]->hasTexture, m->materials[0]->hasLightmap);
  596.         }else if(streq(t.tokv[0], "NUM_VERTICES")){
  597.             this->numVertices = this->numColors = this->numNormals = this->numTexCoords = atoi(t.tokv[1]);
  598.             this->vertices=new float[this->numVertices*3];
  599.             this->colors=new float[this->numColors*3];
  600.             this->normals=new float[this->numNormals*3];
  601.             this->texCoords1=new float[this->numTexCoords*2];
  602.             this->texCoords2=new float[this->numTexCoords*2];
  603.         }else if(streq(t.tokv[0], "VERTEX_LIST")){
  604.             f->readLine(256, buff, true);
  605.  
  606.             //int n;
  607.             for(int i=0;i<this->numVertices;i++){
  608.                 f->readLine(256, buff, true);
  609. /*
  610.                 sscanf(buff, "VERTEX %i: %f %f %f  %f %f %f  %f %f  %f %f"
  611.                     , &n
  612.                     , &m->vertices[i*3+0], &m->vertices[i*3+1], &m->vertices[i*3+2]
  613.                     , &m->colors[i*3+0], &m->colors[i*3+1], &m->colors[i*3+2]
  614.                     , &m->texCoords1[i*2+0], &m->texCoords1[i*2+1]
  615.                     , &m->texCoords2[i*2+0], &m->texCoords2[i*2+1]);
  616. */
  617.                 Tokenizer t2(buff, " ", "");
  618.                 vectorInit3d((float)atof(t2.tokv[2]), (float)atof(t2.tokv[3]), (float)atof(t2.tokv[4]), &this->vertices[i*3]);
  619.                 vectorInit3d((float)atof(t2.tokv[5]), (float)atof(t2.tokv[6]), (float)atof(t2.tokv[7]), &this->colors[i*3]);
  620.                 vectorInit2d((float)atof(t2.tokv[8]), (float)atof(t2.tokv[9]), &this->texCoords1[i*2]);
  621.                 vectorInit2d((float)atof(t2.tokv[10]), (float)atof(t2.tokv[11]), &this->texCoords2[i*2]);
  622.  
  623.                 vectorInit3d(0.0f, 0.0f, 0.0f, &this->normals[i*3]);
  624. //                printf(" -- %i --\n", i);
  625.             }
  626.  
  627.             f->readLine(256, buff, true);
  628.  
  629.         }else if(streq(t.tokv[0], "NUM_FACES")){
  630.             this->numFaces = atoi(t.tokv[1]);
  631.             this->faces = new Face*[this->numFaces];
  632.             this->numIndices = this->numFaces*3;
  633.             this->indices = new GLuint[this->numIndices];
  634.         }else if(streq(t.tokv[0], "FACE_LIST")){
  635.             f->readLine(256, buff, true);
  636.  
  637.             int ind1, ind2, ind3;
  638.             for(int i=0;i<this->numFaces;i++){
  639.                 Face* fa = new Face();
  640.  
  641.                 f->readLine(256, buff, true);
  642. /*
  643.                 sscanf(buff, "FACE %i: %i %i %i  %f %f %f"
  644.                     , &n
  645.                     , &ind1, &ind2, &ind3
  646.                     , &fa->normal[0], &fa->normal[1], &fa->normal[2]);
  647. */
  648.                 Tokenizer t2(buff, " ", "");
  649.                 ind1=atoi(t2.tokv[2]);
  650.                 ind2=atoi(t2.tokv[3]);
  651.                 ind3=atoi(t2.tokv[4]);
  652.  
  653.                 this->indices[i*3+0] = ind1;
  654.                 this->indices[i*3+1] = ind2;
  655.                 this->indices[i*3+2] = ind3;
  656.                 //vectorInit3d((float)atof(t2.tokv[5]), (float)atof(t2.tokv[6]), (float)atof(t2.tokv[7]), fa->normal);
  657.  
  658.                 fa->numIndices=3;
  659.                 fa->indices=new unsigned int[3];
  660.                 fa->indices[0]=ind1;
  661.                 fa->indices[1]=ind2;
  662.                 fa->indices[2]=ind3;
  663.  
  664.                 fa->numVertices=3;
  665.                 fa->vertices=new float[9];
  666.                 vectorCopy3d(&this->vertices[ind1*3], &fa->vertices[0]);
  667.                 vectorCopy3d(&this->vertices[ind2*3], &fa->vertices[3]);
  668.                 vectorCopy3d(&this->vertices[ind3*3], &fa->vertices[6]);
  669.                 
  670.                 fa->numColors=3;
  671.                 fa->colors=new float[9];
  672.                 vectorCopy3d(&this->colors[ind1*3], &fa->colors[0]);
  673.                 vectorCopy3d(&this->colors[ind2*3], &fa->colors[3]);
  674.                 vectorCopy3d(&this->colors[ind3*3], &fa->colors[6]);
  675. /*
  676.                 fa->numNormals=3;
  677.                 fa->normals=new float[9];
  678.                 vectorCopy3d(&m->normals[ind1*3], &fa->normals[0]);
  679.                 vectorCopy3d(&m->normals[ind2*3], &fa->normals[3]);
  680.                 vectorCopy3d(&m->normals[ind3*3], &fa->normals[6]);
  681. */
  682.                 fa->numTexCoords=3;
  683.                 fa->texCoords1=new float[6];
  684.                 vectorCopy2d(&this->texCoords1[ind1*2], &fa->texCoords1[0]);
  685.                 vectorCopy2d(&this->texCoords1[ind2*2], &fa->texCoords1[2]);
  686.                 vectorCopy2d(&this->texCoords1[ind3*2], &fa->texCoords1[4]);
  687.                 fa->texCoords2=new float[6];
  688.                 vectorCopy2d(&this->texCoords2[ind1*2], &fa->texCoords2[0]);
  689.                 vectorCopy2d(&this->texCoords2[ind2*2], &fa->texCoords2[2]);
  690.                 vectorCopy2d(&this->texCoords2[ind3*2], &fa->texCoords2[4]);
  691.  
  692.                 fa->material=this->materials[0];
  693.  
  694.                 fa->calcNormal();
  695.  
  696.                 // correct vertex normals!
  697.                 fa->numNormals=3;
  698.                 fa->normals=new float[9];
  699.                 vectorCopy3d(fa->normal, &fa->normals[0]);
  700.                 vectorCopy3d(fa->normal, &fa->normals[3]);
  701.                 vectorCopy3d(fa->normal, &fa->normals[6]);
  702.                 vectorCopy3d(fa->normal, &this->normals[ind1*3]);
  703.                 vectorCopy3d(fa->normal, &this->normals[ind2*3]);
  704.                 vectorCopy3d(fa->normal, &this->normals[ind3*3]);
  705.  
  706.  
  707.                 this->faces[i]=fa;
  708.  
  709.             }
  710.  
  711.             f->readLine(256, buff, true);
  712.  
  713.         }else if(streq(t.tokv[0], "{")){
  714.             continue;
  715.         }else if(streq(t.tokv[0], "}")){
  716.             break;//return true;
  717.         }else{
  718.             warn("(in Mesh::readFromFile() (%s, line %i)): Unknown token '%s'.\n\n", f->filename, f->line, t.tokv[0]);
  719.         }
  720.     }
  721.  
  722.     this->setRenderMode();
  723.  
  724.     return true;
  725. }
  726.  
  727. void Mesh::dump(){
  728.     int i;
  729.  
  730.     printf("** Mesh '%s': numMaterials: %i, numFaces: %i, numVertices: %i, numChilds: %i\n", name, numMaterials, numFaces, numVertices, numChilds);
  731.     printf("              matrix: ");
  732.     matrixPrint(transformationMatrix, 4);
  733. //    for(i=0;i<numFaces;i++){
  734. //        faces[i]->dump();
  735. //    }
  736.     for(i=0;i<numChilds;i++){
  737.         childs[i]->dump();        
  738.     }
  739. }
  740.  
  741.