Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

further reformating

File size: 33.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
15#include "bsp_file.h"
16#include "bsp_tree_node.h"
17#include <fstream>
18#include <sys/stat.h>
19#include <string.h>
20#include "debug.h"
21#include "material.h"
22#include "vertex_array_model.h"
23// Necessary ?
24#include "base_object.h"
25#include "vector.h"
26#include "util/loading/resource_manager.h"
27
28#include <SDL/SDL_endian.h>
29#include <SDL/SDL_image.h>
30
31using namespace std;
32
33
34// Constructor
35BspFile::BspFile()
36{}
37
38int BspFile::read(char* name)
39{
40  int offset;
41  int size;
42  struct stat results;
43  name = "/root/data/Kanti175.bsp";
44
45  if (stat( name , &results) == 0) {
46    PRINTF(0)("BSP FILE: Datei %s gefunden. \n", name);
47    ifstream bspFile (name, ios::in | ios::binary);
48    bspFile.read(this->header, 260);
49    PRINTF(0)("BSP FILE: BSPVersion: %i. \n", ((int *)(header) )[1]);
50    if(SDL_SwapLE32(((int *)(header) )[1]) == 46)    PRINTF(0)("BSP FILE: This is the good one! :-)  \n");
51    else  PRINTF(0)("BSP FILE: Wrong BSPVersion.\n");   //!< now, we should do some error handling
52
53    // Get the Nodes
54    offset = SDL_SwapLE32(((int *)(header) )[8]);
55    size    = SDL_SwapLE32(((int *)(header))[9]);
56    PRINTF(4)("BSP FILE: NodeSize: %i Bytes. \n", size);
57    PRINTF(4)("BSP FILE: NumNodes: %i. \n", size / sizeof(node));
58    PRINTF(4)("BSP FILE: Remainder: %i. \n", size % sizeof(node));
59    PRINTF(4)("BSP FILE: NodeOffset: %i. \n", offset);
60    this->numNodes = size/sizeof(node);
61    this->nodes = new node [this->numNodes];
62    bspFile.seekg(offset);
63    bspFile.read((char*)this->nodes, size);
64
65    // and their Planes
66    offset = SDL_SwapLE32(((int *)(header) )[6]);
67    size    = SDL_SwapLE32(((int *)(header))[7]);
68    PRINTF(4)("BSP FILE: PlanesSize: %i Bytes. \n", size);
69    PRINTF(4)("BSP FILE: NumPlanes: %i. \n", size / sizeof(plane));
70    PRINTF(4)("BSP FILE: Remainder: %i. \n", sizeof(plane));
71    PRINTF(4)("BSP FILE: PlanesOffset: %i. \n", offset);
72    this->numPlanes = size/sizeof(plane);
73    this->planes = new plane [this->numPlanes];
74    bspFile.seekg(offset);
75    bspFile.read((char*)this->planes, size);
76
77    // Get the Leafs
78    offset = SDL_SwapLE32(((int *)(header) )[10]);
79    size    = SDL_SwapLE32(((int *)(header))[11]);
80    PRINTF(4)("BSP FILE: LeaveSize: %i Bytes. \n", size);
81    PRINTF(4)("BSP FILE: NumLeaves: %i. \n", size / sizeof(leaf));
82    PRINTF(4)("BSP FILE: Remainder: %i. \n", size % sizeof(leaf));
83    PRINTF(4)("BSP FILE: LeaveOffset: %i. \n", offset);
84    this->numLeafs = size/sizeof(leaf);
85    this->leaves = new leaf [this->numLeafs];
86    bspFile.seekg(offset);
87    bspFile.read((char*)this->leaves, size);
88
89    // Get the Models
90    offset = SDL_SwapLE32(((int *)(header))[16]);
91    size    = SDL_SwapLE32(((int *)(header))[17]);
92    PRINTF(4)("BSP FILE: ModelsSize: %i Bytes. \n", size);
93    PRINTF(4)("BSP FILE: NumModels: %i. \n", size / sizeof(model));
94    PRINTF(4)("BSP FILE: Remainder: %i. \n", size % sizeof(model));
95    PRINTF(4)("BSP FILE: ModelsOffset: %i. \n", offset);
96    this->numBspModels = size/sizeof(model);
97    this->bspModels = new model [this->numBspModels];
98    bspFile.seekg(offset);
99    bspFile.read((char*)this->bspModels, size);
100
101    // Get the leafFaces
102    offset = SDL_SwapLE32(((int *)(header))[12]);
103    size    = SDL_SwapLE32(((int *)(header))[13]);
104    PRINTF(4)("BSP FILE: leafFacesSize: %i Bytes. \n", size);
105    PRINTF(4)("BSP FILE: NumleafFaces: %i. \n", size / 4);
106    PRINTF(4)("BSP FILE: Remainder: %i. \n", size % 4);
107    PRINTF(4)("BSP FILE: leafFacesOffset: %i. \n", offset);
108    this->numLeafFaces = size/4;
109    this->leafFaces = new char [size];
110    bspFile.seekg(offset);
111    bspFile.read(this->leafFaces, size);
112
113    // Get the leafBrushes
114    offset = SDL_SwapLE32(((int *)(header))[14]);
115    size    = SDL_SwapLE32(((int *)(header))[15]);
116    PRINTF(4)("BSP FILE: leafBrushesSize: %i Bytes. \n", size);
117    PRINTF(4)("BSP FILE: NumleafBrushes: %i. \n", size / 4);
118    PRINTF(4)("BSP FILE: Remainder: %i. \n", size % 4);
119    PRINTF(4)("BSP FILE: leafBrushesOffset: %i. \n", offset);
120    this->numLeafBrushes = size/4;
121    this->leafBrushes = new char [size];
122    bspFile.seekg(offset);
123    bspFile.read(this->leafBrushes, size);
124
125    // Get the brushes
126    offset = SDL_SwapLE32(((int *)(header))[18]);
127    size    = SDL_SwapLE32(((int *)(header))[19]);
128    PRINTF(4)("BSP FILE: BrushesSize: %i Bytes. \n", size);
129    PRINTF(4)("BSP FILE: NumBrushes: %i. \n", size / 12);
130    PRINTF(4)("BSP FILE: Remainder: %i. \n", size % 12);
131    PRINTF(4)("BSP FILE: BrushesOffset: %i. \n", offset);
132    this->brushes = new char [size];
133    bspFile.seekg(offset);
134    bspFile.read(this->brushes, size);
135
136    // Get the brushSides
137    offset =   SDL_SwapLE32(((int *)(header))[20]);
138    size    = SDL_SwapLE32(((int *)(header))[21]);
139    PRINTF(4)("BSP FILE: BrushSidesSize: %i Bytes. \n", size);
140    PRINTF(4)("BSP FILE: NumBrushSides: %i. \n", size / 8);
141    PRINTF(4)("BSP FILE: Remainder: %i. \n", size % 8);
142    PRINTF(4)("BSP FILE: BrushSidesOffset: %i. \n", offset);
143    this->brushSides = new char [size];
144    this->numBrushSides = size/8;
145    bspFile.seekg(offset);
146    bspFile.read(this->brushSides, size);
147
148    // Get the Vertice
149    offset = SDL_SwapLE32(((int *)(header))[22]);
150    size    = SDL_SwapLE32(((int *)(header))[23]);
151    PRINTF(4)("BSP FILE: VerticeSize: %i Bytes. \n", size);
152    PRINTF(4)("BSP FILE: NumVertice: %i. \n", size / 44);
153    PRINTF(4)("BSP FILE: Remainder: %i. \n", size % 44);
154    PRINTF(4)("BSP FILE: VerticeOffset: %i. \n", offset);
155    this->numVertex = size/44;
156    this->vertice = new char [size];
157    bspFile.seekg(offset);
158    bspFile.read(this->vertice, size);
159
160    // Get the MeshVerts
161    offset = SDL_SwapLE32(((int *)(header))[24]);
162    size    = SDL_SwapLE32(((int *)(header))[25]);
163    PRINTF(4)("BSP FILE: MeshVertsSize: %i Bytes. \n", size);
164    PRINTF(4)("BSP FILE: NumMeshVerts: %i. \n", size / 4);
165    PRINTF(4)("BSP FILE: Remainder: %i. \n", size % 4);
166    PRINTF(4)("BSP FILE: MeshVertsOffset: %i. \n", offset);
167    this->meshverts = new char [size];
168    bspFile.seekg(offset);
169    bspFile.read(this->meshverts, size);
170
171    // Get the Faces
172    offset = SDL_SwapLE32(((int *)(header))[28]);
173    size    = SDL_SwapLE32(((int *)(header))[29]);
174    PRINTF(4)("BSP FILE: FacesSize: %i Bytes. \n", size);
175    PRINTF(4)("BSP FILE: NumFaces: %i. \n", size / sizeof(face));
176    PRINTF(4)("BSP FILE: Remainder: %i. \n", size % sizeof(face));
177    PRINTF(4)("BSP FILE: FacesOffset: %i. \n", offset);
178    this->numFaces = size/sizeof(face);
179    this->faces = new face [this->numFaces];
180    bspFile.seekg(offset);
181    bspFile.read((char*)this->faces, size);
182
183    // Get the Visdata
184    offset = SDL_SwapLE32(((int *)(header))[34]);
185    size    = SDL_SwapLE32(((int *)(header))[35]);
186
187    this->visData = new char [size];
188    bspFile.seekg(offset);
189    bspFile.read(this->visData, size);
190
191    PRINTF(4)("BSP FILE: VisDataSize: %i Bytes. \n", size);
192    PRINTF(4)("BSP FILE: NumVisData: %i. \n", size /1 - 8);
193    PRINTF(4)("BSP FILE: Remainder: %i. \n", size % 1);
194    PRINTF(4)("BSP FILE: VisDataOffset: %i. \n", offset);
195
196    // Get the Textures
197    offset = SDL_SwapLE32(((int *)(header))[4]);
198    size    = SDL_SwapLE32(((int *)(header))[5]);
199
200    this->textures= new char [size];
201    bspFile.seekg(offset);
202    bspFile.read(this->textures, size);
203
204    PRINTF(4)("BSP FILE: TextureSize: %i Bytes. \n", size);
205    PRINTF(4)("BSP FILE: NumTextures: %i. \n", size /72);
206    PRINTF(4)("BSP FILE: Remainder: %i. \n", size % 72);
207    PRINTF(4)("BSP FILE: TextureOffset: %i. \n", offset);
208    this->numTextures = size/72;
209
210    bspFile.close();
211
212    for(int i = 0 ; i < this->numTextures; i++)
213      PRINTF(4)("BSP FILE: Texture 0: %s. \n", &this->textures[8+ 72*i]);
214    this->load_textures();
215
216    // Get the number of patches
217    this->numPatches = 0;
218    this->patchOffset   = 0;
219
220    for( int i = 0; i < this->numFaces; i++) {
221      face& cFace = ((face *)(this->faces))[i];
222      if (      cFace.type == 2)
223        this->numPatches += (cFace.size[0] -1 ) / 2  * (cFace.size[1] -1) / 2;
224    }
225
226    // Allocate Memory
227    this->patchVertice = new char[8*8*44*(this->numPatches+10)];
228    this->patchIndexes = new char[7*8*2*4*(this->numPatches+10)];
229    this->patchRowIndexes = new int*[7*4*this->numPatches];
230    this->patchTrianglesPerRow = new char[7*4*this->numPatches];
231    this->VertexArrayModels  = new VertexArrayModel*[this->numPatches];
232
233    PRINTF(0)("BSP FILE:NumberOfPatches: %i . \n", numPatches);
234    // Do tesselation for all Faces of type 2
235    for( int i = 0; i < this->numFaces; i++) {
236      if (      ((face *)(this->faces))[i].type == 2)
237        this->tesselate(i);
238    }
239
240    PRINTF(0)("BSP FILE:PatchOffset: %i . \n", this->patchOffset);
241
242    return  1;
243  } else {
244    PRINTF(0)("BSP FILE: Datei nicht gefunden. \n");
245    return -1;
246  }
247
248}
249
250void BspFile::build_tree()
251{
252
253  PRINTF(0)("BSP FILE:\n");
254  PRINTF(0)("BSP FILE: Building Tree...\n");
255  root = this->build_tree_rec(0);
256  PRINTF(0)("BSP FILE: ...done. \n");
257  PRINTF(0)("BSP FILE:  \n");
258  PRINTF(0)("BSP FILE: Node #0: \n");
259  PRINTF(0)("BSP FILE:  x: %f \n",root->plane.x);
260  PRINTF(0)("BSP FILE:  y: %f\n",root->plane.y);
261  PRINTF(0)("BSP FILE:  z: %f\n",root->plane.z);
262}
263
264BspTreeNode*   BspFile::build_tree_rec(int i)
265{
266  // PRINTF(0)("BSP FILE: Node #%i\n", i);
267  BspTreeNode* thisNode = new BspTreeNode();
268  int left =(((node *) nodes) [i]).left;
269  int right =(((node *) nodes) [i]).right;
270  int planeIndex = (((node *) nodes) [i]).plane;
271  float x1 =(((plane *) this->planes) [planeIndex]).x;
272  float y1 =(((plane *) this->planes) [planeIndex]).y;
273  float z1 =(((plane *) this->planes) [planeIndex]).z;
274  thisNode->leafIndex = 0;
275  thisNode->d          = (((plane *) this->planes) [planeIndex]).d;
276
277  thisNode->plane = Vector(x1,y1,z1);
278  thisNode->isLeaf = false;
279
280  if(left >= 0) {
281    thisNode->left = this->build_tree_rec(left);
282  } else {
283    //BspTreeLeaf tmp =  BspTreeLeaf();
284    //tmp.isLeaf = true;
285    //tmp.leafIndex = -left -1;
286    //thisNode->left = (BspTreeNode*) (&tmp);
287    thisNode->left  = new BspTreeNode();
288    thisNode->left->isLeaf = true;
289    thisNode->left->leafIndex = - (left +1);
290    //PRINTF(0)("BSP FILE:  LeafIndex: %i\n",-left);
291  } // assign leav
292  if(right >= 0) {
293    thisNode->right =  this->build_tree_rec(right);
294  } else {
295    //BspTreeLeaf tmp =  BspTreeLeaf();
296    //tmp.isLeaf = true;
297    //tmp.leafIndex = -right -1;
298    //thisNode->right = (BspTreeNode*) (&tmp);
299    thisNode->right = new BspTreeNode();
300    thisNode->right->isLeaf = true;
301    thisNode->right->leafIndex = -(right +1);
302    //PRINTF(0)("BSP FILE:  LeafIndex: %i\n",-right);
303  } // assign leaf
304  return thisNode;
305}
306
307BspTreeNode* BspFile::get_root()
308{
309  return root;
310}
311
312void BspFile::load_textures()
313{
314  char absFileName [228];
315  char fileName [228];
316  char ext [100];
317  struct stat results;
318
319  this->Materials = new AMat[this->numTextures];
320  for(int i = 0 ; i < this->numTextures; i++) {
321    PRINTF(0)("BSP FILE: Texture : %s. \n", &this->textures[8+ 72*i]);
322
323    // Check for tga
324    strcpy(fileName, &this->textures[8+ 72*i]);
325    strcpy (absFileName,"/root/data/trunk/");
326    strcpy(ext, ".tga");
327    strncat (fileName, ext, strlen(fileName));
328    strncat(absFileName,fileName,strlen(fileName));
329    // absFileName = ResourceManager::getFullName(fileName);
330
331    if(stat( absFileName , &results) == 0) {
332      PRINTF(0)("BSP FILE: gefunden . \n");
333      this->Materials[i] = this->loadMat(fileName);
334      continue;
335    }
336    // Check for TGA
337    strcpy(fileName, &this->textures[8+ 72*i]);
338    strcpy (absFileName,"/root/data/trunk/");
339    strcpy(ext, ".TGA");
340    strncat (fileName, ext, strlen(fileName));
341    strncat(absFileName,fileName,strlen(fileName));
342    // absFileName = ResourceManager::getFullName(fileName);
343
344    if(stat( absFileName , &results) == 0) {
345      PRINTF(0)("BSP FILE: gefunden . \n");
346      this->Materials[i] = this->loadMat(fileName);
347      continue;
348    }
349    // Check for jpg
350    strcpy(fileName, &this->textures[8+ 72*i]);
351    strcpy (absFileName,"/root/data/trunk/");
352    strcpy(ext, ".jpg");
353    strncat (fileName, ext, strlen(fileName));
354    strncat(absFileName,fileName,strlen(fileName));
355    // absFileName = ResourceManager::getFullName(fileName);
356    if(stat( absFileName , &results) == 0) {
357      PRINTF(0)("BSP FILE: gefunden . \n");
358      this->Materials[i] =this->loadMat(fileName);
359      continue;
360    }
361
362
363    // Check for JPG
364    strcpy(fileName, &this->textures[8+ 72*i]);
365    strcpy (absFileName,"/root/data/trunk/");
366    strcpy(ext, ".JPG");
367    strncat (fileName, ext, strlen(fileName));
368    strncat(absFileName,fileName,strlen(fileName));
369    // absFileName = ResourceManager::getFullName(fileName);
370    if(stat( absFileName , &results) == 0) {
371      PRINTF(0)("BSP FILE: gefunden . \n");
372      this->Materials[i] =this->loadMat(fileName);
373      continue;
374    }
375
376    // Check for jpeg
377    strcpy(fileName, &this->textures[8+ 72*i]);
378    strcpy (absFileName,"/root/data/trunk/");
379    strcpy(ext, ".jpeg");
380    strncat (fileName, ext, strlen(fileName));
381    strncat(absFileName,fileName,strlen(fileName));
382    // absFileName = ResourceManager::getFullName(fileName);
383    if(stat( absFileName , &results) == 0) {
384      PRINTF(0)("BSP FILE: gefunden . \n");
385      this->Materials[i] =this->loadMat(fileName);
386      continue;
387    }
388
389
390    // Check for JPEG
391    strcpy(fileName, &this->textures[8+ 72*i]);
392    strcpy (absFileName,"/root/data/trunk/");
393    strcpy(ext, ".JPEG");
394    strncat (fileName, ext, strlen(fileName));
395    strncat(absFileName,fileName,strlen(fileName));
396    // absFileName = ResourceManager::getFullName(fileName);
397    PRINTF(0)("BSP FILE: %s . \n", absFileName);
398    if(stat( absFileName , &results) == 0) {
399      PRINTF(0)("BSP FILE: gefunden . \n");
400      this->Materials[i] =this->loadMat(fileName);
401      continue;
402    }
403
404    // Check for bmp
405    strcpy(fileName, &this->textures[8+ 72*i]);
406    strcpy (absFileName,"/root/data/trunk/");
407    strcpy(ext, ".bmp");
408    strncat (fileName, ext, strlen(fileName));
409    strncat(absFileName,fileName,strlen(fileName));
410    // absFileName = ResourceManager::getFullName(fileName);
411
412    if(stat( absFileName , &results) == 0) {
413      PRINTF(0)("BSP FILE: gefunden . \n");
414      this->Materials[i] =this->loadMat(fileName);
415      continue;
416    }
417
418    // Check for BMP
419    strcpy(fileName, &this->textures[8+ 72*i]);
420    strcpy (absFileName,"/root/data/trunk/");
421    strcpy(ext, ".BMP");
422    strncat (fileName, ext, strlen(fileName));
423    strncat(absFileName,fileName,strlen(fileName));
424    // absFileName = ResourceManager::getFullName(fileName);
425
426    if(stat( absFileName , &results) == 0) {
427      PRINTF(0)("BSP FILE: gefunden . \n");
428      this->Materials[i] = this->loadMat(fileName);
429      continue;
430    }
431    //  Default Material
432    this->Materials[i].mat = new Material();
433    this->Materials[i].mat->setDiffuse(0.1,0.1,0.1);
434    this->Materials[i].mat->setAmbient(0.1,0.1,0.1 );
435    this->Materials[i].mat->setSpecular(0.4,0.4,0.4);
436    //this->Materials[i]->setShininess(100.0);
437    this->Materials[i].mat->setTransparency(1.0);
438    this->Materials[i].mat->setDiffuseMap("pictures/ground.tga");
439    this->Materials[i].mat->setAmbientMap("pictures/ground.tga");
440    this->Materials[i].mat->setSpecularMap("pictures/ground.tga");
441    this->Materials[i].alpha = false;
442  }
443}
444
445
446AMat BspFile::loadMat(char* mat)
447{
448  AMat tmpAMat;
449  this->testSurf = NULL;
450
451  this->testSurf = IMG_Load(ResourceManager::getFullName(mat).c_str());
452  if(this->testSurf != NULL) {
453    if(this->testSurf->format->Amask != 0 ) tmpAMat.alpha = true;
454    else                                       tmpAMat.alpha = false;
455  } else   tmpAMat.alpha = false;
456
457  Material* tmp = new Material();
458  tmp->setDiffuse(1.0,1.0,1.0);
459  tmp->setAmbient(1.0,1.0,1.0 );
460  tmp->setSpecular(1.0,1.0,1.0);
461  //    tmp->setShininess(.5);
462  //    tmp->setTransparency(1.0);
463
464  tmp->setDiffuseMap(mat);
465  tmp->setAmbientMap(mat);
466  tmp->setSpecularMap(mat);
467
468  tmpAMat.mat = tmp;
469
470  return tmpAMat;
471}
472
473void BspFile::tesselate(int iface)
474{
475  face*  Face =  & (((face *)(this->faces))[iface]);
476  BspVertex *  BspVrtx = (BspVertex*)this->vertice;
477  int level = 7;
478  int level1 = 8;
479
480  // For each patch...
481  for(int i = 0; i <(Face->size[0] - 1)    ; i+=2) {
482    for(int j = 0; j <(Face->size[1] -1)    ; j+=2) {
483
484
485      // Make a patch...
486      // Get controls[9];
487      BspVec controls[9];
488      BspVec controlsTmp[9];
489      BspVertex VControls[9];
490      for(int k = 0; k < 3; k++) {
491        for(int l = 0; l < 3; l++) {
492          controls[k +3*l]. position[0] =   BspVrtx[Face->vertex +( j * Face->size[0])+ i + l+ Face->size[0]*k].position[0];
493          controls[k +3*l]. position[1] =   BspVrtx[Face->vertex + (j * Face->size[0])+ i +l+ Face->size[0]*k].position[1];
494          controls[k +3*l]. position[2] =   BspVrtx[Face->vertex + (j * Face->size[0])+ i + l+ Face->size[0]*k].position[2]; /*Face->n_vertexes*/
495
496          controlsTmp[2-k +6-3*l]. position[0] =   BspVrtx[Face->vertex +( j * Face->size[0])+ i + l+ Face->size[0]*k].position[0];
497          controlsTmp[2-k +6-3*l]. position[1] =   BspVrtx[Face->vertex + (j * Face->size[0])+ i +l+ Face->size[0]*k].position[1];
498          controlsTmp[2-k +6-3*l]. position[2] =   BspVrtx[Face->vertex + (j * Face->size[0])+ i + l+ Face->size[0]*k].position[2]; /*Face->n_vertexes*/
499
500          VControls[k +3*l]. position[0] =   BspVrtx[Face->vertex +( j * Face->size[0])+ i + l+ Face->size[0]*k].position[0];
501          VControls[k +3*l]. position[1] =   BspVrtx[Face->vertex + (j * Face->size[0])+ i +l+ Face->size[0]*k].position[1];
502          VControls[k +3*l]. position[2] =   BspVrtx[Face->vertex + (j * Face->size[0])+ i + l+ Face->size[0]*k].position[2];
503
504          VControls[k +3*l]. normal[0] =   BspVrtx[Face->vertex +( j * Face->size[0])+ i + l+ Face->size[0]*k].normal[0];
505          VControls[k +3*l]. normal[1] =   BspVrtx[Face->vertex + (j * Face->size[0])+ i +l+ Face->size[0]*k].normal[1];
506          VControls[k +3*l]. normal[2] =   BspVrtx[Face->vertex + (j * Face->size[0])+ i + l+ Face->size[0]*k].normal[2];
507
508          VControls[k +3*l]. texcoord[0][0]=   BspVrtx[Face->vertex +( j * Face->size[0])+ i + l+ Face->size[0]*k].texcoord[0][0];
509          VControls[k +3*l]. texcoord[0][1] =   BspVrtx[Face->vertex + (j * Face->size[0])+ i +l+ Face->size[0]*k].texcoord[0][1];
510          VControls[k +3*l]. texcoord[1][0] =   BspVrtx[Face->vertex +( j * Face->size[0])+ i + l+ Face->size[0]*k].texcoord[1][0];
511          VControls[k +3*l]. texcoord[1][1] =   BspVrtx[Face->vertex + (j * Face->size[0])+ i +l+ Face->size[0]*k].texcoord[1][1];
512
513
514        }
515      }
516      //***********************************************************************************************************************
517      // Compute the vertice
518      //***********************************************************************************************************************
519      float px, py;
520      BspVertex temp[3];
521      BspVertex* Vertice = &(((BspVertex*)this->patchVertice)[level1*level1*this->patchOffset]);
522
523      for(int v=0; v<=level; ++v) {
524        px=(float)v/level;
525
526        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);
527        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);
528        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);
529
530        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);
531        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);
532        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);
533
534        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);
535        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);
536        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);
537        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);
538
539      }
540
541
542      for(int u=1; u<=level; ++u) {
543        py=(float)u/level;
544
545        // temp[0]=controlPoints[0]*((1.0f-py)*(1.0f-py))+ controlPoints[1]*((1.0f-py)*py*2)+ controlPoints[2]*(py*py);
546        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);
547        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);
548        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);
549
550        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);
551        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);
552        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);
553
554        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);
555        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);
556        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);
557        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);
558
559
560
561        // temp[1]=controlPoints[3]*((1.0f-py)*(1.0f-py))+ controlPoints[4]*((1.0f-py)*py*2)+ controlPoints[5]*(py*py);
562        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);
563        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);
564        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);
565
566        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);
567        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);
568        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);
569
570        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);
571        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);
572        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);
573        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);
574
575
576        // temp[2]=controlPoints[6]*((1.0f-py)*(1.0f-py))+controlPoints[7]*((1.0f-py)*py*2)+controlPoints[8]*(py*py);
577        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);
578        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);
579        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);
580
581        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);
582        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);
583        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);
584
585        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);
586        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);
587        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);
588        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);
589
590
591
592
593        for(int v=0; v<=level; ++v) {
594          px=(float)v/level;
595
596          //Vertice[u*(tesselation+1)+v]=       temp[0]*((1.0f-px)*(1.0f-px))+ temp[1]*((1.0f-px)*px*2)+ temp[2]*(px*px);
597          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);
598          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);
599          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);
600
601          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);
602          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);
603          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);
604
605          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);
606          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);
607          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);
608          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);
609
610
611
612        }
613      }
614
615      //Create indices
616      GLuint* indices= & ((GLuint*)(this->patchIndexes))[level*level1*2*this->patchOffset];
617
618      for(int row=0; row<level; ++row) {
619        for(int point=0; point<=level; ++point) {
620          //calculate indices
621          //reverse them to reverse winding
622          indices[(row*(level1)+point)*2+1]=row*(level1)+point;
623          indices[(row*(level1)+point)*2]=(row+1)*(level1)+point;
624        }
625      }
626
627
628      //***********************************************************************************************************************
629      // Debug Model
630      //***********************************************************************************************************************
631      this->VertexArrayModels[this->patchOffset] = new VertexArrayModel();
632      VertexArrayModel*  tmp = this->VertexArrayModels[this->patchOffset];
633      tmp->newStripe();
634      int a = 0;
635      int b = -1;
636      tmp->addVertex(controlsTmp[0].position[0],controlsTmp[0].position[1], controlsTmp[0].position[2]);
637      tmp->addNormal(1,0,0);
638      tmp->addTexCoor(0.0,0.0);
639      tmp->addColor(0.3,0.0,0.0);
640      tmp->addIndice(1+b);
641      tmp->addIndice(4+a);
642      tmp->addVertex(controlsTmp[1].position[0],controlsTmp[1].position[1], controlsTmp[1].position[2]);
643      tmp->addNormal(1,0,0);
644      tmp->addTexCoor(0.0,0.4);
645      tmp->addColor(0.3,0.0,0.0);
646      tmp->addIndice(2+b);
647      tmp->addIndice(5+a);
648      tmp->addVertex(controlsTmp[2].position[0],controlsTmp[2].position[1], controlsTmp[2].position[2]);
649      tmp->addNormal(1,0,0);
650      tmp->addTexCoor(0.0,1.0);
651      tmp->addColor(0.1,0.0,0.0);
652      tmp->addIndice(3+b);
653      tmp->addIndice(6+a);
654
655      tmp->addVertex(controlsTmp[2].position[0],controlsTmp[2].position[1], controlsTmp[2].position[2]);
656      tmp->addNormal(1,0,0);
657      tmp->addTexCoor(0.0,1.0);
658      tmp->addColor(0.1,0.0,0.0);
659      tmp->addIndice(7+a);
660      //tmp->addIndice(6);
661
662      tmp->newStripe();
663
664      tmp->addVertex(controlsTmp[0].position[0],controlsTmp[0].position[1], controlsTmp[0].position[2]);
665      tmp->addNormal(1,0,0);
666      tmp->addTexCoor(0.0,0.0);
667      tmp->addColor(0.1,0.1,0.1);
668      tmp->addIndice(5+b);
669      tmp->addIndice(8+a);
670      tmp->addVertex(controlsTmp[1].position[0],controlsTmp[1].position[1], controlsTmp[1].position[2]);
671      tmp->addNormal(1,0,0);
672      tmp->addTexCoor(0.0,0.4);
673      tmp->addColor(0.1,0.1,0.1);
674      tmp->addIndice(6+b);
675      tmp->addIndice(9+a);
676      tmp->addVertex(controlsTmp[2].position[0],controlsTmp[2].position[1], controlsTmp[2].position[2]);
677      tmp->addNormal(1,0,0);
678      tmp->addTexCoor(0.0,1.0);
679      tmp->addColor(0.1,0.1,0.1);
680      tmp->addIndice(7+b);
681      tmp->addIndice(10+a);
682      tmp->addVertex(controlsTmp[2].position[0],controlsTmp[2].position[1], controlsTmp[2].position[2]+0.01);
683      tmp->addNormal(1,0,0);
684      tmp->addTexCoor(0.0,1.0);
685      tmp->addColor(0.1,0.1,0.1);
686      //tmp->addIndice(5);
687      tmp->addIndice(11+a);
688
689      tmp->newStripe();
690
691
692
693      tmp->addVertex(controlsTmp[3].position[0],controlsTmp[3].position[1], controlsTmp[3].position[2]);
694      tmp->addNormal(0,1,0);
695      tmp->addTexCoor(0.5,0.0);
696      tmp->addColor(0.1,0.1,0.1);
697      tmp->addIndice(9+b);
698      tmp->addIndice(12+a);
699      tmp->addVertex(controlsTmp[4].position[0],controlsTmp[4].position[1], controlsTmp[4].position[2]);
700      tmp->addNormal(1,0,0);
701      tmp->addTexCoor(0.5,0.5);
702      tmp->addColor(0.1,0.1,0.1);
703      tmp->addIndice(10+b);
704      tmp->addIndice(13+a);
705      tmp->addVertex(controlsTmp[5].position[0],controlsTmp[5].position[1], controlsTmp[5].position[2]);
706      tmp->addNormal(1,0,0);
707      tmp->addTexCoor(0.5,1.0);
708      tmp->addColor(0.1,0.1,0.1);
709      tmp->addIndice(11+b);
710      tmp->addIndice(14+a);
711      tmp->addVertex(controlsTmp[5].position[0],controlsTmp[5].position[1], controlsTmp[5].position[2]+0.01);
712      tmp->addNormal(1,0,0);
713      tmp->addTexCoor(0.5,1.0);
714      tmp->addColor(0.1,0.1,0.1);
715
716      tmp->addIndice(15+a);
717      //tmp->addIndice(9);
718      tmp->newStripe();
719
720      tmp->addVertex(controlsTmp[6].position[0],controlsTmp[6].position[1], controlsTmp[6].position[2]);
721      tmp->addNormal(1,0,0);
722      tmp->addTexCoor(1.0,0.0);
723      tmp->addColor(0.1,0.1,0.1);
724      tmp->addIndice(13+b);
725      tmp->addIndice(16+a);
726
727      tmp->addVertex(controlsTmp[7].position[0],controlsTmp[7].position[1], controlsTmp[7].position[2]);
728      tmp->addNormal(0,1,0);
729      tmp->addTexCoor(1.0,0.5);
730      tmp->addColor(0.1,0.1,0.1);
731      tmp->addIndice(14+b);
732      tmp->addIndice(17+a);
733      tmp->addVertex(controlsTmp[8].position[0],controlsTmp[8].position[1], controlsTmp[8].position[2]);
734      tmp->addNormal(1,0,0);
735      tmp->addTexCoor(1.0,1.0);
736      tmp->addColor(0.1,0.1,0.1);
737      tmp->addIndice(15+b);
738      tmp->addIndice(18+a);
739      tmp->addVertex(controlsTmp[8].position[0],controlsTmp[8].position[1], controlsTmp[8].position[2]);
740      tmp->addNormal(1,0,0);
741      tmp->addTexCoor(1.0,1.0);
742      tmp->addColor(0.1,0.1,0.1);
743      tmp->addIndice(19+a);
744      //tmp->addIndice(13);
745
746      tmp->newStripe();
747      tmp->addVertex(controlsTmp[6].position[0],controlsTmp[6].position[1], controlsTmp[6].position[2]);
748      tmp->addNormal(1,0,0);
749      tmp->addTexCoor(1.0,0.0);
750      tmp->addColor(0.1,0.1,0.1);
751      tmp->addIndice(17+b);
752
753      tmp->addVertex(controlsTmp[7].position[0],controlsTmp[7].position[1], controlsTmp[7].position[2]);
754      tmp->addNormal(0,1,0);
755      tmp->addTexCoor(1.0,0.5);
756      tmp->addColor(0.1,0.1,0.1);
757      tmp->addIndice(18+b);
758
759      tmp->addVertex(controlsTmp[8].position[0],controlsTmp[8].position[1], controlsTmp[8].position[2]);
760      tmp->addNormal(1,0,0);
761      tmp->addTexCoor(1.0,1.0);
762      tmp->addColor(0.1,0.1,0.1);
763      tmp->addIndice(19+b);
764
765      tmp->addVertex(controlsTmp[8].position[0],controlsTmp[8].position[1], controlsTmp[8].position[2]);
766      tmp->addNormal(1,0,0);
767      tmp->addTexCoor(1.0,1.0);
768      tmp->addColor(0.1,0.1,0.1);
769
770      tmp->newStripe();
771
772      tmp->finalize();
773      // End of DebugModel
774
775      this->patchOffset++;
776    }// For
777  } // For
778
779  // Overwrite Face->meshvert;
780  // Overwrite Face->n_meshverts;
781  int sz = (Face->size[0] -1)/2 * (Face->size[1] -1)/2; // num patches
782  Face->meshvert = patchOffset -sz;  //3*(patchOffset-sz)*level1*level1;
783  Face->n_meshverts = sz;
784  PRINTF(0)("BSP FILE: sz: %i. \n", sz);
785  PRINTF(0)("BSP FILE: Face->meshvert %i . \n", Face->meshvert);
786
787  //Face->n_meshverts = sz;
788}
Note: See TracBrowser for help on using the repository browser.