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