Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/bsp_model/src/lib/graphics/importer/bsp_file.cc @ 8087

Last change on this file since 8087 was 8087, checked in by bensch, 18 years ago

merges some merges

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