| 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_entity.h" |
|---|
| 19 | #include "terrain/terrain.h" |
|---|
| 20 | #include "util/loading/load_param.h" |
|---|
| 21 | #include "util/loading/factory.h" |
|---|
| 22 | #include "spatial_separation.h" |
|---|
| 23 | |
|---|
| 24 | #include "util/loading/resource_manager.h" |
|---|
| 25 | #include "model.h" |
|---|
| 26 | #include "network_game_manager.h" |
|---|
| 27 | #include "vector.h" |
|---|
| 28 | #include "material.h" |
|---|
| 29 | |
|---|
| 30 | #include "glincl.h" |
|---|
| 31 | |
|---|
| 32 | #include "state.h" |
|---|
| 33 | |
|---|
| 34 | CREATE_FACTORY( TerrainEntity, CL_TERRAIN_ENTITY ); |
|---|
| 35 | |
|---|
| 36 | /** |
|---|
| 37 | * standard constructor |
|---|
| 38 | */ |
|---|
| 39 | TerrainEntity::TerrainEntity (const TiXmlElement* root) |
|---|
| 40 | { |
|---|
| 41 | this->init(); |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | if( root != NULL) |
|---|
| 45 | this->loadParams(root); |
|---|
| 46 | terrain->build( ); |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | /** |
|---|
| 51 | * Constructor for loading a TerrainEntity out of a file |
|---|
| 52 | * @param fileName The file to load data from. |
|---|
| 53 | |
|---|
| 54 | this either loads out of an OBJ-file, or loads a heightmap if no .obj-extension is found. |
|---|
| 55 | */ |
|---|
| 56 | TerrainEntity::TerrainEntity(const std::string& fileName ) |
|---|
| 57 | { |
|---|
| 58 | this->init(); |
|---|
| 59 | |
|---|
| 60 | if (fileName.rfind(".obj" ) != std::string::npos || fileName.rfind(".OBJ") != std::string::npos ) |
|---|
| 61 | { |
|---|
| 62 | this->loadModel(fileName); |
|---|
| 63 | } |
|---|
| 64 | else |
|---|
| 65 | { |
|---|
| 66 | // load the hightMap here. |
|---|
| 67 | } |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | /** |
|---|
| 71 | * a Constructor for the Debug-Worlds |
|---|
| 72 | */ |
|---|
| 73 | TerrainEntity::TerrainEntity(DebugTerrainEntity debugTerrainEntity) |
|---|
| 74 | { |
|---|
| 75 | this->init(); |
|---|
| 76 | this->buildDebugTerrainEntity(debugTerrainEntity); |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | /** |
|---|
| 80 | * standard deconstructor |
|---|
| 81 | |
|---|
| 82 | */ |
|---|
| 83 | TerrainEntity::~TerrainEntity () |
|---|
| 84 | { |
|---|
| 85 | if (objectList) |
|---|
| 86 | glDeleteLists(this->objectList, 1); |
|---|
| 87 | |
|---|
| 88 | if (this->vegetation) |
|---|
| 89 | { |
|---|
| 90 | ResourceManager::getInstance()->unload( this->vegetation ); |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | if( this->terrain ) |
|---|
| 94 | delete terrain; |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | void TerrainEntity::init() |
|---|
| 99 | { |
|---|
| 100 | this->setClassID( CL_TERRAIN_ENTITY, "TerrainEntity"); |
|---|
| 101 | this->toList(OM_ENVIRON); |
|---|
| 102 | this->toReflectionList(); |
|---|
| 103 | |
|---|
| 104 | this->objectList = 0; |
|---|
| 105 | this->vegetation = NULL; |
|---|
| 106 | |
|---|
| 107 | this->terrain = new Terrain(); |
|---|
| 108 | |
|---|
| 109 | //this->heightMapMaterial = new Material(); |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | |
|---|
| 113 | void TerrainEntity::loadParams(const TiXmlElement* root) |
|---|
| 114 | { |
|---|
| 115 | WorldEntity::loadParams( root ); |
|---|
| 116 | |
|---|
| 117 | LoadParam(root, "scale", this, TerrainEntity, setScale) |
|---|
| 118 | .describe("The scale in x,y,z direction"); |
|---|
| 119 | |
|---|
| 120 | LoadParam( root, "light_map", this, TerrainEntity, loadLightmap ) |
|---|
| 121 | .describe("The name of the lightmap."); |
|---|
| 122 | |
|---|
| 123 | LoadParam( root, "elevation_map", this, TerrainEntity, loadElevationmap ) |
|---|
| 124 | .describe( "The name of the elevation map. Must be an 8bit image" ); |
|---|
| 125 | ResourceManager *manager = ResourceManager::getInstance(); |
|---|
| 126 | TiXmlElement * layer = (TiXmlElement*)root->FirstChild( "material_layer" ); |
|---|
| 127 | while ( layer ) |
|---|
| 128 | { |
|---|
| 129 | LayerInfo *info = new LayerInfo(); |
|---|
| 130 | TiXmlElement *detail = (TiXmlElement*)layer->FirstChild( "detail_map" ); |
|---|
| 131 | if ( detail ) |
|---|
| 132 | { |
|---|
| 133 | std::string detailmap( detail->Attribute( "file" ) ); |
|---|
| 134 | info->detail = (Texture*)manager->load( detailmap, |
|---|
| 135 | IMAGE, RP_GAME, (int)GL_TEXTURE_2D ); |
|---|
| 136 | if ( !info->detail ) |
|---|
| 137 | { |
|---|
| 138 | PRINTF(0)( "%s not found\n", detailmap.c_str() ); |
|---|
| 139 | } |
|---|
| 140 | else |
|---|
| 141 | PRINTF(0)( "loaded %s\n", detailmap.c_str() ); |
|---|
| 142 | TiXmlElement *repeat = (TiXmlElement*)detail->FirstChild( "repeat" ); |
|---|
| 143 | if ( repeat ) |
|---|
| 144 | { |
|---|
| 145 | repeat->QueryFloatAttribute( "x", &info->repeatX ); |
|---|
| 146 | repeat->QueryFloatAttribute( "z", &info->repeatZ ); |
|---|
| 147 | } |
|---|
| 148 | } |
|---|
| 149 | else |
|---|
| 150 | { |
|---|
| 151 | PRINTF(0)( "Please specify a detail-map" ); |
|---|
| 152 | } |
|---|
| 153 | TiXmlElement *alpha = (TiXmlElement*)layer->FirstChild( "alpha" ); |
|---|
| 154 | if ( alpha ) |
|---|
| 155 | { |
|---|
| 156 | if ( !alpha->Attribute( "full" ) ) |
|---|
| 157 | { |
|---|
| 158 | std::string alphamap( alpha->Attribute( "file" ) ); |
|---|
| 159 | info->alpha = (Texture*)manager->load( alphamap, |
|---|
| 160 | IMAGE, RP_GAME, (int)GL_TEXTURE_2D ); |
|---|
| 161 | if ( !info->alpha ) |
|---|
| 162 | PRINTF(0)( "%s not found\n", alphamap.c_str() ); |
|---|
| 163 | else |
|---|
| 164 | PRINTF(0)( "loaded %s\n", alphamap.c_str() ); |
|---|
| 165 | } |
|---|
| 166 | } |
|---|
| 167 | terrain->addMaterialLayer( info ); |
|---|
| 168 | layer = (TiXmlElement*)layer->NextSibling( "material_layer" ); |
|---|
| 169 | } |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | void TerrainEntity::setScale(float x, float y, float z) |
|---|
| 173 | { |
|---|
| 174 | terrain->setScale( Vector( x, y, z ) ); |
|---|
| 175 | } |
|---|
| 176 | |
|---|
| 177 | void TerrainEntity::loadElevationmap( const std::string& _eleFile ) |
|---|
| 178 | { |
|---|
| 179 | terrain->setHeightmap( _eleFile ); |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | void TerrainEntity::loadLightmap( const std::string& _lightFile ) |
|---|
| 183 | { |
|---|
| 184 | ResourceManager *manager = ResourceManager::getInstance(); |
|---|
| 185 | Texture *lightmap = (Texture*)manager->load( _lightFile, |
|---|
| 186 | IMAGE, RP_GAME, (int)GL_TEXTURE_2D ); |
|---|
| 187 | if ( lightmap ) |
|---|
| 188 | terrain->setLightmap( lightmap ); |
|---|
| 189 | else |
|---|
| 190 | { |
|---|
| 191 | PRINTF(0)("no lightmap %s\n", _lightFile.c_str() ); |
|---|
| 192 | |
|---|
| 193 | } |
|---|
| 194 | } |
|---|
| 195 | |
|---|
| 196 | |
|---|
| 197 | |
|---|
| 198 | void TerrainEntity::loadVegetation(const std::string& vegetationFile) |
|---|
| 199 | { |
|---|
| 200 | PRINTF(4)("loadVegetation: %s\n", vegetationFile.c_str()); |
|---|
| 201 | if (this->vegetation) |
|---|
| 202 | ResourceManager::getInstance()->unload(this->vegetation, RP_LEVEL); |
|---|
| 203 | if (!vegetationFile.empty()) |
|---|
| 204 | { |
|---|
| 205 | PRINTF(4)("fetching %s\n", vegetationFile.c_str()); |
|---|
| 206 | this->vegetation = dynamic_cast<Model*>(ResourceManager::getInstance()->load(vegetationFile, OBJ, RP_CAMPAIGN)); |
|---|
| 207 | } |
|---|
| 208 | else |
|---|
| 209 | this->vegetation = NULL; |
|---|
| 210 | } |
|---|
| 211 | |
|---|
| 212 | |
|---|
| 213 | |
|---|
| 214 | void TerrainEntity::tick( float _dt ) { if ( terrain ) terrain->tick( _dt ); } |
|---|
| 215 | |
|---|
| 216 | void TerrainEntity::draw () const |
|---|
| 217 | { |
|---|
| 218 | glMatrixMode( GL_MODELVIEW ); |
|---|
| 219 | glPushMatrix(); |
|---|
| 220 | |
|---|
| 221 | glTranslatef( this->getAbsCoor().x, |
|---|
| 222 | this->getAbsCoor().y, |
|---|
| 223 | this->getAbsCoor().z ); |
|---|
| 224 | |
|---|
| 225 | Vector cam = State::getCameraNode()->getAbsCoor(); |
|---|
| 226 | |
|---|
| 227 | if ( this->terrain ) |
|---|
| 228 | { |
|---|
| 229 | terrain->setCameraPosition( cam ); |
|---|
| 230 | terrain->draw( ); |
|---|
| 231 | } |
|---|
| 232 | glPopMatrix(); |
|---|
| 233 | |
|---|
| 234 | |
|---|
| 235 | } |
|---|
| 236 | |
|---|
| 237 | |
|---|
| 238 | void TerrainEntity::buildDebugTerrainEntity(DebugTerrainEntity debugTerrainEntity) |
|---|
| 239 | { |
|---|
| 240 | terrain->build( ); |
|---|
| 241 | /* |
|---|
| 242 | // if the TerrainEntity is the TerrainEntity of Dave |
|---|
| 243 | if (debugTerrainEntity == TERRAINENTITY_DAVE) |
|---|
| 244 | { |
|---|
| 245 | objectList = glGenLists(1); |
|---|
| 246 | glNewList (objectList, GL_COMPILE); |
|---|
| 247 | |
|---|
| 248 | glColor3f(1.0,0,0); |
|---|
| 249 | |
|---|
| 250 | int sizeX = 100; |
|---|
| 251 | int sizeZ = 80; |
|---|
| 252 | float length = 1000; |
|---|
| 253 | float width = 200; |
|---|
| 254 | float widthX = float (length /sizeX); |
|---|
| 255 | float widthZ = float (width /sizeZ); |
|---|
| 256 | |
|---|
| 257 | float height [sizeX][sizeZ]; |
|---|
| 258 | Vector normal_vectors[sizeX][sizeZ]; |
|---|
| 259 | |
|---|
| 260 | |
|---|
| 261 | for ( int i = 0; i<sizeX-1; i+=1) |
|---|
| 262 | for (int j = 0; j<sizeZ-1;j+=1) |
|---|
| 263 | //height[i][j] = rand()/20046 + (j-25)*(j-25)/30; |
|---|
| 264 | #ifdef __WIN32__ |
|---|
| 265 | height[i][j]=(sin((float)j/3)*rand()*i/182400)*.5; |
|---|
| 266 | #else |
|---|
| 267 | height[i][j]=(sin((float)j/3)*rand()*(long)i/6282450500.0)*.5; |
|---|
| 268 | #endif |
|---|
| 269 | |
|---|
| 270 | //Die Huegel ein wenig glaetten |
|---|
| 271 | for (int h=1; h<2;h++) |
|---|
| 272 | for (int i=1;i<sizeX-2 ;i+=1 ) |
|---|
| 273 | for(int j=1;j<sizeZ-2;j+=1) |
|---|
| 274 | height[i][j]=(height[i+1][j]+height[i][j+1]+height[i-1][j]+height[i][j-1])/4; |
|---|
| 275 | |
|---|
| 276 | //Berechnung von normalen Vektoren |
|---|
| 277 | for(int i=1;i<sizeX-2;i+=1) |
|---|
| 278 | for(int j=1;j<sizeZ-2 ;j+=1) |
|---|
| 279 | { |
|---|
| 280 | Vector v1 = Vector (widthX*(1), height[i][j], widthZ*(j) ); |
|---|
| 281 | Vector v2 = Vector (widthX*(i-1), height[i-1][j], widthZ*(j)); |
|---|
| 282 | Vector v3 = Vector (widthX*(i), height[i][j+1], widthZ*(j+1)); |
|---|
| 283 | Vector v4 = Vector (widthX*(i+1), height[i+1][j], widthZ*(j)); |
|---|
| 284 | Vector v5 = Vector (widthX*(i), height[i][j-1], widthZ*(j-1)); |
|---|
| 285 | |
|---|
| 286 | Vector c1 = v2 - v1; |
|---|
| 287 | Vector c2 = v3 - v1; |
|---|
| 288 | Vector c3= v4 - v1; |
|---|
| 289 | Vector c4 = v5 - v1; |
|---|
| 290 | Vector zero = Vector (0,0,0); |
|---|
| 291 | normal_vectors[i][j]=c1.cross(v3-v5)+c2.cross(v4-v2)+c3.cross(v5-v3)+c4.cross(v2-v4); |
|---|
| 292 | normal_vectors[i][j].normalize(); |
|---|
| 293 | } |
|---|
| 294 | |
|---|
| 295 | glBegin(GL_QUADS); |
|---|
| 296 | int snowheight=3; |
|---|
| 297 | for ( int i = 0; i<sizeX; i+=1) |
|---|
| 298 | for (int j = 0; j<sizeZ;j+=1) |
|---|
| 299 | { |
|---|
| 300 | Vector v1 = Vector (widthX*(i), height[i][j]-20, widthZ*(j) -width/2); |
|---|
| 301 | Vector v2 = Vector (widthX*(i+1), height[i+1][j]-20, widthZ*(j) -width/2); |
|---|
| 302 | Vector v3 = Vector (widthX*(i+1), height[i+1][j+1]-20, widthZ*(j+1)-width/2); |
|---|
| 303 | Vector v4 = Vector (widthX*(i), height[i][j+1]-20, widthZ*(j+1)-width/2); |
|---|
| 304 | float a[3]; |
|---|
| 305 | if(height[i][j]<snowheight) |
|---|
| 306 | { |
|---|
| 307 | a[0]=0; |
|---|
| 308 | a[1]=1.0-height[i][j]/10-.3; |
|---|
| 309 | a[2]=0; |
|---|
| 310 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
|---|
| 311 | } |
|---|
| 312 | else |
|---|
| 313 | { |
|---|
| 314 | a[0]=1.0; |
|---|
| 315 | a[1]=1.0; |
|---|
| 316 | a[2]=1.0; |
|---|
| 317 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
|---|
| 318 | |
|---|
| 319 | } |
|---|
| 320 | glNormal3f(normal_vectors[i][j].x, normal_vectors[i][j].y, normal_vectors[i][j].z); |
|---|
| 321 | glVertex3f(v1.x, v1.y, v1.z); |
|---|
| 322 | if(height[i+1][j]<snowheight) |
|---|
| 323 | { |
|---|
| 324 | a[0]=0; |
|---|
| 325 | a[1] =1.0-height[i+1][j]/10-.3; |
|---|
| 326 | a[2]=0; |
|---|
| 327 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
|---|
| 328 | } |
|---|
| 329 | else |
|---|
| 330 | { |
|---|
| 331 | a[0]=1.0; |
|---|
| 332 | a[1]=1.0; |
|---|
| 333 | a[2]=1.0; |
|---|
| 334 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
|---|
| 335 | |
|---|
| 336 | } |
|---|
| 337 | glNormal3f(normal_vectors[i+1][j].x, normal_vectors[i+1][j].y, normal_vectors[i+1][j].z); |
|---|
| 338 | glVertex3f(v2.x, v2.y, v2.z); |
|---|
| 339 | if(height[i+1][j+1]<snowheight) |
|---|
| 340 | { |
|---|
| 341 | a[0]=0; |
|---|
| 342 | a[1] =1.0-height[i+1][j+1]/10-.3; |
|---|
| 343 | a[2]=0; |
|---|
| 344 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
|---|
| 345 | } |
|---|
| 346 | else |
|---|
| 347 | { |
|---|
| 348 | a[0]=1.0; |
|---|
| 349 | a[1]=1.0; |
|---|
| 350 | a[2]=1.0; |
|---|
| 351 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
|---|
| 352 | |
|---|
| 353 | |
|---|
| 354 | } |
|---|
| 355 | glNormal3f(normal_vectors[i+1][j+1].x, normal_vectors[i+1][j+1].y, normal_vectors[i+1][j+1].z); |
|---|
| 356 | glVertex3f(v3.x, v3.y, v3.z); |
|---|
| 357 | if(height[i][j+1]<snowheight) |
|---|
| 358 | { |
|---|
| 359 | a[0]=0; |
|---|
| 360 | a[1] =1.0-height[i+1][j+1]/10-.3; |
|---|
| 361 | a[2]=0; |
|---|
| 362 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
|---|
| 363 | } |
|---|
| 364 | else |
|---|
| 365 | { |
|---|
| 366 | a[0]=1.0; |
|---|
| 367 | a[1]=1.0; |
|---|
| 368 | a[2]=1.0; |
|---|
| 369 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
|---|
| 370 | } |
|---|
| 371 | glNormal3f(normal_vectors[i][j+1].x, normal_vectors[i][j+1].y, normal_vectors[i][j+1].z); |
|---|
| 372 | glVertex3f(v4.x, v4.y, v4.z); |
|---|
| 373 | |
|---|
| 374 | } |
|---|
| 375 | glEnd(); |
|---|
| 376 | glEndList(); |
|---|
| 377 | } |
|---|
| 378 | */ |
|---|
| 379 | if (debugTerrainEntity == TERRAINENTITY_BENSCH) |
|---|
| 380 | { |
|---|
| 381 | /* |
|---|
| 382 | this->model = (OBJModel*) new Model(); |
|---|
| 383 | this->model->setName("CUBE"); |
|---|
| 384 | this->model->addVertex (-0.5, -0.5, 0.5); |
|---|
| 385 | this->model->addVertex (0.5, -0.5, 0.5); |
|---|
| 386 | this->model->addVertex (-0.5, 0.5, 0.5); |
|---|
| 387 | this->model->addVertex (0.5, 0.5, 0.5); |
|---|
| 388 | this->model->addVertex (-0.5, 0.5, -0.5); |
|---|
| 389 | this->model->addVertex (0.5, 0.5, -0.5); |
|---|
| 390 | this->model->addVertex (-0.5, -0.5, -0.5); |
|---|
| 391 | this->model->addVertex (0.5, -0.5, -0.5); |
|---|
| 392 | |
|---|
| 393 | this->model->addVertexTexture (0.0, 0.0); |
|---|
| 394 | this->model->addVertexTexture (1.0, 0.0); |
|---|
| 395 | this->model->addVertexTexture (0.0, 1.0); |
|---|
| 396 | this->model->addVertexTexture (1.0, 1.0); |
|---|
| 397 | this->model->addVertexTexture (0.0, 2.0); |
|---|
| 398 | this->model->addVertexTexture (1.0, 2.0); |
|---|
| 399 | this->model->addVertexTexture (0.0, 3.0); |
|---|
| 400 | this->model->addVertexTexture (1.0, 3.0); |
|---|
| 401 | this->model->addVertexTexture (0.0, 4.0); |
|---|
| 402 | this->model->addVertexTexture (1.0, 4.0); |
|---|
| 403 | this->model->addVertexTexture (2.0, 0.0); |
|---|
| 404 | this->model->addVertexTexture (2.0, 1.0); |
|---|
| 405 | this->model->addVertexTexture (-1.0, 0.0); |
|---|
| 406 | this->model->addVertexTexture (-1.0, 1.0); |
|---|
| 407 | |
|---|
| 408 | this->model->finalize(); |
|---|
| 409 | */ |
|---|
| 410 | } |
|---|
| 411 | } |
|---|
| 412 | void TerrainEntity::getAltitude( Vector& _position, Vector& _normal ) |
|---|
| 413 | { |
|---|
| 414 | Vector altitude( _position.x-getAbsCoor().x, 0.0f, _position.z-getAbsCoor().z ), |
|---|
| 415 | normal( 0.0f, 0.0f, 0.0f ); |
|---|
| 416 | if ( terrain ) |
|---|
| 417 | terrain->getAltitude( altitude, normal ); |
|---|
| 418 | |
|---|
| 419 | _position.y = altitude.y+getAbsCoor().y; |
|---|
| 420 | _normal.z = normal.z; _normal.y = normal.y; _normal.z = normal.z; |
|---|
| 421 | } |
|---|
| 422 | |
|---|
| 423 | float TerrainEntity::getHeight( float x, float z ) |
|---|
| 424 | { |
|---|
| 425 | Vector altitude( x-getAbsCoor().x, 0.0f, z-getAbsCoor().z ), |
|---|
| 426 | normal( 0.0f, 0.0f, 0.0f ); |
|---|
| 427 | if ( terrain ) |
|---|
| 428 | terrain->getAltitude( altitude, normal ); |
|---|
| 429 | |
|---|
| 430 | return altitude.y+getAbsCoor().y; |
|---|
| 431 | } |
|---|