| 1 | /* |
|---|
| 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. |
|---|
| 10 | |
|---|
| 11 | ### File Specific: |
|---|
| 12 | main-programmer: bottac@ee.ethz.ch |
|---|
| 13 | |
|---|
| 14 | review: patrick boenzli, patrick@orxonox.ethz.ch |
|---|
| 15 | */ |
|---|
| 16 | |
|---|
| 17 | #include "height_map.h" |
|---|
| 18 | #include "model.h" |
|---|
| 19 | #include "texture.h" |
|---|
| 20 | #include "vector.h" |
|---|
| 21 | #include "material.h" |
|---|
| 22 | #include "p_node.h" |
|---|
| 23 | #include "state.h" |
|---|
| 24 | #include "debug.h" |
|---|
| 25 | |
|---|
| 26 | // INCLUDING SDL_Image |
|---|
| 27 | #ifdef HAVE_SDL_IMAGE_H |
|---|
| 28 | #include <SDL_image.h> |
|---|
| 29 | #else |
|---|
| 30 | #include <SDL/SDL_image.h> |
|---|
| 31 | #endif |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | /** |
|---|
| 35 | * default constructor |
|---|
| 36 | * @param xOffset |
|---|
| 37 | */ |
|---|
| 38 | Tile::Tile(int xOffset, int yOffset, int i2, int j2, HeightMap* heightMapReference ) |
|---|
| 39 | { |
|---|
| 40 | PRINTF(0)("Tile Constructor\n"); |
|---|
| 41 | |
|---|
| 42 | this->highResModel = new VertexArrayModel(); |
|---|
| 43 | this->lowResModel = new VertexArrayModel(); |
|---|
| 44 | |
|---|
| 45 | this->heightMapReference = heightMapReference; |
|---|
| 46 | |
|---|
| 47 | // create high res model |
|---|
| 48 | this->load(xOffset, yOffset, i2, j2, this->highResModel, 4); |
|---|
| 49 | // create low res model |
|---|
| 50 | this->load(xOffset, yOffset, i2, j2, this->lowResModel, 8); |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | Tile::~Tile() |
|---|
| 55 | { |
|---|
| 56 | if( highResModel) |
|---|
| 57 | delete highResModel; |
|---|
| 58 | if( lowResModel) |
|---|
| 59 | delete lowResModel; |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | /** |
|---|
| 64 | * this darws the tile in diefferent resolutions |
|---|
| 65 | */ |
|---|
| 66 | void Tile::draw() |
|---|
| 67 | { |
|---|
| 68 | // draw the tile depending on the distance from the camera with different LOD (level of details) |
|---|
| 69 | float cameraDistance = fabs((State::getCameraNode()->getAbsCoor() - Vector(this->x, heightMapReference->offsetY , this->z) ).len()); |
|---|
| 70 | |
|---|
| 71 | if (cameraDistance < HM_LOD_HIGH_RES ) |
|---|
| 72 | { |
|---|
| 73 | this->drawHighRes(); |
|---|
| 74 | } |
|---|
| 75 | else if( cameraDistance < HM_LOD_LOW_RES) |
|---|
| 76 | { |
|---|
| 77 | this->drawLowRes(); |
|---|
| 78 | } |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | /** |
|---|
| 83 | * loads a tile |
|---|
| 84 | */ |
|---|
| 85 | void Tile::load(int xOffset, int yOffset, int i2, int j2, VertexArrayModel* model, int sampleRate) |
|---|
| 86 | { |
|---|
| 87 | |
|---|
| 88 | // #define heightMap this->heightMapReference->heightMap |
|---|
| 89 | #define colors this->heightMapReference->colors |
|---|
| 90 | #define scaleX this->heightMapReference->scaleX |
|---|
| 91 | #define scaleY this->heightMapReference->scaleY |
|---|
| 92 | #define scaleZ this->heightMapReference->scaleZ |
|---|
| 93 | #define shiftX this->heightMapReference->shiftX |
|---|
| 94 | #define shiftY this->heightMapReference->shiftY |
|---|
| 95 | #define shiftZ this->heightMapReference->shiftZ |
|---|
| 96 | #define normalVectorField this->heightMapReference->normalVectorField |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | //FIXME: OLD implementation |
|---|
| 100 | this->x = this->heightMapReference->offsetX + ( this->heightMapReference->heightMap->h - ((xOffset + i2) / 2)) * scaleX; |
|---|
| 101 | this->z = this->heightMapReference->offsetZ + ((yOffset + j2 ) / 2 ) * scaleZ; |
|---|
| 102 | //NEW: |
|---|
| 103 | this->setAbsCoor(this->heightMapReference->offsetX + ( this->heightMapReference->heightMap->h - ((xOffset + i2) / 2)) * scaleX, |
|---|
| 104 | 0, |
|---|
| 105 | this->heightMapReference->offsetZ + ((yOffset + j2 ) / 2 ) * scaleZ); |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | float height = 0; |
|---|
| 109 | |
|---|
| 110 | float r = 0.0; |
|---|
| 111 | float g = 0.0; |
|---|
| 112 | float b = 0.0; |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | //if( this->heightMapReference->heightMap != NULL && this->heightMapReference->heightMap->format->BitsPerPixel == 8 ) |
|---|
| 116 | |
|---|
| 117 | SDL_LockSurface(this->heightMapReference->heightMap); |
|---|
| 118 | SDL_LockSurface(this->heightMapReference->colorMap); |
|---|
| 119 | |
|---|
| 120 | for( int i = xOffset ; i <= i2 ; i +=sampleRate) |
|---|
| 121 | { |
|---|
| 122 | int w = 0; |
|---|
| 123 | |
|---|
| 124 | // adjust the colors acoring to the color map |
|---|
| 125 | if( this->heightMapReference->hasColourMap) |
|---|
| 126 | { |
|---|
| 127 | r = colors[3 * w + 2 + 3 * i * this->heightMapReference->heightMap->w]; |
|---|
| 128 | g = colors[3 * w + 1 + 3 * i * this->heightMapReference->heightMap->w]; |
|---|
| 129 | b = colors[3 * w + 0 + 3 * i * this->heightMapReference->heightMap->w]; |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | w = yOffset; |
|---|
| 133 | |
|---|
| 134 | // PRINTF(0)("Values: i = %i, w = %i\n", i, w); |
|---|
| 135 | |
|---|
| 136 | // add a vertex to the list |
|---|
| 137 | model->addVertex(scaleX * (this->heightMapReference->heightMap->h - i) + shiftX, shiftY, scaleZ * w + shiftZ); // Top Right |
|---|
| 138 | model->addNormal(normalVectorField[i % this->heightMapReference->heightMap->h][w % this->heightMapReference->heightMap->w].y, |
|---|
| 139 | normalVectorField[i % this->heightMapReference->heightMap->h][w % this->heightMapReference->heightMap->w].z, |
|---|
| 140 | normalVectorField[i % this->heightMapReference->heightMap->h][w % this->heightMapReference->heightMap->w].x); |
|---|
| 141 | model->addTexCoor((float)(yOffset-sampleRate) /(HM_TEX_RATE), (float)(i %this->heightMapReference->heightMap->h)/(HM_TEX_RATE)); |
|---|
| 142 | model->addColor(r/255.0f, g/255.0f, b/255.0f); |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | for(int j = yOffset; j <= j2; j += sampleRate) |
|---|
| 146 | { |
|---|
| 147 | // adjust the colors acording to the color map |
|---|
| 148 | if( this->heightMapReference->hasColourMap) |
|---|
| 149 | { |
|---|
| 150 | r = colors[3 * j + 2 + 3 * i * this->heightMapReference->heightMap->w]; |
|---|
| 151 | g = colors[3 * j + 1 + 3 * i * this->heightMapReference->heightMap->w]; |
|---|
| 152 | b = colors[3 * j + 0 + 3 * i * this->heightMapReference->heightMap->w]; |
|---|
| 153 | } |
|---|
| 154 | height = (float)(unsigned char)this->heightMapReference->heights[j + sampleRate + i * (this->heightMapReference->heightMap->w)]; |
|---|
| 155 | height += (float)(unsigned char)this->heightMapReference->heights[j + 1 + sampleRate + (i + 1) * |
|---|
| 156 | this->heightMapReference->heightMap->w]; |
|---|
| 157 | height += (float)(unsigned char)this->heightMapReference->heights[j - 1 + sampleRate + (i + 1) * |
|---|
| 158 | this->heightMapReference->heightMap->w]; |
|---|
| 159 | height += (float)(unsigned char)this->heightMapReference->heights[j + sampleRate + (i + 2) * |
|---|
| 160 | this->heightMapReference->heightMap->w]; |
|---|
| 161 | height += (float)(unsigned char)this->heightMapReference->heights[j + sampleRate + i * (this->heightMapReference->heightMap->w)]; |
|---|
| 162 | height /= 5.0; |
|---|
| 163 | |
|---|
| 164 | model->addVertex(scaleX * (this->heightMapReference->heightMap->h -i) + shiftX , |
|---|
| 165 | ((double)(height)*scaleY) + shiftY , |
|---|
| 166 | scaleZ*(j) + shiftZ); // Top Right |
|---|
| 167 | model->addNormal(normalVectorField[i % this->heightMapReference->heightMap->h][j % this->heightMapReference->heightMap->w].y, |
|---|
| 168 | normalVectorField[i % this->heightMapReference->heightMap->h][j % this->heightMapReference->heightMap->w].z, |
|---|
| 169 | normalVectorField[i % this->heightMapReference->heightMap->h][j % this->heightMapReference->heightMap->w].x); |
|---|
| 170 | model->addTexCoor((float)(j) /(HM_TEX_RATE), (float)(i %this->heightMapReference->heightMap->h)/(HM_TEX_RATE)); |
|---|
| 171 | model->addColor(r/255.0f, g/255.0f, b/255.0f); |
|---|
| 172 | //PRINTF(0)("TexCoord: %f %f \n",(float)j / 100.0, (float)(i %this->heightMapReference->h)/100.0); |
|---|
| 173 | |
|---|
| 174 | w = j; |
|---|
| 175 | } |
|---|
| 176 | model->addVertex(scaleX*(this->heightMapReference->heightMap->h -i)+ shiftX,shiftY,scaleZ*(w)+ shiftZ); // Top Right |
|---|
| 177 | model->addNormal(normalVectorField[i % this->heightMapReference->heightMap->h][w % this->heightMapReference->heightMap->w].y, |
|---|
| 178 | normalVectorField[i % this->heightMapReference->heightMap->h][w % this->heightMapReference->heightMap->w].z, |
|---|
| 179 | normalVectorField[i% this->heightMapReference->heightMap->h][w % this->heightMapReference->heightMap->w].x); |
|---|
| 180 | model->addTexCoor((float)(j2+sampleRate) /(HM_TEX_RATE), (float)(i %this->heightMapReference->heightMap->h)/(HM_TEX_RATE)); |
|---|
| 181 | model->addColor(r/255.0f, g/255.0f, b/255.0f); |
|---|
| 182 | } |
|---|
| 183 | |
|---|
| 184 | SDL_UnlockSurface(this->heightMapReference->heightMap); |
|---|
| 185 | int cnt = 0; |
|---|
| 186 | |
|---|
| 187 | for( int i = xOffset; i < i2; i +=sampleRate) |
|---|
| 188 | { |
|---|
| 189 | for( int j = yOffset-sampleRate; j < j2 + 2 * sampleRate; j += sampleRate) |
|---|
| 190 | { |
|---|
| 191 | model->addIndice(cnt); |
|---|
| 192 | model->addIndice(cnt + (j2 -yOffset + 3* sampleRate )/ sampleRate ); |
|---|
| 193 | cnt++; |
|---|
| 194 | } |
|---|
| 195 | model->newStripe(); |
|---|
| 196 | } |
|---|
| 197 | cnt += (j2 -yOffset + 3* sampleRate)/ sampleRate; |
|---|
| 198 | |
|---|
| 199 | for( int j = yOffset; j <= j2; j += sampleRate) |
|---|
| 200 | { |
|---|
| 201 | int i = xOffset; |
|---|
| 202 | |
|---|
| 203 | // To be fixed |
|---|
| 204 | if(this->heightMapReference->hasColourMap) |
|---|
| 205 | { |
|---|
| 206 | r = (float)colors[3 * j + 2 + 3 * i * this->heightMapReference->heightMap->w]; |
|---|
| 207 | g = (float)colors[3 * j + 1 + 3 * i * this->heightMapReference->heightMap->w]; |
|---|
| 208 | b = (float)colors[3 * j + 0 + 3 * i * this->heightMapReference->heightMap->w]; |
|---|
| 209 | } |
|---|
| 210 | |
|---|
| 211 | model->addVertex(scaleX*(this->heightMapReference->heightMap->h -i) + shiftX , |
|---|
| 212 | shiftY , |
|---|
| 213 | scaleZ*(j) + shiftZ); // Top Right |
|---|
| 214 | model->addNormal(normalVectorField[i % this->heightMapReference->heightMap->h][j % this->heightMapReference->heightMap->w].y, |
|---|
| 215 | normalVectorField[i % this->heightMapReference->heightMap->h][j % this->heightMapReference->heightMap->w].z, |
|---|
| 216 | normalVectorField[i % this->heightMapReference->heightMap->h][j % this->heightMapReference->heightMap->w].x); |
|---|
| 217 | model->addTexCoor((float)j /(HM_TEX_RATE), (float)((i - sampleRate) %this->heightMapReference->heightMap->h)/(HM_TEX_RATE)); |
|---|
| 218 | model->addColor(r/255.0f, g/255.0f, b/255.0f); |
|---|
| 219 | } |
|---|
| 220 | |
|---|
| 221 | |
|---|
| 222 | for(int j = yOffset ; j <= j2 ; j += sampleRate) |
|---|
| 223 | { |
|---|
| 224 | int i = xOffset; |
|---|
| 225 | height = (float)(unsigned char)this->heightMapReference->heights[j + sampleRate + i * this->heightMapReference->heightMap->w]; |
|---|
| 226 | height += (float)(unsigned char)this->heightMapReference->heights[j + 1 + sampleRate + (i + 1) * |
|---|
| 227 | this->heightMapReference->heightMap->w]; |
|---|
| 228 | height += (float)(unsigned char)this->heightMapReference->heights[j - 1 + sampleRate + (i + 1) * |
|---|
| 229 | this->heightMapReference->heightMap->w]; |
|---|
| 230 | height += (float)(unsigned char)this->heightMapReference->heights[j + sampleRate + (i + 2) * |
|---|
| 231 | this->heightMapReference->heightMap->w]; |
|---|
| 232 | height += (float)(unsigned char)this->heightMapReference->heights[j + sampleRate + i * this->heightMapReference->heightMap->w]; |
|---|
| 233 | height=height/5.0; |
|---|
| 234 | |
|---|
| 235 | model->addVertex(scaleX*(this->heightMapReference->heightMap->h -i) + shiftX , |
|---|
| 236 | ((double)(height)*scaleY) +shiftY , |
|---|
| 237 | scaleZ*(j) + shiftZ); // Top Right |
|---|
| 238 | model->addNormal(normalVectorField[i % this->heightMapReference->heightMap->h][j % this->heightMapReference->heightMap->w].y, |
|---|
| 239 | normalVectorField[i % this->heightMapReference->heightMap->h][j% this->heightMapReference->heightMap->w].z, |
|---|
| 240 | normalVectorField[i%this->heightMapReference->heightMap->h][j%this->heightMapReference->heightMap->w].x); |
|---|
| 241 | model->addTexCoor((float)j /(HM_TEX_RATE), (float)(i %this->heightMapReference->heightMap->h)/(HM_TEX_RATE)); |
|---|
| 242 | model->addColor(r/255.0f, g/255.0f, b/255.0f); |
|---|
| 243 | } |
|---|
| 244 | |
|---|
| 245 | |
|---|
| 246 | for(int j = yOffset; j <= j2; j += sampleRate) |
|---|
| 247 | { |
|---|
| 248 | int i = i2; |
|---|
| 249 | // To be fixed |
|---|
| 250 | if( this->heightMapReference->hasColourMap) |
|---|
| 251 | { |
|---|
| 252 | r = (float)colors[3 * j + 2 + 3 * i * this->heightMapReference->heightMap->w]; |
|---|
| 253 | g = (float)colors[3 * j + 1 + 3 * i * this->heightMapReference->heightMap->w]; |
|---|
| 254 | b = (float)colors[3 * j + 0 + 3 * i * this->heightMapReference->heightMap->w]; |
|---|
| 255 | } |
|---|
| 256 | |
|---|
| 257 | model->addVertex(scaleX*(this->heightMapReference->heightMap->h -i) + shiftX , |
|---|
| 258 | shiftY , |
|---|
| 259 | scaleZ*(j) + shiftZ); // Top Right |
|---|
| 260 | model->addNormal(normalVectorField[i%this->heightMapReference->heightMap->h][j%this->heightMapReference->heightMap->w].y, |
|---|
| 261 | normalVectorField[i%this->heightMapReference->heightMap->h][j%this->heightMapReference->heightMap->w].z, |
|---|
| 262 | normalVectorField[i%this->heightMapReference->heightMap->h][j%this->heightMapReference->heightMap->w].x); |
|---|
| 263 | model->addTexCoor((float)j /(HM_TEX_RATE), (float)((i+ sampleRate) %this->heightMapReference->heightMap->h)/(HM_TEX_RATE)); |
|---|
| 264 | model->addColor(r/255.0f, g/255.0f, b/255.0f); |
|---|
| 265 | } |
|---|
| 266 | |
|---|
| 267 | |
|---|
| 268 | for(int j = yOffset; j <= j2; j += sampleRate) |
|---|
| 269 | { |
|---|
| 270 | int i = i2; |
|---|
| 271 | height = (float)(unsigned char)this->heightMapReference->heights[j + sampleRate + i * this->heightMapReference->heightMap->w]; |
|---|
| 272 | height += (float)(unsigned char)this->heightMapReference->heights[j + 1 + sampleRate + (i + 1) |
|---|
| 273 | * this->heightMapReference->heightMap->w]; |
|---|
| 274 | height += (float)(unsigned char)this->heightMapReference->heights[j - 1 + sampleRate + (i + 1) * |
|---|
| 275 | this->heightMapReference->heightMap->w]; |
|---|
| 276 | height += (float)(unsigned char)this->heightMapReference->heights[j + sampleRate + (i + 2) * |
|---|
| 277 | this->heightMapReference->heightMap->w]; |
|---|
| 278 | height += (float)(unsigned char)this->heightMapReference->heights[j + sampleRate + i * this->heightMapReference->heightMap->w]; |
|---|
| 279 | height /= 5.0; |
|---|
| 280 | model->addVertex(scaleX*(this->heightMapReference->heightMap->h -i) + shiftX , |
|---|
| 281 | ((double)(height)*scaleY) +shiftY , |
|---|
| 282 | scaleZ*(j) + shiftZ); // Top Right |
|---|
| 283 | model->addNormal(normalVectorField[i%this->heightMapReference->heightMap->h][j%this->heightMapReference->heightMap->w].y, |
|---|
| 284 | normalVectorField[i%this->heightMapReference->heightMap->h][j%this->heightMapReference->heightMap->w].z, |
|---|
| 285 | normalVectorField[i%this->heightMapReference->heightMap->h][j%this->heightMapReference->heightMap->w].x); |
|---|
| 286 | model->addTexCoor((float)j /(HM_TEX_RATE), (float)(i %this->heightMapReference->heightMap->h)/(HM_TEX_RATE)); |
|---|
| 287 | model->addColor(r/255.0f, g/255.0f, b/255.0f); |
|---|
| 288 | } |
|---|
| 289 | |
|---|
| 290 | // link Boarder Stripe |
|---|
| 291 | for(int j = yOffset - sampleRate; j < j2; j += sampleRate) |
|---|
| 292 | { |
|---|
| 293 | model->addIndice(cnt); |
|---|
| 294 | model->addIndice(cnt + (j2 - yOffset + sampleRate) / sampleRate ); |
|---|
| 295 | cnt++; |
|---|
| 296 | } |
|---|
| 297 | cnt++; |
|---|
| 298 | |
|---|
| 299 | |
|---|
| 300 | model->newStripe(); |
|---|
| 301 | |
|---|
| 302 | |
|---|
| 303 | cnt += (j2-yOffset)/ sampleRate; |
|---|
| 304 | // link 2nd BoarderStripe |
|---|
| 305 | for(int j = yOffset-sampleRate; j < j2; j += sampleRate) |
|---|
| 306 | { |
|---|
| 307 | model->addIndice(cnt); |
|---|
| 308 | model->addIndice(cnt + (j2 - yOffset + sampleRate) / sampleRate); |
|---|
| 309 | cnt++; |
|---|
| 310 | } |
|---|
| 311 | |
|---|
| 312 | SDL_UnlockSurface(this->heightMapReference->colorMap); |
|---|
| 313 | |
|---|
| 314 | model->finalize(); |
|---|
| 315 | |
|---|
| 316 | // #undef heightMap |
|---|
| 317 | #undef colors |
|---|
| 318 | #undef scaleX |
|---|
| 319 | #undef scaleY |
|---|
| 320 | #undef scaleZ |
|---|
| 321 | #undef shiftX |
|---|
| 322 | #undef shiftY |
|---|
| 323 | #undef shiftZ |
|---|
| 324 | #undef normalVectorField |
|---|
| 325 | } |
|---|
| 326 | |
|---|
| 327 | |
|---|
| 328 | ObjectListDefinition(HeightMap); |
|---|
| 329 | |
|---|
| 330 | |
|---|
| 331 | /** |
|---|
| 332 | * constructor |
|---|
| 333 | * @param heightMapName file name of the height map |
|---|
| 334 | */ |
|---|
| 335 | HeightMap::HeightMap(const std::string& heightMapName) |
|---|
| 336 | : VertexArrayModel() |
|---|
| 337 | { |
|---|
| 338 | this->init(heightMapName); |
|---|
| 339 | |
|---|
| 340 | this->colorMap = NULL; |
|---|
| 341 | } |
|---|
| 342 | |
|---|
| 343 | |
|---|
| 344 | /** |
|---|
| 345 | * constructor |
|---|
| 346 | * @param heightMapName file name of the height map |
|---|
| 347 | * @param colorMapName file name of the color map |
|---|
| 348 | */ |
|---|
| 349 | HeightMap::HeightMap(const std::string& heightMapName, const std::string& colorMapName) |
|---|
| 350 | : VertexArrayModel() |
|---|
| 351 | { |
|---|
| 352 | this->init(heightMapName); |
|---|
| 353 | |
|---|
| 354 | this->colorMap = IMG_Load(colorMapName.c_str()); |
|---|
| 355 | if( this->colorMap != NULL) |
|---|
| 356 | { |
|---|
| 357 | PRINTF(0)("loading Image %s\n", colorMapName.c_str()); |
|---|
| 358 | PRINTF(0)("width : %i\n", this->colorMap->w); |
|---|
| 359 | PRINTF(0)("height : %i\n", this->colorMap->h); |
|---|
| 360 | PRINTF(0)("%i Byte(s) per Pixel \n", this->colorMap->format->BytesPerPixel); |
|---|
| 361 | PRINTF(0)("Rshift : %i\n", this->colorMap->format->Rshift); |
|---|
| 362 | PRINTF(0)("Bshift: %i\n", this->colorMap->format->Bshift); |
|---|
| 363 | PRINTF(0)("Gshift: %i\n", this->colorMap->format->Gshift); |
|---|
| 364 | PRINTF(0)("Rmask: %i\n", this->colorMap->format->Rmask); |
|---|
| 365 | PRINTF(0)("Gmask: %i\n", this->colorMap->format->Gmask); |
|---|
| 366 | |
|---|
| 367 | this->colors = (unsigned char *) colorMap->pixels; |
|---|
| 368 | this->hasColourMap = true; |
|---|
| 369 | } |
|---|
| 370 | else |
|---|
| 371 | { |
|---|
| 372 | PRINTF(0)("oops! couldn't load colorMap for some reason.\n"); |
|---|
| 373 | this->hasColourMap = false; |
|---|
| 374 | } |
|---|
| 375 | } |
|---|
| 376 | |
|---|
| 377 | |
|---|
| 378 | /** |
|---|
| 379 | * deconstructor |
|---|
| 380 | */ |
|---|
| 381 | HeightMap::~HeightMap() |
|---|
| 382 | { |
|---|
| 383 | if( heightMap) |
|---|
| 384 | delete heightMap; |
|---|
| 385 | if( colorMap) |
|---|
| 386 | delete colorMap; |
|---|
| 387 | |
|---|
| 388 | if( this->tiles) |
|---|
| 389 | { |
|---|
| 390 | for( int i = 0; i < heightMap->h / HM_TILE_SIZE; i++) { |
|---|
| 391 | for( int j = 0; j < heightMap->w / HM_TILE_SIZE; j++) { |
|---|
| 392 | delete tiles [i][j]; |
|---|
| 393 | } |
|---|
| 394 | } |
|---|
| 395 | |
|---|
| 396 | for( int i = 0; i < heightMap->h / HM_TILE_SIZE; i++) |
|---|
| 397 | delete[] tiles[i]; |
|---|
| 398 | delete[] tiles; |
|---|
| 399 | } |
|---|
| 400 | |
|---|
| 401 | if( this->normalVectorField) |
|---|
| 402 | { |
|---|
| 403 | for(int i = 0; i < heightMap->h; i++) |
|---|
| 404 | delete[] normalVectorField[i]; |
|---|
| 405 | delete[] normalVectorField; |
|---|
| 406 | } |
|---|
| 407 | } |
|---|
| 408 | |
|---|
| 409 | |
|---|
| 410 | /** |
|---|
| 411 | * this is the init function with stuff shared between all constructors |
|---|
| 412 | */ |
|---|
| 413 | void HeightMap::init(const std::string& heightMapName) |
|---|
| 414 | { |
|---|
| 415 | this->registerObject(this, HeightMap::_objectList); |
|---|
| 416 | |
|---|
| 417 | this->shiftX = 0; |
|---|
| 418 | this->shiftY = 0; |
|---|
| 419 | this->shiftZ = 0; |
|---|
| 420 | |
|---|
| 421 | heightMap = IMG_Load(heightMapName.c_str()); |
|---|
| 422 | if( heightMap != NULL) |
|---|
| 423 | { |
|---|
| 424 | /* |
|---|
| 425 | WHAT about following checks: |
|---|
| 426 | - image size (rectangular?) |
|---|
| 427 | - image file type (bw?) |
|---|
| 428 | */ |
|---|
| 429 | PRINTF(1)("loading Image %s\n", heightMapName.c_str()); |
|---|
| 430 | PRINTF(1)("width : %i\n", heightMap->w); |
|---|
| 431 | PRINTF(1)("height : %i\n", heightMap->h); |
|---|
| 432 | PRINTF(1)("%i Byte(s) per Pixel \n", heightMap->format->BytesPerPixel); |
|---|
| 433 | PRINTF(1)("Rshift : %i\n", heightMap->format->Rshift); |
|---|
| 434 | PRINTF(1)("Bshift: %i\n", heightMap->format->Bshift); |
|---|
| 435 | PRINTF(1)("Gshift: %i\n", heightMap->format->Gshift); |
|---|
| 436 | PRINTF(1)("Rmask: %i\n", heightMap->format->Rmask); |
|---|
| 437 | PRINTF(1)("Gmask: %i\n", heightMap->format->Gmask); |
|---|
| 438 | } |
|---|
| 439 | else |
|---|
| 440 | PRINTF(1)("oops! couldn't load %s for some reason.\n", heightMapName.c_str()); |
|---|
| 441 | |
|---|
| 442 | this->generateNormalVectorField(); |
|---|
| 443 | |
|---|
| 444 | this->heights = (unsigned char*)heightMap->pixels; |
|---|
| 445 | } |
|---|
| 446 | |
|---|
| 447 | |
|---|
| 448 | /** |
|---|
| 449 | * this function loads the heightmap by creatin tiles |
|---|
| 450 | */ |
|---|
| 451 | void HeightMap::load() |
|---|
| 452 | { |
|---|
| 453 | // create a dynamicly sized 2D-array for tiles |
|---|
| 454 | this->tiles = new Tile**[this->heightMap->h / HM_TILE_SIZE]; |
|---|
| 455 | |
|---|
| 456 | for( int i = 0;i < heightMap->h / HM_TILE_SIZE; i++) |
|---|
| 457 | this->tiles [i]= new Tile*[this->heightMap->w / HM_TILE_SIZE]; |
|---|
| 458 | |
|---|
| 459 | // setup arrays |
|---|
| 460 | for( int i = 0; i < this->heightMap->h / HM_TILE_SIZE; i++) { |
|---|
| 461 | for( int j = 0; j < this->heightMap->w / HM_TILE_SIZE; j++) { |
|---|
| 462 | this->tiles[i][j] = new Tile( i * HM_TILE_SIZE , j * HM_TILE_SIZE , (i+1) * HM_TILE_SIZE, (j+1) * HM_TILE_SIZE , this); |
|---|
| 463 | } |
|---|
| 464 | } |
|---|
| 465 | } |
|---|
| 466 | |
|---|
| 467 | |
|---|
| 468 | /** |
|---|
| 469 | * this function draws the height map |
|---|
| 470 | */ |
|---|
| 471 | void HeightMap::draw() const |
|---|
| 472 | { |
|---|
| 473 | Vector v = State::getCameraNode()->getAbsCoor(); |
|---|
| 474 | |
|---|
| 475 | int i_max = (heightMap->h )/ HM_TILE_SIZE; |
|---|
| 476 | int j_max= (heightMap->w ) / HM_TILE_SIZE; |
|---|
| 477 | |
|---|
| 478 | |
|---|
| 479 | |
|---|
| 480 | /* process the draw command to the tiles, FIXME: think of something more efficient*/ |
|---|
| 481 | for( int i = 0; i < i_max; i++) { |
|---|
| 482 | for( int j = 0; j < j_max; j++) { |
|---|
| 483 | tiles[i][j]->draw(); |
|---|
| 484 | } |
|---|
| 485 | } |
|---|
| 486 | } |
|---|
| 487 | |
|---|
| 488 | |
|---|
| 489 | /** |
|---|
| 490 | * this function generates the normal vector field |
|---|
| 491 | */ |
|---|
| 492 | void HeightMap::generateNormalVectorField() |
|---|
| 493 | { |
|---|
| 494 | int delta = 1; |
|---|
| 495 | heights = (unsigned char*) heightMap->pixels; |
|---|
| 496 | |
|---|
| 497 | //Create a Dynamicly sized 2D-Array to store our normals |
|---|
| 498 | normalVectorField = new Vector* [heightMap->h]; |
|---|
| 499 | for(int i=0;i<heightMap->h;i++) |
|---|
| 500 | normalVectorField [i]= new (Vector [heightMap->w]); |
|---|
| 501 | |
|---|
| 502 | // Initialize |
|---|
| 503 | for(int i=0; i< heightMap->h; i++) |
|---|
| 504 | { |
|---|
| 505 | for(int j = 0; j> heightMap->w; j++) |
|---|
| 506 | { |
|---|
| 507 | Vector v = Vector(0.0, 1.0, 0.0); |
|---|
| 508 | normalVectorField[i][j] = v; |
|---|
| 509 | } |
|---|
| 510 | } |
|---|
| 511 | |
|---|
| 512 | // !!! Does not yet calculate the normals of some border points!!!!! |
|---|
| 513 | |
|---|
| 514 | if(heightMap != NULL && heightMap->format->BitsPerPixel == 8 ) |
|---|
| 515 | { |
|---|
| 516 | SDL_LockSurface(heightMap); |
|---|
| 517 | for(int i = 0 ; i < heightMap->h - 1 ; i ++) |
|---|
| 518 | { |
|---|
| 519 | for(int j = 0; j < heightMap->w - 1 ; j ++) |
|---|
| 520 | { |
|---|
| 521 | |
|---|
| 522 | |
|---|
| 523 | delta = (int)heights[j + (i+1)*(heightMap->w )] - (int) heights[j + i*(heightMap->w )]; |
|---|
| 524 | Vector a = Vector(-scaleX,(float)delta*scaleY ,0.0f); |
|---|
| 525 | |
|---|
| 526 | delta = (int)heights[j+1 + i*(heightMap->w )] - (int)heights[j + i*(heightMap->w )]; |
|---|
| 527 | Vector b = Vector(0.0f,(float) delta*scaleY ,scaleZ); |
|---|
| 528 | |
|---|
| 529 | |
|---|
| 530 | normalVectorField[i][j] = b.cross(a); |
|---|
| 531 | normalVectorField[i][j].normalize(); |
|---|
| 532 | |
|---|
| 533 | } |
|---|
| 534 | } |
|---|
| 535 | SDL_UnlockSurface(heightMap); |
|---|
| 536 | |
|---|
| 537 | } |
|---|
| 538 | } |
|---|
| 539 | |
|---|
| 540 | |
|---|
| 541 | /** |
|---|
| 542 | * scales the height map about a vector |
|---|
| 543 | * @param v scaling vector |
|---|
| 544 | */ |
|---|
| 545 | void HeightMap::scale(Vector v) |
|---|
| 546 | { |
|---|
| 547 | scaleX = v.x; |
|---|
| 548 | scaleY = v.y; |
|---|
| 549 | scaleZ = v.z; |
|---|
| 550 | generateNormalVectorField(); |
|---|
| 551 | } |
|---|
| 552 | |
|---|
| 553 | |
|---|
| 554 | /** |
|---|
| 555 | * sets the absolute coordinates of the height map |
|---|
| 556 | * @param v the moving vector |
|---|
| 557 | */ |
|---|
| 558 | void HeightMap::setAbsCoor(Vector v) |
|---|
| 559 | { |
|---|
| 560 | offsetX = v.x; |
|---|
| 561 | offsetY = v.y; |
|---|
| 562 | offsetZ = v.z; |
|---|
| 563 | } |
|---|
| 564 | |
|---|
| 565 | |
|---|
| 566 | /** |
|---|
| 567 | * returns the height at a given 2D coordinate |
|---|
| 568 | * @param x x coordinate of the height map (world space) |
|---|
| 569 | * @param y y coordinate of the height map (world space) |
|---|
| 570 | * @return the height (z) |
|---|
| 571 | */ |
|---|
| 572 | float HeightMap::getHeight(float x, float y) |
|---|
| 573 | { |
|---|
| 574 | x -= offsetX; |
|---|
| 575 | y -= offsetZ; |
|---|
| 576 | |
|---|
| 577 | int xInt = (int)( x / scaleX); |
|---|
| 578 | x -= (float)((int)x); |
|---|
| 579 | xInt = heightMap->h - xInt; |
|---|
| 580 | int yInt = (int)( y / scaleZ); |
|---|
| 581 | y -= (float) ((int) y); /*yInt = heightMap->w - yInt;*/ |
|---|
| 582 | |
|---|
| 583 | //PRINTF(0)("xInt: %i, yInt: %i, x: %f, y: %f\n", xInt, yInt, x, y); |
|---|
| 584 | |
|---|
| 585 | if(xInt <= 0 || xInt >= heightMap->h || yInt <= 0 || yInt >= heightMap->w ) |
|---|
| 586 | return 0; |
|---|
| 587 | if( y >= 0.5*x) |
|---|
| 588 | { |
|---|
| 589 | // Check for ... |
|---|
| 590 | } |
|---|
| 591 | |
|---|
| 592 | float height = heights[yInt + (xInt)*heightMap->w]*scaleY; |
|---|
| 593 | |
|---|
| 594 | float a = normalVectorField[(xInt)][yInt].x; |
|---|
| 595 | float b = normalVectorField [(xInt)][yInt].z; |
|---|
| 596 | float c = normalVectorField [(xInt)][yInt].y; |
|---|
| 597 | |
|---|
| 598 | // PRINTF(0)("a: %f \n" ,a); |
|---|
| 599 | // PRINTF(0)("b: %f \n" ,b); |
|---|
| 600 | // PRINTF(0)("c: %f \n" ,c); |
|---|
| 601 | |
|---|
| 602 | |
|---|
| 603 | height -= ( (a/c)*(x) + (b/c)*(y)); |
|---|
| 604 | |
|---|
| 605 | // PRINTF(0)("height: %f \n" ,height ); |
|---|
| 606 | return (height + offsetZ); |
|---|
| 607 | |
|---|
| 608 | } |
|---|