Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/ODE/src/lib/graphics/importer/bsp/bsp_file.cc @ 10355

Last change on this file since 10355 was 10355, checked in by bottac, 17 years ago

Here comes the updated version.

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