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
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2006 orx
5
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.
10
11   ### File Specific:
12   main-programmer: bottac@ee.ethz.ch
13
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/
18*/
19
20
21#include "bsp_file.h"
22#include "bsp_tree_node.h"
23#include <fstream>
24#include "util/loading/resource_manager.h"
25
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"
34#include "movie_player.h"
35
36#include <SDL/SDL_endian.h>
37#include <SDL/SDL_image.h>
38
39// STL Containers
40#include <vector>
41
42#ifdef WITH_ODE
43#include <ode/ode.h>
44#endif
45
46
47
48// Constructor
49BspFile::BspFile()
50{}
51
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;
71
72  for(int i = 0; i < this->numPatches; ++i) delete this->VertexArrayModels[i];
73 // delete  [] VertexArrayModels;
74
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;
82
83
84
85}
86
87/**
88 *  Loads a quake3 level (*.bsp)
89 * @param name the Name of the *.bsp file
90 */
91int BspFile::read(const char* name)
92{
93  this->scale = 1.0;
94  int offset;
95  int size;
96  struct stat results;
97
98
99  if (stat( name , &results) == 0) {
100    PRINTF(0)("BSP FILE: Datei %s gefunden. \n", name);
101    std::ifstream bspFile (name, std::ios::in | std::ios::binary);
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];
116    bspFile.seekg(offset);
117    bspFile.read((char*)this->nodes, size);
118
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];
128    bspFile.seekg(offset);
129    bspFile.read((char*)this->planes, size);
130
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];
140    bspFile.seekg(offset);
141    bspFile.read((char*)this->leaves, size);
142
143    // Get the Models
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];
152    bspFile.seekg(offset);
153    bspFile.read((char*)this->bspModels, size);
154
155    // Get the leafFaces
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);
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
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);
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
180    offset = SDL_SwapLE32(((int *)(header))[18]);
181    size    = SDL_SwapLE32(((int *)(header))[19]);
182    PRINTF(4)("BSP FILE: BrushesSize: %i Bytes. \n", size);
183    PRINTF(4)("BSP FILE: NumBrushes: %i. \n", size / sizeof(brush));
184    PRINTF(4)("BSP FILE: Remainder: %i. \n", size % sizeof(brush));
185    PRINTF(4)("BSP FILE: BrushesOffset: %i. \n", offset);
186    this->brushes = new brush [size/sizeof(brush)];
187    bspFile.seekg(offset);
188    bspFile.read((char*)this->brushes, size);
189
190    // Get the brushSides
191    offset =   SDL_SwapLE32(((int *)(header))[20]);
192    size    = SDL_SwapLE32(((int *)(header))[21]);
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);
197    this->numBrushSides = size/sizeof(brushside);
198    this->brushSides = new brushside [this->numBrushSides];
199    bspFile.seekg(offset);
200    bspFile.read((char*)this->brushSides, size);
201
202    // Get the Vertice
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);
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
215    offset = SDL_SwapLE32(((int *)(header))[24]);
216    size    = SDL_SwapLE32(((int *)(header))[25]);
217    PRINTF(4)("BSP FILE: MeshVertsSize: %i Bytes. \n", size);
218    PRINTF(4)("BSP FILE: NumMeshVerts: %i. \n", size / sizeof(meshvert));
219    PRINTF(4)("BSP FILE: Remainder: %i. \n", size % sizeof(meshvert));
220    PRINTF(4)("BSP FILE: MeshVertsOffset: %i. \n", offset);
221    this->meshverts = new meshvert [size / sizeof(meshvert)];
222    bspFile.seekg(offset);
223    bspFile.read((char*)this->meshverts, size);
224
225    // Get the Faces
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];
234    bspFile.seekg(offset);
235    bspFile.read((char*)this->faces, size);
236
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
246    // Get the Visdata
247    offset = SDL_SwapLE32(((int *)(header))[34]);
248    size    = SDL_SwapLE32(((int *)(header))[35]);
249
250    this->visData = new char [size];
251    bspFile.seekg(offset);
252    bspFile.read(this->visData, size);
253
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);
258
259    // Get the Textures
260    offset = SDL_SwapLE32(((int *)(header))[4]);
261    size    = SDL_SwapLE32(((int *)(header))[5]);
262
263    this->textures= new char [size];
264    bspFile.seekg(offset);
265    bspFile.read(this->textures, size);
266
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);
271    this->numTextures = size/72;
272
273    bspFile.close();
274
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();
278
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]);
283
284    //Create white texture for if no lightmap specified
285    glGenTextures(1, &this->whiteLightMap);
286    glBindTexture(GL_TEXTURE_2D, this->whiteLightMap);
287        //Create texture
288    this->whiteTexture[0]=255;
289    this->whiteTexture[1]=255;
290    this->whiteTexture[2]=255;
291
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);
296
297    glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_PRIORITY, 2);
298
299
300
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,
308                 GL_RGBA8,
309                 1,
310                 1,
311                 0,
312                 GL_RGB,
313                 GL_UNSIGNED_BYTE,
314                 (const GLvoid *)&(this->whiteTexture));
315
316   gluBuild2DMipmaps(   GL_TEXTURE_2D, GL_RGBA8, 1, 1,
317                      GL_RGB, GL_UNSIGNED_BYTE,(const GLvoid *) &(this->whiteTexture));
318
319
320
321    // Get the number of patches
322    this->numPatches = 0;
323    this->patchOffset   = 0;
324
325    for( int i = 0; i < this->numFaces; i++) {
326      face& cFace = ((face *)(this->faces))[i];
327      if (SDL_SwapLE32(cFace.type) == 2)
328        this->numPatches += (SDL_SwapLE32(cFace.size[0]) -1 ) / 2  * (SDL_SwapLE32(cFace.size[1]) -1) / 2;
329    }
330
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)];
334 // this->patchRowIndexes = new int*[7*4*this->numPatches]; Not needed?
335    #ifdef WITH_ODE
336    this->ODE_Geometry = new dTriMeshDataID[this->numPatches*7*7];
337    #endif
338    this->patchTrianglesPerRow = new char[7*4*this->numPatches];
339    this->VertexArrayModels  = new VertexArrayModel*[this->numPatches];
340
341    PRINTF(4)("BSP FILE:NumberOfPatches: %i . \n", numPatches);
342
343    this->swapAllBspCoordinates();
344
345    // Do tesselation for all Faces of type 2
346    for( int i = 0; i < this->numFaces; i++) {
347      if (SDL_SwapLE32((this->faces)[i].type) == 2)
348        this->tesselate(i);
349    }
350
351    PRINTF(4)("BSP FILE:PatchOffset: %i . \n", this->patchOffset);
352
353    return  1;
354  } else {
355    PRINTF(4)("BSP FILE: Datei nicht gefunden. \n");
356    return -1;
357  }
358
359
360
361}
362
363void BspFile::build_tree()
364{
365
366  PRINTF(4)("BSP FILE:\n");
367  PRINTF(4)("BSP FILE: Building Tree...\n");
368  root = this->build_tree_rec(0);
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);
375}
376
377/**
378 *  Called by BspFile::build_tree() only.
379 */
380BspTreeNode*   BspFile::build_tree_rec(int i)
381{
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;
391  thisNode->d          = (((plane *) this->planes) [planeIndex]).d;
392
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);
407  } // assign leav
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;
421}
422
423/**
424 *  returns the root node of the bsp-tree
425 */
426BspTreeNode* BspFile::get_root()
427{
428  return root;
429}
430
431void BspFile::load_textures()
432{
433  ::std::string absFileName;
434  char* baseName = "/worlds/bsp/";
435
436  char fileName [500];
437  char ext [500];
438  struct stat results;
439
440
441  this->Materials = new AMat[this->numTextures];
442  for(int i = 0 ; i < this->numTextures; i++) {
443    PRINTF(4)("BSP FILE: Texture : %s. \n", &this->textures[8+ 72*i]);
444
445
446    strcpy(fileName, &this->textures[8+ 72*i]);
447    if(strlen(fileName) == 0)
448    {
449
450     //         Default Material
451    this->Materials[i].mat = new Material();
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);
455    //this->Materials[i]->setShininess(100.0);
456   // this->Materials[i].mat->setTransparency(1.0);
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;
461    this->Materials[i].animated = false;
462     continue;
463    }
464
465      // Check for mov
466    strcpy(fileName, baseName);
467    strcpy(ext, &this->textures[8+ 72*i]);
468    strncat(fileName, ext, strlen(fileName) + strlen(&this->textures[8+ 72*i]) );
469    strcpy(ext, ".mov");
470    strncat (fileName, ext, strlen(fileName) );
471
472    PRINTF(4)("BSP FILE: Name %s . \n", fileName);
473
474    absFileName = Resources::ResourceManager::getInstance()->prependAbsoluteMainPath(fileName);
475
476    if(File(absFileName).exists()) {
477      PRINTF(4)("BSP FILE: gefunden . \n");
478      this->Materials[i] = this->loadAVI(fileName);
479      continue;
480    }
481
482    // Check for avi
483    strcpy(fileName, baseName);
484    strcpy(ext, &this->textures[8+ 72*i]);
485    strncat(fileName, ext, strlen(fileName) + strlen(&this->textures[8+ 72*i]) );
486    strcpy(ext, ".avi");
487    strncat (fileName, ext, strlen(fileName));
488
489    absFileName = Resources::ResourceManager::getInstance()->prependAbsoluteMainPath(fileName);
490
491    if(File(absFileName).exists()) {
492      PRINTF(4)("BSP FILE: gefunden . \n");
493      this->Materials[i] = this->loadAVI(fileName);
494      continue;
495    }
496
497       // Check for mpg
498    strcpy(fileName, baseName);
499    strcpy(ext, &this->textures[8+ 72*i]);
500    strncat(fileName, ext, strlen(fileName) + strlen(&this->textures[8+ 72*i]) );
501    strcpy(ext, ".mpg");
502    strncat (fileName, ext, strlen(fileName));
503
504    absFileName = Resources::ResourceManager::getInstance()->prependAbsoluteMainPath(fileName);
505
506    if(File(absFileName).exists()) {
507      PRINTF(4)("BSP FILE: gefunden . \n");
508      this->Materials[i] = this->loadAVI(fileName);
509      continue;
510    }
511
512    // Check for tga
513    strcpy(fileName, baseName);
514    strcpy(ext, &this->textures[8+ 72*i]);
515    strncat(fileName, ext, strlen(fileName) + strlen(&this->textures[8+ 72*i]) );
516    strcpy(ext, ".tga");
517    strncat (fileName, ext, strlen(fileName));
518
519    absFileName = Resources::ResourceManager::getInstance()->prependAbsoluteMainPath(fileName);
520
521    if(File(absFileName).exists()) {
522      PRINTF(4)("BSP FILE: gefunden . \n");
523      this->Materials[i] = this->loadMat(fileName);
524      continue;
525    }
526    // Check for TGA
527    strcpy(fileName, baseName);
528    strcpy(ext, &this->textures[8+ 72*i]);
529    strncat(fileName, ext, strlen(fileName) + strlen(&this->textures[8+ 72*i]) );
530    strcpy(ext, ".TGA");
531    strncat (fileName, ext, strlen(fileName));
532    absFileName = Resources::ResourceManager::getInstance()->prependAbsoluteMainPath(fileName);
533
534    if(File(absFileName).exists()/*stat( absFileName.c_str() , &results) == 0*/) {
535      PRINTF(4)("BSP FILE: gefunden . \n");
536      this->Materials[i] = this->loadMat(fileName);
537      continue;
538    }
539    // Check for jpg
540    strcpy(fileName, baseName);
541    strcpy(ext, &this->textures[8+ 72*i]);
542    strncat(fileName, ext, strlen(fileName) + strlen(&this->textures[8+ 72*i]) );
543    strcpy(ext, ".jpg");
544    strncat (fileName, ext, strlen(fileName));
545    absFileName = Resources::ResourceManager::getInstance()->prependAbsoluteMainPath(fileName);
546    if(File(absFileName).exists()) {
547      PRINTF(4)("BSP FILE: gefunden . \n");
548      this->Materials[i] =this->loadMat(fileName);
549      continue;
550    }
551
552
553    // Check for JPG
554    strcpy(fileName, baseName);
555    strcpy(ext, &this->textures[8+ 72*i]);
556    strncat(fileName, ext, strlen(fileName) + strlen(&this->textures[8+ 72*i]) );
557    strcpy(ext, ".JPG");
558    strncat (fileName, ext, strlen(fileName));
559    absFileName = Resources::ResourceManager::getInstance()->prependAbsoluteMainPath(fileName);
560    if(File(absFileName).exists()) {
561      PRINTF(4)("BSP FILE: gefunden . \n");
562      this->Materials[i] =this->loadMat(fileName);
563      continue;
564    }
565
566
567
568    // Check for bmp
569    strcpy(fileName, baseName);
570    strcpy(ext, &this->textures[8+ 72*i]);
571    strncat(fileName, ext, strlen(fileName) + strlen(&this->textures[8+ 72*i]) );
572    strcpy(ext, ".bmp");
573    strncat (fileName, ext, strlen(fileName));
574    absFileName = Resources::ResourceManager::getInstance()->prependAbsoluteMainPath(fileName);
575
576    if(File(absFileName).exists()) {
577      PRINTF(4)("BSP FILE: gefunden . \n");
578      this->Materials[i] =this->loadMat(fileName);
579      continue;
580    }
581
582    // Check for BMP
583    strcpy(fileName, baseName);
584    strcpy(ext, &this->textures[8+ 72*i]);
585    strncat(fileName, ext, strlen(fileName) + strlen(&this->textures[8+ 72*i]) );
586    strcpy(ext, ".BMP");
587    strncat (fileName, ext, strlen(fileName));
588    absFileName = Resources::ResourceManager::getInstance()->prependAbsoluteMainPath(fileName);
589
590    if(File(absFileName).exists()) {
591      PRINTF(4)("BSP FILE: gefunden . \n");
592      this->Materials[i] = this->loadMat(fileName);
593      continue;
594    }
595
596    PRINTF(0)("BSP FILE: Texture %s not found.\n",&this->textures[8+ 72*i] );
597    //  Default Material
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);
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;
609
610  }
611}
612
613
614AMat BspFile::loadMat(char* mat)
615{
616  AMat tmpAMat;
617  this->testSurf = NULL;
618
619  this->testSurf = IMG_Load(Resources::ResourceManager::getInstance()->prependAbsoluteMainPath(mat).c_str());
620  if(this->testSurf != NULL) {
621    if(this->testSurf->format->Amask != 0 ) tmpAMat.alpha = true;
622    else                                       tmpAMat.alpha = false;
623  } else   tmpAMat.alpha = false;
624
625  Material* tmp = new Material();
626   tmp->setDiffuse(1.0,1.0,1.0);
627   tmp->setAmbient(1.0,1.0,1.0 );
628  tmp->setSpecular(1.0,1.0,1.0);
629  //    tmp->setShininess(.5);
630  //    tmp->setTransparency(0.0);
631
632  tmp->setDiffuseMap(mat);
633
634  tmpAMat.mat = tmp;
635  tmpAMat.animated = false;
636  return tmpAMat;
637}
638
639AMat BspFile::loadAVI(char* mat)
640{
641  AMat tmpAMat;
642
643
644  MoviePlayer * testMC = new MoviePlayer(mat);
645  testMC->start(0);
646
647  this->MovieMaterials.push_back(testMC);
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);
653  //    tmp->setShininess(.5);tmpAMat
654  //    tmp->setTransparency(0.0);
655
656  //tmp->setDiffuseMap(mat);
657
658  tmpAMat.aviMat = testMC;
659  tmpAMat.animated = true;
660  tmpAMat.alpha = true;
661  return tmpAMat;
662}
663
664unsigned int BspFile::loadLightMapToGL(lightmap& lightMapTexture)
665{
666  int      errorCode = 0;           //!< the error code for the texture loading functions
667  unsigned int   lightMap;          //!< the OpenGL texture handle
668  int      mipmapLevel = 0;         //!< the maximum mipmap level for this texture
669  int      mipmapWidth = 0;         //!< the width of the mipmap
670  int      mipmapHight = 0;         //!< the height of the mipmap3
671  float sc, scale, temp;
672  for(int i = 0; i < 128*128*3 ; i++)
673  {
674  sc =  ((unsigned char *)(&lightMapTexture))[i];
675  sc *= 1/255.0;
676
677  scale = 1.0f; // Adjust brightness here
678
679  if(sc > 1.0f && (temp = (1.0f/sc)) < scale) scale=temp;
680  scale*=255.0;
681  sc*=scale;
682  if(false)
683    ((unsigned char *)(&lightMapTexture))[i] = (unsigned char)sc + 75;
684  else
685    ((unsigned char *)(&lightMapTexture))[i] = (unsigned char)sc ;
686  }
687
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
713
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
726
727  return lightMap;
728
729}
730
731/**
732 * Generates a vertex-array, a indice-array and a texture-coordinates-array for iface.
733 * @param iface integer index of face
734 * @todo cleanup this function, let the user choose the level of tesselation
735 */
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
745
746void BspFile::tesselate(int iface)
747{
748  face*  Face =  &((this->faces)[iface]);
749  BspVertex *  BspVrtx = (BspVertex*)this->vertice;
750  int level = 7;
751  int level1 = 8;
752  int size0 = (Face->size[0]);
753  int size1 = (Face->size[1]);
754  // For each patch...
755  for(int i = 0; i <  ( size0 - 1)    ; i+=2) {
756    for(int j = 0; j < ( size1 -1)    ; j+=2) {
757
758
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++) {
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*/
769
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*/
773
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]);
777
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]);
781
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]);
786
787
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]);
796
797      for(int v=0; v<=level; ++v) {
798        px=(float)v/level;
799
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);
803
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);
807
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);
812
813      }
814
815
816      for(int u=1; u<=level; ++u) {
817        py=(float)u/level;
818
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);
823
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);
827
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);
832
833
834
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);
839
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);
843
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);
848
849
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);
854
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);
858
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);
863
864
865
866
867        for(int v=0; v<=level; ++v) {
868          px=(float)v/level;
869
870
871
872
873
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);
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);
878
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);
882
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);
887
888
889
890        }
891      }
892
893      //Create indices
894      GLuint* indices= & ((GLuint*)(this->patchIndexes))[level*level1*2*this->patchOffset];
895
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
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
910
911
912      //***********************************************************************************************************************
913      // Debug Model
914      //***********************************************************************************************************************
915
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;
1066  int sz = (size0 -1)/2 * (size1 -1)/2; // num patches
1067  Face->meshvert = patchOffset -sz;  //3*(patchOffset-sz)*level1*level1;
1068  Face->n_meshverts = sz;
1069  PRINTF(0)("BSP FILE: sz: %i. \n", sz);
1070  PRINTF(0)("BSP FILE: Face->meshvert %i . \n", Face->meshvert);
1071
1072  //Face->n_meshverts = sz;
1073}
1074
1075//!TODO: This is a good place to do LittleEndian to BigEndian conversion!
1076void BspFile::swapAllBspCoordinates()
1077{
1078
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
1084
1085  }
1086
1087  for(int i = 0; i < this->numLeafs ; ++i)
1088  {
1089    this->swapCoords(this->leaves[i].mins);
1090    this->swapCoords(this->leaves[i].maxs);
1091
1092  }
1093
1094  for(int i = 0; i < this->numPlanes; ++i)
1095  {
1096    float sto = this->planes[i].x;
1097    this->planes[i].x =  this->planes[i].y;
1098    this->planes[i].y =  this->planes[i].z;
1099    this->planes[i].z = sto;
1100    this->planes[i].d = scale * this->planes[i].d ;
1101  }
1102
1103
1104  for(int i = 0; i < this->numFaces; ++i)
1105  {
1106    this->swapCoords(this->faces[i].normal);
1107  }
1108
1109}
1110
1111void BspFile::swapCoords(int *array)
1112{
1113  if( scale < 1)
1114  {
1115  int sto  =  array[0];
1116  array[0] =  array[1] / (int) ( 1/ scale);
1117  array[1] = array[2] / (int) (1/scale);
1118  array[2] =  sto / (int) (1/scale);
1119  }
1120  else
1121  {
1122    int sto  =  array[0];
1123    array[0] =  scale * array[1] ;
1124    array[1] =  scale * array[2];
1125    array[2] =  scale * sto ;
1126  }
1127
1128}
1129
1130void BspFile::swapCoords(float * array)
1131{
1132  float sto  =  array[0];
1133  array[0] =  scale * array[1];
1134  array[1] =  scale * array[2];
1135  array[2] =  scale * sto;
1136}
1137
Note: See TracBrowser for help on using the repository browser.