/* 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 Inspired by: Rendering Q3 Maps by Morgan McGuire http://graphics.cs.brown.edu/games/quake/quake3.html Unofficial Quake 3 Map Specs by Kekoa Proudfoot http://graphics.stanford.edu/~kekoa/q3/ Quake 3 Collision Detection by Nathan Ostgard http://www.devmaster.net/articles/quake3collision/ */ #include "bsp_file.h" #include "bsp_tree_node.h" #include "util/loading/resource_manager.h" #include #include #include #include "filesys/binary_file.h" #include "debug.h" #include "material.h" #include "vertex_array_model.h" // Necessary ? #include "base_object.h" #include "vector.h" #include "movie_player.h" #include #include // STL Containers #include using namespace std; static const char* HEADER_SEMANTICS = "b4i35"; static const char* NODE_SEMANTICS = "i9"; static const char* LEAF_SEMANTICS = "i12"; static const char* MODEL_SEMANTICS = "i10"; static const char* BRUSH_SEMANTICS = "i3"; static const char* BRUSHSIDE_SEMANTICS = "i2"; static const char* FACE_SEMANTICS = "i26"; static const char* VERTEX_SEMANTICS = "i10b4"; static const char* LIGHTMAP_SEMANTICS = "b49152"; static const char* TEXTURE_SEMANTICS = "b64i2"; static const char* PLANE_SEMANTICS = "f4"; // Constructor BspFile::BspFile() {} BspFile::~ BspFile() { delete [] nodes; delete [] leaves; delete [] planes; delete [] bspModels; delete [] leafFaces; delete [] faces; delete [] leafBrushes; delete [] brushes; delete [] brushSides; delete [] vertice; delete [] meshverts; delete [] visData; delete [] textures; delete [] patchVertice; delete [] patchIndexes; // delete [] patchTrianglesPerRow; delete [] lightMaps; for(int i = 0; i < this->numPatches; ++i) delete this->VertexArrayModels[i]; // delete [] VertexArrayModels; for(int i = 0; i < this->numTextures; ++i) { delete this->Materials[i].mat; //delete this->Materials[i].aviMat; } delete [] this->Materials; //delete [] testSurf; } void BspFile::readEntities() { /* We do nothing here */ } void BspFile::readTextures() { size_t slurpedBytes; bsp_lump& lump = header.lumps[Textures]; this->numTextures = lump.length/sizeof(BspTexture); this->textures = new char[lump.length]; file.seekg( lump.offset ); file.read( TEXTURE_SEMANTICS, this->numTextures, this->textures, slurpedBytes ); } void BspFile::readPlanes() { size_t slurpedBytes; bsp_lump& lump = header.lumps[Planes]; this->numPlanes = lump.length/sizeof(plane); this->planes = new plane[this->numPlanes]; file.seekg( lump.offset ); file.read( PLANE_SEMANTICS, this->numPlanes, this->planes, slurpedBytes ); } void BspFile::readNodes() { size_t slurpedBytes; bsp_lump& lump = header.lumps[Nodes]; this->numNodes = lump.length/sizeof(node); this->nodes = new node[this->numNodes]; file.seekg( lump.offset ); file.read( NODE_SEMANTICS, this->numNodes, this->nodes, slurpedBytes ); } void BspFile::readLeafs() { size_t slurpedBytes; bsp_lump& lump = header.lumps[Leafs]; this->numLeafs = lump.length/sizeof(leaf); this->leaves = new leaf[this->numLeafs]; file.seekg( lump.offset ); file.read( LEAF_SEMANTICS, this->numLeafs, this->leaves, slurpedBytes ); } void BspFile::readLeafFaces() { size_t slurpedBytes; bsp_lump& lump = header.lumps[Leaffaces]; this->numLeafFaces = lump.length/sizeof(int); this->leafFaces = (char*)( new int[this->numLeafFaces] ); file.seekg( lump.offset ); file.read( "i", this->numLeafFaces, this->leafFaces, slurpedBytes ); } void BspFile::readLeafBrushes() { size_t slurpedBytes; bsp_lump& lump = header.lumps[Leafbrushes]; this->numLeafBrushes = lump.length/sizeof(int); this->leafBrushes = (char*) ( new int[this->numLeafBrushes] ); file.seekg( lump.offset ); file.read( "i", this->numLeafBrushes, this->leafBrushes, slurpedBytes ); } void BspFile::readModels() { size_t slurpedBytes; bsp_lump& lump = header.lumps[Models]; this->numBspModels = lump.length/sizeof(model); this->bspModels = new model[this->numBspModels]; file.seekg( lump.offset ); file.read( MODEL_SEMANTICS, this->numBspModels, this->bspModels, slurpedBytes ); } void BspFile::readBrushes() { size_t slurpedBytes; bsp_lump& lump = header.lumps[Brushes]; int numBrushes = lump.length/sizeof(brush); this->brushes = new brush[numBrushes]; file.seekg( lump.offset ); file.read( BRUSH_SEMANTICS, numBrushes, this->brushes, slurpedBytes ); } void BspFile::readVertices() { size_t slurpedBytes; bsp_lump& lump = header.lumps[Vertices]; this->numVertex = lump.length/sizeof(BspVertex); this->vertice = (char*)( new BspVertex[this->numVertex] ); file.seekg( lump.offset ); file.read( VERTEX_SEMANTICS, this->numVertex, this->vertice, slurpedBytes ); } void BspFile::readMeshVerts() { size_t slurpedBytes; bsp_lump& lump = header.lumps[Meshverts]; int num = lump.length / sizeof( meshvert ); this->meshverts = new meshvert[num]; file.seekg( lump.offset ); file.read( "i", num, this->meshverts, slurpedBytes ); } void BspFile::readEffects() { /* not loaded atm */ } void BspFile::readFaces() { size_t slurpedBytes; bsp_lump& lump = header.lumps[Faces]; this->numFaces = lump.length/sizeof(face); this->faces = new face[this->numFaces]; file.seekg( lump.offset ); file.read( FACE_SEMANTICS, this->numFaces, this->faces, slurpedBytes ); } void BspFile::readLightmaps() { size_t slurpedBytes; bsp_lump& lump = header.lumps[Lightmaps]; this->numLightMaps = lump.length/sizeof(lightmap); this->lightMaps = new lightmap[this->numLightMaps]; file.seekg( lump.offset ); file.read( LIGHTMAP_SEMANTICS, this->numLightMaps, this->lightMaps, slurpedBytes ); } void BspFile::readLightvols() { /* not loaded atm */ } void BspFile::readBrushSides() { size_t slurpedBytes; bsp_lump& lump = header.lumps[Brushsides]; this->numBrushSides = lump.length/sizeof(brushside); printf( "numBrushSides: %d\n", numBrushSides ); this->brushSides = new brushside[this->numBrushSides]; file.seekg( lump.offset ); file.read( BRUSHSIDE_SEMANTICS, this->numBrushSides, this->brushSides, slurpedBytes ); } void BspFile::readVisdata() { size_t slurpedBytes; bsp_lump& lump = header.lumps[Visdata]; this->visData = new char[lump.length]; file.seekg( lump.offset ); file.read( "i2", 1, this->visData, slurpedBytes ); file.read( "b", lump.length-8, this->visData+8, slurpedBytes ); } /** * Loads a quake3 level (*.bsp) * @param name the Name of the *.bsp file */ int BspFile::read(const char* name) { this->scale = 1.0; size_t slurpedBytes; string theBSPFile( name ); file.setFileName( theBSPFile ); if (file.exists() && file.open( File::ReadOnly ) ) { PRINTF(0)("BSP FILE: Datei %s gefunden. \n", name); //BSP-Files have little endian order. file.setByteorder( LittleEndian ); file.read( HEADER_SEMANTICS, (void*)&header, slurpedBytes ); if ( header.version != 46 ) { PRINTF(0)("BSP FILE: Wrong BSPVersion. We only handle 0x2e-files!\n"); //!< now, we should do some error handling return ( -1 ); } readEntities(); readTextures(); readPlanes(); readNodes(); readLeafs(); readLeafFaces(); readLeafBrushes(); readModels(); readVertices(); readMeshVerts(); readBrushSides(); readBrushes(); readEffects(); readFaces(); readLightmaps(); readLightvols(); readVisdata( file.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 ( cFace.type == 2) this->numPatches += ( cFace.size[0] -1 ) / 2 * ( 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]; Not needed? this->patchTrianglesPerRow = new char[7*4*this->numPatches]; this->VertexArrayModels = new VertexArrayModel*[this->numPatches]; PRINTF(4)("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 ( (this->faces)[i].type == 2) this->tesselate(i); } PRINTF(4)("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(4)("BSP FILE:\n"); PRINTF(4)("BSP FILE: Building Tree...\n"); root = this->build_tree_rec(0); PRINTF(4)("BSP FILE: ...done. \n"); PRINTF(4)("BSP FILE: \n"); PRINTF(4)("BSP FILE: Node #0: \n"); PRINTF(4)("BSP FILE: x: %f \n",root->plane.x); PRINTF(4)("BSP FILE: y: %f\n",root->plane.y); PRINTF(4)("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* baseName = "/worlds/bsp/"; char fileName [500]; char ext [500]; //struct stat results; this->Materials = new AMat[this->numTextures]; for(int i = 0 ; i < this->numTextures; i++) { PRINTF(4)("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, baseName); strcpy(ext, &this->textures[8+ 72*i]); strncat(fileName, ext, strlen(fileName) + strlen(&this->textures[8+ 72*i]) ); strcpy(ext, ".mov"); strncat (fileName, ext, strlen(fileName) ); PRINTF(4)("BSP FILE: Name %s . \n", fileName); absFileName = ResourceManager::getFullName(fileName); if(File(absFileName).exists()) { PRINTF(4)("BSP FILE: gefunden . \n"); this->Materials[i] = this->loadAVI(fileName); continue; } // Check for avi strcpy(fileName, baseName); strcpy(ext, &this->textures[8+ 72*i]); strncat(fileName, ext, strlen(fileName) + strlen(&this->textures[8+ 72*i]) ); strcpy(ext, ".avi"); strncat (fileName, ext, strlen(fileName)); absFileName = ResourceManager::getFullName(fileName); if(File(absFileName).exists()) { PRINTF(4)("BSP FILE: gefunden . \n"); this->Materials[i] = this->loadAVI(fileName); continue; } // Check for mpg strcpy(fileName, baseName); strcpy(ext, &this->textures[8+ 72*i]); strncat(fileName, ext, strlen(fileName) + strlen(&this->textures[8+ 72*i]) ); strcpy(ext, ".mpg"); strncat (fileName, ext, strlen(fileName)); absFileName = ResourceManager::getFullName(fileName); if(File(absFileName).exists()) { PRINTF(4)("BSP FILE: gefunden . \n"); this->Materials[i] = this->loadAVI(fileName); continue; } // Check for tga strcpy(fileName, baseName); strcpy(ext, &this->textures[8+ 72*i]); strncat(fileName, ext, strlen(fileName) + strlen(&this->textures[8+ 72*i]) ); strcpy(ext, ".tga"); strncat (fileName, ext, strlen(fileName)); absFileName = ResourceManager::getFullName(fileName); if(File(absFileName).exists()) { PRINTF(4)("BSP FILE: gefunden . \n"); this->Materials[i] = this->loadMat(fileName); continue; } // Check for TGA strcpy(fileName, baseName); strcpy(ext, &this->textures[8+ 72*i]); strncat(fileName, ext, strlen(fileName) + strlen(&this->textures[8+ 72*i]) ); strcpy(ext, ".TGA"); strncat (fileName, ext, strlen(fileName)); absFileName = ResourceManager::getFullName(fileName); if(File(absFileName).exists()/*stat( absFileName.c_str() , &results) == 0*/) { PRINTF(4)("BSP FILE: gefunden . \n"); this->Materials[i] = this->loadMat(fileName); continue; } // Check for jpg strcpy(fileName, baseName); strcpy(ext, &this->textures[8+ 72*i]); strncat(fileName, ext, strlen(fileName) + strlen(&this->textures[8+ 72*i]) ); strcpy(ext, ".jpg"); strncat (fileName, ext, strlen(fileName)); absFileName = ResourceManager::getFullName(fileName); if(File(absFileName).exists()) { PRINTF(4)("BSP FILE: gefunden . \n"); this->Materials[i] =this->loadMat(fileName); continue; } // Check for JPG strcpy(fileName, baseName); strcpy(ext, &this->textures[8+ 72*i]); strncat(fileName, ext, strlen(fileName) + strlen(&this->textures[8+ 72*i]) ); strcpy(ext, ".JPG"); strncat (fileName, ext, strlen(fileName)); absFileName = ResourceManager::getFullName(fileName); if(File(absFileName).exists()) { PRINTF(4)("BSP FILE: gefunden . \n"); this->Materials[i] =this->loadMat(fileName); continue; } // Check for bmp strcpy(fileName, baseName); strcpy(ext, &this->textures[8+ 72*i]); strncat(fileName, ext, strlen(fileName) + strlen(&this->textures[8+ 72*i]) ); strcpy(ext, ".bmp"); strncat (fileName, ext, strlen(fileName)); absFileName = ResourceManager::getFullName(fileName); if(File(absFileName).exists()) { PRINTF(4)("BSP FILE: gefunden . \n"); this->Materials[i] =this->loadMat(fileName); continue; } // Check for BMP strcpy(fileName, baseName); strcpy(ext, &this->textures[8+ 72*i]); strncat(fileName, ext, strlen(fileName) + strlen(&this->textures[8+ 72*i]) ); strcpy(ext, ".BMP"); strncat (fileName, ext, strlen(fileName)); absFileName = ResourceManager::getFullName(fileName); if(File(absFileName).exists()) { PRINTF(4)("BSP FILE: gefunden . \n"); this->Materials[i] = this->loadMat(fileName); continue; } PRINTF(0)("BSP FILE: Texture %s not found.\n",&this->textures[8+ 72*i] ); // 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); this->MovieMaterials.push_back(testMC); //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 mipmap3*/ float sc, scale, temp; for(int i = 0; i < 128*128*3 ; i++) { sc = ((unsigned char *)(&lightMapTexture))[i]; sc *= 1/255.0; scale = 1.0f; // Adjust brightness here if(sc > 1.0f && (temp = (1.0f/sc)) < scale) scale=temp; scale*=255.0; sc*=scale; if(false) ((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; } //!TODO: This is a good place to do LittleEndian to BigEndian conversion! 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; this->planes[i].d = scale * this->planes[i].d ; } for(int i = 0; i < this->numFaces; ++i) { this->swapCoords(this->faces[i].normal); } } void BspFile::swapCoords(int *array) { if( scale < 1) { int sto = array[0]; array[0] = array[1] / (int) ( 1/ scale); array[1] = array[2] / (int) (1/scale); array[2] = sto / (int) (1/scale); } else { int sto = array[0]; array[0] = scale * array[1] ; array[1] = scale * array[2]; array[2] = scale * sto ; } } void BspFile::swapCoords(float * array) { float sto = array[0]; array[0] = scale * array[1]; array[1] = scale * array[2]; array[2] = scale * sto; }