/* orxonox - the future of 3D-vertical-scrollers Copyright (C) 2006 orx This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. ### File Specific: main-programmer: bottac@ee.ethz.ch */ #include "bsp_file.h" #include "bsp_tree_node.h" #include #include #include #include "debug.h" #include "material.h" #include "vertex_array_model.h" // Necessary ? #include "base_object.h" #include "vector.h" #include "util/loading/resource_manager.h" #include "movie_player.h" #include #include using namespace std; // Constructor BspFile::BspFile() {} /** * Loads a quake3 level (*.bsp) * @param name the Name of the *.bsp file */ int BspFile::read(const char* name) { int offset; int size; struct stat results; if (stat( name , &results) == 0) { PRINTF(0)("BSP FILE: Datei %s gefunden. \n", name); ifstream bspFile (name, ios::in | ios::binary); bspFile.read(this->header, 260); PRINTF(0)("BSP FILE: BSPVersion: %i. \n", ((int *)(header) )[1]); if(SDL_SwapLE32(((int *)(header) )[1]) == 46) PRINTF(0)("BSP FILE: This is the good one! :-) \n"); else PRINTF(0)("BSP FILE: Wrong BSPVersion.\n"); //!< now, we should do some error handling // Get the Nodes offset = SDL_SwapLE32(((int *)(header) )[8]); size = SDL_SwapLE32(((int *)(header))[9]); PRINTF(4)("BSP FILE: NodeSize: %i Bytes. \n", size); PRINTF(4)("BSP FILE: NumNodes: %i. \n", size / sizeof(node)); PRINTF(4)("BSP FILE: Remainder: %i. \n", size % sizeof(node)); PRINTF(4)("BSP FILE: NodeOffset: %i. \n", offset); this->numNodes = size/sizeof(node); this->nodes = new node [this->numNodes]; bspFile.seekg(offset); bspFile.read((char*)this->nodes, size); // and their Planes offset = SDL_SwapLE32(((int *)(header) )[6]); size = SDL_SwapLE32(((int *)(header))[7]); PRINTF(4)("BSP FILE: PlanesSize: %i Bytes. \n", size); PRINTF(4)("BSP FILE: NumPlanes: %i. \n", size / sizeof(plane)); PRINTF(4)("BSP FILE: Remainder: %i. \n", sizeof(plane)); PRINTF(4)("BSP FILE: PlanesOffset: %i. \n", offset); this->numPlanes = size/sizeof(plane); this->planes = new plane [this->numPlanes]; bspFile.seekg(offset); bspFile.read((char*)this->planes, size); // Get the Leafs offset = SDL_SwapLE32(((int *)(header) )[10]); size = SDL_SwapLE32(((int *)(header))[11]); PRINTF(4)("BSP FILE: LeaveSize: %i Bytes. \n", size); PRINTF(4)("BSP FILE: NumLeaves: %i. \n", size / sizeof(leaf)); PRINTF(4)("BSP FILE: Remainder: %i. \n", size % sizeof(leaf)); PRINTF(4)("BSP FILE: LeaveOffset: %i. \n", offset); this->numLeafs = size/sizeof(leaf); this->leaves = new leaf [this->numLeafs]; bspFile.seekg(offset); bspFile.read((char*)this->leaves, size); // Get the Models offset = SDL_SwapLE32(((int *)(header))[16]); size = SDL_SwapLE32(((int *)(header))[17]); PRINTF(4)("BSP FILE: ModelsSize: %i Bytes. \n", size); PRINTF(4)("BSP FILE: NumModels: %i. \n", size / sizeof(model)); PRINTF(4)("BSP FILE: Remainder: %i. \n", size % sizeof(model)); PRINTF(4)("BSP FILE: ModelsOffset: %i. \n", offset); this->numBspModels = size/sizeof(model); this->bspModels = new model [this->numBspModels]; bspFile.seekg(offset); bspFile.read((char*)this->bspModels, size); // Get the leafFaces offset = SDL_SwapLE32(((int *)(header))[12]); size = SDL_SwapLE32(((int *)(header))[13]); PRINTF(4)("BSP FILE: leafFacesSize: %i Bytes. \n", size); PRINTF(4)("BSP FILE: NumleafFaces: %i. \n", size / 4); PRINTF(4)("BSP FILE: Remainder: %i. \n", size % 4); PRINTF(4)("BSP FILE: leafFacesOffset: %i. \n", offset); this->numLeafFaces = size/4; this->leafFaces = new char [size]; bspFile.seekg(offset); bspFile.read(this->leafFaces, size); // Get the leafBrushes offset = SDL_SwapLE32(((int *)(header))[14]); size = SDL_SwapLE32(((int *)(header))[15]); PRINTF(4)("BSP FILE: leafBrushesSize: %i Bytes. \n", size); PRINTF(4)("BSP FILE: NumleafBrushes: %i. \n", size / 4); PRINTF(4)("BSP FILE: Remainder: %i. \n", size % 4); PRINTF(4)("BSP FILE: leafBrushesOffset: %i. \n", offset); this->numLeafBrushes = size/4; this->leafBrushes = new char [size]; bspFile.seekg(offset); bspFile.read(this->leafBrushes, size); // Get the brushes offset = SDL_SwapLE32(((int *)(header))[18]); size = SDL_SwapLE32(((int *)(header))[19]); PRINTF(4)("BSP FILE: BrushesSize: %i Bytes. \n", size); PRINTF(4)("BSP FILE: NumBrushes: %i. \n", size / sizeof(brush)); PRINTF(4)("BSP FILE: Remainder: %i. \n", size % sizeof(brush)); PRINTF(4)("BSP FILE: BrushesOffset: %i. \n", offset); this->brushes = new brush [size/sizeof(brush)]; bspFile.seekg(offset); bspFile.read((char*)this->brushes, size); // Get the brushSides offset = SDL_SwapLE32(((int *)(header))[20]); size = SDL_SwapLE32(((int *)(header))[21]); PRINTF(4)("BSP FILE: BrushSidesSize: %i Bytes. \n", size); PRINTF(4)("BSP FILE: NumBrushSides: %i. \n", size / 8); PRINTF(4)("BSP FILE: Remainder: %i. \n", size % 8); PRINTF(4)("BSP FILE: BrushSidesOffset: %i. \n", offset); this->numBrushSides = size/sizeof(brushside); this->brushSides = new brushside [this->numBrushSides]; bspFile.seekg(offset); bspFile.read((char*)this->brushSides, size); // Get the Vertice offset = SDL_SwapLE32(((int *)(header))[22]); size = SDL_SwapLE32(((int *)(header))[23]); PRINTF(4)("BSP FILE: VerticeSize: %i Bytes. \n", size); PRINTF(4)("BSP FILE: NumVertice: %i. \n", size / 44); PRINTF(4)("BSP FILE: Remainder: %i. \n", size % 44); PRINTF(4)("BSP FILE: VerticeOffset: %i. \n", offset); this->numVertex = size/44; this->vertice = new char [size]; bspFile.seekg(offset); bspFile.read(this->vertice, size); // Get the MeshVerts offset = SDL_SwapLE32(((int *)(header))[24]); size = SDL_SwapLE32(((int *)(header))[25]); PRINTF(4)("BSP FILE: MeshVertsSize: %i Bytes. \n", size); PRINTF(4)("BSP FILE: NumMeshVerts: %i. \n", size / sizeof(meshvert)); PRINTF(4)("BSP FILE: Remainder: %i. \n", size % sizeof(meshvert)); PRINTF(4)("BSP FILE: MeshVertsOffset: %i. \n", offset); this->meshverts = new meshvert [size / sizeof(meshvert)]; bspFile.seekg(offset); bspFile.read((char*)this->meshverts, size); // Get the Faces offset = SDL_SwapLE32(((int *)(header))[28]); size = SDL_SwapLE32(((int *)(header))[29]); PRINTF(4)("BSP FILE: FacesSize: %i Bytes. \n", size); PRINTF(4)("BSP FILE: NumFaces: %i. \n", size / sizeof(face)); PRINTF(4)("BSP FILE: Remainder: %i. \n", size % sizeof(face)); PRINTF(4)("BSP FILE: FacesOffset: %i. \n", offset); this->numFaces = size/sizeof(face); this->faces = new face [this->numFaces]; bspFile.seekg(offset); bspFile.read((char*)this->faces, size); //Get the lightmaps offset = SDL_SwapLE32(((int *)(header))[30]); size = SDL_SwapLE32(((int *)(header))[31]); this->numLightMaps = size/ sizeof(lightmap); this->lightMaps = new lightmap [this->numLightMaps]; bspFile.seekg(offset); bspFile.read((char*)this->lightMaps, size); // Get the Visdata offset = SDL_SwapLE32(((int *)(header))[34]); size = SDL_SwapLE32(((int *)(header))[35]); this->visData = new char [size]; bspFile.seekg(offset); bspFile.read(this->visData, size); PRINTF(4)("BSP FILE: VisDataSize: %i Bytes. \n", size); PRINTF(4)("BSP FILE: NumVisData: %i. \n", size /1 - 8); PRINTF(4)("BSP FILE: Remainder: %i. \n", size % 1); PRINTF(4)("BSP FILE: VisDataOffset: %i. \n", offset); // Get the Textures offset = SDL_SwapLE32(((int *)(header))[4]); size = SDL_SwapLE32(((int *)(header))[5]); this->textures= new char [size]; bspFile.seekg(offset); bspFile.read(this->textures, size); PRINTF(4)("BSP FILE: TextureSize: %i Bytes. \n", size); PRINTF(4)("BSP FILE: NumTextures: %i. \n", size /72); PRINTF(4)("BSP FILE: Remainder: %i. \n", size % 72); PRINTF(4)("BSP FILE: TextureOffset: %i. \n", offset); this->numTextures = size/72; bspFile.close(); for(int i = 0 ; i < this->numTextures; i++) PRINTF(4)("BSP FILE: Texture 0: %s. \n", &this->textures[8+ 72*i]); this->load_textures(); // Load the lightMaps this->glLightMapTextures = new GLuint[this->numLightMaps]; for(int i = 0; i < this->numLightMaps; i++) this->glLightMapTextures[i] = this->loadLightMapToGL(this->lightMaps[i]); //Create white texture for if no lightmap specified glGenTextures(1, &this->whiteLightMap); glBindTexture(GL_TEXTURE_2D, this->whiteLightMap); //Create texture this->whiteTexture[0]=255; this->whiteTexture[1]=255; this->whiteTexture[2]=255; glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_PRIORITY, 2); /* control the mipmap levels */ glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_MIN_LOD, 5); glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_MAX_LOD, 0); /* build the Texture OpenGL V >= 1.1 */ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 1, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, (const GLvoid *)&(this->whiteTexture)); gluBuild2DMipmaps( GL_TEXTURE_2D, GL_RGBA8, 1, 1, GL_RGB, GL_UNSIGNED_BYTE,(const GLvoid *) &(this->whiteTexture)); // Get the number of patches this->numPatches = 0; this->patchOffset = 0; for( int i = 0; i < this->numFaces; i++) { face& cFace = ((face *)(this->faces))[i]; if (SDL_SwapLE32(cFace.type) == 2) this->numPatches += (SDL_SwapLE32(cFace.size[0]) -1 ) / 2 * (SDL_SwapLE32(cFace.size[1]) -1) / 2; } // Allocate Memory this->patchVertice = new char[8*8*44*(this->numPatches+10)]; this->patchIndexes = new char[7*8*2*4*(this->numPatches+10)]; this->patchRowIndexes = new int*[7*4*this->numPatches]; this->patchTrianglesPerRow = new char[7*4*this->numPatches]; this->VertexArrayModels = new VertexArrayModel*[this->numPatches]; PRINTF(0)("BSP FILE:NumberOfPatches: %i . \n", numPatches); this->swapAllBspCoordinates(); // Do tesselation for all Faces of type 2 for( int i = 0; i < this->numFaces; i++) { if (SDL_SwapLE32((this->faces)[i].type) == 2) this->tesselate(i); } PRINTF(0)("BSP FILE:PatchOffset: %i . \n", this->patchOffset); return 1; } else { PRINTF(0)("BSP FILE: Datei nicht gefunden. \n"); return -1; } } void BspFile::build_tree() { PRINTF(0)("BSP FILE:\n"); PRINTF(0)("BSP FILE: Building Tree...\n"); root = this->build_tree_rec(0); PRINTF(0)("BSP FILE: ...done. \n"); PRINTF(0)("BSP FILE: \n"); PRINTF(0)("BSP FILE: Node #0: \n"); PRINTF(0)("BSP FILE: x: %f \n",root->plane.x); PRINTF(0)("BSP FILE: y: %f\n",root->plane.y); PRINTF(0)("BSP FILE: z: %f\n",root->plane.z); } /** * Called by BspFile::build_tree() only. */ BspTreeNode* BspFile::build_tree_rec(int i) { // PRINTF(0)("BSP FILE: Node #%i\n", i); BspTreeNode* thisNode = new BspTreeNode(); int left =(((node *) nodes) [i]).left; int right =(((node *) nodes) [i]).right; int planeIndex = (((node *) nodes) [i]).plane; float x1 =(((plane *) this->planes) [planeIndex]).x; float y1 =(((plane *) this->planes) [planeIndex]).y; float z1 =(((plane *) this->planes) [planeIndex]).z; thisNode->leafIndex = 0; thisNode->d = (((plane *) this->planes) [planeIndex]).d; thisNode->plane = Vector(x1,y1,z1); thisNode->isLeaf = false; if(left >= 0) { thisNode->left = this->build_tree_rec(left); } else { //BspTreeLeaf tmp = BspTreeLeaf(); //tmp.isLeaf = true; //tmp.leafIndex = -left -1; //thisNode->left = (BspTreeNode*) (&tmp); thisNode->left = new BspTreeNode(); thisNode->left->isLeaf = true; thisNode->left->leafIndex = - (left +1); //PRINTF(0)("BSP FILE: LeafIndex: %i\n",-left); } // assign leav if(right >= 0) { thisNode->right = this->build_tree_rec(right); } else { //BspTreeLeaf tmp = BspTreeLeaf(); //tmp.isLeaf = true; //tmp.leafIndex = -right -1; //thisNode->right = (BspTreeNode*) (&tmp); thisNode->right = new BspTreeNode(); thisNode->right->isLeaf = true; thisNode->right->leafIndex = -(right +1); //PRINTF(0)("BSP FILE: LeafIndex: %i\n",-right); } // assign leaf return thisNode; } /** * returns the root node of the bsp-tree */ BspTreeNode* BspFile::get_root() { return root; } void BspFile::load_textures() { ::std::string absFileName; char fileName [228]; char ext [100]; struct stat results; this->Materials = new AMat[this->numTextures]; for(int i = 0 ; i < this->numTextures; i++) { PRINTF(0)("BSP FILE: Texture : %s. \n", &this->textures[8+ 72*i]); strcpy(fileName, &this->textures[8+ 72*i]); if(strlen(fileName) == 0) { // Default Material this->Materials[i].mat = new Material(); this->Materials[i].mat->setDiffuse(0.1,0.1,1.0); this->Materials[i].mat->setAmbient(0.1,0.1,1.0 ); this->Materials[i].mat->setSpecular(0.4,0.4,1.0); //this->Materials[i]->setShininess(100.0); // this->Materials[i].mat->setTransparency(1.0); this->Materials[i].mat->setDiffuseMap("pictures/ground.tga"); this->Materials[i].mat->setAmbientMap("pictures/ground.tga"); this->Materials[i].mat->setSpecularMap("pictures/ground.tga"); this->Materials[i].alpha = false; this->Materials[i].animated = false; continue; } // Check for mov strcpy(fileName, &this->textures[8+ 72*i]); strcpy(ext, ".mov"); strncat (fileName, ext, strlen(fileName)); absFileName = ResourceManager::getFullName(fileName); if(ResourceManager::isFile(absFileName)) { PRINTF(0)("BSP FILE: gefunden . \n"); this->Materials[i] = this->loadAVI(fileName); continue; } // Check for avi strcpy(fileName, &this->textures[8+ 72*i]); strcpy(ext, ".avi"); strncat (fileName, ext, strlen(fileName)); absFileName = ResourceManager::getFullName(fileName); if(ResourceManager::isFile(absFileName)) { PRINTF(0)("BSP FILE: gefunden . \n"); this->Materials[i] = this->loadAVI(fileName); continue; } // Check for mpg strcpy(fileName, &this->textures[8+ 72*i]); strcpy(ext, ".mpg"); strncat (fileName, ext, strlen(fileName)); absFileName = ResourceManager::getFullName(fileName); if(ResourceManager::isFile(absFileName)) { PRINTF(0)("BSP FILE: gefunden . \n"); this->Materials[i] = this->loadAVI(fileName); continue; } // Check for tga strcpy(fileName, &this->textures[8+ 72*i]); strcpy(ext, ".tga"); strncat (fileName, ext, strlen(fileName)); absFileName = ResourceManager::getFullName(fileName); if(ResourceManager::isFile(absFileName)) { PRINTF(0)("BSP FILE: gefunden . \n"); this->Materials[i] = this->loadMat(fileName); continue; } // Check for TGA strcpy(fileName, &this->textures[8+ 72*i]); strcpy(ext, ".TGA"); strncat (fileName, ext, strlen(fileName)); absFileName = ResourceManager::getFullName(fileName); if(ResourceManager::isFile(absFileName)/*stat( absFileName.c_str() , &results) == 0*/) { PRINTF(0)("BSP FILE: gefunden . \n"); this->Materials[i] = this->loadMat(fileName); continue; } // Check for jpg strcpy(fileName, &this->textures[8+ 72*i]); strcpy(ext, ".jpg"); strncat (fileName, ext, strlen(fileName)); absFileName = ResourceManager::getFullName(fileName); if(ResourceManager::isFile(absFileName)) { PRINTF(0)("BSP FILE: gefunden . \n"); this->Materials[i] =this->loadMat(fileName); continue; } // Check for JPG strcpy(fileName, &this->textures[8+ 72*i]); strcpy(ext, ".JPG"); strncat (fileName, ext, strlen(fileName)); absFileName = ResourceManager::getFullName(fileName); if(ResourceManager::isFile(absFileName)) { PRINTF(0)("BSP FILE: gefunden . \n"); this->Materials[i] =this->loadMat(fileName); continue; } // Check for bmp strcpy(fileName, &this->textures[8+ 72*i]); strcpy(ext, ".bmp"); strncat (fileName, ext, strlen(fileName)); absFileName = ResourceManager::getFullName(fileName); if(ResourceManager::isFile(absFileName)) { PRINTF(0)("BSP FILE: gefunden . \n"); this->Materials[i] =this->loadMat(fileName); continue; } // Check for BMP strcpy(fileName, &this->textures[8+ 72*i]); strcpy(ext, ".BMP"); strncat (fileName, ext, strlen(fileName)); absFileName = ResourceManager::getFullName(fileName); if(ResourceManager::isFile(absFileName)) { PRINTF(0)("BSP FILE: gefunden . \n"); this->Materials[i] = this->loadMat(fileName); continue; } // Default Material this->Materials[i].mat = new Material(); this->Materials[i].mat->setDiffuse(0.1,0.1,0.1); this->Materials[i].mat->setAmbient(0.1,0.1,0.1 ); this->Materials[i].mat->setSpecular(0.4,0.4,0.4); //this->Materials[i]->setShininess(100.0); //this->Materials[i].mat->setTransparency(1.0); this->Materials[i].mat->setDiffuseMap("pictures/error_texture.png"); this->Materials[i].mat->setAmbientMap("pictures/error_texture.png"); this->Materials[i].mat->setSpecularMap("pictures/error_texture.png"); this->Materials[i].alpha = true; this->Materials[i].animated = false; } } AMat BspFile::loadMat(char* mat) { AMat tmpAMat; this->testSurf = NULL; this->testSurf = IMG_Load(ResourceManager::getFullName(mat).c_str()); if(this->testSurf != NULL) { if(this->testSurf->format->Amask != 0 ) tmpAMat.alpha = true; else tmpAMat.alpha = false; } else tmpAMat.alpha = false; Material* tmp = new Material(); tmp->setDiffuse(1.0,1.0,1.0); tmp->setAmbient(1.0,1.0,1.0 ); tmp->setSpecular(1.0,1.0,1.0); // tmp->setShininess(.5); // tmp->setTransparency(0.0); tmp->setDiffuseMap(mat); tmpAMat.mat = tmp; tmpAMat.animated = false; return tmpAMat; } AMat BspFile::loadAVI(char* mat) { AMat tmpAMat; MoviePlayer * testMC = new MoviePlayer(mat); testMC->start(0); //Material* tmp = new Material(); // tmp->setDiffuse(1.0,1.0,1.0); //tmp->setAmbient(1.0,1.0,1.0 ); //tmp->setSpecular(1.0,1.0,1.0); // tmp->setShininess(.5);tmpAMat // tmp->setTransparency(0.0); //tmp->setDiffuseMap(mat); tmpAMat.aviMat = testMC; tmpAMat.animated = true; tmpAMat.alpha = true; return tmpAMat; } unsigned int BspFile::loadLightMapToGL(lightmap& lightMapTexture) { int errorCode = 0; //!< the error code for the texture loading functions unsigned int lightMap; //!< the OpenGL texture handle int mipmapLevel = 0; //!< the maximum mipmap level for this texture int mipmapWidth = 0; //!< the width of the mipmap int mipmapHight = 0; //!< the height of the mipmap float sc, scale, temp; for(int i = 0; i < 128*128*3 ; i++) { sc = ((unsigned char *)(&lightMapTexture))[i]; sc *= 1/255.0; scale = 1.0; if(sc > 1.0f && (temp = (1.0f/sc)) < scale) scale=temp; scale*=255.0; sc*=scale; if(sc <= 180) ((unsigned char *)(&lightMapTexture))[i] = (unsigned char)sc + 75; else ((unsigned char *)(&lightMapTexture))[i] = (unsigned char)sc; } glGenTextures(1, &lightMap); glBindTexture(GL_TEXTURE_2D, lightMap); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_PRIORITY, 2); /* control the mipmap levels */ glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_MIN_LOD, 5); glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_MAX_LOD, 0); /* build the Texture OpenGL V >= 1.1 */ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 128, 128, 0, GL_RGB, GL_UNSIGNED_BYTE, (const GLvoid *)&lightMapTexture); // build the MipMaps automaticaly errorCode = gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA8, 128, 128, GL_RGB, GL_UNSIGNED_BYTE, (const GLvoid *)&lightMapTexture ); return lightMap; } /** * Generates a vertex-array, a indice-array and a texture-coordinates-array for iface. * @param iface integer index of face * @todo cleanup this function, let the user choose the level of tesselation */ void BspFile::tesselate(int iface) { face* Face = &((this->faces)[iface]); BspVertex * BspVrtx = (BspVertex*)this->vertice; int level = 7; int level1 = 8; int size0 = (Face->size[0]); int size1 = (Face->size[1]); // For each patch... for(int i = 0; i < ( size0 - 1) ; i+=2) { for(int j = 0; j < ( size1 -1) ; j+=2) { // Make a patch... // Get controls[9]; BspVec controls[9]; BspVec controlsTmp[9]; BspVertex VControls[9]; for(int k = 0; k < 3; k++) { for(int l = 0; l < 3; l++) { controls[k +3*l]. position[0] = ( BspVrtx[Face->vertex + (j * size0)+ i + l+ size0*k].position[0]); controls[k +3*l]. position[1] = ( BspVrtx[Face->vertex + (j * size0)+ i +l+ size0*k].position[1]); controls[k +3*l]. position[2] = ( BspVrtx[Face->vertex + (j * size0)+ i + l+ size0*k].position[2]); /*Face->n_vertexes*/ controlsTmp[2-k +6-3*l]. position[0] = ( BspVrtx[Face->vertex +( j * size0)+ i + l+ size0*k].position[0]); controlsTmp[2-k +6-3*l]. position[1] = ( BspVrtx[Face->vertex + (j * size0)+ i +l+ size0*k].position[1]); controlsTmp[2-k +6-3*l]. position[2] = ( BspVrtx[Face->vertex + (j * size0)+ i + l+ size0*k].position[2]); /*Face->n_vertexes*/ VControls[k +3*l]. position[0] = ( BspVrtx[Face->vertex +( j * size0)+ i + l+ size0*k].position[0]); VControls[k +3*l]. position[1] = ( BspVrtx[Face->vertex + (j * size0)+ i +l+ size0*k].position[1]); VControls[k +3*l]. position[2] = ( BspVrtx[Face->vertex + (j * size0)+ i + l+ size0*k].position[2]); VControls[k +3*l]. normal[0] = ( BspVrtx[Face->vertex +( j * size0)+ i + l+ size0*k].normal[0]); VControls[k +3*l]. normal[1] = ( BspVrtx[Face->vertex + (j * size0)+ i +l+ size0*k].normal[1]); VControls[k +3*l]. normal[2] = ( BspVrtx[Face->vertex + (j * size0)+ i + l+ size0*k].normal[2]); VControls[k +3*l]. texcoord[0][0]= ( BspVrtx[Face->vertex +( j * size0)+ i + l+ size0*k].texcoord[0][0]); VControls[k +3*l]. texcoord[0][1] = ( BspVrtx[Face->vertex + (j * size0)+ i +l+ size0*k].texcoord[0][1]); VControls[k +3*l]. texcoord[1][0] = ( BspVrtx[Face->vertex +( j * size0)+ i + l+ size0*k].texcoord[1][0]); VControls[k +3*l]. texcoord[1][1] = ( BspVrtx[Face->vertex + (j * size0)+ i +l+ size0*k].texcoord[1][1]); } } //*********************************************************************************************************************** // Compute the vertice //*********************************************************************************************************************** float px, py; BspVertex temp[3]; BspVertex* Vertice = &(((BspVertex*)this->patchVertice)[level1*level1*this->patchOffset]); for(int v=0; v<=level; ++v) { px=(float)v/level; Vertice[v].position[0]=VControls[0].position[0]*((1.0f-px)*(1.0f-px))+VControls[3].position[0]*((1.0f-px)*px*2)+VControls[6].position[0]*(px*px); Vertice[v].position[1]=VControls[0].position[1]*((1.0f-px)*(1.0f-px))+VControls[3].position[1]*((1.0f-px)*px*2)+VControls[6].position[1]*(px*px); Vertice[v].position[2]=VControls[0].position[2]*((1.0f-px)*(1.0f-px))+VControls[3].position[2]*((1.0f-px)*px*2)+VControls[6].position[2]*(px*px); Vertice[v].normal[0]=VControls[0].normal[0]*((1.0f-px)*(1.0f-px))+VControls[3].normal[0]*((1.0f-px)*px*2)+VControls[6].normal[0]*(px*px); Vertice[v].normal[1]=VControls[0].normal[1]*((1.0f-px)*(1.0f-px))+VControls[3].normal[1]*((1.0f-px)*px*2)+VControls[6].normal[1]*(px*px); Vertice[v].normal[2]=VControls[0].normal[2]*((1.0f-px)*(1.0f-px))+VControls[3].normal[2]*((1.0f-px)*px*2)+VControls[6].normal[2]*(px*px); Vertice[v].texcoord[0][0]=VControls[0].texcoord[0][0]*((1.0f-px)*(1.0f-px))+VControls[3].texcoord[0][0]*((1.0f-px)*px*2)+VControls[6].texcoord[0][0]*(px*px); Vertice[v].texcoord[0][1]=VControls[0].texcoord[0][1]*((1.0f-px)*(1.0f-px))+VControls[3].texcoord[0][1]*((1.0f-px)*px*2)+VControls[6].texcoord[0][1]*(px*px); Vertice[v].texcoord[1][0]=VControls[0].texcoord[1][0]*((1.0f-px)*(1.0f-px))+VControls[3].texcoord[1][0]*((1.0f-px)*px*2)+VControls[6].texcoord[1][0]*(px*px); Vertice[v].texcoord[1][1]=VControls[0].texcoord[1][1]*((1.0f-px)*(1.0f-px))+VControls[3].texcoord[1][1]*((1.0f-px)*px*2)+VControls[6].texcoord[1][1]*(px*px); } for(int u=1; u<=level; ++u) { py=(float)u/level; // temp[0]=controlPoints[0]*((1.0f-py)*(1.0f-py))+ controlPoints[1]*((1.0f-py)*py*2)+ controlPoints[2]*(py*py); temp[0].position[0]=VControls[0].position[0]*((1.0f-py)*(1.0f-py))+VControls[1].position[0]*((1.0f-py)*py*2)+VControls[2].position[0]*(py*py); temp[0].position[1]=VControls[0].position[1]*((1.0f-py)*(1.0f-py))+VControls[1].position[1]*((1.0f-py)*py*2)+VControls[2].position[1]*(py*py); temp[0].position[2]=VControls[0].position[2]*((1.0f-py)*(1.0f-py))+VControls[1].position[2]*((1.0f-py)*py*2)+VControls[2].position[2]*(py*py); temp[0].normal[0]=VControls[0].normal[0]*((1.0f-py)*(1.0f-py))+VControls[1].normal[0]*((1.0f-py)*py*2)+VControls[2].normal[0]*(py*py); temp[0].normal[1]=VControls[0].normal[1]*((1.0f-py)*(1.0f-py))+VControls[1].normal[1]*((1.0f-py)*py*2)+VControls[2].normal[1]*(py*py); temp[0].normal[2]=VControls[0].normal[2]*((1.0f-py)*(1.0f-py))+VControls[1].normal[2]*((1.0f-py)*py*2)+VControls[2].normal[2]*(py*py); temp[0].texcoord[0][0]=VControls[0].texcoord[0][0]*((1.0f-py)*(1.0f-py))+VControls[1].texcoord[0][0]*((1.0f-py)*py*2)+VControls[2].texcoord[0][0]*(py*py); temp[0].texcoord[0][1]=VControls[0].texcoord[0][1]*((1.0f-py)*(1.0f-py))+VControls[1].texcoord[0][1]*((1.0f-py)*py*2)+VControls[2].texcoord[0][1]*(py*py); temp[0].texcoord[1][0]=VControls[0].texcoord[1][0]*((1.0f-py)*(1.0f-py))+VControls[1].texcoord[1][0]*((1.0f-py)*py*2)+VControls[2].texcoord[1][0]*(py*py); temp[0].texcoord[1][1]=VControls[0].texcoord[1][1]*((1.0f-py)*(1.0f-py))+VControls[1].texcoord[1][1]*((1.0f-py)*py*2)+VControls[2].texcoord[1][1]*(py*py); // temp[1]=controlPoints[3]*((1.0f-py)*(1.0f-py))+ controlPoints[4]*((1.0f-py)*py*2)+ controlPoints[5]*(py*py); temp[1].position[0]=VControls[3].position[0]*((1.0f-py)*(1.0f-py))+VControls[4].position[0]*((1.0f-py)*py*2)+VControls[5].position[0]*(py*py); temp[1].position[1]=VControls[3].position[1]*((1.0f-py)*(1.0f-py))+VControls[4].position[1]*((1.0f-py)*py*2)+VControls[5].position[1]*(py*py); temp[1].position[2]=VControls[3].position[2]*((1.0f-py)*(1.0f-py))+VControls[4].position[2]*((1.0f-py)*py*2)+VControls[5].position[2]*(py*py); temp[1].normal[0]=VControls[3].normal[0]*((1.0f-py)*(1.0f-py))+VControls[4].normal[0]*((1.0f-py)*py*2)+VControls[5].normal[0]*(py*py); temp[1].normal[1]=VControls[3].normal[1]*((1.0f-py)*(1.0f-py))+VControls[4].normal[1]*((1.0f-py)*py*2)+VControls[5].normal[1]*(py*py); temp[1].normal[2]=VControls[3].normal[2]*((1.0f-py)*(1.0f-py))+VControls[4].normal[2]*((1.0f-py)*py*2)+VControls[5].normal[2]*(py*py); temp[1].texcoord[0][0]=VControls[3].texcoord[0][0]*((1.0f-py)*(1.0f-py))+VControls[4].texcoord[0][0]*((1.0f-py)*py*2)+VControls[5].texcoord[0][0]*(py*py); temp[1].texcoord[0][1]=VControls[3].texcoord[0][1]*((1.0f-py)*(1.0f-py))+VControls[4].texcoord[0][1]*((1.0f-py)*py*2)+VControls[5].texcoord[0][1]*(py*py); temp[1].texcoord[1][0]=VControls[3].texcoord[1][0]*((1.0f-py)*(1.0f-py))+VControls[4].texcoord[1][0]*((1.0f-py)*py*2)+VControls[5].texcoord[1][0]*(py*py); temp[1].texcoord[1][1]=VControls[3].texcoord[1][1]*((1.0f-py)*(1.0f-py))+VControls[4].texcoord[1][1]*((1.0f-py)*py*2)+VControls[5].texcoord[1][1]*(py*py); // temp[2]=controlPoints[6]*((1.0f-py)*(1.0f-py))+controlPoints[7]*((1.0f-py)*py*2)+controlPoints[8]*(py*py); temp[2].position[0]=VControls[6].position[0]*((1.0f-py)*(1.0f-py))+VControls[7].position[0]*((1.0f-py)*py*2)+VControls[8].position[0]*(py*py); temp[2].position[1]=VControls[6].position[1]*((1.0f-py)*(1.0f-py))+VControls[7].position[1]*((1.0f-py)*py*2)+VControls[8].position[1]*(py*py); temp[2].position[2]=VControls[6].position[2]*((1.0f-py)*(1.0f-py))+VControls[7].position[2]*((1.0f-py)*py*2)+VControls[8].position[2]*(py*py); temp[2].normal[0]=VControls[6].normal[0]*((1.0f-py)*(1.0f-py))+VControls[7].normal[0]*((1.0f-py)*py*2)+VControls[8].normal[0]*(py*py); temp[2].normal[1]=VControls[6].normal[1]*((1.0f-py)*(1.0f-py))+VControls[7].normal[1]*((1.0f-py)*py*2)+VControls[8].normal[1]*(py*py); temp[2].normal[2]=VControls[6].normal[2]*((1.0f-py)*(1.0f-py))+VControls[7].normal[2]*((1.0f-py)*py*2)+VControls[8].normal[2]*(py*py); temp[2].texcoord[0][0]=VControls[6].texcoord[0][0]*((1.0f-py)*(1.0f-py))+VControls[7].texcoord[0][0]*((1.0f-py)*py*2)+VControls[8].texcoord[0][0]*(py*py); temp[2].texcoord[0][1]=VControls[6].texcoord[0][1]*((1.0f-py)*(1.0f-py))+VControls[7].texcoord[0][1]*((1.0f-py)*py*2)+VControls[8].texcoord[0][1]*(py*py); temp[2].texcoord[1][0]=VControls[6].texcoord[1][0]*((1.0f-py)*(1.0f-py))+VControls[7].texcoord[1][0]*((1.0f-py)*py*2)+VControls[8].texcoord[1][0]*(py*py); temp[2].texcoord[1][1]=VControls[6].texcoord[1][1]*((1.0f-py)*(1.0f-py))+VControls[7].texcoord[1][1]*((1.0f-py)*py*2)+VControls[8].texcoord[1][1]*(py*py); for(int v=0; v<=level; ++v) { px=(float)v/level; //Vertice[u*(tesselation+1)+v]= temp[0]*((1.0f-px)*(1.0f-px))+ temp[1]*((1.0f-px)*px*2)+ temp[2]*(px*px); Vertice[u*(level1)+v].position[0]=temp[0].position[0]*((1.0f-px)*(1.0f-px))+temp[1].position[0]*((1.0f-px)*px*2)+temp[2].position[0]*(px*px); Vertice[u*(level1)+v].position[1]=temp[0].position[1]*((1.0f-px)*(1.0f-px))+temp[1].position[1]*((1.0f-px)*px*2)+temp[2].position[1]*(px*px); Vertice[u*(level1)+v].position[2]=temp[0].position[2]*((1.0f-px)*(1.0f-px))+temp[1].position[2]*((1.0f-px)*px*2)+temp[2].position[2]*(px*px); Vertice[u*(level1)+v].normal[0]=temp[0].normal[0]*((1.0f-px)*(1.0f-px))+temp[1].normal[0]*((1.0f-px)*px*2)+temp[2].normal[0]*(px*px); Vertice[u*(level1)+v].normal[1]=temp[0].normal[1]*((1.0f-px)*(1.0f-px))+temp[1].normal[1]*((1.0f-px)*px*2)+temp[2].normal[1]*(px*px); Vertice[u*(level1)+v].normal[2]=temp[0].normal[2]*((1.0f-px)*(1.0f-px))+temp[1].normal[2]*((1.0f-px)*px*2)+temp[2].normal[2]*(px*px); Vertice[u*(level1)+v].texcoord[0][0]=temp[0].texcoord[0][0]*((1.0f-px)*(1.0f-px))+temp[1].texcoord[0][0]*((1.0f-px)*px*2)+temp[2].texcoord[0][0]*(px*px); Vertice[u*(level1)+v].texcoord[0][1]=temp[0].texcoord[0][1]*((1.0f-px)*(1.0f-px))+temp[1].texcoord[0][1]*((1.0f-px)*px*2)+temp[2].texcoord[0][1]*(px*px); Vertice[u*(level1)+v].texcoord[1][0]=temp[0].texcoord[1][0]*((1.0f-px)*(1.0f-px))+temp[1].texcoord[1][0]*((1.0f-px)*px*2)+temp[2].texcoord[1][0]*(px*px); Vertice[u*(level1)+v].texcoord[1][1]=temp[0].texcoord[1][1]*((1.0f-px)*(1.0f-px))+temp[1].texcoord[1][1]*((1.0f-px)*px*2)+temp[2].texcoord[1][1]*(px*px); } } //Create indices GLuint* indices= & ((GLuint*)(this->patchIndexes))[level*level1*2*this->patchOffset]; for(int row=0; rowVertexArrayModels[this->patchOffset] = new VertexArrayModel(); VertexArrayModel* tmp = this->VertexArrayModels[this->patchOffset]; tmp->newStripe(); int a = 0; int b = -1; tmp->addVertex(controlsTmp[0].position[0],controlsTmp[0].position[1], controlsTmp[0].position[2]); tmp->addNormal(1,0,0); tmp->addTexCoor(0.0,0.0); tmp->addColor(0.3,0.0,0.0); tmp->addIndice(1+b); tmp->addIndice(4+a); tmp->addVertex(controlsTmp[1].position[0],controlsTmp[1].position[1], controlsTmp[1].position[2]); tmp->addNormal(1,0,0); tmp->addTexCoor(0.0,0.4); tmp->addColor(0.3,0.0,0.0); tmp->addIndice(2+b); tmp->addIndice(5+a); tmp->addVertex(controlsTmp[2].position[0],controlsTmp[2].position[1], controlsTmp[2].position[2]); tmp->addNormal(1,0,0); tmp->addTexCoor(0.0,1.0); tmp->addColor(0.1,0.0,0.0); tmp->addIndice(3+b); tmp->addIndice(6+a); tmp->addVertex(controlsTmp[2].position[0],controlsTmp[2].position[1], controlsTmp[2].position[2]); tmp->addNormal(1,0,0); tmp->addTexCoor(0.0,1.0); tmp->addColor(0.1,0.0,0.0); tmp->addIndice(7+a); //tmp->addIndice(6); tmp->newStripe(); tmp->addVertex(controlsTmp[0].position[0],controlsTmp[0].position[1], controlsTmp[0].position[2]); tmp->addNormal(1,0,0); tmp->addTexCoor(0.0,0.0); tmp->addColor(0.1,0.1,0.1); tmp->addIndice(5+b); tmp->addIndice(8+a); tmp->addVertex(controlsTmp[1].position[0],controlsTmp[1].position[1], controlsTmp[1].position[2]); tmp->addNormal(1,0,0); tmp->addTexCoor(0.0,0.4); tmp->addColor(0.1,0.1,0.1); tmp->addIndice(6+b); tmp->addIndice(9+a); tmp->addVertex(controlsTmp[2].position[0],controlsTmp[2].position[1], controlsTmp[2].position[2]); tmp->addNormal(1,0,0); tmp->addTexCoor(0.0,1.0); tmp->addColor(0.1,0.1,0.1); tmp->addIndice(7+b); tmp->addIndice(10+a); tmp->addVertex(controlsTmp[2].position[0],controlsTmp[2].position[1], controlsTmp[2].position[2]+0.01); tmp->addNormal(1,0,0); tmp->addTexCoor(0.0,1.0); tmp->addColor(0.1,0.1,0.1); //tmp->addIndice(5); tmp->addIndice(11+a); tmp->newStripe(); tmp->addVertex(controlsTmp[3].position[0],controlsTmp[3].position[1], controlsTmp[3].position[2]); tmp->addNormal(0,1,0); tmp->addTexCoor(0.5,0.0); tmp->addColor(0.1,0.1,0.1); tmp->addIndice(9+b); tmp->addIndice(12+a); tmp->addVertex(controlsTmp[4].position[0],controlsTmp[4].position[1], controlsTmp[4].position[2]); tmp->addNormal(1,0,0); tmp->addTexCoor(0.5,0.5); tmp->addColor(0.1,0.1,0.1); tmp->addIndice(10+b); tmp->addIndice(13+a); tmp->addVertex(controlsTmp[5].position[0],controlsTmp[5].position[1], controlsTmp[5].position[2]); tmp->addNormal(1,0,0); tmp->addTexCoor(0.5,1.0); tmp->addColor(0.1,0.1,0.1); tmp->addIndice(11+b); tmp->addIndice(14+a); tmp->addVertex(controlsTmp[5].position[0],controlsTmp[5].position[1], controlsTmp[5].position[2]+0.01); tmp->addNormal(1,0,0); tmp->addTexCoor(0.5,1.0); tmp->addColor(0.1,0.1,0.1); tmp->addIndice(15+a); //tmp->addIndice(9); tmp->newStripe(); tmp->addVertex(controlsTmp[6].position[0],controlsTmp[6].position[1], controlsTmp[6].position[2]); tmp->addNormal(1,0,0); tmp->addTexCoor(1.0,0.0); tmp->addColor(0.1,0.1,0.1); tmp->addIndice(13+b); tmp->addIndice(16+a); tmp->addVertex(controlsTmp[7].position[0],controlsTmp[7].position[1], controlsTmp[7].position[2]); tmp->addNormal(0,1,0); tmp->addTexCoor(1.0,0.5); tmp->addColor(0.1,0.1,0.1); tmp->addIndice(14+b); tmp->addIndice(17+a); tmp->addVertex(controlsTmp[8].position[0],controlsTmp[8].position[1], controlsTmp[8].position[2]); tmp->addNormal(1,0,0); tmp->addTexCoor(1.0,1.0); tmp->addColor(0.1,0.1,0.1); tmp->addIndice(15+b); tmp->addIndice(18+a); tmp->addVertex(controlsTmp[8].position[0],controlsTmp[8].position[1], controlsTmp[8].position[2]); tmp->addNormal(1,0,0); tmp->addTexCoor(1.0,1.0); tmp->addColor(0.1,0.1,0.1); tmp->addIndice(19+a); //tmp->addIndice(13); tmp->newStripe(); tmp->addVertex(controlsTmp[6].position[0],controlsTmp[6].position[1], controlsTmp[6].position[2]); tmp->addNormal(1,0,0); tmp->addTexCoor(1.0,0.0); tmp->addColor(0.1,0.1,0.1); tmp->addIndice(17+b); tmp->addVertex(controlsTmp[7].position[0],controlsTmp[7].position[1], controlsTmp[7].position[2]); tmp->addNormal(0,1,0); tmp->addTexCoor(1.0,0.5); tmp->addColor(0.1,0.1,0.1); tmp->addIndice(18+b); tmp->addVertex(controlsTmp[8].position[0],controlsTmp[8].position[1], controlsTmp[8].position[2]); tmp->addNormal(1,0,0); tmp->addTexCoor(1.0,1.0); tmp->addColor(0.1,0.1,0.1); tmp->addIndice(19+b); tmp->addVertex(controlsTmp[8].position[0],controlsTmp[8].position[1], controlsTmp[8].position[2]); tmp->addNormal(1,0,0); tmp->addTexCoor(1.0,1.0); tmp->addColor(0.1,0.1,0.1); tmp->newStripe(); tmp->finalize(); // End of DebugModel this->patchOffset++; }// For } // For // Overwrite Face->meshvert; // Overwrite Face->n_meshverts; int sz = (size0 -1)/2 * (size1 -1)/2; // num patches Face->meshvert = patchOffset -sz; //3*(patchOffset-sz)*level1*level1; Face->n_meshverts = sz; PRINTF(0)("BSP FILE: sz: %i. \n", sz); PRINTF(0)("BSP FILE: Face->meshvert %i . \n", Face->meshvert); //Face->n_meshverts = sz; } void BspFile::swapAllBspCoordinates() { for(int i = 0; i < this->numVertex ; ++i) { this->swapCoords(&((BspVertex *)this->vertice)[i].position[0]); this->swapCoords(&((BspVertex *)this->vertice)[i].normal[0]); } for(int i = 0; i < this->numLeafs ; ++i) { this->swapCoords(this->leaves[i].mins); this->swapCoords(this->leaves[i].maxs); } for(int i = 0; i < this->numPlanes; ++i) { float sto = this->planes[i].x; this->planes[i].x = this->planes[i].y; this->planes[i].y = this->planes[i].z; this->planes[i].z = sto; } for(int i = 0; i < this->numFaces; ++i) { this->swapCoords(this->faces[i].normal); } } void BspFile::swapCoords(int *array) { int sto = array[0]; array[0] = array[1]; array[1] = array[2]; array[2] = sto; } void BspFile::swapCoords(float * array) { float sto = array[0]; array[0] = array[1]; array[1] = array[2]; array[2] = sto; }