Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/graphics/importer/bsp/bsp_file.cc @ 10033

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

moved some of the importer sources, probably will need to rebuild the project

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];
[7353]432  struct stat results;
[8088]433
434
[7385]435  this->Materials = new AMat[this->numTextures];
[7395]436  for(int i = 0 ; i < this->numTextures; i++) {
[8894]437    PRINTF(4)("BSP FILE: Texture : %s. \n", &this->textures[8+ 72*i]);
[8088]438
439
[7544]440    strcpy(fileName, &this->textures[8+ 72*i]);
441    if(strlen(fileName) == 0)
442    {
[8490]443
[8088]444     //         Default Material
[8490]445    this->Materials[i].mat = new Material();
[7579]446    this->Materials[i].mat->setDiffuse(0.1,0.1,1.0);
447    this->Materials[i].mat->setAmbient(0.1,0.1,1.0 );
448    this->Materials[i].mat->setSpecular(0.4,0.4,1.0);
[7544]449    //this->Materials[i]->setShininess(100.0);
[7801]450   // this->Materials[i].mat->setTransparency(1.0);
[7544]451    this->Materials[i].mat->setDiffuseMap("pictures/ground.tga");
452    this->Materials[i].mat->setAmbientMap("pictures/ground.tga");
453    this->Materials[i].mat->setSpecularMap("pictures/ground.tga");
454    this->Materials[i].alpha = false;
[7801]455    this->Materials[i].animated = false;
[7544]456     continue;
[8088]457    }
458
[7801]459      // Check for mov
[8490]460    strcpy(fileName, baseName);
461    strcpy(ext, &this->textures[8+ 72*i]);
462    strncat(fileName, ext, strlen(fileName) + strlen(&this->textures[8+ 72*i]) );
[7801]463    strcpy(ext, ".mov");
[8490]464    strncat (fileName, ext, strlen(fileName) );
[7801]465
[8894]466    PRINTF(4)("BSP FILE: Name %s . \n", fileName);
[8490]467
[9869]468    absFileName = Resources::ResourceManager::getInstance()->prependAbsoluteMainPath(fileName);
[7801]469
[8088]470    if(File(absFileName).exists()) {
[8894]471      PRINTF(4)("BSP FILE: gefunden . \n");
[7801]472      this->Materials[i] = this->loadAVI(fileName);
473      continue;
474    }
[8088]475
[7801]476    // Check for avi
[8490]477    strcpy(fileName, baseName);
478    strcpy(ext, &this->textures[8+ 72*i]);
479    strncat(fileName, ext, strlen(fileName) + strlen(&this->textures[8+ 72*i]) );
[7801]480    strcpy(ext, ".avi");
481    strncat (fileName, ext, strlen(fileName));
482
[9869]483    absFileName = Resources::ResourceManager::getInstance()->prependAbsoluteMainPath(fileName);
[7801]484
[8088]485    if(File(absFileName).exists()) {
[8894]486      PRINTF(4)("BSP FILE: gefunden . \n");
[7801]487      this->Materials[i] = this->loadAVI(fileName);
488      continue;
489    }
[8088]490
[7801]491       // Check for mpg
[8490]492    strcpy(fileName, baseName);
493    strcpy(ext, &this->textures[8+ 72*i]);
494    strncat(fileName, ext, strlen(fileName) + strlen(&this->textures[8+ 72*i]) );
[7801]495    strcpy(ext, ".mpg");
496    strncat (fileName, ext, strlen(fileName));
497
[9869]498    absFileName = Resources::ResourceManager::getInstance()->prependAbsoluteMainPath(fileName);
[7801]499
[8088]500    if(File(absFileName).exists()) {
[9003]501      PRINTF(4)("BSP FILE: gefunden . \n");
[7801]502      this->Materials[i] = this->loadAVI(fileName);
503      continue;
504    }
[8088]505
[7395]506    // Check for tga
[8490]507    strcpy(fileName, baseName);
508    strcpy(ext, &this->textures[8+ 72*i]);
509    strncat(fileName, ext, strlen(fileName) + strlen(&this->textures[8+ 72*i]) );
[7353]510    strcpy(ext, ".tga");
511    strncat (fileName, ext, strlen(fileName));
[7395]512
[9869]513    absFileName = Resources::ResourceManager::getInstance()->prependAbsoluteMainPath(fileName);
[7544]514
[8088]515    if(File(absFileName).exists()) {
[8894]516      PRINTF(4)("BSP FILE: gefunden . \n");
[7395]517      this->Materials[i] = this->loadMat(fileName);
518      continue;
[7353]519    }
520    // Check for TGA
[8490]521    strcpy(fileName, baseName);
522    strcpy(ext, &this->textures[8+ 72*i]);
523    strncat(fileName, ext, strlen(fileName) + strlen(&this->textures[8+ 72*i]) );
[7353]524    strcpy(ext, ".TGA");
525    strncat (fileName, ext, strlen(fileName));
[9869]526    absFileName = Resources::ResourceManager::getInstance()->prependAbsoluteMainPath(fileName);
[7385]527
[8088]528    if(File(absFileName).exists()/*stat( absFileName.c_str() , &results) == 0*/) {
[8894]529      PRINTF(4)("BSP FILE: gefunden . \n");
[7395]530      this->Materials[i] = this->loadMat(fileName);
531      continue;
[7353]532    }
533    // Check for jpg
[8490]534    strcpy(fileName, baseName);
535    strcpy(ext, &this->textures[8+ 72*i]);
536    strncat(fileName, ext, strlen(fileName) + strlen(&this->textures[8+ 72*i]) );
[7353]537    strcpy(ext, ".jpg");
538    strncat (fileName, ext, strlen(fileName));
[9869]539    absFileName = Resources::ResourceManager::getInstance()->prependAbsoluteMainPath(fileName);
[8088]540    if(File(absFileName).exists()) {
[8894]541      PRINTF(4)("BSP FILE: gefunden . \n");
[7395]542      this->Materials[i] =this->loadMat(fileName);
543      continue;
[7353]544    }
545
546
[7395]547    // Check for JPG
[8490]548    strcpy(fileName, baseName);
549    strcpy(ext, &this->textures[8+ 72*i]);
550    strncat(fileName, ext, strlen(fileName) + strlen(&this->textures[8+ 72*i]) );
[7395]551    strcpy(ext, ".JPG");
552    strncat (fileName, ext, strlen(fileName));
[9869]553    absFileName = Resources::ResourceManager::getInstance()->prependAbsoluteMainPath(fileName);
[8088]554    if(File(absFileName).exists()) {
[8894]555      PRINTF(4)("BSP FILE: gefunden . \n");
[7395]556      this->Materials[i] =this->loadMat(fileName);
557      continue;
[7353]558    }
559
560
[8088]561
[7395]562    // Check for bmp
[8490]563    strcpy(fileName, baseName);
564    strcpy(ext, &this->textures[8+ 72*i]);
565    strncat(fileName, ext, strlen(fileName) + strlen(&this->textures[8+ 72*i]) );
[7395]566    strcpy(ext, ".bmp");
567    strncat (fileName, ext, strlen(fileName));
[9869]568    absFileName = Resources::ResourceManager::getInstance()->prependAbsoluteMainPath(fileName);
[7395]569
[8088]570    if(File(absFileName).exists()) {
[8894]571      PRINTF(4)("BSP FILE: gefunden . \n");
[7395]572      this->Materials[i] =this->loadMat(fileName);
[7544]573      continue;
[7353]574    }
575
[7395]576    // Check for BMP
[8490]577    strcpy(fileName, baseName);
578    strcpy(ext, &this->textures[8+ 72*i]);
579    strncat(fileName, ext, strlen(fileName) + strlen(&this->textures[8+ 72*i]) );
[7353]580    strcpy(ext, ".BMP");
581    strncat (fileName, ext, strlen(fileName));
[9869]582    absFileName = Resources::ResourceManager::getInstance()->prependAbsoluteMainPath(fileName);
[7353]583
[8088]584    if(File(absFileName).exists()) {
[8894]585      PRINTF(4)("BSP FILE: gefunden . \n");
[7395]586      this->Materials[i] = this->loadMat(fileName);
587      continue;
[7353]588    }
[8088]589
[9025]590    PRINTF(0)("BSP FILE: Texture %s not found.\n",&this->textures[8+ 72*i] );
[8088]591    //  Default Material
[7395]592    this->Materials[i].mat = new Material();
593    this->Materials[i].mat->setDiffuse(0.1,0.1,0.1);
594    this->Materials[i].mat->setAmbient(0.1,0.1,0.1 );
595    this->Materials[i].mat->setSpecular(0.4,0.4,0.4);
596    //this->Materials[i]->setShininess(100.0);
[7801]597    //this->Materials[i].mat->setTransparency(1.0);
598    this->Materials[i].mat->setDiffuseMap("pictures/error_texture.png");
599    this->Materials[i].mat->setAmbientMap("pictures/error_texture.png");
600    this->Materials[i].mat->setSpecularMap("pictures/error_texture.png");
601    this->Materials[i].alpha = true;
602    this->Materials[i].animated = false;
[8088]603
[7353]604  }
605}
606
607
[7385]608AMat BspFile::loadMat(char* mat)
[7353]609{
[7385]610  AMat tmpAMat;
611  this->testSurf = NULL;
612
[9869]613  this->testSurf = IMG_Load(Resources::ResourceManager::getInstance()->prependAbsoluteMainPath(mat).c_str());
[7395]614  if(this->testSurf != NULL) {
615    if(this->testSurf->format->Amask != 0 ) tmpAMat.alpha = true;
[8088]616    else                                       tmpAMat.alpha = false;
[7395]617  } else   tmpAMat.alpha = false;
[7385]618
[7353]619  Material* tmp = new Material();
[7801]620   tmp->setDiffuse(1.0,1.0,1.0);
621   tmp->setAmbient(1.0,1.0,1.0 );
[7395]622  tmp->setSpecular(1.0,1.0,1.0);
[8088]623  //    tmp->setShininess(.5);
624  //    tmp->setTransparency(0.0);
[7353]625
[7395]626  tmp->setDiffuseMap(mat);
627
[7385]628  tmpAMat.mat = tmp;
[7801]629  tmpAMat.animated = false;
630  return tmpAMat;
631}
[7353]632
[7801]633AMat BspFile::loadAVI(char* mat)
634{
635  AMat tmpAMat;
636
637
638  MoviePlayer * testMC = new MoviePlayer(mat);
639  testMC->start(0);
[9025]640
[8490]641  this->MovieMaterials.push_back(testMC);
[7801]642
643  //Material* tmp = new Material();
644 // tmp->setDiffuse(1.0,1.0,1.0);
645  //tmp->setAmbient(1.0,1.0,1.0 );
646  //tmp->setSpecular(1.0,1.0,1.0);
[8088]647  //    tmp->setShininess(.5);tmpAMat
648  //    tmp->setTransparency(0.0);
[7801]649
650  //tmp->setDiffuseMap(mat);
651
652  tmpAMat.aviMat = testMC;
653  tmpAMat.animated = true;
654  tmpAMat.alpha = true;
[7385]655  return tmpAMat;
[7353]656}
657
[7465]658unsigned int BspFile::loadLightMapToGL(lightmap& lightMapTexture)
659{
660  int      errorCode = 0;           //!< the error code for the texture loading functions
[7544]661  unsigned int   lightMap;          //!< the OpenGL texture handle
[7465]662  int      mipmapLevel = 0;         //!< the maximum mipmap level for this texture
663  int      mipmapWidth = 0;         //!< the width of the mipmap
[8081]664  int      mipmapHight = 0;         //!< the height of the mipmap3
[7465]665  float sc, scale, temp;
666  for(int i = 0; i < 128*128*3 ; i++)
667  {
668  sc =  ((unsigned char *)(&lightMapTexture))[i];
[7544]669  sc *= 1/255.0;
[8490]670
671  scale = 1.0f; // Adjust brightness here
672
[7465]673  if(sc > 1.0f && (temp = (1.0f/sc)) < scale) scale=temp;
674  scale*=255.0;
675  sc*=scale;
[8030]676  if(false)
[8088]677    ((unsigned char *)(&lightMapTexture))[i] = (unsigned char)sc + 75;
[7544]678  else
[8081]679    ((unsigned char *)(&lightMapTexture))[i] = (unsigned char)sc ;
[7465]680  }
[8088]681
[7465]682  glGenTextures(1, &lightMap);
683  glBindTexture(GL_TEXTURE_2D,  lightMap);
684  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
685  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
686  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
687  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
688
689  glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_PRIORITY, 2);
690
691
692  /* control the mipmap levels */
693  glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_MIN_LOD, 5);
694  glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_MAX_LOD, 0);
695
696  /* build the Texture  OpenGL V >= 1.1 */
697   glTexImage2D(GL_TEXTURE_2D,
698                   0,
699                   GL_RGBA8,
700                   128,
701                   128,
702                   0,
703                   GL_RGB,
704                   GL_UNSIGNED_BYTE,
705                   (const GLvoid *)&lightMapTexture);
706
[8088]707
[7465]708   // build the MipMaps automaticaly
709   errorCode = gluBuild2DMipmaps(GL_TEXTURE_2D,
710GL_RGBA8,
711                                 128,
712                                 128,
713                                 GL_RGB,
714                                 GL_UNSIGNED_BYTE,
715                                 (const GLvoid *)&lightMapTexture
716                                );
717
718
719
[8088]720
[7465]721  return lightMap;
722
723}
724
[7511]725/**
726 * Generates a vertex-array, a indice-array and a texture-coordinates-array for iface.
[8088]727 * @param iface integer index of face
[7511]728 * @todo cleanup this function, let the user choose the level of tesselation
729 */
[8088]730
731
[7353]732void BspFile::tesselate(int iface)
733{
[7410]734  face*  Face =  &((this->faces)[iface]);
[7395]735  BspVertex *  BspVrtx = (BspVertex*)this->vertice;
736  int level = 7;
737  int level1 = 8;
[7801]738  int size0 = (Face->size[0]);
739  int size1 = (Face->size[1]);
[7395]740  // For each patch...
[7410]741  for(int i = 0; i <  ( size0 - 1)    ; i+=2) {
742    for(int j = 0; j < ( size1 -1)    ; j+=2) {
[7353]743
744
[7395]745      // Make a patch...
746      // Get controls[9];
747      BspVec controls[9];
748      BspVec controlsTmp[9];
749      BspVertex VControls[9];
750      for(int k = 0; k < 3; k++) {
751        for(int l = 0; l < 3; l++) {
[7801]752          controls[k +3*l]. position[0] =   (    BspVrtx[Face->vertex + (j * size0)+ i + l+ size0*k].position[0]);
753          controls[k +3*l]. position[1] =   (    BspVrtx[Face->vertex + (j * size0)+ i +l+ size0*k].position[1]);
754          controls[k +3*l]. position[2] =   (    BspVrtx[Face->vertex + (j * size0)+ i + l+ size0*k].position[2]); /*Face->n_vertexes*/
[7353]755
[7801]756          controlsTmp[2-k +6-3*l]. position[0] =   (    BspVrtx[Face->vertex +( j * size0)+ i + l+ size0*k].position[0]);
757          controlsTmp[2-k +6-3*l]. position[1] =   (    BspVrtx[Face->vertex + (j * size0)+ i +l+ size0*k].position[1]);
758          controlsTmp[2-k +6-3*l]. position[2] =   (    BspVrtx[Face->vertex + (j * size0)+ i + l+ size0*k].position[2]); /*Face->n_vertexes*/
[7353]759
[7801]760          VControls[k +3*l]. position[0] =   (    BspVrtx[Face->vertex +( j * size0)+ i + l+ size0*k].position[0]);
761          VControls[k +3*l]. position[1] =   (    BspVrtx[Face->vertex + (j * size0)+ i +l+ size0*k].position[1]);
762          VControls[k +3*l]. position[2] =   (    BspVrtx[Face->vertex + (j * size0)+ i + l+ size0*k].position[2]);
[7353]763
[7801]764          VControls[k +3*l]. normal[0] =   (    BspVrtx[Face->vertex +( j * size0)+ i + l+ size0*k].normal[0]);
765          VControls[k +3*l]. normal[1] =   (    BspVrtx[Face->vertex + (j * size0)+ i +l+ size0*k].normal[1]);
766          VControls[k +3*l]. normal[2] =   (    BspVrtx[Face->vertex + (j * size0)+ i + l+ size0*k].normal[2]);
[7353]767
[7801]768          VControls[k +3*l]. texcoord[0][0]=    (    BspVrtx[Face->vertex +( j * size0)+ i + l+ size0*k].texcoord[0][0]);
769          VControls[k +3*l]. texcoord[0][1] =   (    BspVrtx[Face->vertex + (j * size0)+ i +l+ size0*k].texcoord[0][1]);
770          VControls[k +3*l]. texcoord[1][0] =   (    BspVrtx[Face->vertex +( j * size0)+ i + l+ size0*k].texcoord[1][0]);
771          VControls[k +3*l]. texcoord[1][1] =   (    BspVrtx[Face->vertex + (j * size0)+ i +l+ size0*k].texcoord[1][1]);
[7353]772
773
[7395]774        }
775      }
776      //***********************************************************************************************************************
777      // Compute the vertice
778      //***********************************************************************************************************************
779      float px, py;
780      BspVertex temp[3];
781      BspVertex* Vertice = &(((BspVertex*)this->patchVertice)[level1*level1*this->patchOffset]);
[7353]782
[7395]783      for(int v=0; v<=level; ++v) {
784        px=(float)v/level;
[7353]785
[7395]786        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);
787        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);
788        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]789
[7395]790        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);
791        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);
792        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]793
[7395]794        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);
795        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);
796        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);
797        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]798
[7395]799      }
[7353]800
801
[7395]802      for(int u=1; u<=level; ++u) {
803        py=(float)u/level;
[7353]804
[7395]805        // temp[0]=controlPoints[0]*((1.0f-py)*(1.0f-py))+ controlPoints[1]*((1.0f-py)*py*2)+ controlPoints[2]*(py*py);
806        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);
807        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);
808        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]809
[7395]810        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);
811        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);
812        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]813
[7395]814        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);
815        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);
816        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);
817        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]818
819
820
[7395]821        // temp[1]=controlPoints[3]*((1.0f-py)*(1.0f-py))+ controlPoints[4]*((1.0f-py)*py*2)+ controlPoints[5]*(py*py);
822        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);
823        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);
824        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]825
[7395]826        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);
827        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);
828        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]829
[7395]830        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);
831        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);
832        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);
833        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]834
835
[7395]836        // temp[2]=controlPoints[6]*((1.0f-py)*(1.0f-py))+controlPoints[7]*((1.0f-py)*py*2)+controlPoints[8]*(py*py);
837        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);
838        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);
839        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]840
[7395]841        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);
842        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);
843        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]844
[7395]845        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);
846        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);
847        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);
848        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]849
850
851
852
[7395]853        for(int v=0; v<=level; ++v) {
854          px=(float)v/level;
[7353]855
[7410]856
857
858
859
[8088]860          //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]861          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);
862          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);
863          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]864
[7395]865          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);
866          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);
867          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]868
[7395]869          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);
870          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);
871          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);
872          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]873
874
875
[7395]876        }
877      }
[7353]878
[7395]879      //Create indices
880      GLuint* indices= & ((GLuint*)(this->patchIndexes))[level*level1*2*this->patchOffset];
[7353]881
[7395]882      for(int row=0; row<level; ++row) {
883        for(int point=0; point<=level; ++point) {
884          //calculate indices
885          //reverse them to reverse winding
886          indices[(row*(level1)+point)*2+1]=row*(level1)+point;
887          indices[(row*(level1)+point)*2]=(row+1)*(level1)+point;
888        }
889      }
890
891
892      //***********************************************************************************************************************
893      // Debug Model
894      //***********************************************************************************************************************
[8088]895
[7395]896      this->VertexArrayModels[this->patchOffset] = new VertexArrayModel();
897      VertexArrayModel*  tmp = this->VertexArrayModels[this->patchOffset];
898      tmp->newStripe();
899      int a = 0;
900      int b = -1;
901      tmp->addVertex(controlsTmp[0].position[0],controlsTmp[0].position[1], controlsTmp[0].position[2]);
902      tmp->addNormal(1,0,0);
903      tmp->addTexCoor(0.0,0.0);
904      tmp->addColor(0.3,0.0,0.0);
905      tmp->addIndice(1+b);
906      tmp->addIndice(4+a);
907      tmp->addVertex(controlsTmp[1].position[0],controlsTmp[1].position[1], controlsTmp[1].position[2]);
908      tmp->addNormal(1,0,0);
909      tmp->addTexCoor(0.0,0.4);
910      tmp->addColor(0.3,0.0,0.0);
911      tmp->addIndice(2+b);
912      tmp->addIndice(5+a);
913      tmp->addVertex(controlsTmp[2].position[0],controlsTmp[2].position[1], controlsTmp[2].position[2]);
914      tmp->addNormal(1,0,0);
915      tmp->addTexCoor(0.0,1.0);
916      tmp->addColor(0.1,0.0,0.0);
917      tmp->addIndice(3+b);
918      tmp->addIndice(6+a);
919
920      tmp->addVertex(controlsTmp[2].position[0],controlsTmp[2].position[1], controlsTmp[2].position[2]);
921      tmp->addNormal(1,0,0);
922      tmp->addTexCoor(0.0,1.0);
923      tmp->addColor(0.1,0.0,0.0);
924      tmp->addIndice(7+a);
925      //tmp->addIndice(6);
926
927      tmp->newStripe();
928
929      tmp->addVertex(controlsTmp[0].position[0],controlsTmp[0].position[1], controlsTmp[0].position[2]);
930      tmp->addNormal(1,0,0);
931      tmp->addTexCoor(0.0,0.0);
932      tmp->addColor(0.1,0.1,0.1);
933      tmp->addIndice(5+b);
934      tmp->addIndice(8+a);
935      tmp->addVertex(controlsTmp[1].position[0],controlsTmp[1].position[1], controlsTmp[1].position[2]);
936      tmp->addNormal(1,0,0);
937      tmp->addTexCoor(0.0,0.4);
938      tmp->addColor(0.1,0.1,0.1);
939      tmp->addIndice(6+b);
940      tmp->addIndice(9+a);
941      tmp->addVertex(controlsTmp[2].position[0],controlsTmp[2].position[1], controlsTmp[2].position[2]);
942      tmp->addNormal(1,0,0);
943      tmp->addTexCoor(0.0,1.0);
944      tmp->addColor(0.1,0.1,0.1);
945      tmp->addIndice(7+b);
946      tmp->addIndice(10+a);
947      tmp->addVertex(controlsTmp[2].position[0],controlsTmp[2].position[1], controlsTmp[2].position[2]+0.01);
948      tmp->addNormal(1,0,0);
949      tmp->addTexCoor(0.0,1.0);
950      tmp->addColor(0.1,0.1,0.1);
951      //tmp->addIndice(5);
952      tmp->addIndice(11+a);
953
954      tmp->newStripe();
955
956
957
958      tmp->addVertex(controlsTmp[3].position[0],controlsTmp[3].position[1], controlsTmp[3].position[2]);
959      tmp->addNormal(0,1,0);
960      tmp->addTexCoor(0.5,0.0);
961      tmp->addColor(0.1,0.1,0.1);
962      tmp->addIndice(9+b);
963      tmp->addIndice(12+a);
964      tmp->addVertex(controlsTmp[4].position[0],controlsTmp[4].position[1], controlsTmp[4].position[2]);
965      tmp->addNormal(1,0,0);
966      tmp->addTexCoor(0.5,0.5);
967      tmp->addColor(0.1,0.1,0.1);
968      tmp->addIndice(10+b);
969      tmp->addIndice(13+a);
970      tmp->addVertex(controlsTmp[5].position[0],controlsTmp[5].position[1], controlsTmp[5].position[2]);
971      tmp->addNormal(1,0,0);
972      tmp->addTexCoor(0.5,1.0);
973      tmp->addColor(0.1,0.1,0.1);
974      tmp->addIndice(11+b);
975      tmp->addIndice(14+a);
976      tmp->addVertex(controlsTmp[5].position[0],controlsTmp[5].position[1], controlsTmp[5].position[2]+0.01);
977      tmp->addNormal(1,0,0);
978      tmp->addTexCoor(0.5,1.0);
979      tmp->addColor(0.1,0.1,0.1);
980
981      tmp->addIndice(15+a);
982      //tmp->addIndice(9);
983      tmp->newStripe();
984
985      tmp->addVertex(controlsTmp[6].position[0],controlsTmp[6].position[1], controlsTmp[6].position[2]);
986      tmp->addNormal(1,0,0);
987      tmp->addTexCoor(1.0,0.0);
988      tmp->addColor(0.1,0.1,0.1);
989      tmp->addIndice(13+b);
990      tmp->addIndice(16+a);
991
992      tmp->addVertex(controlsTmp[7].position[0],controlsTmp[7].position[1], controlsTmp[7].position[2]);
993      tmp->addNormal(0,1,0);
994      tmp->addTexCoor(1.0,0.5);
995      tmp->addColor(0.1,0.1,0.1);
996      tmp->addIndice(14+b);
997      tmp->addIndice(17+a);
998      tmp->addVertex(controlsTmp[8].position[0],controlsTmp[8].position[1], controlsTmp[8].position[2]);
999      tmp->addNormal(1,0,0);
1000      tmp->addTexCoor(1.0,1.0);
1001      tmp->addColor(0.1,0.1,0.1);
1002      tmp->addIndice(15+b);
1003      tmp->addIndice(18+a);
1004      tmp->addVertex(controlsTmp[8].position[0],controlsTmp[8].position[1], controlsTmp[8].position[2]);
1005      tmp->addNormal(1,0,0);
1006      tmp->addTexCoor(1.0,1.0);
1007      tmp->addColor(0.1,0.1,0.1);
1008      tmp->addIndice(19+a);
1009      //tmp->addIndice(13);
1010
1011      tmp->newStripe();
1012      tmp->addVertex(controlsTmp[6].position[0],controlsTmp[6].position[1], controlsTmp[6].position[2]);
1013      tmp->addNormal(1,0,0);
1014      tmp->addTexCoor(1.0,0.0);
1015      tmp->addColor(0.1,0.1,0.1);
1016      tmp->addIndice(17+b);
1017
1018      tmp->addVertex(controlsTmp[7].position[0],controlsTmp[7].position[1], controlsTmp[7].position[2]);
1019      tmp->addNormal(0,1,0);
1020      tmp->addTexCoor(1.0,0.5);
1021      tmp->addColor(0.1,0.1,0.1);
1022      tmp->addIndice(18+b);
1023
1024      tmp->addVertex(controlsTmp[8].position[0],controlsTmp[8].position[1], controlsTmp[8].position[2]);
1025      tmp->addNormal(1,0,0);
1026      tmp->addTexCoor(1.0,1.0);
1027      tmp->addColor(0.1,0.1,0.1);
1028      tmp->addIndice(19+b);
1029
1030      tmp->addVertex(controlsTmp[8].position[0],controlsTmp[8].position[1], controlsTmp[8].position[2]);
1031      tmp->addNormal(1,0,0);
1032      tmp->addTexCoor(1.0,1.0);
1033      tmp->addColor(0.1,0.1,0.1);
1034
1035      tmp->newStripe();
1036
1037      tmp->finalize();
1038      // End of DebugModel
1039
1040      this->patchOffset++;
1041    }// For
1042  } // For
1043
1044  // Overwrite Face->meshvert;
1045  // Overwrite Face->n_meshverts;
[7465]1046  int sz = (size0 -1)/2 * (size1 -1)/2; // num patches
[7395]1047  Face->meshvert = patchOffset -sz;  //3*(patchOffset-sz)*level1*level1;
1048  Face->n_meshverts = sz;
[9003]1049  PRINTF(0)("BSP FILE: sz: %i. \n", sz);
1050  PRINTF(0)("BSP FILE: Face->meshvert %i . \n", Face->meshvert);
[7395]1051
1052  //Face->n_meshverts = sz;
[7353]1053}
[7801]1054
[8081]1055//!TODO: This is a good place to do LittleEndian to BigEndian conversion!
[7801]1056void BspFile::swapAllBspCoordinates()
1057{
[8088]1058
[7805]1059  for(int i = 0; i < this->numVertex ; ++i)
1060  {
1061    this->swapCoords(&((BspVertex *)this->vertice)[i].position[0]);
1062    this->swapCoords(&((BspVertex *)this->vertice)[i].normal[0]);
1063
[8088]1064
[7805]1065  }
[8088]1066
[7805]1067  for(int i = 0; i < this->numLeafs ; ++i)
1068  {
1069    this->swapCoords(this->leaves[i].mins);
1070    this->swapCoords(this->leaves[i].maxs);
[8088]1071
[7805]1072  }
[8088]1073
[7805]1074  for(int i = 0; i < this->numPlanes; ++i)
1075  {
1076    float sto = this->planes[i].x;
[8030]1077    this->planes[i].x =  this->planes[i].y;
1078    this->planes[i].y =  this->planes[i].z;
[7805]1079    this->planes[i].z = sto;
[8030]1080    this->planes[i].d = scale * this->planes[i].d ;
[7805]1081  }
1082
[8088]1083
[7805]1084  for(int i = 0; i < this->numFaces; ++i)
1085  {
1086    this->swapCoords(this->faces[i].normal);
1087  }
[8088]1088
[7801]1089}
1090
[7805]1091void BspFile::swapCoords(int *array)
[7801]1092{
[8081]1093  if( scale < 1)
1094  {
[7805]1095  int sto  =  array[0];
[8030]1096  array[0] =  array[1] / (int) ( 1/ scale);
1097  array[1] = array[2] / (int) (1/scale);
1098  array[2] =  sto / (int) (1/scale);
[8081]1099  }
1100  else
1101  {
1102    int sto  =  array[0];
1103    array[0] =  scale * array[1] ;
1104    array[1] =  scale * array[2];
[8088]1105    array[2] =  scale * sto ;
[8081]1106  }
[8088]1107
[7801]1108}
1109
[7805]1110void BspFile::swapCoords(float * array)
[7801]1111{
[7805]1112  float sto  =  array[0];
[8030]1113  array[0] =  scale * array[1];
1114  array[1] =  scale * array[2];
1115  array[2] =  scale * sto;
[7801]1116}
1117
Note: See TracBrowser for help on using the repository browser.