Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 9993 was 9919, checked in by bottac, 18 years ago

CrPhysicsFullWalk on Static Models and BSP Patches almost working. libODE≥0.7 required.
Screenshot: http://people.ee.ethz.ch/~bottac/Collision_ODE/

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#include<ode/ode.h>
43
44
45
46
47// Constructor
48BspFile::BspFile()
49{}
50
51BspFile::~ BspFile()
52{
53  delete [] nodes;
54  delete [] leaves;
55  delete [] planes;
56  delete [] bspModels;
57  delete [] leafFaces;
58  delete [] faces;
59  delete [] leafBrushes;
60  delete [] brushes;
61  delete [] brushSides;
62  delete [] vertice;
63  delete [] meshverts;
64  delete [] visData;
65  delete [] textures;
66  delete [] patchVertice;
67  delete [] patchIndexes;
68 // delete [] patchTrianglesPerRow;
69  delete [] lightMaps;
70
71  for(int i = 0; i < this->numPatches; ++i) delete this->VertexArrayModels[i];
72 // delete  [] VertexArrayModels;
73
74  for(int i = 0; i < this->numTextures; ++i)
75  {
76    delete this->Materials[i].mat;
77    //delete this->Materials[i].aviMat;
78  }
79  delete [] this->Materials;
80  //delete [] testSurf;
81
82
83
84}
85
86/**
87 *  Loads a quake3 level (*.bsp)
88 * @param name the Name of the *.bsp file
89 */
90int BspFile::read(const char* name)
91{
92  this->scale = 1.0;
93  int offset;
94  int size;
95  struct stat results;
96
97
98  if (stat( name , &results) == 0) {
99    PRINTF(0)("BSP FILE: Datei %s gefunden. \n", name);
100    std::ifstream bspFile (name, std::ios::in | std::ios::binary);
101    bspFile.read(this->header, 260);
102    PRINTF(0)("BSP FILE: BSPVersion: %i. \n", ((int *)(header) )[1]);
103    if(SDL_SwapLE32(((int *)(header) )[1]) == 46)    PRINTF(0)("BSP FILE: This is the good one! :-)  \n");
104    else  PRINTF(0)("BSP FILE: Wrong BSPVersion.\n");   //!< now, we should do some error handling
105
106    // Get the Nodes
107    offset = SDL_SwapLE32(((int *)(header) )[8]);
108    size    = SDL_SwapLE32(((int *)(header))[9]);
109    PRINTF(4)("BSP FILE: NodeSize: %i Bytes. \n", size);
110    PRINTF(4)("BSP FILE: NumNodes: %i. \n", size / sizeof(node));
111    PRINTF(4)("BSP FILE: Remainder: %i. \n", size % sizeof(node));
112    PRINTF(4)("BSP FILE: NodeOffset: %i. \n", offset);
113    this->numNodes = size/sizeof(node);
114    this->nodes = new node [this->numNodes];
115    bspFile.seekg(offset);
116    bspFile.read((char*)this->nodes, size);
117
118    // and their Planes
119    offset = SDL_SwapLE32(((int *)(header) )[6]);
120    size    = SDL_SwapLE32(((int *)(header))[7]);
121    PRINTF(4)("BSP FILE: PlanesSize: %i Bytes. \n", size);
122    PRINTF(4)("BSP FILE: NumPlanes: %i. \n", size / sizeof(plane));
123    PRINTF(4)("BSP FILE: Remainder: %i. \n", sizeof(plane));
124    PRINTF(4)("BSP FILE: PlanesOffset: %i. \n", offset);
125    this->numPlanes = size/sizeof(plane);
126    this->planes = new plane [this->numPlanes];
127    bspFile.seekg(offset);
128    bspFile.read((char*)this->planes, size);
129
130    // Get the Leafs
131    offset = SDL_SwapLE32(((int *)(header) )[10]);
132    size    = SDL_SwapLE32(((int *)(header))[11]);
133    PRINTF(4)("BSP FILE: LeaveSize: %i Bytes. \n", size);
134    PRINTF(4)("BSP FILE: NumLeaves: %i. \n", size / sizeof(leaf));
135    PRINTF(4)("BSP FILE: Remainder: %i. \n", size % sizeof(leaf));
136    PRINTF(4)("BSP FILE: LeaveOffset: %i. \n", offset);
137    this->numLeafs = size/sizeof(leaf);
138    this->leaves = new leaf [this->numLeafs];
139    bspFile.seekg(offset);
140    bspFile.read((char*)this->leaves, size);
141
142    // Get the Models
143    offset = SDL_SwapLE32(((int *)(header))[16]);
144    size    = SDL_SwapLE32(((int *)(header))[17]);
145    PRINTF(4)("BSP FILE: ModelsSize: %i Bytes. \n", size);
146    PRINTF(4)("BSP FILE: NumModels: %i. \n", size / sizeof(model));
147    PRINTF(4)("BSP FILE: Remainder: %i. \n", size % sizeof(model));
148    PRINTF(4)("BSP FILE: ModelsOffset: %i. \n", offset);
149    this->numBspModels = size/sizeof(model);
150    this->bspModels = new model [this->numBspModels];
151    bspFile.seekg(offset);
152    bspFile.read((char*)this->bspModels, size);
153
154    // Get the leafFaces
155    offset = SDL_SwapLE32(((int *)(header))[12]);
156    size    = SDL_SwapLE32(((int *)(header))[13]);
157    PRINTF(4)("BSP FILE: leafFacesSize: %i Bytes. \n", size);
158    PRINTF(4)("BSP FILE: NumleafFaces: %i. \n", size / 4);
159    PRINTF(4)("BSP FILE: Remainder: %i. \n", size % 4);
160    PRINTF(4)("BSP FILE: leafFacesOffset: %i. \n", offset);
161    this->numLeafFaces = size/4;
162    this->leafFaces = new char [size];
163    bspFile.seekg(offset);
164    bspFile.read(this->leafFaces, size);
165
166    // Get the leafBrushes
167    offset = SDL_SwapLE32(((int *)(header))[14]);
168    size    = SDL_SwapLE32(((int *)(header))[15]);
169    PRINTF(4)("BSP FILE: leafBrushesSize: %i Bytes. \n", size);
170    PRINTF(4)("BSP FILE: NumleafBrushes: %i. \n", size / 4);
171    PRINTF(4)("BSP FILE: Remainder: %i. \n", size % 4);
172    PRINTF(4)("BSP FILE: leafBrushesOffset: %i. \n", offset);
173    this->numLeafBrushes = size/4;
174    this->leafBrushes = new char [size];
175    bspFile.seekg(offset);
176    bspFile.read(this->leafBrushes, size);
177
178    // Get the brushes
179    offset = SDL_SwapLE32(((int *)(header))[18]);
180    size    = SDL_SwapLE32(((int *)(header))[19]);
181    PRINTF(4)("BSP FILE: BrushesSize: %i Bytes. \n", size);
182    PRINTF(4)("BSP FILE: NumBrushes: %i. \n", size / sizeof(brush));
183    PRINTF(4)("BSP FILE: Remainder: %i. \n", size % sizeof(brush));
184    PRINTF(4)("BSP FILE: BrushesOffset: %i. \n", offset);
185    this->brushes = new brush [size/sizeof(brush)];
186    bspFile.seekg(offset);
187    bspFile.read((char*)this->brushes, size);
188
189    // Get the brushSides
190    offset =   SDL_SwapLE32(((int *)(header))[20]);
191    size    = SDL_SwapLE32(((int *)(header))[21]);
192    PRINTF(4)("BSP FILE: BrushSidesSize: %i Bytes. \n", size);
193    PRINTF(4)("BSP FILE: NumBrushSides: %i. \n", size / 8);
194    PRINTF(4)("BSP FILE: Remainder: %i. \n", size % 8);
195    PRINTF(4)("BSP FILE: BrushSidesOffset: %i. \n", offset);
196    this->numBrushSides = size/sizeof(brushside);
197    this->brushSides = new brushside [this->numBrushSides];
198    bspFile.seekg(offset);
199    bspFile.read((char*)this->brushSides, size);
200
201    // Get the Vertice
202    offset = SDL_SwapLE32(((int *)(header))[22]);
203    size    = SDL_SwapLE32(((int *)(header))[23]);
204    PRINTF(4)("BSP FILE: VerticeSize: %i Bytes. \n", size);
205    PRINTF(4)("BSP FILE: NumVertice: %i. \n", size / 44);
206    PRINTF(4)("BSP FILE: Remainder: %i. \n", size % 44);
207    PRINTF(4)("BSP FILE: VerticeOffset: %i. \n", offset);
208    this->numVertex = size/44;
209    this->vertice = new char [size];
210    bspFile.seekg(offset);
211    bspFile.read(this->vertice, size);
212
213    // Get the MeshVerts
214    offset = SDL_SwapLE32(((int *)(header))[24]);
215    size    = SDL_SwapLE32(((int *)(header))[25]);
216    PRINTF(4)("BSP FILE: MeshVertsSize: %i Bytes. \n", size);
217    PRINTF(4)("BSP FILE: NumMeshVerts: %i. \n", size / sizeof(meshvert));
218    PRINTF(4)("BSP FILE: Remainder: %i. \n", size % sizeof(meshvert));
219    PRINTF(4)("BSP FILE: MeshVertsOffset: %i. \n", offset);
220    this->meshverts = new meshvert [size / sizeof(meshvert)];
221    bspFile.seekg(offset);
222    bspFile.read((char*)this->meshverts, size);
223
224    // Get the Faces
225    offset = SDL_SwapLE32(((int *)(header))[28]);
226    size    = SDL_SwapLE32(((int *)(header))[29]);
227    PRINTF(4)("BSP FILE: FacesSize: %i Bytes. \n", size);
228    PRINTF(4)("BSP FILE: NumFaces: %i. \n", size / sizeof(face));
229    PRINTF(4)("BSP FILE: Remainder: %i. \n", size % sizeof(face));
230    PRINTF(4)("BSP FILE: FacesOffset: %i. \n", offset);
231    this->numFaces = size/sizeof(face);
232    this->faces = new face [this->numFaces];
233    bspFile.seekg(offset);
234    bspFile.read((char*)this->faces, size);
235
236    //Get the lightmaps
237    offset = SDL_SwapLE32(((int *)(header))[30]);
238    size    = SDL_SwapLE32(((int *)(header))[31]);
239    this->numLightMaps = size/ sizeof(lightmap);
240    this->lightMaps = new lightmap [this->numLightMaps];
241    bspFile.seekg(offset);
242    bspFile.read((char*)this->lightMaps, size);
243
244
245    // Get the Visdata
246    offset = SDL_SwapLE32(((int *)(header))[34]);
247    size    = SDL_SwapLE32(((int *)(header))[35]);
248
249    this->visData = new char [size];
250    bspFile.seekg(offset);
251    bspFile.read(this->visData, size);
252
253    PRINTF(4)("BSP FILE: VisDataSize: %i Bytes. \n", size);
254    PRINTF(4)("BSP FILE: NumVisData: %i. \n", size /1 - 8);
255    PRINTF(4)("BSP FILE: Remainder: %i. \n", size % 1);
256    PRINTF(4)("BSP FILE: VisDataOffset: %i. \n", offset);
257
258    // Get the Textures
259    offset = SDL_SwapLE32(((int *)(header))[4]);
260    size    = SDL_SwapLE32(((int *)(header))[5]);
261
262    this->textures= new char [size];
263    bspFile.seekg(offset);
264    bspFile.read(this->textures, size);
265
266    PRINTF(4)("BSP FILE: TextureSize: %i Bytes. \n", size);
267    PRINTF(4)("BSP FILE: NumTextures: %i. \n", size /72);
268    PRINTF(4)("BSP FILE: Remainder: %i. \n", size % 72);
269    PRINTF(4)("BSP FILE: TextureOffset: %i. \n", offset);
270    this->numTextures = size/72;
271
272    bspFile.close();
273
274    for(int i = 0 ; i < this->numTextures; i++)
275      PRINTF(4)("BSP FILE: Texture 0: %s. \n", &this->textures[8+ 72*i]);
276    this->load_textures();
277
278    // Load the lightMaps
279    this->glLightMapTextures = new GLuint[this->numLightMaps];
280    for(int i = 0; i < this->numLightMaps; i++)
281      this->glLightMapTextures[i] = this->loadLightMapToGL(this->lightMaps[i]);
282
283    //Create white texture for if no lightmap specified
284    glGenTextures(1, &this->whiteLightMap);
285    glBindTexture(GL_TEXTURE_2D, this->whiteLightMap);
286        //Create texture
287    this->whiteTexture[0]=255;
288    this->whiteTexture[1]=255;
289    this->whiteTexture[2]=255;
290
291    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
292    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
293    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
294    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
295
296    glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_PRIORITY, 2);
297
298
299
300    /* control the mipmap levels */
301    glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_MIN_LOD, 5);
302    glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_MAX_LOD, 0);
303
304    /* build the Texture  OpenGL V >= 1.1 */
305    glTexImage2D(GL_TEXTURE_2D,
306                 0,
307                 GL_RGBA8,
308                 1,
309                 1,
310                 0,
311                 GL_RGB,
312                 GL_UNSIGNED_BYTE,
313                 (const GLvoid *)&(this->whiteTexture));
314
315   gluBuild2DMipmaps(   GL_TEXTURE_2D, GL_RGBA8, 1, 1,
316                      GL_RGB, GL_UNSIGNED_BYTE,(const GLvoid *) &(this->whiteTexture));
317
318
319
320    // Get the number of patches
321    this->numPatches = 0;
322    this->patchOffset   = 0;
323
324    for( int i = 0; i < this->numFaces; i++) {
325      face& cFace = ((face *)(this->faces))[i];
326      if (SDL_SwapLE32(cFace.type) == 2)
327        this->numPatches += (SDL_SwapLE32(cFace.size[0]) -1 ) / 2  * (SDL_SwapLE32(cFace.size[1]) -1) / 2;
328    }
329
330    // Allocate Memory
331    this->patchVertice = new char[8*8*44*(this->numPatches+10)];
332    this->patchIndexes = new char[7*8*2*4*(this->numPatches+10)];
333 // this->patchRowIndexes = new int*[7*4*this->numPatches]; Not needed?
334    this->patchTrianglesPerRow = new char[7*4*this->numPatches];
335    this->VertexArrayModels  = new VertexArrayModel*[this->numPatches];
336   
337    this->ODE_Geometry = new dTriMeshDataID[this->numPatches*7*7];
338    this->ODE_Geom_IDs = new dGeomID[this->numPatches* 7 *7];
339
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
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
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        this->ODE_Geometry[this->patchOffset] = dGeomTriMeshDataCreate();
906     // Create ODE Triangle Mesh Geometry
907        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) );
908
909
910      //***********************************************************************************************************************
911      // Debug Model
912      //***********************************************************************************************************************
913
914      this->VertexArrayModels[this->patchOffset] = new VertexArrayModel();
915      VertexArrayModel*  tmp = this->VertexArrayModels[this->patchOffset];
916      tmp->newStripe();
917      int a = 0;
918      int b = -1;
919      tmp->addVertex(controlsTmp[0].position[0],controlsTmp[0].position[1], controlsTmp[0].position[2]);
920      tmp->addNormal(1,0,0);
921      tmp->addTexCoor(0.0,0.0);
922      tmp->addColor(0.3,0.0,0.0);
923      tmp->addIndice(1+b);
924      tmp->addIndice(4+a);
925      tmp->addVertex(controlsTmp[1].position[0],controlsTmp[1].position[1], controlsTmp[1].position[2]);
926      tmp->addNormal(1,0,0);
927      tmp->addTexCoor(0.0,0.4);
928      tmp->addColor(0.3,0.0,0.0);
929      tmp->addIndice(2+b);
930      tmp->addIndice(5+a);
931      tmp->addVertex(controlsTmp[2].position[0],controlsTmp[2].position[1], controlsTmp[2].position[2]);
932      tmp->addNormal(1,0,0);
933      tmp->addTexCoor(0.0,1.0);
934      tmp->addColor(0.1,0.0,0.0);
935      tmp->addIndice(3+b);
936      tmp->addIndice(6+a);
937
938      tmp->addVertex(controlsTmp[2].position[0],controlsTmp[2].position[1], controlsTmp[2].position[2]);
939      tmp->addNormal(1,0,0);
940      tmp->addTexCoor(0.0,1.0);
941      tmp->addColor(0.1,0.0,0.0);
942      tmp->addIndice(7+a);
943      //tmp->addIndice(6);
944
945      tmp->newStripe();
946
947      tmp->addVertex(controlsTmp[0].position[0],controlsTmp[0].position[1], controlsTmp[0].position[2]);
948      tmp->addNormal(1,0,0);
949      tmp->addTexCoor(0.0,0.0);
950      tmp->addColor(0.1,0.1,0.1);
951      tmp->addIndice(5+b);
952      tmp->addIndice(8+a);
953      tmp->addVertex(controlsTmp[1].position[0],controlsTmp[1].position[1], controlsTmp[1].position[2]);
954      tmp->addNormal(1,0,0);
955      tmp->addTexCoor(0.0,0.4);
956      tmp->addColor(0.1,0.1,0.1);
957      tmp->addIndice(6+b);
958      tmp->addIndice(9+a);
959      tmp->addVertex(controlsTmp[2].position[0],controlsTmp[2].position[1], controlsTmp[2].position[2]);
960      tmp->addNormal(1,0,0);
961      tmp->addTexCoor(0.0,1.0);
962      tmp->addColor(0.1,0.1,0.1);
963      tmp->addIndice(7+b);
964      tmp->addIndice(10+a);
965      tmp->addVertex(controlsTmp[2].position[0],controlsTmp[2].position[1], controlsTmp[2].position[2]+0.01);
966      tmp->addNormal(1,0,0);
967      tmp->addTexCoor(0.0,1.0);
968      tmp->addColor(0.1,0.1,0.1);
969      //tmp->addIndice(5);
970      tmp->addIndice(11+a);
971
972      tmp->newStripe();
973
974
975
976      tmp->addVertex(controlsTmp[3].position[0],controlsTmp[3].position[1], controlsTmp[3].position[2]);
977      tmp->addNormal(0,1,0);
978      tmp->addTexCoor(0.5,0.0);
979      tmp->addColor(0.1,0.1,0.1);
980      tmp->addIndice(9+b);
981      tmp->addIndice(12+a);
982      tmp->addVertex(controlsTmp[4].position[0],controlsTmp[4].position[1], controlsTmp[4].position[2]);
983      tmp->addNormal(1,0,0);
984      tmp->addTexCoor(0.5,0.5);
985      tmp->addColor(0.1,0.1,0.1);
986      tmp->addIndice(10+b);
987      tmp->addIndice(13+a);
988      tmp->addVertex(controlsTmp[5].position[0],controlsTmp[5].position[1], controlsTmp[5].position[2]);
989      tmp->addNormal(1,0,0);
990      tmp->addTexCoor(0.5,1.0);
991      tmp->addColor(0.1,0.1,0.1);
992      tmp->addIndice(11+b);
993      tmp->addIndice(14+a);
994      tmp->addVertex(controlsTmp[5].position[0],controlsTmp[5].position[1], controlsTmp[5].position[2]+0.01);
995      tmp->addNormal(1,0,0);
996      tmp->addTexCoor(0.5,1.0);
997      tmp->addColor(0.1,0.1,0.1);
998
999      tmp->addIndice(15+a);
1000      //tmp->addIndice(9);
1001      tmp->newStripe();
1002
1003      tmp->addVertex(controlsTmp[6].position[0],controlsTmp[6].position[1], controlsTmp[6].position[2]);
1004      tmp->addNormal(1,0,0);
1005      tmp->addTexCoor(1.0,0.0);
1006      tmp->addColor(0.1,0.1,0.1);
1007      tmp->addIndice(13+b);
1008      tmp->addIndice(16+a);
1009
1010      tmp->addVertex(controlsTmp[7].position[0],controlsTmp[7].position[1], controlsTmp[7].position[2]);
1011      tmp->addNormal(0,1,0);
1012      tmp->addTexCoor(1.0,0.5);
1013      tmp->addColor(0.1,0.1,0.1);
1014      tmp->addIndice(14+b);
1015      tmp->addIndice(17+a);
1016      tmp->addVertex(controlsTmp[8].position[0],controlsTmp[8].position[1], controlsTmp[8].position[2]);
1017      tmp->addNormal(1,0,0);
1018      tmp->addTexCoor(1.0,1.0);
1019      tmp->addColor(0.1,0.1,0.1);
1020      tmp->addIndice(15+b);
1021      tmp->addIndice(18+a);
1022      tmp->addVertex(controlsTmp[8].position[0],controlsTmp[8].position[1], controlsTmp[8].position[2]);
1023      tmp->addNormal(1,0,0);
1024      tmp->addTexCoor(1.0,1.0);
1025      tmp->addColor(0.1,0.1,0.1);
1026      tmp->addIndice(19+a);
1027      //tmp->addIndice(13);
1028
1029      tmp->newStripe();
1030      tmp->addVertex(controlsTmp[6].position[0],controlsTmp[6].position[1], controlsTmp[6].position[2]);
1031      tmp->addNormal(1,0,0);
1032      tmp->addTexCoor(1.0,0.0);
1033      tmp->addColor(0.1,0.1,0.1);
1034      tmp->addIndice(17+b);
1035
1036      tmp->addVertex(controlsTmp[7].position[0],controlsTmp[7].position[1], controlsTmp[7].position[2]);
1037      tmp->addNormal(0,1,0);
1038      tmp->addTexCoor(1.0,0.5);
1039      tmp->addColor(0.1,0.1,0.1);
1040      tmp->addIndice(18+b);
1041
1042      tmp->addVertex(controlsTmp[8].position[0],controlsTmp[8].position[1], controlsTmp[8].position[2]);
1043      tmp->addNormal(1,0,0);
1044      tmp->addTexCoor(1.0,1.0);
1045      tmp->addColor(0.1,0.1,0.1);
1046      tmp->addIndice(19+b);
1047
1048      tmp->addVertex(controlsTmp[8].position[0],controlsTmp[8].position[1], controlsTmp[8].position[2]);
1049      tmp->addNormal(1,0,0);
1050      tmp->addTexCoor(1.0,1.0);
1051      tmp->addColor(0.1,0.1,0.1);
1052
1053      tmp->newStripe();
1054
1055      tmp->finalize();
1056      // End of DebugModel
1057
1058      this->patchOffset++;
1059    }// For
1060  } // For
1061
1062  // Overwrite Face->meshvert;
1063  // Overwrite Face->n_meshverts;
1064  int sz = (size0 -1)/2 * (size1 -1)/2; // num patches
1065  Face->meshvert = patchOffset -sz;  //3*(patchOffset-sz)*level1*level1;
1066  Face->n_meshverts = sz;
1067  PRINTF(0)("BSP FILE: sz: %i. \n", sz);
1068  PRINTF(0)("BSP FILE: Face->meshvert %i . \n", Face->meshvert);
1069
1070  //Face->n_meshverts = sz;
1071}
1072
1073//!TODO: This is a good place to do LittleEndian to BigEndian conversion!
1074void BspFile::swapAllBspCoordinates()
1075{
1076
1077  for(int i = 0; i < this->numVertex ; ++i)
1078  {
1079    this->swapCoords(&((BspVertex *)this->vertice)[i].position[0]);
1080    this->swapCoords(&((BspVertex *)this->vertice)[i].normal[0]);
1081
1082
1083  }
1084
1085  for(int i = 0; i < this->numLeafs ; ++i)
1086  {
1087    this->swapCoords(this->leaves[i].mins);
1088    this->swapCoords(this->leaves[i].maxs);
1089
1090  }
1091
1092  for(int i = 0; i < this->numPlanes; ++i)
1093  {
1094    float sto = this->planes[i].x;
1095    this->planes[i].x =  this->planes[i].y;
1096    this->planes[i].y =  this->planes[i].z;
1097    this->planes[i].z = sto;
1098    this->planes[i].d = scale * this->planes[i].d ;
1099  }
1100
1101
1102  for(int i = 0; i < this->numFaces; ++i)
1103  {
1104    this->swapCoords(this->faces[i].normal);
1105  }
1106
1107}
1108
1109void BspFile::swapCoords(int *array)
1110{
1111  if( scale < 1)
1112  {
1113  int sto  =  array[0];
1114  array[0] =  array[1] / (int) ( 1/ scale);
1115  array[1] = array[2] / (int) (1/scale);
1116  array[2] =  sto / (int) (1/scale);
1117  }
1118  else
1119  {
1120    int sto  =  array[0];
1121    array[0] =  scale * array[1] ;
1122    array[1] =  scale * array[2];
1123    array[2] =  scale * sto ;
1124  }
1125
1126}
1127
1128void BspFile::swapCoords(float * array)
1129{
1130  float sto  =  array[0];
1131  array[0] =  scale * array[1];
1132  array[1] =  scale * array[2];
1133  array[2] =  scale * sto;
1134}
1135
Note: See TracBrowser for help on using the repository browser.