Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Added source files to branch bsp_model.

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