Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/mount_points/src/lib/graphics/importer/bsp/bsp_file.cc @ 10165

Last change on this file since 10165 was 10165, checked in by patrick, 17 years ago

some warnings fixed

File size: 42.5 KB
RevLine 
[7353]1/*
2   orxonox - the future of 3D-vertical-scrollers
[8088]3
[7353]4   Copyright (C) 2006 orx
[8088]5
[7353]6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
[8088]10
[7353]11   ### File Specific:
12   main-programmer: bottac@ee.ethz.ch
[8088]13
[8081]14   Inspired by:
15   Rendering Q3 Maps by Morgan McGuire                  http://graphics.cs.brown.edu/games/quake/quake3.html
16   Unofficial Quake 3 Map Specs by Kekoa Proudfoot      http://graphics.stanford.edu/~kekoa/q3/
17   Quake 3 Collision Detection by Nathan Ostgard        http://www.devmaster.net/articles/quake3collision/
[7353]18*/
19
[8088]20
[7353]21#include "bsp_file.h"
22#include "bsp_tree_node.h"
23#include <fstream>
[8330]24#include "util/loading/resource_manager.h"
25
[7353]26#include <sys/stat.h>
27#include <string.h>
28#include "debug.h"
29#include "material.h"
30#include "vertex_array_model.h"
31// Necessary ?
32#include "base_object.h"
33#include "vector.h"
[7801]34#include "movie_player.h"
[7353]35
[7385]36#include <SDL/SDL_endian.h>
37#include <SDL/SDL_image.h>
38
[8490]39// STL Containers
40#include <vector>
41
[7353]42
43
[9406]44
[7353]45// Constructor
46BspFile::BspFile()
[7395]47{}
[7353]48
[9003]49BspFile::~ BspFile()
50{
51  delete [] nodes;
52  delete [] leaves;
53  delete [] planes;
54  delete [] bspModels;
55  delete [] leafFaces;
56  delete [] faces;
57  delete [] leafBrushes;
58  delete [] brushes;
59  delete [] brushSides;
60  delete [] vertice;
61  delete [] meshverts;
62  delete [] visData;
63  delete [] textures;
64  delete [] patchVertice;
65  delete [] patchIndexes;
66 // delete [] patchTrianglesPerRow;
67  delete [] lightMaps;
[9025]68
[9003]69  for(int i = 0; i < this->numPatches; ++i) delete this->VertexArrayModels[i];
70 // delete  [] VertexArrayModels;
[9025]71
[9003]72  for(int i = 0; i < this->numTextures; ++i)
73  {
74    delete this->Materials[i].mat;
75    //delete this->Materials[i].aviMat;
76  }
77  delete [] this->Materials;
78  //delete [] testSurf;
[9025]79
80
81
[9003]82}
83
[7511]84/**
85 *  Loads a quake3 level (*.bsp)
86 * @param name the Name of the *.bsp file
87 */
[7596]88int BspFile::read(const char* name)
[7353]89{
[8490]90  this->scale = 1.0;
[7353]91  int offset;
92  int size;
93  struct stat results;
94
[7801]95
[7395]96  if (stat( name , &results) == 0) {
97    PRINTF(0)("BSP FILE: Datei %s gefunden. \n", name);
[9406]98    std::ifstream bspFile (name, std::ios::in | std::ios::binary);
[7395]99    bspFile.read(this->header, 260);
100    PRINTF(0)("BSP FILE: BSPVersion: %i. \n", ((int *)(header) )[1]);
101    if(SDL_SwapLE32(((int *)(header) )[1]) == 46)    PRINTF(0)("BSP FILE: This is the good one! :-)  \n");
102    else  PRINTF(0)("BSP FILE: Wrong BSPVersion.\n");   //!< now, we should do some error handling
103
104    // Get the Nodes
105    offset = SDL_SwapLE32(((int *)(header) )[8]);
106    size    = SDL_SwapLE32(((int *)(header))[9]);
107    PRINTF(4)("BSP FILE: NodeSize: %i Bytes. \n", size);
108    PRINTF(4)("BSP FILE: NumNodes: %i. \n", size / sizeof(node));
109    PRINTF(4)("BSP FILE: Remainder: %i. \n", size % sizeof(node));
110    PRINTF(4)("BSP FILE: NodeOffset: %i. \n", offset);
111    this->numNodes = size/sizeof(node);
112    this->nodes = new node [this->numNodes];
[7353]113    bspFile.seekg(offset);
[7395]114    bspFile.read((char*)this->nodes, size);
[7353]115
[7395]116    // and their Planes
117    offset = SDL_SwapLE32(((int *)(header) )[6]);
118    size    = SDL_SwapLE32(((int *)(header))[7]);
119    PRINTF(4)("BSP FILE: PlanesSize: %i Bytes. \n", size);
120    PRINTF(4)("BSP FILE: NumPlanes: %i. \n", size / sizeof(plane));
121    PRINTF(4)("BSP FILE: Remainder: %i. \n", sizeof(plane));
122    PRINTF(4)("BSP FILE: PlanesOffset: %i. \n", offset);
123    this->numPlanes = size/sizeof(plane);
124    this->planes = new plane [this->numPlanes];
[7353]125    bspFile.seekg(offset);
[7395]126    bspFile.read((char*)this->planes, size);
[7353]127
[7395]128    // Get the Leafs
129    offset = SDL_SwapLE32(((int *)(header) )[10]);
130    size    = SDL_SwapLE32(((int *)(header))[11]);
131    PRINTF(4)("BSP FILE: LeaveSize: %i Bytes. \n", size);
132    PRINTF(4)("BSP FILE: NumLeaves: %i. \n", size / sizeof(leaf));
133    PRINTF(4)("BSP FILE: Remainder: %i. \n", size % sizeof(leaf));
134    PRINTF(4)("BSP FILE: LeaveOffset: %i. \n", offset);
135    this->numLeafs = size/sizeof(leaf);
136    this->leaves = new leaf [this->numLeafs];
[7353]137    bspFile.seekg(offset);
[7395]138    bspFile.read((char*)this->leaves, size);
[7353]139
140    // Get the Models
[7395]141    offset = SDL_SwapLE32(((int *)(header))[16]);
142    size    = SDL_SwapLE32(((int *)(header))[17]);
143    PRINTF(4)("BSP FILE: ModelsSize: %i Bytes. \n", size);
144    PRINTF(4)("BSP FILE: NumModels: %i. \n", size / sizeof(model));
145    PRINTF(4)("BSP FILE: Remainder: %i. \n", size % sizeof(model));
146    PRINTF(4)("BSP FILE: ModelsOffset: %i. \n", offset);
147    this->numBspModels = size/sizeof(model);
148    this->bspModels = new model [this->numBspModels];
[7353]149    bspFile.seekg(offset);
[7395]150    bspFile.read((char*)this->bspModels, size);
[7353]151
152    // Get the leafFaces
[7395]153    offset = SDL_SwapLE32(((int *)(header))[12]);
154    size    = SDL_SwapLE32(((int *)(header))[13]);
155    PRINTF(4)("BSP FILE: leafFacesSize: %i Bytes. \n", size);
156    PRINTF(4)("BSP FILE: NumleafFaces: %i. \n", size / 4);
157    PRINTF(4)("BSP FILE: Remainder: %i. \n", size % 4);
158    PRINTF(4)("BSP FILE: leafFacesOffset: %i. \n", offset);
[7353]159    this->numLeafFaces = size/4;
160    this->leafFaces = new char [size];
161    bspFile.seekg(offset);
162    bspFile.read(this->leafFaces, size);
163
164    // Get the leafBrushes
[7395]165    offset = SDL_SwapLE32(((int *)(header))[14]);
166    size    = SDL_SwapLE32(((int *)(header))[15]);
167    PRINTF(4)("BSP FILE: leafBrushesSize: %i Bytes. \n", size);
168    PRINTF(4)("BSP FILE: NumleafBrushes: %i. \n", size / 4);
169    PRINTF(4)("BSP FILE: Remainder: %i. \n", size % 4);
170    PRINTF(4)("BSP FILE: leafBrushesOffset: %i. \n", offset);
[7353]171    this->numLeafBrushes = size/4;
172    this->leafBrushes = new char [size];
173    bspFile.seekg(offset);
174    bspFile.read(this->leafBrushes, size);
175
176    // Get the brushes
[7395]177    offset = SDL_SwapLE32(((int *)(header))[18]);
178    size    = SDL_SwapLE32(((int *)(header))[19]);
179    PRINTF(4)("BSP FILE: BrushesSize: %i Bytes. \n", size);
[7410]180    PRINTF(4)("BSP FILE: NumBrushes: %i. \n", size / sizeof(brush));
181    PRINTF(4)("BSP FILE: Remainder: %i. \n", size % sizeof(brush));
[7395]182    PRINTF(4)("BSP FILE: BrushesOffset: %i. \n", offset);
[7410]183    this->brushes = new brush [size/sizeof(brush)];
[7353]184    bspFile.seekg(offset);
[7410]185    bspFile.read((char*)this->brushes, size);
[7353]186
187    // Get the brushSides
[7395]188    offset =   SDL_SwapLE32(((int *)(header))[20]);
[7385]189    size    = SDL_SwapLE32(((int *)(header))[21]);
[7395]190    PRINTF(4)("BSP FILE: BrushSidesSize: %i Bytes. \n", size);
191    PRINTF(4)("BSP FILE: NumBrushSides: %i. \n", size / 8);
192    PRINTF(4)("BSP FILE: Remainder: %i. \n", size % 8);
193    PRINTF(4)("BSP FILE: BrushSidesOffset: %i. \n", offset);
[7507]194    this->numBrushSides = size/sizeof(brushside);
195    this->brushSides = new brushside [this->numBrushSides];
[7353]196    bspFile.seekg(offset);
[7507]197    bspFile.read((char*)this->brushSides, size);
[7353]198
199    // Get the Vertice
[7395]200    offset = SDL_SwapLE32(((int *)(header))[22]);
201    size    = SDL_SwapLE32(((int *)(header))[23]);
202    PRINTF(4)("BSP FILE: VerticeSize: %i Bytes. \n", size);
203    PRINTF(4)("BSP FILE: NumVertice: %i. \n", size / 44);
204    PRINTF(4)("BSP FILE: Remainder: %i. \n", size % 44);
205    PRINTF(4)("BSP FILE: VerticeOffset: %i. \n", offset);
[7353]206    this->numVertex = size/44;
207    this->vertice = new char [size];
208    bspFile.seekg(offset);
209    bspFile.read(this->vertice, size);
210
211    // Get the MeshVerts
[7395]212    offset = SDL_SwapLE32(((int *)(header))[24]);
213    size    = SDL_SwapLE32(((int *)(header))[25]);
214    PRINTF(4)("BSP FILE: MeshVertsSize: %i Bytes. \n", size);
[7410]215    PRINTF(4)("BSP FILE: NumMeshVerts: %i. \n", size / sizeof(meshvert));
216    PRINTF(4)("BSP FILE: Remainder: %i. \n", size % sizeof(meshvert));
[7395]217    PRINTF(4)("BSP FILE: MeshVertsOffset: %i. \n", offset);
[7410]218    this->meshverts = new meshvert [size / sizeof(meshvert)];
[7353]219    bspFile.seekg(offset);
[7410]220    bspFile.read((char*)this->meshverts, size);
[7353]221
222    // Get the Faces
[7395]223    offset = SDL_SwapLE32(((int *)(header))[28]);
224    size    = SDL_SwapLE32(((int *)(header))[29]);
225    PRINTF(4)("BSP FILE: FacesSize: %i Bytes. \n", size);
226    PRINTF(4)("BSP FILE: NumFaces: %i. \n", size / sizeof(face));
227    PRINTF(4)("BSP FILE: Remainder: %i. \n", size % sizeof(face));
228    PRINTF(4)("BSP FILE: FacesOffset: %i. \n", offset);
229    this->numFaces = size/sizeof(face);
230    this->faces = new face [this->numFaces];
[7353]231    bspFile.seekg(offset);
[7395]232    bspFile.read((char*)this->faces, size);
[7353]233
[7465]234    //Get the lightmaps
235    offset = SDL_SwapLE32(((int *)(header))[30]);
236    size    = SDL_SwapLE32(((int *)(header))[31]);
237    this->numLightMaps = size/ sizeof(lightmap);
238    this->lightMaps = new lightmap [this->numLightMaps];
239    bspFile.seekg(offset);
240    bspFile.read((char*)this->lightMaps, size);
241
242
[7353]243    // Get the Visdata
[7395]244    offset = SDL_SwapLE32(((int *)(header))[34]);
245    size    = SDL_SwapLE32(((int *)(header))[35]);
[7353]246
247    this->visData = new char [size];
248    bspFile.seekg(offset);
249    bspFile.read(this->visData, size);
250
[8894]251    PRINTF(4)("BSP FILE: VisDataSize: %i Bytes. \n", size);
252    PRINTF(4)("BSP FILE: NumVisData: %i. \n", size /1 - 8);
253    PRINTF(4)("BSP FILE: Remainder: %i. \n", size % 1);
254    PRINTF(4)("BSP FILE: VisDataOffset: %i. \n", offset);
[7353]255
256    // Get the Textures
[7395]257    offset = SDL_SwapLE32(((int *)(header))[4]);
258    size    = SDL_SwapLE32(((int *)(header))[5]);
[7353]259
260    this->textures= new char [size];
261    bspFile.seekg(offset);
262    bspFile.read(this->textures, size);
263
[7395]264    PRINTF(4)("BSP FILE: TextureSize: %i Bytes. \n", size);
265    PRINTF(4)("BSP FILE: NumTextures: %i. \n", size /72);
266    PRINTF(4)("BSP FILE: Remainder: %i. \n", size % 72);
267    PRINTF(4)("BSP FILE: TextureOffset: %i. \n", offset);
[7353]268    this->numTextures = size/72;
269
[7395]270    bspFile.close();
[7353]271
[7395]272    for(int i = 0 ; i < this->numTextures; i++)
273      PRINTF(4)("BSP FILE: Texture 0: %s. \n", &this->textures[8+ 72*i]);
274    this->load_textures();
[8088]275
[7465]276    // Load the lightMaps
277    this->glLightMapTextures = new GLuint[this->numLightMaps];
278    for(int i = 0; i < this->numLightMaps; i++)
279      this->glLightMapTextures[i] = this->loadLightMapToGL(this->lightMaps[i]);
[8088]280
[7465]281    //Create white texture for if no lightmap specified
282    glGenTextures(1, &this->whiteLightMap);
283    glBindTexture(GL_TEXTURE_2D, this->whiteLightMap);
[8088]284        //Create texture
[7465]285    this->whiteTexture[0]=255;
286    this->whiteTexture[1]=255;
287    this->whiteTexture[2]=255;
[8088]288
[7465]289    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
290    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
291    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
292    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
[7353]293
[7465]294    glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_PRIORITY, 2);
295
296
[8088]297
[7465]298    /* control the mipmap levels */
299    glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_MIN_LOD, 5);
300    glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_MAX_LOD, 0);
301
302    /* build the Texture  OpenGL V >= 1.1 */
303    glTexImage2D(GL_TEXTURE_2D,
304                 0,
[7544]305                 GL_RGBA8,
[7465]306                 1,
307                 1,
308                 0,
309                 GL_RGB,
310                 GL_UNSIGNED_BYTE,
311                 (const GLvoid *)&(this->whiteTexture));
312
[8088]313   gluBuild2DMipmaps(   GL_TEXTURE_2D, GL_RGBA8, 1, 1,
[7544]314                      GL_RGB, GL_UNSIGNED_BYTE,(const GLvoid *) &(this->whiteTexture));
[7465]315
[8088]316
317
[7395]318    // Get the number of patches
319    this->numPatches = 0;
320    this->patchOffset   = 0;
[7353]321
[7395]322    for( int i = 0; i < this->numFaces; i++) {
323      face& cFace = ((face *)(this->faces))[i];
[7410]324      if (SDL_SwapLE32(cFace.type) == 2)
325        this->numPatches += (SDL_SwapLE32(cFace.size[0]) -1 ) / 2  * (SDL_SwapLE32(cFace.size[1]) -1) / 2;
[7395]326    }
[7353]327
[7395]328    // Allocate Memory
329    this->patchVertice = new char[8*8*44*(this->numPatches+10)];
330    this->patchIndexes = new char[7*8*2*4*(this->numPatches+10)];
[9003]331 // this->patchRowIndexes = new int*[7*4*this->numPatches]; Not needed?
[7395]332    this->patchTrianglesPerRow = new char[7*4*this->numPatches];
333    this->VertexArrayModels  = new VertexArrayModel*[this->numPatches];
334
[8894]335    PRINTF(4)("BSP FILE:NumberOfPatches: %i . \n", numPatches);
[8088]336
[7805]337    this->swapAllBspCoordinates();
[8088]338
[7353]339    // Do tesselation for all Faces of type 2
[7395]340    for( int i = 0; i < this->numFaces; i++) {
[7410]341      if (SDL_SwapLE32((this->faces)[i].type) == 2)
[7395]342        this->tesselate(i);
343    }
[7353]344
[8894]345    PRINTF(4)("BSP FILE:PatchOffset: %i . \n", this->patchOffset);
[7353]346
[7395]347    return  1;
348  } else {
[8894]349    PRINTF(4)("BSP FILE: Datei nicht gefunden. \n");
[7395]350    return -1;
[7353]351  }
[7395]352
[8088]353
354
[7353]355}
356
357void BspFile::build_tree()
358{
359
[8894]360  PRINTF(4)("BSP FILE:\n");
361  PRINTF(4)("BSP FILE: Building Tree...\n");
[7395]362  root = this->build_tree_rec(0);
[8894]363  PRINTF(4)("BSP FILE: ...done. \n");
364  PRINTF(4)("BSP FILE:  \n");
365  PRINTF(4)("BSP FILE: Node #0: \n");
366  PRINTF(4)("BSP FILE:  x: %f \n",root->plane.x);
367  PRINTF(4)("BSP FILE:  y: %f\n",root->plane.y);
368  PRINTF(4)("BSP FILE:  z: %f\n",root->plane.z);
[7353]369}
370
[7511]371/**
372 *  Called by BspFile::build_tree() only.
373 */
[7353]374BspTreeNode*   BspFile::build_tree_rec(int i)
375{
[7395]376  // PRINTF(0)("BSP FILE: Node #%i\n", i);
377  BspTreeNode* thisNode = new BspTreeNode();
378  int left =(((node *) nodes) [i]).left;
379  int right =(((node *) nodes) [i]).right;
380  int planeIndex = (((node *) nodes) [i]).plane;
381  float x1 =(((plane *) this->planes) [planeIndex]).x;
382  float y1 =(((plane *) this->planes) [planeIndex]).y;
383  float z1 =(((plane *) this->planes) [planeIndex]).z;
384  thisNode->leafIndex = 0;
[8088]385  thisNode->d          = (((plane *) this->planes) [planeIndex]).d;
[7353]386
[7395]387  thisNode->plane = Vector(x1,y1,z1);
388  thisNode->isLeaf = false;
389
390  if(left >= 0) {
391    thisNode->left = this->build_tree_rec(left);
392  } else {
393    //BspTreeLeaf tmp =  BspTreeLeaf();
394    //tmp.isLeaf = true;
395    //tmp.leafIndex = -left -1;
396    //thisNode->left = (BspTreeNode*) (&tmp);
397    thisNode->left  = new BspTreeNode();
398    thisNode->left->isLeaf = true;
399    thisNode->left->leafIndex = - (left +1);
400    //PRINTF(0)("BSP FILE:  LeafIndex: %i\n",-left);
[7353]401  } // assign leav
[7395]402  if(right >= 0) {
403    thisNode->right =  this->build_tree_rec(right);
404  } else {
405    //BspTreeLeaf tmp =  BspTreeLeaf();
406    //tmp.isLeaf = true;
407    //tmp.leafIndex = -right -1;
408    //thisNode->right = (BspTreeNode*) (&tmp);
409    thisNode->right = new BspTreeNode();
410    thisNode->right->isLeaf = true;
411    thisNode->right->leafIndex = -(right +1);
412    //PRINTF(0)("BSP FILE:  LeafIndex: %i\n",-right);
413  } // assign leaf
414  return thisNode;
[7353]415}
416
[7511]417/**
418 *  returns the root node of the bsp-tree
419 */
[7353]420BspTreeNode* BspFile::get_root()
421{
[7395]422  return root;
[7353]423}
424
425void BspFile::load_textures()
426{
[7544]427  ::std::string absFileName;
[8490]428  char* baseName = "/worlds/bsp/";
429
430  char fileName [500];
431  char ext [500];
[8088]432
433
[7385]434  this->Materials = new AMat[this->numTextures];
[7395]435  for(int i = 0 ; i < this->numTextures; i++) {
[8894]436    PRINTF(4)("BSP FILE: Texture : %s. \n", &this->textures[8+ 72*i]);
[8088]437
438
[7544]439    strcpy(fileName, &this->textures[8+ 72*i]);
440    if(strlen(fileName) == 0)
441    {
[8490]442
[8088]443     //         Default Material
[8490]444    this->Materials[i].mat = new Material();
[7579]445    this->Materials[i].mat->setDiffuse(0.1,0.1,1.0);
446    this->Materials[i].mat->setAmbient(0.1,0.1,1.0 );
447    this->Materials[i].mat->setSpecular(0.4,0.4,1.0);
[7544]448    //this->Materials[i]->setShininess(100.0);
[7801]449   // this->Materials[i].mat->setTransparency(1.0);
[7544]450    this->Materials[i].mat->setDiffuseMap("pictures/ground.tga");
451    this->Materials[i].mat->setAmbientMap("pictures/ground.tga");
452    this->Materials[i].mat->setSpecularMap("pictures/ground.tga");
453    this->Materials[i].alpha = false;
[7801]454    this->Materials[i].animated = false;
[7544]455     continue;
[8088]456    }
457
[7801]458      // Check for mov
[8490]459    strcpy(fileName, baseName);
460    strcpy(ext, &this->textures[8+ 72*i]);
461    strncat(fileName, ext, strlen(fileName) + strlen(&this->textures[8+ 72*i]) );
[7801]462    strcpy(ext, ".mov");
[8490]463    strncat (fileName, ext, strlen(fileName) );
[7801]464
[8894]465    PRINTF(4)("BSP FILE: Name %s . \n", fileName);
[8490]466
[9869]467    absFileName = Resources::ResourceManager::getInstance()->prependAbsoluteMainPath(fileName);
[7801]468
[8088]469    if(File(absFileName).exists()) {
[8894]470      PRINTF(4)("BSP FILE: gefunden . \n");
[7801]471      this->Materials[i] = this->loadAVI(fileName);
472      continue;
473    }
[8088]474
[7801]475    // Check for avi
[8490]476    strcpy(fileName, baseName);
477    strcpy(ext, &this->textures[8+ 72*i]);
478    strncat(fileName, ext, strlen(fileName) + strlen(&this->textures[8+ 72*i]) );
[7801]479    strcpy(ext, ".avi");
480    strncat (fileName, ext, strlen(fileName));
481
[9869]482    absFileName = Resources::ResourceManager::getInstance()->prependAbsoluteMainPath(fileName);
[7801]483
[8088]484    if(File(absFileName).exists()) {
[8894]485      PRINTF(4)("BSP FILE: gefunden . \n");
[7801]486      this->Materials[i] = this->loadAVI(fileName);
487      continue;
488    }
[8088]489
[7801]490       // Check for mpg
[8490]491    strcpy(fileName, baseName);
492    strcpy(ext, &this->textures[8+ 72*i]);
493    strncat(fileName, ext, strlen(fileName) + strlen(&this->textures[8+ 72*i]) );
[7801]494    strcpy(ext, ".mpg");
495    strncat (fileName, ext, strlen(fileName));
496
[9869]497    absFileName = Resources::ResourceManager::getInstance()->prependAbsoluteMainPath(fileName);
[7801]498
[8088]499    if(File(absFileName).exists()) {
[9003]500      PRINTF(4)("BSP FILE: gefunden . \n");
[7801]501      this->Materials[i] = this->loadAVI(fileName);
502      continue;
503    }
[8088]504
[7395]505    // Check for tga
[8490]506    strcpy(fileName, baseName);
507    strcpy(ext, &this->textures[8+ 72*i]);
508    strncat(fileName, ext, strlen(fileName) + strlen(&this->textures[8+ 72*i]) );
[7353]509    strcpy(ext, ".tga");
510    strncat (fileName, ext, strlen(fileName));
[7395]511
[9869]512    absFileName = Resources::ResourceManager::getInstance()->prependAbsoluteMainPath(fileName);
[7544]513
[8088]514    if(File(absFileName).exists()) {
[8894]515      PRINTF(4)("BSP FILE: gefunden . \n");
[7395]516      this->Materials[i] = this->loadMat(fileName);
517      continue;
[7353]518    }
519    // Check for TGA
[8490]520    strcpy(fileName, baseName);
521    strcpy(ext, &this->textures[8+ 72*i]);
522    strncat(fileName, ext, strlen(fileName) + strlen(&this->textures[8+ 72*i]) );
[7353]523    strcpy(ext, ".TGA");
524    strncat (fileName, ext, strlen(fileName));
[9869]525    absFileName = Resources::ResourceManager::getInstance()->prependAbsoluteMainPath(fileName);
[7385]526
[8088]527    if(File(absFileName).exists()/*stat( absFileName.c_str() , &results) == 0*/) {
[8894]528      PRINTF(4)("BSP FILE: gefunden . \n");
[7395]529      this->Materials[i] = this->loadMat(fileName);
530      continue;
[7353]531    }
532    // Check for jpg
[8490]533    strcpy(fileName, baseName);
534    strcpy(ext, &this->textures[8+ 72*i]);
535    strncat(fileName, ext, strlen(fileName) + strlen(&this->textures[8+ 72*i]) );
[7353]536    strcpy(ext, ".jpg");
537    strncat (fileName, ext, strlen(fileName));
[9869]538    absFileName = Resources::ResourceManager::getInstance()->prependAbsoluteMainPath(fileName);
[8088]539    if(File(absFileName).exists()) {
[8894]540      PRINTF(4)("BSP FILE: gefunden . \n");
[7395]541      this->Materials[i] =this->loadMat(fileName);
542      continue;
[7353]543    }
544
545
[7395]546    // Check for JPG
[8490]547    strcpy(fileName, baseName);
548    strcpy(ext, &this->textures[8+ 72*i]);
549    strncat(fileName, ext, strlen(fileName) + strlen(&this->textures[8+ 72*i]) );
[7395]550    strcpy(ext, ".JPG");
551    strncat (fileName, ext, strlen(fileName));
[9869]552    absFileName = Resources::ResourceManager::getInstance()->prependAbsoluteMainPath(fileName);
[8088]553    if(File(absFileName).exists()) {
[8894]554      PRINTF(4)("BSP FILE: gefunden . \n");
[7395]555      this->Materials[i] =this->loadMat(fileName);
556      continue;
[7353]557    }
558
559
[8088]560
[7395]561    // Check for bmp
[8490]562    strcpy(fileName, baseName);
563    strcpy(ext, &this->textures[8+ 72*i]);
564    strncat(fileName, ext, strlen(fileName) + strlen(&this->textures[8+ 72*i]) );
[7395]565    strcpy(ext, ".bmp");
566    strncat (fileName, ext, strlen(fileName));
[9869]567    absFileName = Resources::ResourceManager::getInstance()->prependAbsoluteMainPath(fileName);
[7395]568
[8088]569    if(File(absFileName).exists()) {
[8894]570      PRINTF(4)("BSP FILE: gefunden . \n");
[7395]571      this->Materials[i] =this->loadMat(fileName);
[7544]572      continue;
[7353]573    }
574
[7395]575    // Check for BMP
[8490]576    strcpy(fileName, baseName);
577    strcpy(ext, &this->textures[8+ 72*i]);
578    strncat(fileName, ext, strlen(fileName) + strlen(&this->textures[8+ 72*i]) );
[7353]579    strcpy(ext, ".BMP");
580    strncat (fileName, ext, strlen(fileName));
[9869]581    absFileName = Resources::ResourceManager::getInstance()->prependAbsoluteMainPath(fileName);
[7353]582
[8088]583    if(File(absFileName).exists()) {
[8894]584      PRINTF(4)("BSP FILE: gefunden . \n");
[7395]585      this->Materials[i] = this->loadMat(fileName);
586      continue;
[7353]587    }
[8088]588
[9025]589    PRINTF(0)("BSP FILE: Texture %s not found.\n",&this->textures[8+ 72*i] );
[8088]590    //  Default Material
[7395]591    this->Materials[i].mat = new Material();
592    this->Materials[i].mat->setDiffuse(0.1,0.1,0.1);
593    this->Materials[i].mat->setAmbient(0.1,0.1,0.1 );
594    this->Materials[i].mat->setSpecular(0.4,0.4,0.4);
595    //this->Materials[i]->setShininess(100.0);
[7801]596    //this->Materials[i].mat->setTransparency(1.0);
597    this->Materials[i].mat->setDiffuseMap("pictures/error_texture.png");
598    this->Materials[i].mat->setAmbientMap("pictures/error_texture.png");
599    this->Materials[i].mat->setSpecularMap("pictures/error_texture.png");
600    this->Materials[i].alpha = true;
601    this->Materials[i].animated = false;
[8088]602
[7353]603  }
604}
605
606
[7385]607AMat BspFile::loadMat(char* mat)
[7353]608{
[7385]609  AMat tmpAMat;
610  this->testSurf = NULL;
611
[9869]612  this->testSurf = IMG_Load(Resources::ResourceManager::getInstance()->prependAbsoluteMainPath(mat).c_str());
[7395]613  if(this->testSurf != NULL) {
614    if(this->testSurf->format->Amask != 0 ) tmpAMat.alpha = true;
[8088]615    else                                       tmpAMat.alpha = false;
[7395]616  } else   tmpAMat.alpha = false;
[7385]617
[7353]618  Material* tmp = new Material();
[7801]619   tmp->setDiffuse(1.0,1.0,1.0);
620   tmp->setAmbient(1.0,1.0,1.0 );
[7395]621  tmp->setSpecular(1.0,1.0,1.0);
[8088]622  //    tmp->setShininess(.5);
623  //    tmp->setTransparency(0.0);
[7353]624
[7395]625  tmp->setDiffuseMap(mat);
626
[7385]627  tmpAMat.mat = tmp;
[7801]628  tmpAMat.animated = false;
629  return tmpAMat;
630}
[7353]631
[7801]632AMat BspFile::loadAVI(char* mat)
633{
634  AMat tmpAMat;
635
636
637  MoviePlayer * testMC = new MoviePlayer(mat);
638  testMC->start(0);
[9025]639
[8490]640  this->MovieMaterials.push_back(testMC);
[7801]641
642  //Material* tmp = new Material();
643 // tmp->setDiffuse(1.0,1.0,1.0);
644  //tmp->setAmbient(1.0,1.0,1.0 );
645  //tmp->setSpecular(1.0,1.0,1.0);
[8088]646  //    tmp->setShininess(.5);tmpAMat
647  //    tmp->setTransparency(0.0);
[7801]648
649  //tmp->setDiffuseMap(mat);
650
651  tmpAMat.aviMat = testMC;
652  tmpAMat.animated = true;
653  tmpAMat.alpha = true;
[7385]654  return tmpAMat;
[7353]655}
656
[7465]657unsigned int BspFile::loadLightMapToGL(lightmap& lightMapTexture)
658{
659  int      errorCode = 0;           //!< the error code for the texture loading functions
[7544]660  unsigned int   lightMap;          //!< the OpenGL texture handle
[10165]661  //int      mipmapLevel = 0;         //!< the maximum mipmap level for this texture
662  //int      mipmapWidth = 0;         //!< the width of the mipmap
663  //int      mipmapHight = 0;         //!< the height of the mipmap3
[7465]664  float sc, scale, temp;
665  for(int i = 0; i < 128*128*3 ; i++)
666  {
667  sc =  ((unsigned char *)(&lightMapTexture))[i];
[7544]668  sc *= 1/255.0;
[8490]669
670  scale = 1.0f; // Adjust brightness here
671
[7465]672  if(sc > 1.0f && (temp = (1.0f/sc)) < scale) scale=temp;
673  scale*=255.0;
674  sc*=scale;
[8030]675  if(false)
[8088]676    ((unsigned char *)(&lightMapTexture))[i] = (unsigned char)sc + 75;
[7544]677  else
[8081]678    ((unsigned char *)(&lightMapTexture))[i] = (unsigned char)sc ;
[7465]679  }
[8088]680
[7465]681  glGenTextures(1, &lightMap);
682  glBindTexture(GL_TEXTURE_2D,  lightMap);
683  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
684  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
685  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
686  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
687
688  glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_PRIORITY, 2);
689
690
691  /* control the mipmap levels */
692  glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_MIN_LOD, 5);
693  glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_MAX_LOD, 0);
694
695  /* build the Texture  OpenGL V >= 1.1 */
696   glTexImage2D(GL_TEXTURE_2D,
697                   0,
698                   GL_RGBA8,
699                   128,
700                   128,
701                   0,
702                   GL_RGB,
703                   GL_UNSIGNED_BYTE,
704                   (const GLvoid *)&lightMapTexture);
705
[8088]706
[7465]707   // build the MipMaps automaticaly
708   errorCode = gluBuild2DMipmaps(GL_TEXTURE_2D,
709GL_RGBA8,
710                                 128,
711                                 128,
712                                 GL_RGB,
713                                 GL_UNSIGNED_BYTE,
714                                 (const GLvoid *)&lightMapTexture
715                                );
716
717
718
[8088]719
[7465]720  return lightMap;
721
722}
723
[7511]724/**
725 * Generates a vertex-array, a indice-array and a texture-coordinates-array for iface.
[8088]726 * @param iface integer index of face
[7511]727 * @todo cleanup this function, let the user choose the level of tesselation
728 */
[8088]729
730
[7353]731void BspFile::tesselate(int iface)
732{
[7410]733  face*  Face =  &((this->faces)[iface]);
[7395]734  BspVertex *  BspVrtx = (BspVertex*)this->vertice;
735  int level = 7;
736  int level1 = 8;
[7801]737  int size0 = (Face->size[0]);
738  int size1 = (Face->size[1]);
[7395]739  // For each patch...
[7410]740  for(int i = 0; i <  ( size0 - 1)    ; i+=2) {
741    for(int j = 0; j < ( size1 -1)    ; j+=2) {
[7353]742
743
[7395]744      // Make a patch...
745      // Get controls[9];
746      BspVec controls[9];
747      BspVec controlsTmp[9];
748      BspVertex VControls[9];
749      for(int k = 0; k < 3; k++) {
750        for(int l = 0; l < 3; l++) {
[7801]751          controls[k +3*l]. position[0] =   (    BspVrtx[Face->vertex + (j * size0)+ i + l+ size0*k].position[0]);
752          controls[k +3*l]. position[1] =   (    BspVrtx[Face->vertex + (j * size0)+ i +l+ size0*k].position[1]);
753          controls[k +3*l]. position[2] =   (    BspVrtx[Face->vertex + (j * size0)+ i + l+ size0*k].position[2]); /*Face->n_vertexes*/
[7353]754
[7801]755          controlsTmp[2-k +6-3*l]. position[0] =   (    BspVrtx[Face->vertex +( j * size0)+ i + l+ size0*k].position[0]);
756          controlsTmp[2-k +6-3*l]. position[1] =   (    BspVrtx[Face->vertex + (j * size0)+ i +l+ size0*k].position[1]);
757          controlsTmp[2-k +6-3*l]. position[2] =   (    BspVrtx[Face->vertex + (j * size0)+ i + l+ size0*k].position[2]); /*Face->n_vertexes*/
[7353]758
[7801]759          VControls[k +3*l]. position[0] =   (    BspVrtx[Face->vertex +( j * size0)+ i + l+ size0*k].position[0]);
760          VControls[k +3*l]. position[1] =   (    BspVrtx[Face->vertex + (j * size0)+ i +l+ size0*k].position[1]);
761          VControls[k +3*l]. position[2] =   (    BspVrtx[Face->vertex + (j * size0)+ i + l+ size0*k].position[2]);
[7353]762
[7801]763          VControls[k +3*l]. normal[0] =   (    BspVrtx[Face->vertex +( j * size0)+ i + l+ size0*k].normal[0]);
764          VControls[k +3*l]. normal[1] =   (    BspVrtx[Face->vertex + (j * size0)+ i +l+ size0*k].normal[1]);
765          VControls[k +3*l]. normal[2] =   (    BspVrtx[Face->vertex + (j * size0)+ i + l+ size0*k].normal[2]);
[7353]766
[7801]767          VControls[k +3*l]. texcoord[0][0]=    (    BspVrtx[Face->vertex +( j * size0)+ i + l+ size0*k].texcoord[0][0]);
768          VControls[k +3*l]. texcoord[0][1] =   (    BspVrtx[Face->vertex + (j * size0)+ i +l+ size0*k].texcoord[0][1]);
769          VControls[k +3*l]. texcoord[1][0] =   (    BspVrtx[Face->vertex +( j * size0)+ i + l+ size0*k].texcoord[1][0]);
770          VControls[k +3*l]. texcoord[1][1] =   (    BspVrtx[Face->vertex + (j * size0)+ i +l+ size0*k].texcoord[1][1]);
[7353]771
772
[7395]773        }
774      }
775      //***********************************************************************************************************************
776      // Compute the vertice
777      //***********************************************************************************************************************
778      float px, py;
779      BspVertex temp[3];
780      BspVertex* Vertice = &(((BspVertex*)this->patchVertice)[level1*level1*this->patchOffset]);
[7353]781
[7395]782      for(int v=0; v<=level; ++v) {
783        px=(float)v/level;
[7353]784
[7395]785        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);
786        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);
787        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);
[7353]788
[7395]789        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);
790        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);
791        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);
[7353]792
[7395]793        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);
794        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);
795        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);
796        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);
[7353]797
[7395]798      }
[7353]799
800
[7395]801      for(int u=1; u<=level; ++u) {
802        py=(float)u/level;
[7353]803
[7395]804        // temp[0]=controlPoints[0]*((1.0f-py)*(1.0f-py))+ controlPoints[1]*((1.0f-py)*py*2)+ controlPoints[2]*(py*py);
805        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);
806        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);
807        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);
[7353]808
[7395]809        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);
810        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);
811        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);
[7353]812
[7395]813        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);
814        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);
815        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);
816        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);
[7353]817
818
819
[7395]820        // temp[1]=controlPoints[3]*((1.0f-py)*(1.0f-py))+ controlPoints[4]*((1.0f-py)*py*2)+ controlPoints[5]*(py*py);
821        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);
822        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);
823        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);
[7353]824
[7395]825        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);
826        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);
827        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);
[7353]828
[7395]829        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);
830        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);
831        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);
832        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);
[7353]833
834
[7395]835        // temp[2]=controlPoints[6]*((1.0f-py)*(1.0f-py))+controlPoints[7]*((1.0f-py)*py*2)+controlPoints[8]*(py*py);
836        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);
837        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);
838        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);
[7353]839
[7395]840        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);
841        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);
842        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);
[7353]843
[7395]844        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);
845        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);
846        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);
847        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);
[7353]848
849
850
851
[7395]852        for(int v=0; v<=level; ++v) {
853          px=(float)v/level;
[7353]854
[7410]855
856
857
858
[8088]859          //Vertice[u*(tesselation+1)+v]=       temp[0]*((1.0f-px)*(1.0f-px))+ temp[1]*((1.0f-px)*px*2)+ temp[2]*(px*px);
[7395]860          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);
861          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);
862          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);
[7353]863
[7395]864          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);
865          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);
866          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);
[7353]867
[7395]868          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);
869          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);
870          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);
871          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);
[7353]872
873
874
[7395]875        }
876      }
[7353]877
[7395]878      //Create indices
879      GLuint* indices= & ((GLuint*)(this->patchIndexes))[level*level1*2*this->patchOffset];
[7353]880
[7395]881      for(int row=0; row<level; ++row) {
882        for(int point=0; point<=level; ++point) {
883          //calculate indices
884          //reverse them to reverse winding
885          indices[(row*(level1)+point)*2+1]=row*(level1)+point;
886          indices[(row*(level1)+point)*2]=(row+1)*(level1)+point;
887        }
888      }
889
890
891      //***********************************************************************************************************************
892      // Debug Model
893      //***********************************************************************************************************************
[8088]894
[7395]895      this->VertexArrayModels[this->patchOffset] = new VertexArrayModel();
896      VertexArrayModel*  tmp = this->VertexArrayModels[this->patchOffset];
897      tmp->newStripe();
898      int a = 0;
899      int b = -1;
900      tmp->addVertex(controlsTmp[0].position[0],controlsTmp[0].position[1], controlsTmp[0].position[2]);
901      tmp->addNormal(1,0,0);
902      tmp->addTexCoor(0.0,0.0);
903      tmp->addColor(0.3,0.0,0.0);
904      tmp->addIndice(1+b);
905      tmp->addIndice(4+a);
906      tmp->addVertex(controlsTmp[1].position[0],controlsTmp[1].position[1], controlsTmp[1].position[2]);
907      tmp->addNormal(1,0,0);
908      tmp->addTexCoor(0.0,0.4);
909      tmp->addColor(0.3,0.0,0.0);
910      tmp->addIndice(2+b);
911      tmp->addIndice(5+a);
912      tmp->addVertex(controlsTmp[2].position[0],controlsTmp[2].position[1], controlsTmp[2].position[2]);
913      tmp->addNormal(1,0,0);
914      tmp->addTexCoor(0.0,1.0);
915      tmp->addColor(0.1,0.0,0.0);
916      tmp->addIndice(3+b);
917      tmp->addIndice(6+a);
918
919      tmp->addVertex(controlsTmp[2].position[0],controlsTmp[2].position[1], controlsTmp[2].position[2]);
920      tmp->addNormal(1,0,0);
921      tmp->addTexCoor(0.0,1.0);
922      tmp->addColor(0.1,0.0,0.0);
923      tmp->addIndice(7+a);
924      //tmp->addIndice(6);
925
926      tmp->newStripe();
927
928      tmp->addVertex(controlsTmp[0].position[0],controlsTmp[0].position[1], controlsTmp[0].position[2]);
929      tmp->addNormal(1,0,0);
930      tmp->addTexCoor(0.0,0.0);
931      tmp->addColor(0.1,0.1,0.1);
932      tmp->addIndice(5+b);
933      tmp->addIndice(8+a);
934      tmp->addVertex(controlsTmp[1].position[0],controlsTmp[1].position[1], controlsTmp[1].position[2]);
935      tmp->addNormal(1,0,0);
936      tmp->addTexCoor(0.0,0.4);
937      tmp->addColor(0.1,0.1,0.1);
938      tmp->addIndice(6+b);
939      tmp->addIndice(9+a);
940      tmp->addVertex(controlsTmp[2].position[0],controlsTmp[2].position[1], controlsTmp[2].position[2]);
941      tmp->addNormal(1,0,0);
942      tmp->addTexCoor(0.0,1.0);
943      tmp->addColor(0.1,0.1,0.1);
944      tmp->addIndice(7+b);
945      tmp->addIndice(10+a);
946      tmp->addVertex(controlsTmp[2].position[0],controlsTmp[2].position[1], controlsTmp[2].position[2]+0.01);
947      tmp->addNormal(1,0,0);
948      tmp->addTexCoor(0.0,1.0);
949      tmp->addColor(0.1,0.1,0.1);
950      //tmp->addIndice(5);
951      tmp->addIndice(11+a);
952
953      tmp->newStripe();
954
955
956
957      tmp->addVertex(controlsTmp[3].position[0],controlsTmp[3].position[1], controlsTmp[3].position[2]);
958      tmp->addNormal(0,1,0);
959      tmp->addTexCoor(0.5,0.0);
960      tmp->addColor(0.1,0.1,0.1);
961      tmp->addIndice(9+b);
962      tmp->addIndice(12+a);
963      tmp->addVertex(controlsTmp[4].position[0],controlsTmp[4].position[1], controlsTmp[4].position[2]);
964      tmp->addNormal(1,0,0);
965      tmp->addTexCoor(0.5,0.5);
966      tmp->addColor(0.1,0.1,0.1);
967      tmp->addIndice(10+b);
968      tmp->addIndice(13+a);
969      tmp->addVertex(controlsTmp[5].position[0],controlsTmp[5].position[1], controlsTmp[5].position[2]);
970      tmp->addNormal(1,0,0);
971      tmp->addTexCoor(0.5,1.0);
972      tmp->addColor(0.1,0.1,0.1);
973      tmp->addIndice(11+b);
974      tmp->addIndice(14+a);
975      tmp->addVertex(controlsTmp[5].position[0],controlsTmp[5].position[1], controlsTmp[5].position[2]+0.01);
976      tmp->addNormal(1,0,0);
977      tmp->addTexCoor(0.5,1.0);
978      tmp->addColor(0.1,0.1,0.1);
979
980      tmp->addIndice(15+a);
981      //tmp->addIndice(9);
982      tmp->newStripe();
983
984      tmp->addVertex(controlsTmp[6].position[0],controlsTmp[6].position[1], controlsTmp[6].position[2]);
985      tmp->addNormal(1,0,0);
986      tmp->addTexCoor(1.0,0.0);
987      tmp->addColor(0.1,0.1,0.1);
988      tmp->addIndice(13+b);
989      tmp->addIndice(16+a);
990
991      tmp->addVertex(controlsTmp[7].position[0],controlsTmp[7].position[1], controlsTmp[7].position[2]);
992      tmp->addNormal(0,1,0);
993      tmp->addTexCoor(1.0,0.5);
994      tmp->addColor(0.1,0.1,0.1);
995      tmp->addIndice(14+b);
996      tmp->addIndice(17+a);
997      tmp->addVertex(controlsTmp[8].position[0],controlsTmp[8].position[1], controlsTmp[8].position[2]);
998      tmp->addNormal(1,0,0);
999      tmp->addTexCoor(1.0,1.0);
1000      tmp->addColor(0.1,0.1,0.1);
1001      tmp->addIndice(15+b);
1002      tmp->addIndice(18+a);
1003      tmp->addVertex(controlsTmp[8].position[0],controlsTmp[8].position[1], controlsTmp[8].position[2]);
1004      tmp->addNormal(1,0,0);
1005      tmp->addTexCoor(1.0,1.0);
1006      tmp->addColor(0.1,0.1,0.1);
1007      tmp->addIndice(19+a);
1008      //tmp->addIndice(13);
1009
1010      tmp->newStripe();
1011      tmp->addVertex(controlsTmp[6].position[0],controlsTmp[6].position[1], controlsTmp[6].position[2]);
1012      tmp->addNormal(1,0,0);
1013      tmp->addTexCoor(1.0,0.0);
1014      tmp->addColor(0.1,0.1,0.1);
1015      tmp->addIndice(17+b);
1016
1017      tmp->addVertex(controlsTmp[7].position[0],controlsTmp[7].position[1], controlsTmp[7].position[2]);
1018      tmp->addNormal(0,1,0);
1019      tmp->addTexCoor(1.0,0.5);
1020      tmp->addColor(0.1,0.1,0.1);
1021      tmp->addIndice(18+b);
1022
1023      tmp->addVertex(controlsTmp[8].position[0],controlsTmp[8].position[1], controlsTmp[8].position[2]);
1024      tmp->addNormal(1,0,0);
1025      tmp->addTexCoor(1.0,1.0);
1026      tmp->addColor(0.1,0.1,0.1);
1027      tmp->addIndice(19+b);
1028
1029      tmp->addVertex(controlsTmp[8].position[0],controlsTmp[8].position[1], controlsTmp[8].position[2]);
1030      tmp->addNormal(1,0,0);
1031      tmp->addTexCoor(1.0,1.0);
1032      tmp->addColor(0.1,0.1,0.1);
1033
1034      tmp->newStripe();
1035
1036      tmp->finalize();
1037      // End of DebugModel
1038
1039      this->patchOffset++;
1040    }// For
1041  } // For
1042
1043  // Overwrite Face->meshvert;
1044  // Overwrite Face->n_meshverts;
[7465]1045  int sz = (size0 -1)/2 * (size1 -1)/2; // num patches
[7395]1046  Face->meshvert = patchOffset -sz;  //3*(patchOffset-sz)*level1*level1;
1047  Face->n_meshverts = sz;
[9003]1048  PRINTF(0)("BSP FILE: sz: %i. \n", sz);
1049  PRINTF(0)("BSP FILE: Face->meshvert %i . \n", Face->meshvert);
[7395]1050
1051  //Face->n_meshverts = sz;
[7353]1052}
[7801]1053
[8081]1054//!TODO: This is a good place to do LittleEndian to BigEndian conversion!
[7801]1055void BspFile::swapAllBspCoordinates()
1056{
[8088]1057
[7805]1058  for(int i = 0; i < this->numVertex ; ++i)
1059  {
1060    this->swapCoords(&((BspVertex *)this->vertice)[i].position[0]);
1061    this->swapCoords(&((BspVertex *)this->vertice)[i].normal[0]);
1062
[8088]1063
[7805]1064  }
[8088]1065
[7805]1066  for(int i = 0; i < this->numLeafs ; ++i)
1067  {
1068    this->swapCoords(this->leaves[i].mins);
1069    this->swapCoords(this->leaves[i].maxs);
[8088]1070
[7805]1071  }
[8088]1072
[7805]1073  for(int i = 0; i < this->numPlanes; ++i)
1074  {
1075    float sto = this->planes[i].x;
[8030]1076    this->planes[i].x =  this->planes[i].y;
1077    this->planes[i].y =  this->planes[i].z;
[7805]1078    this->planes[i].z = sto;
[8030]1079    this->planes[i].d = scale * this->planes[i].d ;
[7805]1080  }
1081
[8088]1082
[7805]1083  for(int i = 0; i < this->numFaces; ++i)
1084  {
1085    this->swapCoords(this->faces[i].normal);
1086  }
[8088]1087
[7801]1088}
1089
[7805]1090void BspFile::swapCoords(int *array)
[7801]1091{
[8081]1092  if( scale < 1)
1093  {
[7805]1094  int sto  =  array[0];
[8030]1095  array[0] =  array[1] / (int) ( 1/ scale);
1096  array[1] = array[2] / (int) (1/scale);
1097  array[2] =  sto / (int) (1/scale);
[8081]1098  }
1099  else
1100  {
1101    int sto  =  array[0];
[10165]1102    array[0] =  (int) scale * array[1] ;
1103    array[1] =  (int) scale * array[2];
1104    array[2] =  (int) scale * sto ;
[8081]1105  }
[8088]1106
[7801]1107}
1108
[7805]1109void BspFile::swapCoords(float * array)
[7801]1110{
[7805]1111  float sto  =  array[0];
[8030]1112  array[0] =  scale * array[1];
1113  array[1] =  scale * array[2];
1114  array[2] =  scale * sto;
[7801]1115}
1116
Note: See TracBrowser for help on using the repository browser.