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