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