/* orxonox - the future of 3D-vertical-scrollers Copyright (C) 2004 orx This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. ### File Specific: main-programmer: Benjamin Grauer co-programmer: ... */ #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY #include "terrain.h" #include "load_param.h" #include "factory.h" #include "spatial_separation.h" #include "resource_manager.h" #include "model.h" #include "network_game_manager.h" #include "height_map.h" #include "material.h" #include "glincl.h" #include "state.h" using namespace std; CREATE_FACTORY(Terrain, CL_TERRAIN); /** * standard constructor */ Terrain::Terrain (const TiXmlElement* root) { this->init(); this->loadParams(root); this->heightMapMaterial = new Material(); heightMapMaterial->setTransparency(1.0); heightMapMaterial->setIllum(0.2); heightMapMaterial->setDiffuse(1.0,1.0,1.0); heightMapMaterial->setAmbient(1.0,1.0,1.0 ); heightMapMaterial->setSpecular(1.0,1.0,1.0); heightMapMaterial->setShininess(.5); heightMapMaterial->setTransparency(1.0); heightMapMaterial->diffuseTexture = NULL; heightMapMaterial->ambientTexture = NULL; heightMapMaterial->specularTexture = NULL; const char* texture_name = "pictures/ground.tga"; heightMapMaterial->setDiffuseMap(texture_name); heightMapMaterial->setAmbientMap(texture_name); heightMapMaterial->setSpecularMap(texture_name); // if (this->model != NULL) //this->ssp = new SpatialSeparation((Model*)this->model, 10.0f); } /** * Constructor for loading a Terrain out of a file * @param fileName The file to load data from. this either loads out of an OBJ-file, or loads a heightmap if no .obj-extension is found. */ Terrain::Terrain(const char* fileName) { this->init(); if (!strstr(fileName, ".obj") || !strstr(fileName, ".OBJ") ) { this->loadModel(fileName); } else { // load the hightMap here. } } /** * a Constructor for the Debug-Worlds */ Terrain::Terrain(DebugTerrain debugTerrain) { this->init(); this->buildDebugTerrain(debugTerrain); } /** * standard deconstructor */ Terrain::~Terrain () { if (objectList) glDeleteLists(this->objectList, 1); if( this->ssp) delete ssp; if (this->vegetation) { ResourceManager::getInstance()->unload(this->vegetation); } } void Terrain::init() { this->setClassID(CL_TERRAIN, "Terrain"); this->toList(OM_ENVIRON_NOTICK); this->objectList = 0; this->ssp = NULL; this->vegetation = NULL; this->heightMap = NULL; this->heightMapMaterial = NULL; } void Terrain::loadParams(const TiXmlElement* root) { WorldEntity::loadParams(root); LoadParam(root, "vegetation", this, Terrain, loadVegetation) .describe("the fileName of the vegetation, that should be loaded onto this terrain. (must be relative to the data-dir)") ; LoadParam(root, "height-map", this, Terrain, loadHeightMap) .describe("The HeightMap, splitted into two strings seperated by ','. 1: HeighMap, 2: ColorMap"); LoadParam(root, "heigt-texture", this, Terrain, loadTexture) .describe("The name of the Texture for this heightMap"); } void Terrain::loadHeightMap(const char* heightMapFile, const char* colorMap) { if (this->heightMap != NULL) delete this->heightMap; this->heightMap = NULL; char* hmName = ResourceManager::getFullName(heightMapFile); char* hmColorName = ResourceManager::getFullName(colorMap); this->heightMap = new HeightMap(hmName, hmColorName); heightMap->scale(Vector(23.0f,1.1f,23.0f)); heightMap->setAbsCoor(this->getAbsCoor()); heightMap->load(); delete[] hmName; delete[] hmColorName; } void Terrain::loadTexture(const char* textureName) { if (this->heightMapMaterial != NULL) delete this->heightMapMaterial; this->heightMapMaterial = new Material(); heightMapMaterial->setTransparency(1.0); heightMapMaterial->setIllum(0.2); heightMapMaterial->setDiffuse(1.0,1.0,1.0); heightMapMaterial->setAmbient(1.0,1.0,1.0 ); heightMapMaterial->setSpecular(1.0,1.0,1.0); heightMapMaterial->setShininess(.5); heightMapMaterial->setTransparency(1.0); heightMapMaterial->diffuseTexture = NULL; heightMapMaterial->ambientTexture = NULL; heightMapMaterial->specularTexture = NULL; heightMapMaterial->setDiffuseMap(textureName); heightMapMaterial->setAmbientMap(textureName); heightMapMaterial->setSpecularMap(textureName); } void Terrain::loadVegetation(const char* vegetationFile) { PRINTF(0)("loadVegetation: %s\n", vegetationFile); if (this->vegetation) ResourceManager::getInstance()->unload(this->vegetation, RP_LEVEL); if (vegetationFile != NULL) { PRINTF(4)("fetching %s\n", vegetationFile); this->vegetation = (Model*)ResourceManager::getInstance()->load(vegetationFile, OBJ, RP_CAMPAIGN); } else this->vegetation = NULL; } void Terrain::draw () const { glMatrixMode(GL_MODELVIEW); glPushMatrix(); /* translate */ glTranslatef (this->getAbsCoor ().x, this->getAbsCoor ().y, this->getAbsCoor ().z); /* rotate */ // Vector tmpRot = this->getAbsDir().getSpacialAxis(); //glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z ); if (this->objectList) glCallList(this->objectList); else if (this->getModel()) this->getModel()->draw(); if (this->vegetation) this->vegetation->draw(); if(this->heightMap) { this->heightMapMaterial->select(); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glEnable(GL_LIGHTING); glColorMaterial ( GL_FRONT_AND_BACK, GL_EMISSION ) ; glEnable (GL_COLOR_MATERIAL) ; this->heightMap->draw(); } glPopMatrix(); glMatrixMode(GL_MODELVIEW); glPushMatrix(); glLoadIdentity(); Vector camera = State::getCamera()->getAbsCoor(); // Go on here ..........!!! float height = heightMap->getHeight(camera.x, camera.z); glEnable (GL_COLOR_MATERIAL) ; glBegin(GL_QUADS); // Draw The Cube Using quads glColor3f(0.0f,1.0f,0.0f); // Color Blue glVertex3f(camera.x + 13.0f,height+13.0f,camera.z-13.0f); // Top Right Of The Quad (Top) glVertex3f(camera.x-13.0f, height+13.0f,camera.z-13.0f); // Top Left Of The Quad (Top) glVertex3f(camera.x-13.0f, height+13.0f, camera.z+13.0f); // Bottom Left Of The Quad (Top) glVertex3f(camera.x+ 13.0f, height+13.0f, camera.z+13.0f); // Bottom Right Of The Quad (Top) glEnd(); // End Drawing The Plan glPopMatrix(); /* THIS IS ONLY FOR DEBUGGING INFORMATION */ if (this->ssp != NULL) this->ssp->drawQuadtree(); } void Terrain::buildDebugTerrain(DebugTerrain debugTerrain) { // if the terrain is the Terrain of Dave if (debugTerrain == TERRAIN_DAVE) { objectList = glGenLists(1); glNewList (objectList, GL_COMPILE); glColor3f(1.0,0,0); int sizeX = 100; int sizeZ = 80; float length = 1000; float width = 200; float widthX = float (length /sizeX); float widthZ = float (width /sizeZ); float height [sizeX][sizeZ]; Vector normal_vectors[sizeX][sizeZ]; for ( int i = 0; imodel = (OBJModel*) new Model(); this->model->setName("CUBE"); this->model->addVertex (-0.5, -0.5, 0.5); this->model->addVertex (0.5, -0.5, 0.5); this->model->addVertex (-0.5, 0.5, 0.5); this->model->addVertex (0.5, 0.5, 0.5); this->model->addVertex (-0.5, 0.5, -0.5); this->model->addVertex (0.5, 0.5, -0.5); this->model->addVertex (-0.5, -0.5, -0.5); this->model->addVertex (0.5, -0.5, -0.5); this->model->addVertexTexture (0.0, 0.0); this->model->addVertexTexture (1.0, 0.0); this->model->addVertexTexture (0.0, 1.0); this->model->addVertexTexture (1.0, 1.0); this->model->addVertexTexture (0.0, 2.0); this->model->addVertexTexture (1.0, 2.0); this->model->addVertexTexture (0.0, 3.0); this->model->addVertexTexture (1.0, 3.0); this->model->addVertexTexture (0.0, 4.0); this->model->addVertexTexture (1.0, 4.0); this->model->addVertexTexture (2.0, 0.0); this->model->addVertexTexture (2.0, 1.0); this->model->addVertexTexture (-1.0, 0.0); this->model->addVertexTexture (-1.0, 1.0); this->model->finalize(); */ } } int Terrain::writeBytes( const byte * data, int length, int sender ) { setRequestedSync( false ); setIsOutOfSync( false ); SYNCHELP_READ_BEGIN(); SYNCHELP_READ_FKT( WorldEntity::writeState ); return SYNCHELP_READ_N; } int Terrain::readBytes( byte * data, int maxLength, int * reciever ) { if ( isOutOfSync() && !requestedSync() && this->getHostID()!=this->getOwner() ) { (NetworkGameManager::getInstance())->sync( this->getUniqueID(), this->getOwner() ); setRequestedSync( true ); } int rec = this->getRequestSync(); if ( rec > 0 ) { *reciever = rec; return WorldEntity::readState( data, maxLength ); } *reciever = 0; return 0; } void Terrain::writeDebug( ) const { } void Terrain::readDebug( ) const { } float Terrain::getHeight(float x, float y) { if(this->heightMap != NULL) return (this->heightMap->getHeight(x, y)); return 0; }