Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Minor improvements (transparancy). Code formatting revised.

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