/* 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 "material.h" #include "height_map.h" #include "glincl.h" using namespace std; HeightMap * hm; CREATE_FACTORY(Terrain, CL_TERRAIN); /** * standard constructor */ Terrain::Terrain (const TiXmlElement* root) { this->init(); this->tmp_mat = new Material(); tmp_mat->setTransparency(1.0); tmp_mat->setIllum(0.3); /* tmp_mat->setDiffuse(0.55,0.4,0.2); tmp_mat->setAmbient(0.6,0.4,0.2); tmp_mat->setSpecular(0.02,0.02,0.02);*/ tmp_mat->setDiffuse(1.0,1.0,1.0); tmp_mat->setAmbient(1.0,1.0,1.0 ); tmp_mat->setSpecular(1.0,1.0,1.0); tmp_mat->setShininess(.1); tmp_mat->setTransparency(1.0); tmp_mat->diffuseTexture = NULL; tmp_mat->ambientTexture = NULL; // tmp_mat->specularTexture = NULL; // this->loadParams(root); char* heightmapName = ResourceManager::getFullName("pictures/heightmapHello.bmp"); char* colourmapName = ResourceManager::getFullName("pictures/heightmapHelloCM.bmp"); hm = new HeightMap(heightmapName, colourmapName); hm->scale(Vector(23.0f,1.7f,23.0f)); hm->load(); this->model=hm; /* this->model = new HeightMap(); this->model->setName("HardCore"); 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->addVertexNormal (0.0, 0.0, 1.0); this->model->addVertexNormal (0.0, 0.0, 1.0); this->model->addVertexNormal (0.0, 0.0, 1.0); this->model->addVertexNormal (0.0, 0.0, 1.0); this->model->addVertexNormal (0.0, 1.0, 0.0); this->model->addVertexNormal (0.0, 1.0, 0.0); this->model->addVertexNormal (0.0, 1.0, 0.0); this->model->addVertexNormal (0.0, 1.0, 0.0); this->model->addVertexNormal (0.0, 0.0, -1.0); this->model->addVertexNormal (0.0, 0.0, -1.0); this->model->addVertexNormal (0.0, 0.0, -1.0); this->model->addVertexNormal (0.0, 0.0, -1.0); this->model->addVertexNormal (0.0, -1.0, 0.0); this->model->addVertexNormal (0.0, -1.0, 0.0); this->model->addVertexNormal (0.0, -1.0, 0.0); this->model->addVertexNormal (0.0, -1.0, 0.0); this->model->addVertexNormal (1.0, 0.0, 0.0); this->model->addVertexNormal (1.0, 0.0, 0.0); this->model->addVertexNormal (1.0, 0.0, 0.0); this->model->addVertexNormal (1.0, 0.0, 0.0); this->model->addVertexNormal (-1.0, 0.0, 0.0); this->model->addVertexNormal (-1.0, 0.0, 0.0); this->model->addVertexNormal (-1.0, 0.0, 0.0); this->model->addVertexNormal (-1.0, 0.0, 0.0); this->model->addFace (4, VERTEX_TEXCOORD_NORMAL, 0,0,0, 1,1,1, 3,3,2, 2,2,3); this->model->addFace (4, VERTEX_TEXCOORD_NORMAL, 2,2,4, 3,3,5, 5,5,6, 4,4,7); this->model->addFace (4, VERTEX_TEXCOORD_NORMAL, 4,4,8, 5,5,9, 7,7,10, 6,6,11); this->model->addFace (4, VERTEX_TEXCOORD_NORMAL, 6,6,12, 7,7,13, 1,9,14, 0,8,15); this->model->addFace (4, VERTEX_TEXCOORD_NORMAL, 1,1,16, 7,10,17, 5,11,18, 3,3,19); this->model->addFace (4, VERTEX_TEXCOORD_NORMAL, 6,12,20, 0,0,21, 2,2,22, 4,13,23); this->model->finalize(); */ /*if (this->model != NULL) this->ssp = new SpatialSeparation((AbstractModel*)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->model = NULL; //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->objectList = 0; this->ssp = NULL; this->vegetation = NULL; } void Terrain::loadParams(const TiXmlElement* root) { static_cast(this)->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)") ; } void Terrain::loadVegetation(const char* 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 - 1000, this->getAbsCoor ().y - 400, this->getAbsCoor ().z - 1000) ; /* rotate */ Vector tmpRot = this->getAbsDir().getSpacialAxis(); glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z ); //this->tmp_mat->select(); hm->draw(); /* if (this->objectList) glCallList(this->objectList); else if (this->model) this->model->draw(); if (this->vegetation) this->vegetation->draw(); */ 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(); */ } }