Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/mount_points/src/lib/graphics/importer/bsp/bsp_file.cc @ 10165

Last change on this file since 10165 was 10165, checked in by patrick, 17 years ago

some warnings fixed

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