1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2004 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: Benjamin Grauer |
---|
13 | co-programmer: ... |
---|
14 | */ |
---|
15 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY |
---|
16 | |
---|
17 | |
---|
18 | #include "terrain.h" |
---|
19 | |
---|
20 | #include "util/loading/resource_manager.h" |
---|
21 | #include "util/loading/load_param.h" |
---|
22 | #include "util/loading/factory.h" |
---|
23 | #include "spatial_separation.h" |
---|
24 | |
---|
25 | #include "model.h" |
---|
26 | #include "static_model.h" |
---|
27 | #include "static_model_data.h" |
---|
28 | #include "network_game_manager.h" |
---|
29 | |
---|
30 | #include "height_map.h" |
---|
31 | #include "material.h" |
---|
32 | |
---|
33 | #include "glincl.h" |
---|
34 | |
---|
35 | #include "state.h" |
---|
36 | #include "debug.h" |
---|
37 | #include "aabb.h" |
---|
38 | #include "cr_defs.h" |
---|
39 | #include "cd_engine.h" |
---|
40 | |
---|
41 | #include "class_id_DEPRECATED.h" |
---|
42 | #include <ode/ode.h> |
---|
43 | ObjectListDefinitionID(Terrain, CL_TERRAIN); |
---|
44 | CREATE_FACTORY(Terrain); |
---|
45 | |
---|
46 | /** |
---|
47 | * standard constructor |
---|
48 | */ |
---|
49 | Terrain::Terrain (const TiXmlElement* root) |
---|
50 | { |
---|
51 | this->loaded = false; |
---|
52 | this->init(); |
---|
53 | |
---|
54 | |
---|
55 | if( root != NULL) |
---|
56 | this->loadParams(root); |
---|
57 | |
---|
58 | |
---|
59 | if(this->getModel()) |
---|
60 | { |
---|
61 | |
---|
62 | StaticModelData::Pointer ModelData = ((StaticModel*)this->getModel())->dataPointer(); |
---|
63 | //ModelData->buildTriangleList(); |
---|
64 | |
---|
65 | PRINTF(0)(" Dieses Model hat %i Vertices \n", ModelData->getVertices().size()); |
---|
66 | PRINTF(0)(" Dieses Model hat %i Dreiecke \n",((ModelData->getTriangles()).size() )); |
---|
67 | this->Ind = new unsigned int [(ModelData->getTriangles()).size()*3]; |
---|
68 | for(int i = 0 ; i < (ModelData->getTriangles()).size(); i++) |
---|
69 | { |
---|
70 | Ind[3*i] = (ModelData->getTrianglesExt())[i].indexToVertices[0] / 3 ; |
---|
71 | Ind[3*i +1] = (ModelData->getTrianglesExt())[i].indexToVertices[1] / 3; |
---|
72 | Ind[3*i +2] = (ModelData->getTrianglesExt())[i].indexToVertices[2] / 3; |
---|
73 | |
---|
74 | } |
---|
75 | |
---|
76 | //((StaticModel*)(this->getModel()))->acquireData(ModelData); |
---|
77 | this->ODE_Geometry = dGeomTriMeshDataCreate(); |
---|
78 | dGeomTriMeshDataBuildSingle(this->ODE_Geometry,&((ModelData->getVertices())[0]), 3*sizeof(float),(ModelData->getVertices().size() /3 ) , &Ind[0] ,((ModelData->getTriangles()).size() *3 ), 3*sizeof(unsigned int) ); |
---|
79 | // Create ODE-Data here! |
---|
80 | |
---|
81 | this->world = dWorldCreate(); |
---|
82 | this->space = dSimpleSpaceCreate(0); |
---|
83 | |
---|
84 | //!fixme |
---|
85 | for (int i=0; i<30; i++) { |
---|
86 | contact[i].surface.mode = dContactBounce | dContactSoftCFM; |
---|
87 | contact[i].surface.mu = dInfinity; |
---|
88 | contact[i].surface.mu2 = 0; |
---|
89 | contact[i].surface.bounce = 0.1; |
---|
90 | contact[i].surface.bounce_vel = 0.1; |
---|
91 | contact[i].surface.soft_cfm = 0.01; |
---|
92 | } |
---|
93 | |
---|
94 | contactgroup = dJointGroupCreate (0); |
---|
95 | dWorldSetGravity (world,0,0,-0.5); |
---|
96 | dWorldSetCFM (world,1e-5); |
---|
97 | |
---|
98 | this->ODE_Geom_ID = dCreateTriMesh(space,this->ODE_Geometry,0 , 0, 0); |
---|
99 | |
---|
100 | |
---|
101 | CDEngine::getInstance()->setTerrain(this); |
---|
102 | this->loaded = true; |
---|
103 | this->toList(OM_ENVIRON); |
---|
104 | } |
---|
105 | // if (this->model != NULL) |
---|
106 | //this->ssp = new SpatialSeparation((Model*)this->model, 10.0f); |
---|
107 | } |
---|
108 | |
---|
109 | |
---|
110 | /** |
---|
111 | * Constructor for loading a Terrain out of a file |
---|
112 | * @param fileName The file to load data from. |
---|
113 | |
---|
114 | this either loads out of an OBJ-file, or loads a heightmap if no .obj-extension is found. |
---|
115 | */ |
---|
116 | Terrain::Terrain(const std::string& fileName) |
---|
117 | { |
---|
118 | this->loaded = false; |
---|
119 | this->init(); |
---|
120 | |
---|
121 | if (fileName.rfind(".obj" ) != std::string::npos || fileName.rfind(".OBJ") != std::string::npos ) |
---|
122 | { |
---|
123 | this->loadModel(fileName); |
---|
124 | |
---|
125 | |
---|
126 | } |
---|
127 | else |
---|
128 | { |
---|
129 | // load the hightMap here. |
---|
130 | } |
---|
131 | } |
---|
132 | |
---|
133 | /** |
---|
134 | * a Constructor for the Debug-Worlds |
---|
135 | */ |
---|
136 | Terrain::Terrain(DebugTerrain debugTerrain) |
---|
137 | { |
---|
138 | this->init(); |
---|
139 | this->buildDebugTerrain(debugTerrain); |
---|
140 | } |
---|
141 | |
---|
142 | /** |
---|
143 | * standard deconstructor |
---|
144 | |
---|
145 | */ |
---|
146 | Terrain::~Terrain () |
---|
147 | { |
---|
148 | if (modelList) |
---|
149 | glDeleteLists(this->modelList, 1); |
---|
150 | if( this->ssp) |
---|
151 | delete ssp; |
---|
152 | |
---|
153 | if(this->heightMap) |
---|
154 | delete heightMap; |
---|
155 | } |
---|
156 | |
---|
157 | |
---|
158 | void Terrain::init() |
---|
159 | { |
---|
160 | this->registerObject(this, Terrain::_objectList); |
---|
161 | this->toList(OM_ENVIRON_NOTICK); |
---|
162 | this->toReflectionList(); |
---|
163 | |
---|
164 | this->modelList = 0; |
---|
165 | this->ssp = NULL; |
---|
166 | this->vegetation = NULL; |
---|
167 | |
---|
168 | this->heightMap = NULL; |
---|
169 | |
---|
170 | this->heightMapMaterial = new Material(); |
---|
171 | |
---|
172 | |
---|
173 | } |
---|
174 | |
---|
175 | |
---|
176 | void Terrain::loadParams(const TiXmlElement* root) |
---|
177 | { |
---|
178 | WorldEntity::loadParams(root); |
---|
179 | |
---|
180 | LoadParam(root, "scale", this, Terrain, setScale) |
---|
181 | .describe("The scale in x,y,z direction"); |
---|
182 | |
---|
183 | LoadParam(root, "texture", this, Terrain, loadTexture) |
---|
184 | .describe("The name of the Texture for this heightMap"); |
---|
185 | |
---|
186 | LoadParam(root, "vegetation", this, Terrain, loadVegetation) |
---|
187 | .describe("the fileName of the vegetation, that should be loaded onto this terrain. (must be relative to the data-dir)") ; |
---|
188 | |
---|
189 | LoadParam(root, "height-map", this, Terrain, loadHeightMap) |
---|
190 | .describe("The HeightMap, splitted into two strings seperated by ','. 1: HeighMap, 2: ColorMap"); |
---|
191 | |
---|
192 | } |
---|
193 | |
---|
194 | void Terrain::setScale(float x, float y, float z) |
---|
195 | { |
---|
196 | this->terrainScale = Vector(x, y, z); |
---|
197 | } |
---|
198 | |
---|
199 | void Terrain::loadHeightMap(const std::string& heightMapFile, const std::string& colorMap) |
---|
200 | { |
---|
201 | if (this->heightMap != NULL) |
---|
202 | delete this->heightMap; |
---|
203 | this->heightMap = NULL; |
---|
204 | |
---|
205 | std::string hmName = Resources::ResourceManager::getInstance()->prependAbsoluteMainPath(heightMapFile); |
---|
206 | std::string hmColorName = Resources::ResourceManager::getInstance()->prependAbsoluteMainPath(colorMap); |
---|
207 | |
---|
208 | |
---|
209 | this->heightMap = new HeightMap(hmName, hmColorName); |
---|
210 | // heightMap->scale(Vector(43.0f,4.7f,43.0f)); |
---|
211 | heightMap->scale(this->terrainScale); |
---|
212 | heightMap->setAbsCoor(this->getAbsCoor()); |
---|
213 | heightMap->load(); |
---|
214 | } |
---|
215 | |
---|
216 | |
---|
217 | void Terrain::loadTexture(const std::string& textureName) |
---|
218 | { |
---|
219 | PRINTF(4)("Load texture: %s\n", textureName.c_str()); |
---|
220 | |
---|
221 | heightMapMaterial->setDiffuse(1.0,1.0,1.0); |
---|
222 | heightMapMaterial->setAmbient(1.0,1.0,1.0 ); |
---|
223 | heightMapMaterial->setSpecular(1.0,1.0,1.0); |
---|
224 | heightMapMaterial->setShininess(.5); |
---|
225 | heightMapMaterial->setTransparency(1.0); |
---|
226 | |
---|
227 | heightMapMaterial->setDiffuseMap(textureName); |
---|
228 | // heightMapMaterial->setAmbientMap(textureName); |
---|
229 | // heightMapMaterial->setSpecularMap(textureName); |
---|
230 | } |
---|
231 | |
---|
232 | |
---|
233 | |
---|
234 | void Terrain::loadVegetation(const std::string& vegetationFile) |
---|
235 | { |
---|
236 | PRINTF(4)("loadVegetation: %s\n", vegetationFile.c_str()); |
---|
237 | if (this->vegetation) |
---|
238 | this->vegetation = 0; |
---|
239 | if (!vegetationFile.empty()) |
---|
240 | { |
---|
241 | PRINTF(4)("fetching %s\n", vegetationFile.c_str()); |
---|
242 | this->loadModel(vegetationFile, 1.0, 2); |
---|
243 | this->vegetation = this->getModel(2); |
---|
244 | } |
---|
245 | else |
---|
246 | this->vegetation = NULL; |
---|
247 | } |
---|
248 | |
---|
249 | |
---|
250 | |
---|
251 | |
---|
252 | |
---|
253 | void Terrain::draw () const |
---|
254 | { |
---|
255 | glPushMatrix(); |
---|
256 | |
---|
257 | /* translate */ |
---|
258 | glTranslatef (this->getAbsCoor ().x, |
---|
259 | this->getAbsCoor ().y, |
---|
260 | this->getAbsCoor ().z); |
---|
261 | /* rotate */ |
---|
262 | // Vector tmpRot = this->getAbsDir().getSpacialAxis(); |
---|
263 | //glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z ); |
---|
264 | |
---|
265 | if (this->modelList) |
---|
266 | glCallList(this->modelList); |
---|
267 | else if (this->getModel()) |
---|
268 | this->getModel()->draw(); |
---|
269 | |
---|
270 | if (this->vegetation) |
---|
271 | this->vegetation->draw(); |
---|
272 | |
---|
273 | if( this->heightMap) |
---|
274 | { |
---|
275 | this->heightMapMaterial->select(); |
---|
276 | |
---|
277 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); |
---|
278 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); |
---|
279 | this->heightMap->draw(); |
---|
280 | } |
---|
281 | glPopMatrix(); |
---|
282 | |
---|
283 | |
---|
284 | /* |
---|
285 | glMatrixMode(GL_MODELVIEW); |
---|
286 | glPushMatrix(); |
---|
287 | glLoadIdentity(); |
---|
288 | Vector camera = State::getCameraNode()->getAbsCoor(); // Go on here ..........!!! |
---|
289 | |
---|
290 | float height = heightMap->getHeight(camera.x, camera.z); |
---|
291 | |
---|
292 | glEnable (GL_COLOR_MATERIAL) ; |
---|
293 | glBegin(GL_QUADS); // Draw The Cube Using quads |
---|
294 | glColor3f(0.0f,1.0f,0.0f); // Color Blue |
---|
295 | glVertex3f(camera.x + 63.0f,Terrain->getHeight(camera.x+63.0f, camera.z-10.0f)+13.0f,camera.z-10.0f); // Top Right Of The Quad (Top) |
---|
296 | glVertex3f(camera.x-63.0f, getHeight(camera.x+63.0f, camera.z-10.0f)+13.0f,camera.z-10.0f); // Top Left Of The Quad (Top) |
---|
297 | glVertex3f(camera.x-63.0f, getHeight(camera.x+63.0f, camera.z+10.0f)+13.0f, camera.z+10.0f); // Bottom Left Of The Quad (Top) |
---|
298 | glVertex3f(camera.x+ 63.0f, getHeight(camera.x+63.0f, camera.z+10.0f)+13.0f, camera.z+10.0f); // Bottom Right Of The Quad (Top) |
---|
299 | glEnd(); // End Drawing The Plan |
---|
300 | |
---|
301 | glPopMatrix();*/ |
---|
302 | |
---|
303 | |
---|
304 | /* THIS IS ONLY FOR DEBUGGING INFORMATION */ |
---|
305 | // if (this->ssp != NULL) |
---|
306 | // this->ssp->drawQuadtree(); |
---|
307 | } |
---|
308 | |
---|
309 | |
---|
310 | void Terrain::buildDebugTerrain(DebugTerrain debugTerrain) |
---|
311 | { |
---|
312 | // if the terrain is the Terrain of Dave |
---|
313 | if (debugTerrain == TERRAIN_DAVE) |
---|
314 | { |
---|
315 | modelList = glGenLists(1); |
---|
316 | glNewList (modelList, GL_COMPILE); |
---|
317 | |
---|
318 | glColor3f(1.0,0,0); |
---|
319 | |
---|
320 | int sizeX = 100; |
---|
321 | int sizeZ = 80; |
---|
322 | float length = 1000; |
---|
323 | float width = 200; |
---|
324 | float widthX = float (length /sizeX); |
---|
325 | float widthZ = float (width /sizeZ); |
---|
326 | |
---|
327 | float height [sizeX][sizeZ]; |
---|
328 | Vector normal_vectors[sizeX][sizeZ]; |
---|
329 | |
---|
330 | |
---|
331 | for ( int i = 0; i<sizeX-1; i+=1) |
---|
332 | for (int j = 0; j<sizeZ-1;j+=1) |
---|
333 | //height[i][j] = rand()/20046 + (j-25)*(j-25)/30; |
---|
334 | #ifdef __WIN32__ |
---|
335 | height[i][j]=(sin((float)j/3)*rand()*i/182400)*.5; |
---|
336 | #else |
---|
337 | height[i][j]=(sin((float)j/3)*rand()*(long)i/6282450500.0)*.5; |
---|
338 | #endif |
---|
339 | |
---|
340 | //Die Huegel ein wenig glaetten |
---|
341 | for (int h=1; h<2;h++) |
---|
342 | for (int i=1;i<sizeX-2 ;i+=1 ) |
---|
343 | for(int j=1;j<sizeZ-2;j+=1) |
---|
344 | height[i][j]=(height[i+1][j]+height[i][j+1]+height[i-1][j]+height[i][j-1])/4; |
---|
345 | |
---|
346 | //Berechnung von normalen Vektoren |
---|
347 | for(int i=1;i<sizeX-2;i+=1) |
---|
348 | for(int j=1;j<sizeZ-2 ;j+=1) |
---|
349 | { |
---|
350 | Vector v1 = Vector (widthX*(1), height[i][j], widthZ*(j) ); |
---|
351 | Vector v2 = Vector (widthX*(i-1), height[i-1][j], widthZ*(j)); |
---|
352 | Vector v3 = Vector (widthX*(i), height[i][j+1], widthZ*(j+1)); |
---|
353 | Vector v4 = Vector (widthX*(i+1), height[i+1][j], widthZ*(j)); |
---|
354 | Vector v5 = Vector (widthX*(i), height[i][j-1], widthZ*(j-1)); |
---|
355 | |
---|
356 | Vector c1 = v2 - v1; |
---|
357 | Vector c2 = v3 - v1; |
---|
358 | Vector c3= v4 - v1; |
---|
359 | Vector c4 = v5 - v1; |
---|
360 | Vector zero = Vector (0,0,0); |
---|
361 | normal_vectors[i][j]=c1.cross(v3-v5)+c2.cross(v4-v2)+c3.cross(v5-v3)+c4.cross(v2-v4); |
---|
362 | normal_vectors[i][j].normalize(); |
---|
363 | } |
---|
364 | |
---|
365 | glBegin(GL_QUADS); |
---|
366 | int snowheight=3; |
---|
367 | for ( int i = 0; i<sizeX; i+=1) |
---|
368 | for (int j = 0; j<sizeZ;j+=1) |
---|
369 | { |
---|
370 | Vector v1 = Vector (widthX*(i), height[i][j]-20, widthZ*(j) -width/2); |
---|
371 | Vector v2 = Vector (widthX*(i+1), height[i+1][j]-20, widthZ*(j) -width/2); |
---|
372 | Vector v3 = Vector (widthX*(i+1), height[i+1][j+1]-20, widthZ*(j+1)-width/2); |
---|
373 | Vector v4 = Vector (widthX*(i), height[i][j+1]-20, widthZ*(j+1)-width/2); |
---|
374 | float a[3]; |
---|
375 | if(height[i][j]<snowheight) |
---|
376 | { |
---|
377 | a[0]=0; |
---|
378 | a[1]=1.0-height[i][j]/10-.3; |
---|
379 | a[2]=0; |
---|
380 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
381 | } |
---|
382 | else |
---|
383 | { |
---|
384 | a[0]=1.0; |
---|
385 | a[1]=1.0; |
---|
386 | a[2]=1.0; |
---|
387 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
388 | |
---|
389 | } |
---|
390 | glNormal3f(normal_vectors[i][j].x, normal_vectors[i][j].y, normal_vectors[i][j].z); |
---|
391 | glVertex3f(v1.x, v1.y, v1.z); |
---|
392 | if(height[i+1][j]<snowheight) |
---|
393 | { |
---|
394 | a[0]=0; |
---|
395 | a[1] =1.0-height[i+1][j]/10-.3; |
---|
396 | a[2]=0; |
---|
397 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
398 | } |
---|
399 | else |
---|
400 | { |
---|
401 | a[0]=1.0; |
---|
402 | a[1]=1.0; |
---|
403 | a[2]=1.0; |
---|
404 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
405 | |
---|
406 | } |
---|
407 | glNormal3f(normal_vectors[i+1][j].x, normal_vectors[i+1][j].y, normal_vectors[i+1][j].z); |
---|
408 | glVertex3f(v2.x, v2.y, v2.z); |
---|
409 | if(height[i+1][j+1]<snowheight) |
---|
410 | { |
---|
411 | a[0]=0; |
---|
412 | a[1] =1.0-height[i+1][j+1]/10-.3; |
---|
413 | a[2]=0; |
---|
414 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
415 | } |
---|
416 | else |
---|
417 | { |
---|
418 | a[0]=1.0; |
---|
419 | a[1]=1.0; |
---|
420 | a[2]=1.0; |
---|
421 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
422 | |
---|
423 | |
---|
424 | } |
---|
425 | glNormal3f(normal_vectors[i+1][j+1].x, normal_vectors[i+1][j+1].y, normal_vectors[i+1][j+1].z); |
---|
426 | glVertex3f(v3.x, v3.y, v3.z); |
---|
427 | if(height[i][j+1]<snowheight) |
---|
428 | { |
---|
429 | a[0]=0; |
---|
430 | a[1] =1.0-height[i+1][j+1]/10-.3; |
---|
431 | a[2]=0; |
---|
432 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
433 | } |
---|
434 | else |
---|
435 | { |
---|
436 | a[0]=1.0; |
---|
437 | a[1]=1.0; |
---|
438 | a[2]=1.0; |
---|
439 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
440 | } |
---|
441 | glNormal3f(normal_vectors[i][j+1].x, normal_vectors[i][j+1].y, normal_vectors[i][j+1].z); |
---|
442 | glVertex3f(v4.x, v4.y, v4.z); |
---|
443 | |
---|
444 | } |
---|
445 | glEnd(); |
---|
446 | glEndList(); |
---|
447 | } |
---|
448 | |
---|
449 | if (debugTerrain == TERRAIN_BENSCH) |
---|
450 | { |
---|
451 | /* |
---|
452 | this->model = (OBJModel*) new Model(); |
---|
453 | this->model->setName("CUBE"); |
---|
454 | this->model->addVertex (-0.5, -0.5, 0.5); |
---|
455 | this->model->addVertex (0.5, -0.5, 0.5); |
---|
456 | this->model->addVertex (-0.5, 0.5, 0.5); |
---|
457 | this->model->addVertex (0.5, 0.5, 0.5); |
---|
458 | this->model->addVertex (-0.5, 0.5, -0.5); |
---|
459 | this->model->addVertex (0.5, 0.5, -0.5); |
---|
460 | this->model->addVertex (-0.5, -0.5, -0.5); |
---|
461 | this->model->addVertex (0.5, -0.5, -0.5); |
---|
462 | |
---|
463 | this->model->addVertexTexture (0.0, 0.0); |
---|
464 | this->model->addVertexTexture (1.0, 0.0); |
---|
465 | this->model->addVertexTexture (0.0, 1.0); |
---|
466 | this->model->addVertexTexture (1.0, 1.0); |
---|
467 | this->model->addVertexTexture (0.0, 2.0); |
---|
468 | this->model->addVertexTexture (1.0, 2.0); |
---|
469 | this->model->addVertexTexture (0.0, 3.0); |
---|
470 | this->model->addVertexTexture (1.0, 3.0); |
---|
471 | this->model->addVertexTexture (0.0, 4.0); |
---|
472 | this->model->addVertexTexture (1.0, 4.0); |
---|
473 | this->model->addVertexTexture (2.0, 0.0); |
---|
474 | this->model->addVertexTexture (2.0, 1.0); |
---|
475 | this->model->addVertexTexture (-1.0, 0.0); |
---|
476 | this->model->addVertexTexture (-1.0, 1.0); |
---|
477 | |
---|
478 | this->model->finalize(); |
---|
479 | */ |
---|
480 | } |
---|
481 | } |
---|
482 | |
---|
483 | float Terrain::getHeight(float x, float y) |
---|
484 | { |
---|
485 | if(this->heightMap != NULL) |
---|
486 | return (this->heightMap->getHeight(x, y)); |
---|
487 | return 0; |
---|
488 | } |
---|
489 | |
---|
490 | void Terrain::go() |
---|
491 | { |
---|
492 | PRINTF(0)("It's me! :-)"); |
---|
493 | } |
---|
494 | |
---|
495 | void Terrain::checkCollisionTerrain(WorldEntity* worldEntity) { |
---|
496 | if(!this->loaded) return; |
---|
497 | |
---|
498 | AABB* box = worldEntity->getModelAABB(); |
---|
499 | dReal aabbox [6]; |
---|
500 | |
---|
501 | if( box != NULL) |
---|
502 | { |
---|
503 | |
---|
504 | dGeomID RayX = dCreateRay(space,box->halfLength[0] *2.0f); |
---|
505 | dGeomRaySet (RayX, worldEntity->getAbsCoor().x - box->halfLength[0] ,worldEntity->getAbsCoor().y, worldEntity->getAbsCoor().z, |
---|
506 | 1.0f, 0.0f, 0.0f); |
---|
507 | dGeomID RayY = dCreateRay(space,box->halfLength[1] *2.0f); |
---|
508 | dGeomRaySet (RayY, worldEntity->getAbsCoor().x ,worldEntity->getAbsCoor().y - box->halfLength[1], worldEntity->getAbsCoor().z, |
---|
509 | 0.0f, 1.0f, 0.0f); |
---|
510 | dGeomID RayZ = dCreateRay(space,box->halfLength[2] *2.0f); |
---|
511 | dGeomRaySet (RayZ, worldEntity->getAbsCoor().x ,worldEntity->getAbsCoor().y, worldEntity->getAbsCoor().z - box->halfLength[2], |
---|
512 | 0.0f, 0.0f, 1.0f); |
---|
513 | |
---|
514 | dGeomID RayXPos = dCreateRay(space,box->halfLength[0]); |
---|
515 | dGeomRaySet (RayX, worldEntity->getAbsCoor().x ,worldEntity->getAbsCoor().y, worldEntity->getAbsCoor().z, |
---|
516 | 1.0f, 0.0f, 0.0f); |
---|
517 | dGeomID RayYPos = dCreateRay(space,box->halfLength[1] ); |
---|
518 | dGeomRaySet (RayY, worldEntity->getAbsCoor().x ,worldEntity->getAbsCoor().y, worldEntity->getAbsCoor().z, |
---|
519 | 0.0f, 1.0f, 0.0f); |
---|
520 | dGeomID RayZPos = dCreateRay(space,box->halfLength[2] ); |
---|
521 | dGeomRaySet (RayZ, worldEntity->getAbsCoor().x ,worldEntity->getAbsCoor().y, worldEntity->getAbsCoor().z, |
---|
522 | 0.0f, 0.0f, 1.0f); |
---|
523 | |
---|
524 | dGeomID RayXNeg = dCreateRay(space,box->halfLength[0] * 40.0f ); |
---|
525 | dGeomRaySet (RayX, worldEntity->getAbsCoor().x ,worldEntity->getAbsCoor().y, worldEntity->getAbsCoor().z, |
---|
526 | -1.0f, 0.0f, 0.0f); |
---|
527 | dGeomID RayYNeg = dCreateRay(space,box->halfLength[1] ); |
---|
528 | dGeomRaySet (RayY, worldEntity->getAbsCoor().x ,worldEntity->getAbsCoor().y , worldEntity->getAbsCoor().z, |
---|
529 | 0.0f, -1.0f, 0.0f); |
---|
530 | dGeomID RayZNeg = dCreateRay(space,box->halfLength[2] ); |
---|
531 | dGeomRaySet (RayZ, worldEntity->getAbsCoor().x ,worldEntity->getAbsCoor().y, worldEntity->getAbsCoor().z , |
---|
532 | 0.0f, 0.0f, -1.0f); |
---|
533 | |
---|
534 | |
---|
535 | dGeomID BBOX = dCreateBox (space,box->halfLength[0]*2.0f, box->halfLength[1]*40.0f ,2.0f* box->halfLength[2]); |
---|
536 | //dGeomSetPosition (BBOX, worldEntity->getAbsCoor().x ,worldEntity->getAbsCoor().y, worldEntity->getAbsCoor().z); |
---|
537 | dGeomSetPosition (BBOX, worldEntity->getAbsCoor().x + box->center.x ,worldEntity->getAbsCoor().y+box->center.y, worldEntity->getAbsCoor().z+box->center.z); |
---|
538 | |
---|
539 | |
---|
540 | |
---|
541 | |
---|
542 | int g; |
---|
543 | bool collision = false; |
---|
544 | |
---|
545 | |
---|
546 | // dGeomGetAABB (this->bspFile->ODE_Geom_IDs[i],aabbox ); |
---|
547 | // BBOX2 = dCreateBox (space, aabbox[1]-aabbox[0], aabbox[3]-aabbox[2], aabbox[5]-aabbox[4]); |
---|
548 | // dGeomSetPosition (BBOX2,(aabbox[1]+ aabbox[0])/2, (aabbox[3]+ aabbox[2])/2, (aabbox[4]+ aabbox[5])/2); |
---|
549 | // dGeomDestroy(BBOX2); |
---|
550 | g = 0; |
---|
551 | if(true || dCollide(this->ODE_Geom_ID,BBOX, 10, &contact[0].geom, sizeof(dContact)) ) { |
---|
552 | |
---|
553 | //PRINTF(0)("Collision \n"); |
---|
554 | for(int i = 0; i <1 ; i++) |
---|
555 | { |
---|
556 | // worldEntity->registerCollision(COLLISION_TYPE_AXIS_Y_NEG , this, worldEntity, Vector(0.0f, 1.0f, 0.0f), |
---|
557 | // Vector(contact[i].geom.pos[0],contact[i].geom.pos[1],contact[i].geom.pos[2]), false); |
---|
558 | } |
---|
559 | |
---|
560 | if(dCollide(this->ODE_Geom_ID,RayX, 1, &contact[0].geom, sizeof(dContact))) { |
---|
561 | if(dCollide(this->ODE_Geom_ID,RayXPos, 1, &contact[0].geom, sizeof(dContact))) { |
---|
562 | worldEntity->registerCollision(COLLISION_TYPE_AXIS_X , this, worldEntity, Vector(1.0f, 0.0f, 0.0f), |
---|
563 | Vector((float)contact[0].geom.pos[0],(float)contact[0].geom.pos[1],contact[0].geom.pos[2]), false); |
---|
564 | |
---|
565 | }//if |
---|
566 | if(dCollide(this->ODE_Geom_ID,RayXNeg, 1, &contact[0].geom, sizeof(dContact))) { |
---|
567 | worldEntity->registerCollision(COLLISION_TYPE_AXIS_X_NEG , this, worldEntity, Vector(1.0f, 0.0f, 0.0f), |
---|
568 | Vector(contact[0].geom.pos[0],contact[0].geom.pos[1],contact[0].geom.pos[2]), false); |
---|
569 | |
---|
570 | }//if |
---|
571 | }//if |
---|
572 | |
---|
573 | |
---|
574 | if(dCollide(this->ODE_Geom_ID,RayY, 1, &contact[0].geom, sizeof(dContact))) { |
---|
575 | |
---|
576 | |
---|
577 | if(dCollide(this->ODE_Geom_ID,RayYNeg, 1, &contact[0].geom, sizeof(dContact))) { |
---|
578 | worldEntity->registerCollision(COLLISION_TYPE_AXIS_Y_NEG , this, worldEntity, Vector(0.0f, 1.0f, 0.0f), |
---|
579 | Vector(contact[0].geom.pos[0],contact[0].geom.pos[1],contact[0].geom.pos[2]), false); |
---|
580 | |
---|
581 | |
---|
582 | }//if |
---|
583 | else if(dCollide(this->ODE_Geom_ID,RayYPos, 1, &contact[0].geom, sizeof(dContact))) { |
---|
584 | worldEntity->registerCollision(COLLISION_TYPE_AXIS_Y_NEG , this, worldEntity, Vector(0.0f, 1.0f, 0.0f), |
---|
585 | Vector(contact[0].geom.pos[0],contact[0].geom.pos[1] ,contact[0].geom.pos[2]), false); |
---|
586 | |
---|
587 | }//if |
---|
588 | else { |
---|
589 | |
---|
590 | worldEntity->registerCollision(COLLISION_TYPE_AXIS_Y_NEG , this, worldEntity, Vector(0.0f, 1.0f, 0.0f), |
---|
591 | Vector(contact[0].geom.pos[0],contact[0].geom.pos[1],contact[0].geom.pos[2]), false); |
---|
592 | |
---|
593 | } |
---|
594 | |
---|
595 | }//if |
---|
596 | |
---|
597 | if(dCollide(this->ODE_Geom_ID,RayZ, 1, &contact[0].geom, sizeof(dContact))) { |
---|
598 | if(dCollide(this->ODE_Geom_ID,RayZPos, 1, &contact[0].geom, sizeof(dContact))) { |
---|
599 | // worldEntity->registerCollision(COLLISION_TYPE_AXIS_Z , this, worldEntity, Vector(0.0f, 0.0f, 1.0f), |
---|
600 | // Vector(contact[0].geom.pos[0],contact[0].geom.pos[1],contact[0].geom.pos[2]), false); |
---|
601 | |
---|
602 | }//if |
---|
603 | if(dCollide(this->ODE_Geom_ID,RayZNeg, 1, &contact[0].geom, sizeof(dContact))) { |
---|
604 | // worldEntity->registerCollision(COLLISION_TYPE_AXIS_Z_NEG , this, worldEntity, Vector(0.0f, 0.0f, 1.0f), |
---|
605 | // Vector(contact[0].geom.pos[0],contact[0].geom.pos[1],contact[0].geom.pos[2]), false); |
---|
606 | |
---|
607 | }//if |
---|
608 | |
---|
609 | |
---|
610 | }//if |
---|
611 | |
---|
612 | } //if |
---|
613 | |
---|
614 | |
---|
615 | dGeomDestroy(RayX); |
---|
616 | dGeomDestroy(RayY); |
---|
617 | dGeomDestroy(RayZ); |
---|
618 | dGeomDestroy(RayXPos); |
---|
619 | dGeomDestroy(RayYPos); |
---|
620 | dGeomDestroy(RayZPos); |
---|
621 | dGeomDestroy(RayXNeg); |
---|
622 | dGeomDestroy(RayYNeg); |
---|
623 | dGeomDestroy(RayZNeg); |
---|
624 | |
---|
625 | dGeomDestroy(BBOX); |
---|
626 | } // if(bbox != 0) |
---|
627 | |
---|
628 | } |
---|