[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 | */ |
---|
| 15 | |
---|
| 16 | |
---|
[3559] | 17 | #include "terrain.h" |
---|
[4607] | 18 | |
---|
[3677] | 19 | #include "model.h" |
---|
[3608] | 20 | #include "vector.h" |
---|
[3559] | 21 | #include "glincl.h" |
---|
[1853] | 22 | |
---|
[4607] | 23 | #include "factory.h" |
---|
| 24 | #include "load_param.h" |
---|
[5143] | 25 | #include "resource_manager.h" |
---|
[4842] | 26 | #include "spatial_separation.h" |
---|
| 27 | |
---|
[1856] | 28 | using namespace std; |
---|
[1853] | 29 | |
---|
[4607] | 30 | CREATE_FACTORY(Terrain); |
---|
[1856] | 31 | |
---|
[3245] | 32 | /** |
---|
[4836] | 33 | * standard constructor |
---|
[3559] | 34 | |
---|
[3245] | 35 | */ |
---|
[4607] | 36 | Terrain::Terrain (const TiXmlElement* root) |
---|
[3365] | 37 | { |
---|
[3564] | 38 | this->init(); |
---|
[4607] | 39 | this->loadParams(root); |
---|
[4844] | 40 | |
---|
[5308] | 41 | // if (this->model != NULL) |
---|
| 42 | //this->ssp = new SpatialSeparation((AbstractModel*)this->model, 10.0f); |
---|
[3365] | 43 | } |
---|
[1853] | 44 | |
---|
[4855] | 45 | |
---|
[3564] | 46 | /** |
---|
[4836] | 47 | * Constructor for loading a Terrain out of a file |
---|
| 48 | * @param fileName The file to load data from. |
---|
[4597] | 49 | |
---|
[3566] | 50 | this either loads out of an OBJ-file, or loads a heightmap if no .obj-extension is found. |
---|
| 51 | */ |
---|
[4597] | 52 | Terrain::Terrain(const char* fileName) |
---|
[3566] | 53 | { |
---|
| 54 | this->init(); |
---|
| 55 | |
---|
[5308] | 56 | if (!strcasestr(fileName, ".obj") ) |
---|
[3566] | 57 | { |
---|
[5308] | 58 | this->loadModel(fileName); |
---|
[3566] | 59 | } |
---|
| 60 | else |
---|
| 61 | { |
---|
| 62 | // load the hightMap here. |
---|
| 63 | } |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | /** |
---|
[4836] | 67 | * a Constructor for the Debug-Worlds |
---|
[5308] | 68 | */ |
---|
[3564] | 69 | Terrain::Terrain(DebugTerrain debugTerrain) |
---|
| 70 | { |
---|
| 71 | this->init(); |
---|
| 72 | this->buildDebugTerrain(debugTerrain); |
---|
| 73 | } |
---|
| 74 | |
---|
[3245] | 75 | /** |
---|
[4836] | 76 | * standard deconstructor |
---|
[1853] | 77 | |
---|
[3245] | 78 | */ |
---|
[4746] | 79 | Terrain::~Terrain () |
---|
[3543] | 80 | { |
---|
[3564] | 81 | if (objectList) |
---|
| 82 | glDeleteLists(this->objectList, 1); |
---|
[4889] | 83 | if( this->ssp) |
---|
| 84 | delete ssp; |
---|
[3543] | 85 | } |
---|
[1853] | 86 | |
---|
[3245] | 87 | |
---|
[4746] | 88 | void Terrain::init() |
---|
[3559] | 89 | { |
---|
[4320] | 90 | this->setClassID(CL_TERRAIN, "Terrain"); |
---|
[4597] | 91 | |
---|
[4136] | 92 | this->objectList = 0; |
---|
[5308] | 93 | this->ssp = NULL; |
---|
[3559] | 94 | } |
---|
| 95 | |
---|
[4924] | 96 | |
---|
[4607] | 97 | void Terrain::loadParams(const TiXmlElement* root) |
---|
| 98 | { |
---|
| 99 | static_cast<WorldEntity*>(this)->loadParams(root); |
---|
[3559] | 100 | |
---|
[4607] | 101 | //LoadParam<Terrain>(root, "DebugTerrain", ); |
---|
| 102 | } |
---|
| 103 | |
---|
[3559] | 104 | void Terrain::draw () |
---|
[4597] | 105 | { |
---|
[3559] | 106 | glMatrixMode(GL_MODELVIEW); |
---|
| 107 | glPushMatrix(); |
---|
[4597] | 108 | |
---|
[3559] | 109 | /* translate */ |
---|
[4597] | 110 | glTranslatef (this->getAbsCoor ().x, |
---|
| 111 | this->getAbsCoor ().y, |
---|
| 112 | this->getAbsCoor ().z); |
---|
[3559] | 113 | /* rotate */ |
---|
[4998] | 114 | Vector tmpRot = this->getAbsDir().getSpacialAxis(); |
---|
| 115 | glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z ); |
---|
[3559] | 116 | |
---|
[4136] | 117 | if (this->objectList) |
---|
| 118 | glCallList(this->objectList); |
---|
| 119 | else if (this->model) |
---|
| 120 | this->model->draw(); |
---|
[3559] | 121 | glPopMatrix(); |
---|
[4904] | 122 | |
---|
[4924] | 123 | /* THIS IS ONLY FOR DEBUGGING INFORMATION */ |
---|
[5308] | 124 | if (this->ssp != NULL) |
---|
| 125 | this->ssp->drawQuadtree(); |
---|
[3559] | 126 | } |
---|
[3564] | 127 | |
---|
| 128 | |
---|
| 129 | void Terrain::buildDebugTerrain(DebugTerrain debugTerrain) |
---|
| 130 | { |
---|
| 131 | // if the terrain is the Terrain of Dave |
---|
| 132 | if (debugTerrain == TERRAIN_DAVE) |
---|
| 133 | { |
---|
| 134 | objectList = glGenLists(1); |
---|
| 135 | glNewList (objectList, GL_COMPILE); |
---|
[4597] | 136 | |
---|
[3564] | 137 | glColor3f(1.0,0,0); |
---|
[4597] | 138 | |
---|
[3564] | 139 | int sizeX = 100; |
---|
| 140 | int sizeZ = 80; |
---|
| 141 | float length = 1000; |
---|
| 142 | float width = 200; |
---|
| 143 | float widthX = float (length /sizeX); |
---|
| 144 | float widthZ = float (width /sizeZ); |
---|
[4597] | 145 | |
---|
[3564] | 146 | float height [sizeX][sizeZ]; |
---|
| 147 | Vector normal_vectors[sizeX][sizeZ]; |
---|
[4597] | 148 | |
---|
| 149 | |
---|
[3564] | 150 | for ( int i = 0; i<sizeX-1; i+=1) |
---|
[4597] | 151 | for (int j = 0; j<sizeZ-1;j+=1) |
---|
| 152 | //height[i][j] = rand()/20046 + (j-25)*(j-25)/30; |
---|
[3564] | 153 | #ifdef __WIN32__ |
---|
[4597] | 154 | height[i][j]=(sin((float)j/3)*rand()*i/182400)*.5; |
---|
[3564] | 155 | #else |
---|
| 156 | height[i][j]=(sin((float)j/3)*rand()*(long)i/6282450500.0)*.5; |
---|
| 157 | #endif |
---|
[4597] | 158 | |
---|
[3564] | 159 | //Die Huegel ein wenig glaetten |
---|
| 160 | for (int h=1; h<2;h++) |
---|
[4597] | 161 | for (int i=1;i<sizeX-2 ;i+=1 ) |
---|
| 162 | for(int j=1;j<sizeZ-2;j+=1) |
---|
| 163 | height[i][j]=(height[i+1][j]+height[i][j+1]+height[i-1][j]+height[i][j-1])/4; |
---|
| 164 | |
---|
[3564] | 165 | //Berechnung von normalen Vektoren |
---|
| 166 | for(int i=1;i<sizeX-2;i+=1) |
---|
[4597] | 167 | for(int j=1;j<sizeZ-2 ;j+=1) |
---|
| 168 | { |
---|
| 169 | Vector v1 = Vector (widthX*(1), height[i][j], widthZ*(j) ); |
---|
| 170 | Vector v2 = Vector (widthX*(i-1), height[i-1][j], widthZ*(j)); |
---|
| 171 | Vector v3 = Vector (widthX*(i), height[i][j+1], widthZ*(j+1)); |
---|
| 172 | Vector v4 = Vector (widthX*(i+1), height[i+1][j], widthZ*(j)); |
---|
| 173 | Vector v5 = Vector (widthX*(i), height[i][j-1], widthZ*(j-1)); |
---|
| 174 | |
---|
| 175 | Vector c1 = v2 - v1; |
---|
| 176 | Vector c2 = v3 - v1; |
---|
| 177 | Vector c3= v4 - v1; |
---|
| 178 | Vector c4 = v5 - v1; |
---|
| 179 | Vector zero = Vector (0,0,0); |
---|
| 180 | normal_vectors[i][j]=c1.cross(v3-v5)+c2.cross(v4-v2)+c3.cross(v5-v3)+c4.cross(v2-v4); |
---|
| 181 | normal_vectors[i][j].normalize(); |
---|
| 182 | } |
---|
| 183 | |
---|
[3564] | 184 | glBegin(GL_QUADS); |
---|
| 185 | int snowheight=3; |
---|
| 186 | for ( int i = 0; i<sizeX; i+=1) |
---|
[4597] | 187 | for (int j = 0; j<sizeZ;j+=1) |
---|
| 188 | { |
---|
| 189 | Vector v1 = Vector (widthX*(i), height[i][j]-20, widthZ*(j) -width/2); |
---|
| 190 | Vector v2 = Vector (widthX*(i+1), height[i+1][j]-20, widthZ*(j) -width/2); |
---|
| 191 | Vector v3 = Vector (widthX*(i+1), height[i+1][j+1]-20, widthZ*(j+1)-width/2); |
---|
| 192 | Vector v4 = Vector (widthX*(i), height[i][j+1]-20, widthZ*(j+1)-width/2); |
---|
| 193 | float a[3]; |
---|
| 194 | if(height[i][j]<snowheight){ |
---|
| 195 | a[0]=0; |
---|
| 196 | a[1]=1.0-height[i][j]/10-.3; |
---|
| 197 | a[2]=0; |
---|
| 198 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
| 199 | } |
---|
| 200 | else{ |
---|
| 201 | a[0]=1.0; |
---|
| 202 | a[1]=1.0; |
---|
| 203 | a[2]=1.0; |
---|
| 204 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
| 205 | |
---|
| 206 | } |
---|
| 207 | glNormal3f(normal_vectors[i][j].x, normal_vectors[i][j].y, normal_vectors[i][j].z); |
---|
| 208 | glVertex3f(v1.x, v1.y, v1.z); |
---|
| 209 | if(height[i+1][j]<snowheight){ |
---|
| 210 | a[0]=0; |
---|
| 211 | a[1] =1.0-height[i+1][j]/10-.3; |
---|
| 212 | a[2]=0; |
---|
| 213 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
| 214 | } |
---|
| 215 | else{ |
---|
| 216 | a[0]=1.0; |
---|
| 217 | a[1]=1.0; |
---|
| 218 | a[2]=1.0; |
---|
| 219 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
| 220 | |
---|
| 221 | } |
---|
| 222 | glNormal3f(normal_vectors[i+1][j].x, normal_vectors[i+1][j].y, normal_vectors[i+1][j].z); |
---|
| 223 | glVertex3f(v2.x, v2.y, v2.z); |
---|
| 224 | if(height[i+1][j+1]<snowheight){ |
---|
| 225 | a[0]=0; |
---|
| 226 | a[1] =1.0-height[i+1][j+1]/10-.3; |
---|
| 227 | a[2]=0; |
---|
| 228 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
| 229 | } |
---|
| 230 | else{ |
---|
| 231 | a[0]=1.0; |
---|
| 232 | a[1]=1.0; |
---|
| 233 | a[2]=1.0; |
---|
| 234 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
| 235 | |
---|
| 236 | |
---|
| 237 | } |
---|
| 238 | glNormal3f(normal_vectors[i+1][j+1].x, normal_vectors[i+1][j+1].y, normal_vectors[i+1][j+1].z); |
---|
| 239 | glVertex3f(v3.x, v3.y, v3.z); |
---|
| 240 | if(height[i][j+1]<snowheight){ |
---|
| 241 | a[0]=0; |
---|
| 242 | a[1] =1.0-height[i+1][j+1]/10-.3; |
---|
| 243 | a[2]=0; |
---|
| 244 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
| 245 | } |
---|
| 246 | else{ |
---|
| 247 | a[0]=1.0; |
---|
| 248 | a[1]=1.0; |
---|
| 249 | a[2]=1.0; |
---|
| 250 | glMaterialfv(GL_FRONT,GL_DIFFUSE,a); |
---|
| 251 | } |
---|
| 252 | glNormal3f(normal_vectors[i][j+1].x, normal_vectors[i][j+1].y, normal_vectors[i][j+1].z); |
---|
| 253 | glVertex3f(v4.x, v4.y, v4.z); |
---|
| 254 | |
---|
| 255 | } |
---|
[3564] | 256 | glEnd(); |
---|
| 257 | glEndList(); |
---|
| 258 | } |
---|
| 259 | |
---|
| 260 | if (debugTerrain == TERRAIN_BENSCH) |
---|
| 261 | { |
---|
| 262 | /* |
---|
[4597] | 263 | this->model = (OBJModel*) new Model(); |
---|
[3564] | 264 | this->model->setName("CUBE"); |
---|
| 265 | this->model->addVertex (-0.5, -0.5, 0.5); |
---|
| 266 | this->model->addVertex (0.5, -0.5, 0.5); |
---|
| 267 | this->model->addVertex (-0.5, 0.5, 0.5); |
---|
| 268 | this->model->addVertex (0.5, 0.5, 0.5); |
---|
| 269 | this->model->addVertex (-0.5, 0.5, -0.5); |
---|
| 270 | this->model->addVertex (0.5, 0.5, -0.5); |
---|
| 271 | this->model->addVertex (-0.5, -0.5, -0.5); |
---|
| 272 | this->model->addVertex (0.5, -0.5, -0.5); |
---|
[4597] | 273 | |
---|
[3564] | 274 | this->model->addVertexTexture (0.0, 0.0); |
---|
| 275 | this->model->addVertexTexture (1.0, 0.0); |
---|
| 276 | this->model->addVertexTexture (0.0, 1.0); |
---|
| 277 | this->model->addVertexTexture (1.0, 1.0); |
---|
| 278 | this->model->addVertexTexture (0.0, 2.0); |
---|
| 279 | this->model->addVertexTexture (1.0, 2.0); |
---|
| 280 | this->model->addVertexTexture (0.0, 3.0); |
---|
| 281 | this->model->addVertexTexture (1.0, 3.0); |
---|
| 282 | this->model->addVertexTexture (0.0, 4.0); |
---|
| 283 | this->model->addVertexTexture (1.0, 4.0); |
---|
| 284 | this->model->addVertexTexture (2.0, 0.0); |
---|
| 285 | this->model->addVertexTexture (2.0, 1.0); |
---|
| 286 | this->model->addVertexTexture (-1.0, 0.0); |
---|
| 287 | this->model->addVertexTexture (-1.0, 1.0); |
---|
| 288 | |
---|
| 289 | this->model->finalize(); |
---|
| 290 | */ |
---|
| 291 | } |
---|
| 292 | } |
---|