Changeset 7221 in orxonox.OLD for trunk/src/world_entities/terrain.cc
- Timestamp:
- Mar 15, 2006, 3:10:45 PM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/world_entities/terrain.cc
r7193 r7221 48 48 this->loadParams(root); 49 49 50 // if (this->model != NULL)51 50 // if (this->model != NULL) 51 //this->ssp = new SpatialSeparation((Model*)this->model, 10.0f); 52 52 } 53 53 … … 59 59 this either loads out of an OBJ-file, or loads a heightmap if no .obj-extension is found. 60 60 */ 61 Terrain::Terrain(const char*fileName)61 Terrain::Terrain(const std::string& fileName) 62 62 { 63 63 this->init(); 64 64 65 if ( !strstr(fileName, ".obj") || !strstr(fileName, ".OBJ"))66 67 68 65 if (fileName.rfind(".obj" ) != -1 || fileName.rfind(".OBJ") != -1 ) 66 { 67 this->loadModel(fileName); 68 } 69 69 else 70 71 72 70 { 71 // load the hightMap here. 72 } 73 73 } 74 74 … … 98 98 99 99 if(this->heightMap) 100 100 delete heightMap; 101 101 } 102 102 … … 122 122 123 123 LoadParam(root, "scale", this, Terrain, setScale) 124 124 .describe("The scale in x,y,z direction"); 125 125 126 126 LoadParam(root, "texture", this, Terrain, loadTexture) 127 127 .describe("The name of the Texture for this heightMap"); 128 128 129 129 LoadParam(root, "vegetation", this, Terrain, loadVegetation) 130 130 .describe("the fileName of the vegetation, that should be loaded onto this terrain. (must be relative to the data-dir)") ; 131 131 132 132 LoadParam(root, "height-map", this, Terrain, loadHeightMap) 133 133 .describe("The HeightMap, splitted into two strings seperated by ','. 1: HeighMap, 2: ColorMap"); 134 134 135 135 } … … 140 140 } 141 141 142 void Terrain::loadHeightMap(const char* heightMapFile, const char*colorMap)142 void Terrain::loadHeightMap(const std::string& heightMapFile, const std::string& colorMap) 143 143 { 144 144 if (this->heightMap != NULL) … … 146 146 this->heightMap = NULL; 147 147 148 char*hmName = ResourceManager::getFullName(heightMapFile);149 char*hmColorName = ResourceManager::getFullName(colorMap);148 std::string hmName = ResourceManager::getFullName(heightMapFile); 149 std::string hmColorName = ResourceManager::getFullName(colorMap); 150 150 151 151 152 152 this->heightMap = new HeightMap(hmName, hmColorName); 153 // heightMap->scale(Vector(43.0f,4.7f,43.0f));153 // heightMap->scale(Vector(43.0f,4.7f,43.0f)); 154 154 heightMap->scale(this->terrainScale); 155 155 heightMap->setAbsCoor(this->getAbsCoor()); 156 156 heightMap->load(); 157 delete[] hmName; 158 delete[] hmColorName; 159 160 } 161 162 163 void Terrain::loadTexture(const char* textureName) 164 { 165 PRINTF(0)("Load texture: %s\n", textureName); 166 167 heightMapMaterial->setDiffuse(1.0,1.0,1.0); 168 heightMapMaterial->setAmbient(1.0,1.0,1.0 ); 169 heightMapMaterial->setSpecular(1.0,1.0,1.0); 170 heightMapMaterial->setShininess(.5); 171 heightMapMaterial->setTransparency(1.0); 172 173 heightMapMaterial->setDiffuseMap(textureName); 174 heightMapMaterial->setAmbientMap(textureName); 175 heightMapMaterial->setSpecularMap(textureName); 176 } 177 178 179 180 void Terrain::loadVegetation(const char* vegetationFile) 181 { 182 PRINTF(0)("loadVegetation: %s\n", vegetationFile); 157 } 158 159 160 void Terrain::loadTexture(const std::string& textureName) 161 { 162 PRINTF(4)("Load texture: %s\n", textureName.c_str()); 163 164 heightMapMaterial->setDiffuse(1.0,1.0,1.0); 165 heightMapMaterial->setAmbient(1.0,1.0,1.0 ); 166 heightMapMaterial->setSpecular(1.0,1.0,1.0); 167 heightMapMaterial->setShininess(.5); 168 heightMapMaterial->setTransparency(1.0); 169 170 heightMapMaterial->setDiffuseMap(textureName); 171 // heightMapMaterial->setAmbientMap(textureName); 172 // heightMapMaterial->setSpecularMap(textureName); 173 } 174 175 176 177 void Terrain::loadVegetation(const std::string& vegetationFile) 178 { 179 PRINTF(4)("loadVegetation: %s\n", vegetationFile.c_str()); 183 180 if (this->vegetation) 184 181 ResourceManager::getInstance()->unload(this->vegetation, RP_LEVEL); 185 if ( vegetationFile != NULL)186 { 187 PRINTF(4)("fetching %s\n", vegetationFile );182 if (!vegetationFile.empty()) 183 { 184 PRINTF(4)("fetching %s\n", vegetationFile.c_str()); 188 185 this->vegetation = dynamic_cast<Model*>(ResourceManager::getInstance()->load(vegetationFile, OBJ, RP_CAMPAIGN)); 189 186 } … … 205 202 this->getAbsCoor ().z); 206 203 /* rotate */ 207 // Vector tmpRot = this->getAbsDir().getSpacialAxis();204 // Vector tmpRot = this->getAbsDir().getSpacialAxis(); 208 205 //glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z ); 209 206 … … 227 224 228 225 229 /*230 glMatrixMode(GL_MODELVIEW);231 glPushMatrix();232 glLoadIdentity();233 Vector camera = State::getCameraNode()->getAbsCoor(); // Go on here ..........!!!234 235 float height = heightMap->getHeight(camera.x, camera.z);236 237 glEnable (GL_COLOR_MATERIAL) ;238 glBegin(GL_QUADS); // Draw The Cube Using quads239 glColor3f(0.0f,1.0f,0.0f); // Color Blue240 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)241 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)242 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)243 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)244 glEnd(); // End Drawing The Plan245 246 glPopMatrix();*/247 248 249 /* THIS IS ONLY FOR DEBUGGING INFORMATION */226 /* 227 glMatrixMode(GL_MODELVIEW); 228 glPushMatrix(); 229 glLoadIdentity(); 230 Vector camera = State::getCameraNode()->getAbsCoor(); // Go on here ..........!!! 231 232 float height = heightMap->getHeight(camera.x, camera.z); 233 234 glEnable (GL_COLOR_MATERIAL) ; 235 glBegin(GL_QUADS); // Draw The Cube Using quads 236 glColor3f(0.0f,1.0f,0.0f); // Color Blue 237 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) 238 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) 239 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) 240 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) 241 glEnd(); // End Drawing The Plan 242 243 glPopMatrix();*/ 244 245 246 /* THIS IS ONLY FOR DEBUGGING INFORMATION */ 250 247 if (this->ssp != NULL) 251 248 this->ssp->drawQuadtree(); … … 257 254 // if the terrain is the Terrain of Dave 258 255 if (debugTerrain == TERRAIN_DAVE) 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 256 { 257 objectList = glGenLists(1); 258 glNewList (objectList, GL_COMPILE); 259 260 glColor3f(1.0,0,0); 261 262 int sizeX = 100; 263 int sizeZ = 80; 264 float length = 1000; 265 float width = 200; 266 float widthX = float (length /sizeX); 267 float widthZ = float (width /sizeZ); 268 269 float height [sizeX][sizeZ]; 270 Vector normal_vectors[sizeX][sizeZ]; 271 272 273 for ( int i = 0; i<sizeX-1; i+=1) 274 for (int j = 0; j<sizeZ-1;j+=1) 275 //height[i][j] = rand()/20046 + (j-25)*(j-25)/30; 279 276 #ifdef __WIN32__ 280 277 height[i][j]=(sin((float)j/3)*rand()*i/182400)*.5; 281 278 #else 282 height[i][j]=(sin((float)j/3)*rand()*(long)i/6282450500.0)*.5;279 height[i][j]=(sin((float)j/3)*rand()*(long)i/6282450500.0)*.5; 283 280 #endif 284 281 285 //Die Huegel ein wenig glaetten 286 for (int h=1; h<2;h++) 287 for (int i=1;i<sizeX-2 ;i+=1 ) 288 for(int j=1;j<sizeZ-2;j+=1) 289 height[i][j]=(height[i+1][j]+height[i][j+1]+height[i-1][j]+height[i][j-1])/4; 290 291 //Berechnung von normalen Vektoren 292 for(int i=1;i<sizeX-2;i+=1) 293 for(int j=1;j<sizeZ-2 ;j+=1) 294 { 295 Vector v1 = Vector (widthX*(1), height[i][j], widthZ*(j) ); 296 Vector v2 = Vector (widthX*(i-1), height[i-1][j], widthZ*(j)); 297 Vector v3 = Vector (widthX*(i), height[i][j+1], widthZ*(j+1)); 298 Vector v4 = Vector (widthX*(i+1), height[i+1][j], widthZ*(j)); 299 Vector v5 = Vector (widthX*(i), height[i][j-1], widthZ*(j-1)); 300 301 Vector c1 = v2 - v1; 302 Vector c2 = v3 - v1; 303 Vector c3= v4 - v1; 304 Vector c4 = v5 - v1; 305 Vector zero = Vector (0,0,0); 306 normal_vectors[i][j]=c1.cross(v3-v5)+c2.cross(v4-v2)+c3.cross(v5-v3)+c4.cross(v2-v4); 307 normal_vectors[i][j].normalize(); 308 } 309 310 glBegin(GL_QUADS); 311 int snowheight=3; 312 for ( int i = 0; i<sizeX; i+=1) 313 for (int j = 0; j<sizeZ;j+=1) 314 { 315 Vector v1 = Vector (widthX*(i), height[i][j]-20, widthZ*(j) -width/2); 316 Vector v2 = Vector (widthX*(i+1), height[i+1][j]-20, widthZ*(j) -width/2); 317 Vector v3 = Vector (widthX*(i+1), height[i+1][j+1]-20, widthZ*(j+1)-width/2); 318 Vector v4 = Vector (widthX*(i), height[i][j+1]-20, widthZ*(j+1)-width/2); 319 float a[3]; 320 if(height[i][j]<snowheight){ 321 a[0]=0; 322 a[1]=1.0-height[i][j]/10-.3; 323 a[2]=0; 324 glMaterialfv(GL_FRONT,GL_DIFFUSE,a); 325 } 326 else{ 327 a[0]=1.0; 328 a[1]=1.0; 329 a[2]=1.0; 330 glMaterialfv(GL_FRONT,GL_DIFFUSE,a); 331 332 } 333 glNormal3f(normal_vectors[i][j].x, normal_vectors[i][j].y, normal_vectors[i][j].z); 334 glVertex3f(v1.x, v1.y, v1.z); 335 if(height[i+1][j]<snowheight){ 336 a[0]=0; 337 a[1] =1.0-height[i+1][j]/10-.3; 338 a[2]=0; 339 glMaterialfv(GL_FRONT,GL_DIFFUSE,a); 340 } 341 else{ 342 a[0]=1.0; 343 a[1]=1.0; 344 a[2]=1.0; 345 glMaterialfv(GL_FRONT,GL_DIFFUSE,a); 346 347 } 348 glNormal3f(normal_vectors[i+1][j].x, normal_vectors[i+1][j].y, normal_vectors[i+1][j].z); 349 glVertex3f(v2.x, v2.y, v2.z); 350 if(height[i+1][j+1]<snowheight){ 351 a[0]=0; 352 a[1] =1.0-height[i+1][j+1]/10-.3; 353 a[2]=0; 354 glMaterialfv(GL_FRONT,GL_DIFFUSE,a); 355 } 356 else{ 357 a[0]=1.0; 358 a[1]=1.0; 359 a[2]=1.0; 360 glMaterialfv(GL_FRONT,GL_DIFFUSE,a); 361 362 363 } 364 glNormal3f(normal_vectors[i+1][j+1].x, normal_vectors[i+1][j+1].y, normal_vectors[i+1][j+1].z); 365 glVertex3f(v3.x, v3.y, v3.z); 366 if(height[i][j+1]<snowheight){ 367 a[0]=0; 368 a[1] =1.0-height[i+1][j+1]/10-.3; 369 a[2]=0; 370 glMaterialfv(GL_FRONT,GL_DIFFUSE,a); 371 } 372 else{ 373 a[0]=1.0; 374 a[1]=1.0; 375 a[2]=1.0; 376 glMaterialfv(GL_FRONT,GL_DIFFUSE,a); 377 } 378 glNormal3f(normal_vectors[i][j+1].x, normal_vectors[i][j+1].y, normal_vectors[i][j+1].z); 379 glVertex3f(v4.x, v4.y, v4.z); 380 381 } 382 glEnd(); 383 glEndList(); 384 } 282 //Die Huegel ein wenig glaetten 283 for (int h=1; h<2;h++) 284 for (int i=1;i<sizeX-2 ;i+=1 ) 285 for(int j=1;j<sizeZ-2;j+=1) 286 height[i][j]=(height[i+1][j]+height[i][j+1]+height[i-1][j]+height[i][j-1])/4; 287 288 //Berechnung von normalen Vektoren 289 for(int i=1;i<sizeX-2;i+=1) 290 for(int j=1;j<sizeZ-2 ;j+=1) 291 { 292 Vector v1 = Vector (widthX*(1), height[i][j], widthZ*(j) ); 293 Vector v2 = Vector (widthX*(i-1), height[i-1][j], widthZ*(j)); 294 Vector v3 = Vector (widthX*(i), height[i][j+1], widthZ*(j+1)); 295 Vector v4 = Vector (widthX*(i+1), height[i+1][j], widthZ*(j)); 296 Vector v5 = Vector (widthX*(i), height[i][j-1], widthZ*(j-1)); 297 298 Vector c1 = v2 - v1; 299 Vector c2 = v3 - v1; 300 Vector c3= v4 - v1; 301 Vector c4 = v5 - v1; 302 Vector zero = Vector (0,0,0); 303 normal_vectors[i][j]=c1.cross(v3-v5)+c2.cross(v4-v2)+c3.cross(v5-v3)+c4.cross(v2-v4); 304 normal_vectors[i][j].normalize(); 305 } 306 307 glBegin(GL_QUADS); 308 int snowheight=3; 309 for ( int i = 0; i<sizeX; i+=1) 310 for (int j = 0; j<sizeZ;j+=1) 311 { 312 Vector v1 = Vector (widthX*(i), height[i][j]-20, widthZ*(j) -width/2); 313 Vector v2 = Vector (widthX*(i+1), height[i+1][j]-20, widthZ*(j) -width/2); 314 Vector v3 = Vector (widthX*(i+1), height[i+1][j+1]-20, widthZ*(j+1)-width/2); 315 Vector v4 = Vector (widthX*(i), height[i][j+1]-20, widthZ*(j+1)-width/2); 316 float a[3]; 317 if(height[i][j]<snowheight) 318 { 319 a[0]=0; 320 a[1]=1.0-height[i][j]/10-.3; 321 a[2]=0; 322 glMaterialfv(GL_FRONT,GL_DIFFUSE,a); 323 } 324 else 325 { 326 a[0]=1.0; 327 a[1]=1.0; 328 a[2]=1.0; 329 glMaterialfv(GL_FRONT,GL_DIFFUSE,a); 330 331 } 332 glNormal3f(normal_vectors[i][j].x, normal_vectors[i][j].y, normal_vectors[i][j].z); 333 glVertex3f(v1.x, v1.y, v1.z); 334 if(height[i+1][j]<snowheight) 335 { 336 a[0]=0; 337 a[1] =1.0-height[i+1][j]/10-.3; 338 a[2]=0; 339 glMaterialfv(GL_FRONT,GL_DIFFUSE,a); 340 } 341 else 342 { 343 a[0]=1.0; 344 a[1]=1.0; 345 a[2]=1.0; 346 glMaterialfv(GL_FRONT,GL_DIFFUSE,a); 347 348 } 349 glNormal3f(normal_vectors[i+1][j].x, normal_vectors[i+1][j].y, normal_vectors[i+1][j].z); 350 glVertex3f(v2.x, v2.y, v2.z); 351 if(height[i+1][j+1]<snowheight) 352 { 353 a[0]=0; 354 a[1] =1.0-height[i+1][j+1]/10-.3; 355 a[2]=0; 356 glMaterialfv(GL_FRONT,GL_DIFFUSE,a); 357 } 358 else 359 { 360 a[0]=1.0; 361 a[1]=1.0; 362 a[2]=1.0; 363 glMaterialfv(GL_FRONT,GL_DIFFUSE,a); 364 365 366 } 367 glNormal3f(normal_vectors[i+1][j+1].x, normal_vectors[i+1][j+1].y, normal_vectors[i+1][j+1].z); 368 glVertex3f(v3.x, v3.y, v3.z); 369 if(height[i][j+1]<snowheight) 370 { 371 a[0]=0; 372 a[1] =1.0-height[i+1][j+1]/10-.3; 373 a[2]=0; 374 glMaterialfv(GL_FRONT,GL_DIFFUSE,a); 375 } 376 else 377 { 378 a[0]=1.0; 379 a[1]=1.0; 380 a[2]=1.0; 381 glMaterialfv(GL_FRONT,GL_DIFFUSE,a); 382 } 383 glNormal3f(normal_vectors[i][j+1].x, normal_vectors[i][j+1].y, normal_vectors[i][j+1].z); 384 glVertex3f(v4.x, v4.y, v4.z); 385 386 } 387 glEnd(); 388 glEndList(); 389 } 385 390 386 391 if (debugTerrain == TERRAIN_BENSCH) 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 392 { 393 /* 394 this->model = (OBJModel*) new Model(); 395 this->model->setName("CUBE"); 396 this->model->addVertex (-0.5, -0.5, 0.5); 397 this->model->addVertex (0.5, -0.5, 0.5); 398 this->model->addVertex (-0.5, 0.5, 0.5); 399 this->model->addVertex (0.5, 0.5, 0.5); 400 this->model->addVertex (-0.5, 0.5, -0.5); 401 this->model->addVertex (0.5, 0.5, -0.5); 402 this->model->addVertex (-0.5, -0.5, -0.5); 403 this->model->addVertex (0.5, -0.5, -0.5); 404 405 this->model->addVertexTexture (0.0, 0.0); 406 this->model->addVertexTexture (1.0, 0.0); 407 this->model->addVertexTexture (0.0, 1.0); 408 this->model->addVertexTexture (1.0, 1.0); 409 this->model->addVertexTexture (0.0, 2.0); 410 this->model->addVertexTexture (1.0, 2.0); 411 this->model->addVertexTexture (0.0, 3.0); 412 this->model->addVertexTexture (1.0, 3.0); 413 this->model->addVertexTexture (0.0, 4.0); 414 this->model->addVertexTexture (1.0, 4.0); 415 this->model->addVertexTexture (2.0, 0.0); 416 this->model->addVertexTexture (2.0, 1.0); 417 this->model->addVertexTexture (-1.0, 0.0); 418 this->model->addVertexTexture (-1.0, 1.0); 419 420 this->model->finalize(); 421 */ 422 } 418 423 } 419 424 … … 453 458 454 459 void Terrain::writeDebug( ) const 455 { 456 } 460 {} 457 461 458 462 void Terrain::readDebug( ) const 459 { 460 } 463 {} 461 464 462 465 float Terrain::getHeight(float x, float y) 463 466 { 464 465 466 467 } 467 if(this->heightMap != NULL) 468 return (this->heightMap->getHeight(x, y)); 469 return 0; 470 }
Note: See TracChangeset
for help on using the changeset viewer.