// ============================================================== // This file is part of Glest Shared Library (www.glest.org) // // Copyright (C) 2001-2008 Martio Figueroa // // You can redistribute this code 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 // ============================================================== #include "model.h" #include #include #include #include "interpolation.h" #include "conversion.h" #include "util.h" #include "platform_common.h" #include "opengl.h" #include "leak_dumper.h" using namespace Shared::Platform; using namespace Shared::PlatformCommon; using namespace Shared::Graphics::Gl; using namespace std; namespace Shared{ namespace Graphics{ using namespace Util; // ===================================================== // class Mesh // ===================================================== // ==================== constructor & destructor ==================== Mesh::Mesh() { textureManager = NULL; frameCount= 0; vertexCount= 0; indexCount= 0; texCoordFrameCount = 0; vertices= NULL; normals= NULL; texCoords= NULL; tangents= NULL; indices= NULL; interpolationData= NULL; for(int i=0; igetPath().c_str(),i); textureManager->endTexture(textures[i]); textures[i] = NULL; } } } textureManager = NULL; } // ========================== shadows & interpolation ========================= void Mesh::buildInterpolationData(){ interpolationData= new InterpolationData(this); } void Mesh::updateInterpolationData(float t, bool cycle) { interpolationData->update(t, cycle); } void Mesh::updateInterpolationVertices(float t, bool cycle) { interpolationData->updateVertices(t, cycle); } void Mesh::BuildVBOs() { if(getVBOSupported() == true) { if(hasBuiltVBOs == false) { //printf("In [%s::%s Line: %d] setting up a VBO...\n",__FILE__,__FUNCTION__,__LINE__); // Generate And Bind The Vertex Buffer glGenBuffersARB( 1, &m_nVBOVertices ); // Get A Valid Name glBindBufferARB( GL_ARRAY_BUFFER_ARB, m_nVBOVertices ); // Bind The Buffer // Load The Data glBufferDataARB( GL_ARRAY_BUFFER_ARB, sizeof(Vec3f)*frameCount*vertexCount, getInterpolationData()->getVertices(), GL_STATIC_DRAW_ARB ); glBindBuffer(GL_ARRAY_BUFFER_ARB, 0); // Generate And Bind The Texture Coordinate Buffer glGenBuffersARB( 1, &m_nVBOTexCoords ); // Get A Valid Name glBindBufferARB( GL_ARRAY_BUFFER_ARB, m_nVBOTexCoords ); // Bind The Buffer // Load The Data glBufferDataARB( GL_ARRAY_BUFFER_ARB, sizeof(Vec2f)*vertexCount, texCoords, GL_STATIC_DRAW_ARB ); glBindBuffer(GL_ARRAY_BUFFER_ARB, 0); // Generate And Bind The Normal Buffer glGenBuffersARB( 1, &m_nVBONormals ); // Get A Valid Name glBindBufferARB( GL_ARRAY_BUFFER_ARB, m_nVBONormals ); // Bind The Buffer // Load The Data glBufferDataARB( GL_ARRAY_BUFFER_ARB, sizeof(Vec3f)*frameCount*vertexCount, getInterpolationData()->getNormals(), GL_STATIC_DRAW_ARB ); glBindBuffer(GL_ARRAY_BUFFER_ARB, 0); // Generate And Bind The Index Buffer glGenBuffersARB( 1, &m_nVBOIndexes ); // Get A Valid Name glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, m_nVBOIndexes ); // Bind The Buffer // Load The Data glBufferDataARB( GL_ELEMENT_ARRAY_BUFFER_ARB, sizeof(uint32)*indexCount, indices, GL_STATIC_DRAW_ARB ); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, 0); // Our Copy Of The Data Is No Longer Necessary, It Is Safe In The Graphics Card delete [] vertices; vertices = NULL; delete [] texCoords; texCoords = NULL; delete [] normals; normals = NULL; delete [] indices; indices = NULL; hasBuiltVBOs = true; } } } void Mesh::ReleaseVBOs() { if(getVBOSupported() == true) { if(hasBuiltVBOs == true) { glDeleteBuffersARB( 1, &m_nVBOVertices ); // Get A Valid Name glDeleteBuffersARB( 1, &m_nVBOTexCoords ); // Get A Valid Name glDeleteBuffersARB( 1, &m_nVBONormals ); // Get A Valid Name glDeleteBuffersARB( 1, &m_nVBOIndexes ); // Get A Valid Name } } } // ==================== load ==================== void Mesh::loadV2(const string &dir, FILE *f, TextureManager *textureManager,bool deletePixMapAfterLoad) { this->textureManager = textureManager; //read header MeshHeaderV2 meshHeader; size_t readBytes = fread(&meshHeader, sizeof(MeshHeaderV2), 1, f); if(meshHeader.normalFrameCount!=meshHeader.vertexFrameCount){ throw runtime_error("Old model: vertex frame count different from normal frame count"); } if(meshHeader.texCoordFrameCount!=1){ throw runtime_error("Old model: texture coord frame count is not 1"); } //init frameCount= meshHeader.vertexFrameCount; vertexCount= meshHeader.pointCount; indexCount= meshHeader.indexCount; texCoordFrameCount = meshHeader.texCoordFrameCount; init(); //misc twoSided= false; customColor= false; //texture if(meshHeader.hasTexture && textureManager!=NULL){ texturePaths[mtDiffuse]= toLower(reinterpret_cast(meshHeader.texName)); string texPath= dir; if(texPath != "") { texPath += "/"; } texPath += texturePaths[mtDiffuse]; textures[mtDiffuse]= static_cast(textureManager->getTexture(texPath)); if(textures[mtDiffuse]==NULL){ textures[mtDiffuse]= textureManager->newTexture2D(); textures[mtDiffuse]->load(texPath); texturesOwned[mtDiffuse]=true; // M.V. Test textures[mtDiffuse]->init(textureManager->getTextureFilter(),textureManager->getMaxAnisotropy()); if(deletePixMapAfterLoad == true) { textures[mtDiffuse]->deletePixels(); } } } //read data readBytes = fread(vertices, sizeof(Vec3f)*frameCount*vertexCount, 1, f); readBytes = fread(normals, sizeof(Vec3f)*frameCount*vertexCount, 1, f); if(textures[mtDiffuse]!=NULL){ readBytes = fread(texCoords, sizeof(Vec2f)*vertexCount, 1, f); } readBytes = fread(&diffuseColor, sizeof(Vec3f), 1, f); readBytes = fread(&opacity, sizeof(float32), 1, f); fseek(f, sizeof(Vec4f)*(meshHeader.colorFrameCount-1), SEEK_CUR); readBytes = fread(indices, sizeof(uint32)*indexCount, 1, f); } void Mesh::loadV3(const string &dir, FILE *f, TextureManager *textureManager,bool deletePixMapAfterLoad) { this->textureManager = textureManager; //read header MeshHeaderV3 meshHeader; size_t readBytes = fread(&meshHeader, sizeof(MeshHeaderV3), 1, f); if(meshHeader.normalFrameCount!=meshHeader.vertexFrameCount){ throw runtime_error("Old model: vertex frame count different from normal frame count"); } //init frameCount= meshHeader.vertexFrameCount; vertexCount= meshHeader.pointCount; indexCount= meshHeader.indexCount; texCoordFrameCount = meshHeader.texCoordFrameCount; init(); //misc twoSided= (meshHeader.properties & mp3TwoSided) != 0; customColor= (meshHeader.properties & mp3CustomColor) != 0; //texture if(!(meshHeader.properties & mp3NoTexture) && textureManager!=NULL){ texturePaths[mtDiffuse]= toLower(reinterpret_cast(meshHeader.texName)); string texPath= dir; if(texPath != "") { texPath += "/"; } texPath += texturePaths[mtDiffuse]; textures[mtDiffuse]= static_cast(textureManager->getTexture(texPath)); if(textures[mtDiffuse]==NULL){ textures[mtDiffuse]= textureManager->newTexture2D(); textures[mtDiffuse]->load(texPath); texturesOwned[mtDiffuse]=true; // M.V. Test textures[mtDiffuse]->init(textureManager->getTextureFilter(),textureManager->getMaxAnisotropy()); if(deletePixMapAfterLoad == true) { textures[mtDiffuse]->deletePixels(); } } } //read data readBytes = fread(vertices, sizeof(Vec3f)*frameCount*vertexCount, 1, f); readBytes = fread(normals, sizeof(Vec3f)*frameCount*vertexCount, 1, f); if(textures[mtDiffuse]!=NULL){ for(unsigned int i=0; itextureManager = textureManager; //read header MeshHeader meshHeader; size_t readBytes = fread(&meshHeader, sizeof(MeshHeader), 1, f); //init frameCount= meshHeader.frameCount; vertexCount= meshHeader.vertexCount; indexCount= meshHeader.indexCount; init(); //properties customColor= (meshHeader.properties & mpfCustomColor) != 0; twoSided= (meshHeader.properties & mpfTwoSided) != 0; //material diffuseColor= Vec3f(meshHeader.diffuseColor); specularColor= Vec3f(meshHeader.specularColor); specularPower= meshHeader.specularPower; opacity= meshHeader.opacity; //maps uint32 flag= 1; for(int i=0; i(cMapPath)); string mapFullPath= dir; if(mapFullPath != "") { mapFullPath += "/"; } mapFullPath += mapPath; textures[i]= static_cast(textureManager->getTexture(mapFullPath)); if(textures[i]==NULL){ textures[i]= textureManager->newTexture2D(); if(meshTextureChannelCount[i] != -1){ textures[i]->getPixmap()->init(meshTextureChannelCount[i]); } textures[i]->load(mapFullPath); texturesOwned[i]=true; textures[i]->init(textureManager->getTextureFilter(),textureManager->getMaxAnisotropy()); if(deletePixMapAfterLoad == true) { textures[i]->deletePixels(); } } } flag*= 2; } //read data readBytes = fread(vertices, sizeof(Vec3f)*frameCount*vertexCount, 1, f); readBytes = fread(normals, sizeof(Vec3f)*frameCount*vertexCount, 1, f); if(meshHeader.textures!=0){ readBytes = fread(texCoords, sizeof(Vec2f)*vertexCount, 1, f); } readBytes = fread(indices, sizeof(uint32)*indexCount, 1, f); //tangents if(textures[mtNormal]!=NULL){ computeTangents(); } } void Mesh::save(const string &dir, FILE *f){ /*MeshHeader meshHeader; meshHeader.vertexFrameCount= vertexFrameCount; meshHeader.normalFrameCount= normalFrameCount; meshHeader.texCoordFrameCount= texCoordFrameCount; meshHeader.colorFrameCount= colorFrameCount; meshHeader.pointCount= pointCount; meshHeader.indexCount= indexCount; meshHeader.properties= 0; if(twoSided) meshHeader.properties|= mpTwoSided; if(customTexture) meshHeader.properties|= mpCustomTexture; if(texture==NULL){ meshHeader.properties|= mpNoTexture; meshHeader.texName[0]= '\0'; } else{ strcpy(reinterpret_cast(meshHeader.texName), texName.c_str()); texture->getPixmap()->saveTga(dir+"/"+texName); } fwrite(&meshHeader, sizeof(MeshHeader), 1, f); fwrite(vertices, sizeof(Vec3f)*vertexFrameCount*pointCount, 1, f); fwrite(normals, sizeof(Vec3f)*normalFrameCount*pointCount, 1, f); fwrite(texCoords, sizeof(Vec2f)*texCoordFrameCount*pointCount, 1, f); fwrite(colors, sizeof(Vec4f)*colorFrameCount, 1, f); fwrite(indices, sizeof(uint32)*indexCount, 1, f);*/ } void Mesh::computeTangents(){ delete [] tangents; tangents= new Vec3f[vertexCount]; for(unsigned int i=0; ideletePixels(); } } } // =============================================== // class Model // =============================================== // ==================== constructor & destructor ==================== Model::Model() { meshCount = 0; meshes = NULL; textureManager = NULL; lastTData = -1; lastCycleData = false; lastTVertex = -1; lastCycleVertex = false; } Model::~Model() { delete [] meshes; meshes = NULL; } // ==================== data ==================== void Model::buildInterpolationData() const{ for(unsigned int i=0; ifileName = path; } void Model::save(const string &path) { string extension= path.substr(path.find_last_of('.')+1); if(extension=="g3d" ||extension=="G3D" || extension=="s3d" || extension=="S3D"){ saveS3d(path); } else{ throw runtime_error("Unknown model format: " + extension); } } /*void Model::loadG3dOld(const string &path){ try{ FILE *f=fopen(path.c_str(),"rb"); if (f==NULL){ throw runtime_error("Error opening 3d model file"); } string dir= cutLastFile(path); //read header ModelHeaderOld modelHeader; fread(&modelHeader, sizeof(ModelHeader), 1, f); meshCount= modelHeader.meshCount; if(modelHeader.id[0]!='G' || modelHeader.id[1]!='3' || modelHeader.id[2]!='D'){ throw runtime_error("Model: "+path+": is not a valid G3D model"); } switch(modelHeader.version){ case 3:{ meshes= new Mesh[meshCount]; for(uint32 i=0; i(fileHeader.id), "G3D", 3) != 0) { printf("In [%s::%s] file = [%s] fileheader.id = [%s][%c]\n",__FILE__,__FUNCTION__,path.c_str(),reinterpret_cast(fileHeader.id),fileHeader.id[0]); throw runtime_error("Not a valid G3D model"); } fileVersion= fileHeader.version; //version 4 if(fileHeader.version == 4) { //model header ModelHeader modelHeader; readBytes = fread(&modelHeader, sizeof(ModelHeader), 1, f); meshCount= modelHeader.meshCount; if(modelHeader.type != mtMorphMesh) { throw runtime_error("Invalid model type"); } //load meshes meshes= new Mesh[meshCount]; for(uint32 i=0; i