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