Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/water/src/lib/graphics/importer/bsp_file.cc @ 8327

Last change on this file since 8327 was 8186, checked in by bensch, 19 years ago

trunk: merged the branche bsp_model back here

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