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