[4597] | 1 | /* |
---|
[1853] | 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. |
---|
[1855] | 10 | |
---|
| 11 | ### File Specific: |
---|
[3565] | 12 | main-programmer: Benjamin Grauer |
---|
[1855] | 13 | co-programmer: ... |
---|
[1853] | 14 | */ |
---|
[5357] | 15 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY |
---|
[1853] | 16 | |
---|
| 17 | |
---|
[3559] | 18 | #include "terrain.h" |
---|
[4607] | 19 | |
---|
[5356] | 20 | #include "load_param.h" |
---|
[4607] | 21 | #include "factory.h" |
---|
[4842] | 22 | #include "spatial_separation.h" |
---|
| 23 | |
---|
[5465] | 24 | #include "resource_manager.h" |
---|
[5511] | 25 | #include "model.h" |
---|
[5940] | 26 | #include "height_map.h" |
---|
[5511] | 27 | #include "glincl.h" |
---|
| 28 | |
---|
[5940] | 29 | |
---|
[1856] | 30 | using namespace std; |
---|
[1853] | 31 | |
---|
[5750] | 32 | CREATE_FACTORY(Terrain, CL_TERRAIN); |
---|
[1856] | 33 | |
---|
[3245] | 34 | /** |
---|
[4836] | 35 | * standard constructor |
---|
[5357] | 36 | */ |
---|
[4607] | 37 | Terrain::Terrain (const TiXmlElement* root) |
---|
[3365] | 38 | { |
---|
[5940] | 39 | |
---|
[3564] | 40 | this->init(); |
---|
[5940] | 41 | |
---|
| 42 | // this->loadParams(root); |
---|
[4844] | 43 | |
---|
[5940] | 44 | HeightMap * hm = new HeightMap(); |
---|
| 45 | hm->load(); |
---|
| 46 | this->model=hm; |
---|
| 47 | |
---|
| 48 | /* this->model = new HeightMap(); |
---|
| 49 | this->model->setName("HardCore"); |
---|
| 50 | this->model->addVertex (-0.5, -0.5, 0.5); |
---|
| 51 | this->model->addVertex (0.5, -0.5, 0.5); |
---|
| 52 | this->model->addVertex (-0.5, 0.5, 0.5); |
---|
| 53 | this->model->addVertex (0.5, 0.5, 0.5); |
---|
| 54 | this->model->addVertex (-0.5, 0.5, -0.5); |
---|
| 55 | this->model->addVertex (0.5, 0.5, -0.5); |
---|
| 56 | this->model->addVertex (-0.5, -0.5, -0.5); |
---|
| 57 | this->model->addVertex (0.5, -0.5, -0.5); |
---|
| 58 | |
---|
| 59 | this->model->addVertexTexture (0.0, 0.0); |
---|
| 60 | this->model->addVertexTexture (1.0, 0.0); |
---|
| 61 | this->model->addVertexTexture (0.0, 1.0); |
---|
| 62 | this->model->addVertexTexture (1.0, 1.0); |
---|
| 63 | this->model->addVertexTexture (0.0, 2.0); |
---|
| 64 | this->model->addVertexTexture (1.0, 2.0); |
---|
| 65 | this->model->addVertexTexture (0.0, 3.0); |
---|
| 66 | this->model->addVertexTexture (1.0, 3.0); |
---|
| 67 | this->model->addVertexTexture (0.0, 4.0); |
---|
| 68 | this->model->addVertexTexture (1.0, 4.0); |
---|
| 69 | this->model->addVertexTexture (2.0, 0.0); |
---|
| 70 | this->model->addVertexTexture (2.0, 1.0); |
---|
| 71 | this->model->addVertexTexture (-1.0, 0.0); |
---|
| 72 | this->model->addVertexTexture (-1.0, 1.0); |
---|
| 73 | |
---|
| 74 | this->model->addVertexNormal (0.0, 0.0, 1.0); |
---|
| 75 | this->model->addVertexNormal (0.0, 0.0, 1.0); |
---|
| 76 | this->model->addVertexNormal (0.0, 0.0, 1.0); |
---|
| 77 | this->model->addVertexNormal (0.0, 0.0, 1.0); |
---|
| 78 | this->model->addVertexNormal (0.0, 1.0, 0.0); |
---|
| 79 | this->model->addVertexNormal (0.0, 1.0, 0.0); |
---|
| 80 | this->model->addVertexNormal (0.0, 1.0, 0.0); |
---|
| 81 | this->model->addVertexNormal (0.0, 1.0, 0.0); |
---|
| 82 | this->model->addVertexNormal (0.0, 0.0, -1.0); |
---|
| 83 | this->model->addVertexNormal (0.0, 0.0, -1.0); |
---|
| 84 | this->model->addVertexNormal (0.0, 0.0, -1.0); |
---|
| 85 | this->model->addVertexNormal (0.0, 0.0, -1.0); |
---|
| 86 | this->model->addVertexNormal (0.0, -1.0, 0.0); |
---|
| 87 | this->model->addVertexNormal (0.0, -1.0, 0.0); |
---|
| 88 | this->model->addVertexNormal (0.0, -1.0, 0.0); |
---|
| 89 | this->model->addVertexNormal (0.0, -1.0, 0.0); |
---|
| 90 | this->model->addVertexNormal (1.0, 0.0, 0.0); |
---|
| 91 | this->model->addVertexNormal (1.0, 0.0, 0.0); |
---|
| 92 | this->model->addVertexNormal (1.0, 0.0, 0.0); |
---|
| 93 | this->model->addVertexNormal (1.0, 0.0, 0.0); |
---|
| 94 | this->model->addVertexNormal (-1.0, 0.0, 0.0); |
---|
| 95 | this->model->addVertexNormal (-1.0, 0.0, 0.0); |
---|
| 96 | this->model->addVertexNormal (-1.0, 0.0, 0.0); |
---|
| 97 | this->model->addVertexNormal (-1.0, 0.0, 0.0); |
---|
| 98 | |
---|
| 99 | this->model->addFace (4, VERTEX_TEXCOORD_NORMAL, 0,0,0, 1,1,1, 3,3,2, 2,2,3); |
---|
| 100 | this->model->addFace (4, VERTEX_TEXCOORD_NORMAL, 2,2,4, 3,3,5, 5,5,6, 4,4,7); |
---|
| 101 | this->model->addFace (4, VERTEX_TEXCOORD_NORMAL, 4,4,8, 5,5,9, 7,7,10, 6,6,11); |
---|
| 102 | this->model->addFace (4, VERTEX_TEXCOORD_NORMAL, 6,6,12, 7,7,13, 1,9,14, 0,8,15); |
---|
| 103 | this->model->addFace (4, VERTEX_TEXCOORD_NORMAL, 1,1,16, 7,10,17, 5,11,18, 3,3,19); |
---|
| 104 | this->model->addFace (4, VERTEX_TEXCOORD_NORMAL, 6,12,20, 0,0,21, 2,2,22, 4,13,23); |
---|
| 105 | |
---|
| 106 | this->model->finalize(); */ |
---|
| 107 | |
---|
| 108 | /*if (this->model != NULL) |
---|
| 109 | this->ssp = new SpatialSeparation((AbstractModel*)this->model, 10.0f);*/ |
---|
[3365] | 110 | } |
---|
[1853] | 111 | |
---|
[4855] | 112 | |
---|
[3564] | 113 | /** |
---|
[4836] | 114 | * Constructor for loading a Terrain out of a file |
---|
| 115 | * @param fileName The file to load data from. |
---|
[4597] | 116 | |
---|
[3566] | 117 | this either loads out of an OBJ-file, or loads a heightmap if no .obj-extension is found. |
---|
| 118 | */ |
---|
[4597] | 119 | Terrain::Terrain(const char* fileName) |
---|
[3566] | 120 | { |
---|
| 121 | this->init(); |
---|
| 122 | |
---|
[5415] | 123 | if (!strstr(fileName, ".obj") || !strstr(fileName, ".OBJ") ) |
---|
[3566] | 124 | { |
---|
[5940] | 125 | this->model = NULL; |
---|
| 126 | //this->loadModel(fileName); |
---|
[3566] | 127 | } |
---|
| 128 | else |
---|
| 129 | { |
---|
| 130 | // load the hightMap here. |
---|
| 131 | } |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | /** |
---|
[4836] | 135 | * a Constructor for the Debug-Worlds |
---|
[5308] | 136 | */ |
---|
[3564] | 137 | Terrain::Terrain(DebugTerrain debugTerrain) |
---|
| 138 | { |
---|
| 139 | this->init(); |
---|
| 140 | this->buildDebugTerrain(debugTerrain); |
---|
| 141 | } |
---|
| 142 | |
---|
[3245] | 143 | /** |
---|
[4836] | 144 | * standard deconstructor |
---|
[1853] | 145 | |
---|
[3245] | 146 | */ |
---|
[4746] | 147 | Terrain::~Terrain () |
---|
[3543] | 148 | { |
---|
[3564] | 149 | if (objectList) |
---|
| 150 | glDeleteLists(this->objectList, 1); |
---|
[4889] | 151 | if( this->ssp) |
---|
| 152 | delete ssp; |
---|
[5465] | 153 | if (this->vegetation) |
---|
| 154 | { |
---|
| 155 | ResourceManager::getInstance()->unload(this->vegetation); |
---|
| 156 | } |
---|
[3543] | 157 | } |
---|
[1853] | 158 | |
---|
[3245] | 159 | |
---|
[4746] | 160 | void Terrain::init() |
---|
[3559] | 161 | { |
---|
[4320] | 162 | this->setClassID(CL_TERRAIN, "Terrain"); |
---|
[4597] | 163 | |
---|
[4136] | 164 | this->objectList = 0; |
---|
[5308] | 165 | this->ssp = NULL; |
---|
[5465] | 166 | this->vegetation = NULL; |
---|
[3559] | 167 | } |
---|
| 168 | |
---|
[4924] | 169 | |
---|
[4607] | 170 | void Terrain::loadParams(const TiXmlElement* root) |
---|
| 171 | { |
---|
| 172 | static_cast<WorldEntity*>(this)->loadParams(root); |
---|
[3559] | 173 | |
---|
[5671] | 174 | LoadParam(root, "vegetation", this, Terrain, loadVegetation) |
---|
[5465] | 175 | .describe("the fileName of the vegetation, that should be loaded onto this terrain. (must be relative to the data-dir)") ; |
---|
[4607] | 176 | } |
---|
| 177 | |
---|
[5465] | 178 | void Terrain::loadVegetation(const char* vegetationFile) |
---|
| 179 | { |
---|
| 180 | if (this->vegetation) |
---|
| 181 | ResourceManager::getInstance()->unload(this->vegetation, RP_LEVEL); |
---|
| 182 | if (vegetationFile != NULL) |
---|
| 183 | { |
---|
| 184 | PRINTF(4)("fetching %s\n", vegetationFile); |
---|
| 185 | this->vegetation = (Model*)ResourceManager::getInstance()->load(vegetationFile, OBJ, RP_CAMPAIGN); |
---|
| 186 | } |
---|
| 187 | else |
---|
| 188 | this->vegetation = NULL; |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | |
---|
| 192 | |
---|
[5500] | 193 | void Terrain::draw () const |
---|
[4597] | 194 | { |
---|
[3559] | 195 | glMatrixMode(GL_MODELVIEW); |
---|
| 196 | glPushMatrix(); |
---|
[4597] | 197 | |
---|
[3559] | 198 | /* translate */ |
---|
[4597] | 199 | glTranslatef (this->getAbsCoor ().x, |
---|
| 200 | this->getAbsCoor ().y, |
---|
| 201 | this->getAbsCoor ().z); |
---|
[3559] | 202 | /* rotate */ |
---|
[4998] | 203 | Vector tmpRot = this->getAbsDir().getSpacialAxis(); |
---|
| 204 | glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z ); |
---|
[5940] | 205 | this->model->draw(); |
---|
[4136] | 206 | if (this->objectList) |
---|
| 207 | glCallList(this->objectList); |
---|
| 208 | else if (this->model) |
---|
| 209 | this->model->draw(); |
---|
[5465] | 210 | if (this->vegetation) |
---|
| 211 | this->vegetation->draw(); |
---|
[3559] | 212 | glPopMatrix(); |
---|
[4904] | 213 | |
---|
[4924] | 214 | /* THIS IS ONLY FOR DEBUGGING INFORMATION */ |
---|
[5940] | 215 | // if (this->ssp != NULL) |
---|
| 216 | // this->ssp->drawQuadtree(); |
---|
[3559] | 217 | } |
---|
[3564] | 218 | |
---|
| 219 | |
---|
| 220 | void Terrain::buildDebugTerrain(DebugTerrain debugTerrain) |
---|
| 221 | { |
---|
| 222 | // if the terrain is the Terrain of Dave |
---|
| 223 | if (debugTerrain == TERRAIN_DAVE) |
---|
| 224 | { |
---|
| 225 | objectList = glGenLists(1); |
---|
| 226 | glNewList (objectList, GL_COMPILE); |
---|
[4597] | 227 | |
---|
[3564] | 228 | glColor3f(1.0,0,0); |
---|
[4597] | 229 | |
---|
[3564] | 230 | int sizeX = 100; |
---|
| 231 | int sizeZ = 80; |
---|
| 232 | float length = 1000; |
---|
| 233 | float width = 200; |
---|
| 234 | float widthX = float (length /sizeX); |
---|
| 235 | float widthZ = float (width /sizeZ); |
---|
[4597] | 236 | |
---|
[3564] | 237 | float height [sizeX][sizeZ]; |
---|
| 238 | Vector normal_vectors[sizeX][sizeZ]; |
---|
[4597] | 239 | |
---|
| 240 | |
---|
[3564] | 241 | for ( int i = 0; i<sizeX-1; i+=1) |
---|
[4597] | 242 | for (int j = 0; j<sizeZ-1;j+=1) |
---|
| 243 | //height[i][j] = rand()/20046 + (j-25)*(j-25)/30; |
---|
[3564] | 244 | #ifdef __WIN32__ |
---|
[4597] | 245 | height[i][j]=(sin((float)j/3)*rand()*i/182400)*.5; |
---|
[3564] | 246 | #else |
---|
| 247 | height[i][j]=(sin((float)j/3)*rand()*(long)i/6282450500.0)*.5; |
---|
| 248 | #endif |
---|
[4597] | 249 | |
---|
[3564] | 250 | //Die Huegel ein wenig glaetten |
---|
| 251 | for (int h=1; h<2;h++) |
---|
[4597] | 252 | for (int i=1;i<sizeX-2 ;i+=1 ) |
---|
| 253 | for(int j=1;j<sizeZ-2;j+=1) |
---|
| 254 | height[i][j]=(height[i+1][j]+height[i][j+1]+height[i-1][j]+height[i][j-1])/4; |
---|
| 255 | |
---|
[3564] | 256 | //Berechnung von normalen Vektoren |
---|
| 257 | for(int i=1;i<sizeX-2;i+=1) |
---|
[4597] | 258 | for(int j=1;j<sizeZ-2 ;j+=1) |
---|
| 259 | { |
---|
| 260 | Vector v1 = Vector (widthX*(1), height[i][j], widthZ*(j) ); |
---|
| 261 | Vector v2 = Vector (widthX*(i-1), height[i-1][j], widthZ*(j)); |
---|
| 262 | Vector v3 = Vector (widthX*(i), height[i][j+1], widthZ*(j+1)); |
---|
| 263 | Vector v4 = Vector (widthX*(i+1), height[i+1][j], widthZ*(j)); |
---|
| 264 | Vector v5 = Vector (widthX*(i), height[i][j-1], widthZ*(j-1)); |
---|
| 265 | |
---|
| 266 | Vector c1 = v2 - v1; |
---|
| 267 | Vector c2 = v3 - v1; |
---|
| 268 | Vector c3= v4 - v1; |
---|
| 269 | Vector c4 = v5 - v1; |
---|
| 270 | Vector zero = Vector (0,0,0); |
---|
| 271 | normal_vectors[i][j]=c1.cross(v3-v5)+c2.cross(v4-v2)+c3.cross(v5-v3)+c4.cross(v2-v4); |
---|
| 272 | normal_vectors[i][j].normalize(); |
---|
| 273 | } |
---|
| 274 | |
---|
[3564] | 275 | glBegin(GL_QUADS); |
---|
| 276 | int snowheight=3; |
---|
| 277 | for ( int i = 0; i<sizeX; i+=1) |
---|
[4597] | 278 | for (int j = 0; j<sizeZ;j+=1) |
---|
| 279 | { |
---|
| 280 | Vector v1 = Vector (widthX*(i), height[i][j]-20, widthZ*(j) -width/2); |
---|
| 281 | Vector v2 = Vector (widthX*(i+1), height[i+1][j]-20, widthZ*(j) -width/2); |
---|
| 282 | Vector v3 = Vector (widthX*(i+1), height[i+1][j+1]-20, widthZ*(j+1)-width/2); |
---|
| 283 | Vector v4 = Vector (widthX*(i), height[i][j+1]-20, widthZ*(j+1)-width/2); |
---|
| 284 | float a[3]; |
---|
| 285 | if(height[i][j]<snowheight){ |
---|
| 286 | a[0]=0; |
---|
| 287 | a[1]=1.0-height[i][j]/10-.3; |
---|
| 288 | a[2]=0; |
---|
| 289 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
| 290 | } |
---|
| 291 | else{ |
---|
| 292 | a[0]=1.0; |
---|
| 293 | a[1]=1.0; |
---|
| 294 | a[2]=1.0; |
---|
| 295 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
| 296 | |
---|
| 297 | } |
---|
| 298 | glNormal3f(normal_vectors[i][j].x, normal_vectors[i][j].y, normal_vectors[i][j].z); |
---|
| 299 | glVertex3f(v1.x, v1.y, v1.z); |
---|
| 300 | if(height[i+1][j]<snowheight){ |
---|
| 301 | a[0]=0; |
---|
| 302 | a[1] =1.0-height[i+1][j]/10-.3; |
---|
| 303 | a[2]=0; |
---|
| 304 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
| 305 | } |
---|
| 306 | else{ |
---|
| 307 | a[0]=1.0; |
---|
| 308 | a[1]=1.0; |
---|
| 309 | a[2]=1.0; |
---|
| 310 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
| 311 | |
---|
| 312 | } |
---|
| 313 | glNormal3f(normal_vectors[i+1][j].x, normal_vectors[i+1][j].y, normal_vectors[i+1][j].z); |
---|
| 314 | glVertex3f(v2.x, v2.y, v2.z); |
---|
| 315 | if(height[i+1][j+1]<snowheight){ |
---|
| 316 | a[0]=0; |
---|
| 317 | a[1] =1.0-height[i+1][j+1]/10-.3; |
---|
| 318 | a[2]=0; |
---|
| 319 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
| 320 | } |
---|
| 321 | else{ |
---|
| 322 | a[0]=1.0; |
---|
| 323 | a[1]=1.0; |
---|
| 324 | a[2]=1.0; |
---|
| 325 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
| 326 | |
---|
| 327 | |
---|
| 328 | } |
---|
| 329 | glNormal3f(normal_vectors[i+1][j+1].x, normal_vectors[i+1][j+1].y, normal_vectors[i+1][j+1].z); |
---|
| 330 | glVertex3f(v3.x, v3.y, v3.z); |
---|
| 331 | if(height[i][j+1]<snowheight){ |
---|
| 332 | a[0]=0; |
---|
| 333 | a[1] =1.0-height[i+1][j+1]/10-.3; |
---|
| 334 | a[2]=0; |
---|
| 335 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
| 336 | } |
---|
| 337 | else{ |
---|
| 338 | a[0]=1.0; |
---|
| 339 | a[1]=1.0; |
---|
| 340 | a[2]=1.0; |
---|
| 341 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
| 342 | } |
---|
| 343 | glNormal3f(normal_vectors[i][j+1].x, normal_vectors[i][j+1].y, normal_vectors[i][j+1].z); |
---|
| 344 | glVertex3f(v4.x, v4.y, v4.z); |
---|
| 345 | |
---|
| 346 | } |
---|
[3564] | 347 | glEnd(); |
---|
| 348 | glEndList(); |
---|
| 349 | } |
---|
| 350 | |
---|
| 351 | if (debugTerrain == TERRAIN_BENSCH) |
---|
| 352 | { |
---|
| 353 | /* |
---|
[4597] | 354 | this->model = (OBJModel*) new Model(); |
---|
[3564] | 355 | this->model->setName("CUBE"); |
---|
| 356 | this->model->addVertex (-0.5, -0.5, 0.5); |
---|
| 357 | this->model->addVertex (0.5, -0.5, 0.5); |
---|
| 358 | this->model->addVertex (-0.5, 0.5, 0.5); |
---|
| 359 | this->model->addVertex (0.5, 0.5, 0.5); |
---|
| 360 | this->model->addVertex (-0.5, 0.5, -0.5); |
---|
| 361 | this->model->addVertex (0.5, 0.5, -0.5); |
---|
| 362 | this->model->addVertex (-0.5, -0.5, -0.5); |
---|
| 363 | this->model->addVertex (0.5, -0.5, -0.5); |
---|
[4597] | 364 | |
---|
[3564] | 365 | this->model->addVertexTexture (0.0, 0.0); |
---|
| 366 | this->model->addVertexTexture (1.0, 0.0); |
---|
| 367 | this->model->addVertexTexture (0.0, 1.0); |
---|
| 368 | this->model->addVertexTexture (1.0, 1.0); |
---|
| 369 | this->model->addVertexTexture (0.0, 2.0); |
---|
| 370 | this->model->addVertexTexture (1.0, 2.0); |
---|
| 371 | this->model->addVertexTexture (0.0, 3.0); |
---|
| 372 | this->model->addVertexTexture (1.0, 3.0); |
---|
| 373 | this->model->addVertexTexture (0.0, 4.0); |
---|
| 374 | this->model->addVertexTexture (1.0, 4.0); |
---|
| 375 | this->model->addVertexTexture (2.0, 0.0); |
---|
| 376 | this->model->addVertexTexture (2.0, 1.0); |
---|
| 377 | this->model->addVertexTexture (-1.0, 0.0); |
---|
| 378 | this->model->addVertexTexture (-1.0, 1.0); |
---|
| 379 | |
---|
| 380 | this->model->finalize(); |
---|
| 381 | */ |
---|
| 382 | } |
---|
| 383 | } |
---|