[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; |
---|
| 110 | int offset = 0; |
---|
[7014] | 111 | |
---|
[7467] | 112 | float r = 0.0; |
---|
| 113 | float g = 0.0; |
---|
| 114 | float b = 0.0; |
---|
[7014] | 115 | |
---|
| 116 | |
---|
[7490] | 117 | //if( this->heightMapReference->heightMap != NULL && this->heightMapReference->heightMap->format->BitsPerPixel == 8 ) |
---|
[7014] | 118 | |
---|
[7469] | 119 | SDL_LockSurface(this->heightMapReference->heightMap); |
---|
[7490] | 120 | SDL_LockSurface(this->heightMapReference->colorMap); |
---|
[6956] | 121 | |
---|
[7499] | 122 | for( int i = xOffset ; i <= i2 ; i +=sampleRate) |
---|
[7467] | 123 | { |
---|
| 124 | int w = 0; |
---|
[6956] | 125 | |
---|
[7490] | 126 | // adjust the colors acoring to the color map |
---|
| 127 | if( this->heightMapReference->hasColourMap) |
---|
[7467] | 128 | { |
---|
[7492] | 129 | r = colors[3 * w + 2 + 3 * i * this->heightMapReference->heightMap->w]; |
---|
| 130 | g = colors[3 * w + 1 + 3 * i * this->heightMapReference->heightMap->w]; |
---|
| 131 | b = colors[3 * w + 0 + 3 * i * this->heightMapReference->heightMap->w]; |
---|
[7467] | 132 | } |
---|
[7014] | 133 | |
---|
[7499] | 134 | w = yOffset; |
---|
[7491] | 135 | |
---|
[7499] | 136 | PRINTF(0)("Values: i = %i, w = %i\n", i, w); |
---|
| 137 | |
---|
[7491] | 138 | // add a vertex to the list |
---|
[7499] | 139 | model->addVertex(scaleX * (this->heightMapReference->heightMap->h - i) + shiftX, shiftY, scaleZ * w + shiftZ); // Top Right |
---|
[7491] | 140 | model->addNormal(normalVectorField[i % this->heightMapReference->heightMap->h][w % this->heightMapReference->heightMap->w].y, |
---|
| 141 | normalVectorField[i % this->heightMapReference->heightMap->h][w % this->heightMapReference->heightMap->w].z, |
---|
| 142 | normalVectorField[i % this->heightMapReference->heightMap->h][w % this->heightMapReference->heightMap->w].x); |
---|
[7500] | 143 | model->addTexCoor((float)(yOffset-sampleRate) /(HM_TEX_RATE), (float)(i %this->heightMapReference->heightMap->h)/(HM_TEX_RATE)); |
---|
[7492] | 144 | model->addColor(r/255.0f, g/255.0f, b/255.0f); |
---|
[7014] | 145 | |
---|
| 146 | |
---|
[7499] | 147 | for(int j = yOffset; j <= j2; j += sampleRate) |
---|
[7467] | 148 | { |
---|
[7491] | 149 | // adjust the colors acording to the color map |
---|
| 150 | if( this->heightMapReference->hasColourMap) |
---|
[7467] | 151 | { |
---|
[7492] | 152 | r = colors[3 * j + 2 + 3 * i * this->heightMapReference->heightMap->w]; |
---|
| 153 | g = colors[3 * j + 1 + 3 * i * this->heightMapReference->heightMap->w]; |
---|
| 154 | b = colors[3 * j + 0 + 3 * i * this->heightMapReference->heightMap->w]; |
---|
[7467] | 155 | } |
---|
[7491] | 156 | height = (float)(unsigned char)this->heightMapReference->heights[j + sampleRate + i * (this->heightMapReference->heightMap->w)]; |
---|
| 157 | height += (float)(unsigned char)this->heightMapReference->heights[j + 1 + sampleRate + (i + 1) * |
---|
[7494] | 158 | this->heightMapReference->heightMap->w]; |
---|
[7491] | 159 | height += (float)(unsigned char)this->heightMapReference->heights[j - 1 + sampleRate + (i + 1) * |
---|
[7494] | 160 | this->heightMapReference->heightMap->w]; |
---|
[7491] | 161 | height += (float)(unsigned char)this->heightMapReference->heights[j + sampleRate + (i + 2) * |
---|
[7494] | 162 | this->heightMapReference->heightMap->w]; |
---|
[7491] | 163 | height += (float)(unsigned char)this->heightMapReference->heights[j + sampleRate + i * (this->heightMapReference->heightMap->w)]; |
---|
| 164 | height /= 5.0; |
---|
[7014] | 165 | |
---|
[7492] | 166 | model->addVertex(scaleX * (this->heightMapReference->heightMap->h -i) + shiftX , |
---|
| 167 | ((double)(height)*scaleY) + shiftY , |
---|
| 168 | scaleZ*(j) + shiftZ); // Top Right |
---|
| 169 | model->addNormal(normalVectorField[i % this->heightMapReference->heightMap->h][j % this->heightMapReference->heightMap->w].y, |
---|
| 170 | normalVectorField[i % this->heightMapReference->heightMap->h][j % this->heightMapReference->heightMap->w].z, |
---|
| 171 | normalVectorField[i % this->heightMapReference->heightMap->h][j % this->heightMapReference->heightMap->w].x); |
---|
[7500] | 172 | model->addTexCoor((float)(j) /(HM_TEX_RATE), (float)(i %this->heightMapReference->heightMap->h)/(HM_TEX_RATE)); |
---|
[7492] | 173 | model->addColor(r/255.0f, g/255.0f, b/255.0f); |
---|
[7469] | 174 | //PRINTF(0)("TexCoord: %f %f \n",(float)j / 100.0, (float)(i %this->heightMapReference->h)/100.0); |
---|
[7014] | 175 | |
---|
[7467] | 176 | w = j; |
---|
| 177 | } |
---|
[7469] | 178 | model->addVertex(scaleX*(this->heightMapReference->heightMap->h -i)+ shiftX,shiftY,scaleZ*(w)+ shiftZ); // Top Right |
---|
[7494] | 179 | model->addNormal(normalVectorField[i % this->heightMapReference->heightMap->h][w % this->heightMapReference->heightMap->w].y, |
---|
| 180 | normalVectorField[i % this->heightMapReference->heightMap->h][w % this->heightMapReference->heightMap->w].z, |
---|
| 181 | normalVectorField[i% this->heightMapReference->heightMap->h][w % this->heightMapReference->heightMap->w].x); |
---|
[7500] | 182 | model->addTexCoor((float)(j2+sampleRate) /(HM_TEX_RATE), (float)(i %this->heightMapReference->heightMap->h)/(HM_TEX_RATE)); |
---|
[7494] | 183 | model->addColor(r/255.0f, g/255.0f, b/255.0f); |
---|
[7467] | 184 | } |
---|
[7014] | 185 | |
---|
[7469] | 186 | SDL_UnlockSurface(this->heightMapReference->heightMap); |
---|
[7467] | 187 | int cnt = 0; |
---|
[7492] | 188 | |
---|
[7499] | 189 | for( int i = xOffset; i < i2; i +=sampleRate) |
---|
[7467] | 190 | { |
---|
[7499] | 191 | for( int j = yOffset-sampleRate; j < j2 + 2 * sampleRate; j += sampleRate) |
---|
[7467] | 192 | { |
---|
| 193 | model->addIndice(cnt); |
---|
[7499] | 194 | model->addIndice(cnt + (j2 -yOffset + 3* sampleRate )/ sampleRate ); |
---|
[7467] | 195 | cnt++; |
---|
| 196 | } |
---|
| 197 | model->newStripe(); |
---|
| 198 | } |
---|
[7499] | 199 | cnt += (j2 -yOffset + 3* sampleRate)/ sampleRate; |
---|
[7014] | 200 | |
---|
[7499] | 201 | for( int j = yOffset; j <= j2; j += sampleRate) |
---|
[7467] | 202 | { |
---|
[7499] | 203 | int i = xOffset; |
---|
[7014] | 204 | |
---|
[7467] | 205 | // To be fixed |
---|
[7469] | 206 | if(this->heightMapReference->hasColourMap) |
---|
[7467] | 207 | { |
---|
[7492] | 208 | r = (float)colors[3 * j + 2 + 3 * i * this->heightMapReference->heightMap->w]; |
---|
| 209 | g = (float)colors[3 * j + 1 + 3 * i * this->heightMapReference->heightMap->w]; |
---|
| 210 | b = (float)colors[3 * j + 0 + 3 * i * this->heightMapReference->heightMap->w]; |
---|
[7467] | 211 | } |
---|
[7014] | 212 | |
---|
[7492] | 213 | model->addVertex(scaleX*(this->heightMapReference->heightMap->h -i) + shiftX , |
---|
| 214 | shiftY , |
---|
| 215 | scaleZ*(j) + shiftZ); // Top Right |
---|
| 216 | model->addNormal(normalVectorField[i % this->heightMapReference->heightMap->h][j % this->heightMapReference->heightMap->w].y, |
---|
| 217 | normalVectorField[i % this->heightMapReference->heightMap->h][j % this->heightMapReference->heightMap->w].z, |
---|
| 218 | normalVectorField[i % this->heightMapReference->heightMap->h][j % this->heightMapReference->heightMap->w].x); |
---|
[7500] | 219 | model->addTexCoor((float)j /(HM_TEX_RATE), (float)((i - sampleRate) %this->heightMapReference->heightMap->h)/(HM_TEX_RATE)); |
---|
[7492] | 220 | model->addColor(r/255.0f, g/255.0f, b/255.0f); |
---|
[7467] | 221 | } |
---|
[7014] | 222 | |
---|
[7494] | 223 | |
---|
[7499] | 224 | for(int j = yOffset ; j <= j2 ; j += sampleRate) |
---|
[7467] | 225 | { |
---|
[7499] | 226 | int i = xOffset; |
---|
[7494] | 227 | height = (float)(unsigned char)this->heightMapReference->heights[j + sampleRate + i * 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 - 1 + sampleRate + (i + 1) * |
---|
| 231 | this->heightMapReference->heightMap->w]; |
---|
| 232 | height += (float)(unsigned char)this->heightMapReference->heights[j + sampleRate + (i + 2) * |
---|
| 233 | this->heightMapReference->heightMap->w]; |
---|
| 234 | height += (float)(unsigned char)this->heightMapReference->heights[j + sampleRate + i * this->heightMapReference->heightMap->w]; |
---|
[7467] | 235 | height=height/5.0; |
---|
[7014] | 236 | |
---|
[7494] | 237 | model->addVertex(scaleX*(this->heightMapReference->heightMap->h -i) + shiftX , |
---|
| 238 | ((double)(height)*scaleY) +shiftY , |
---|
| 239 | scaleZ*(j) + shiftZ); // Top Right |
---|
| 240 | model->addNormal(normalVectorField[i % this->heightMapReference->heightMap->h][j % this->heightMapReference->heightMap->w].y, |
---|
| 241 | normalVectorField[i % this->heightMapReference->heightMap->h][j% this->heightMapReference->heightMap->w].z, |
---|
| 242 | normalVectorField[i%this->heightMapReference->heightMap->h][j%this->heightMapReference->heightMap->w].x); |
---|
[7500] | 243 | model->addTexCoor((float)j /(HM_TEX_RATE), (float)(i %this->heightMapReference->heightMap->h)/(HM_TEX_RATE)); |
---|
[7494] | 244 | model->addColor(r/255.0f, g/255.0f, b/255.0f); |
---|
[7467] | 245 | } |
---|
[7014] | 246 | |
---|
| 247 | |
---|
[7499] | 248 | for(int j = yOffset; j <= j2; j += sampleRate) |
---|
[7467] | 249 | { |
---|
| 250 | int i = i2; |
---|
| 251 | // To be fixed |
---|
[7494] | 252 | if( this->heightMapReference->hasColourMap) |
---|
[7467] | 253 | { |
---|
[7494] | 254 | r = (float)colors[3 * j + 2 + 3 * i * this->heightMapReference->heightMap->w]; |
---|
| 255 | g = (float)colors[3 * j + 1 + 3 * i * this->heightMapReference->heightMap->w]; |
---|
| 256 | b = (float)colors[3 * j + 0 + 3 * i * this->heightMapReference->heightMap->w]; |
---|
[7467] | 257 | } |
---|
[7014] | 258 | |
---|
[7494] | 259 | model->addVertex(scaleX*(this->heightMapReference->heightMap->h -i) + shiftX , |
---|
| 260 | shiftY , |
---|
| 261 | scaleZ*(j) + shiftZ); // Top Right |
---|
| 262 | model->addNormal(normalVectorField[i%this->heightMapReference->heightMap->h][j%this->heightMapReference->heightMap->w].y, |
---|
| 263 | normalVectorField[i%this->heightMapReference->heightMap->h][j%this->heightMapReference->heightMap->w].z, |
---|
| 264 | normalVectorField[i%this->heightMapReference->heightMap->h][j%this->heightMapReference->heightMap->w].x); |
---|
[7500] | 265 | model->addTexCoor((float)j /(HM_TEX_RATE), (float)((i+ sampleRate) %this->heightMapReference->heightMap->h)/(HM_TEX_RATE)); |
---|
[7494] | 266 | model->addColor(r/255.0f, g/255.0f, b/255.0f); |
---|
[7467] | 267 | } |
---|
[7014] | 268 | |
---|
| 269 | |
---|
[7499] | 270 | for(int j = yOffset; j <= j2; j += sampleRate) |
---|
[7467] | 271 | { |
---|
| 272 | int i = i2; |
---|
[7494] | 273 | height = (float)(unsigned char)this->heightMapReference->heights[j + sampleRate + i * this->heightMapReference->heightMap->w]; |
---|
| 274 | height += (float)(unsigned char)this->heightMapReference->heights[j + 1 + sampleRate + (i + 1) |
---|
| 275 | * this->heightMapReference->heightMap->w]; |
---|
[7496] | 276 | height += (float)(unsigned char)this->heightMapReference->heights[j - 1 + sampleRate + (i + 1) * |
---|
| 277 | this->heightMapReference->heightMap->w]; |
---|
| 278 | height += (float)(unsigned char)this->heightMapReference->heights[j + sampleRate + (i + 2) * |
---|
| 279 | this->heightMapReference->heightMap->w]; |
---|
| 280 | height += (float)(unsigned char)this->heightMapReference->heights[j + sampleRate + i * this->heightMapReference->heightMap->w]; |
---|
| 281 | height /= 5.0; |
---|
| 282 | model->addVertex(scaleX*(this->heightMapReference->heightMap->h -i) + shiftX , |
---|
| 283 | ((double)(height)*scaleY) +shiftY , |
---|
| 284 | scaleZ*(j) + shiftZ); // Top Right |
---|
| 285 | model->addNormal(normalVectorField[i%this->heightMapReference->heightMap->h][j%this->heightMapReference->heightMap->w].y, |
---|
| 286 | normalVectorField[i%this->heightMapReference->heightMap->h][j%this->heightMapReference->heightMap->w].z, |
---|
| 287 | normalVectorField[i%this->heightMapReference->heightMap->h][j%this->heightMapReference->heightMap->w].x); |
---|
[7500] | 288 | model->addTexCoor((float)j /(HM_TEX_RATE), (float)(i %this->heightMapReference->heightMap->h)/(HM_TEX_RATE)); |
---|
[7496] | 289 | model->addColor(r/255.0f, g/255.0f, b/255.0f); |
---|
[7467] | 290 | } |
---|
[7014] | 291 | |
---|
[7467] | 292 | // link Boarder Stripe |
---|
[7499] | 293 | for(int j = yOffset - sampleRate; j < j2; j += sampleRate) |
---|
[7467] | 294 | { |
---|
| 295 | model->addIndice(cnt); |
---|
[7499] | 296 | model->addIndice(cnt + (j2 - yOffset + sampleRate) / sampleRate ); |
---|
[7467] | 297 | cnt++; |
---|
| 298 | } |
---|
| 299 | cnt++; |
---|
[7014] | 300 | |
---|
[7496] | 301 | |
---|
[7467] | 302 | model->newStripe(); |
---|
[7014] | 303 | |
---|
| 304 | |
---|
[7499] | 305 | cnt += (j2-yOffset)/ sampleRate; |
---|
[7467] | 306 | // link 2nd BoarderStripe |
---|
[7499] | 307 | for(int j = yOffset-sampleRate; j < j2; j += sampleRate) |
---|
[7467] | 308 | { |
---|
| 309 | model->addIndice(cnt); |
---|
[7499] | 310 | model->addIndice(cnt + (j2 - yOffset + sampleRate) / sampleRate); |
---|
[7467] | 311 | cnt++; |
---|
| 312 | } |
---|
[7014] | 313 | |
---|
[7490] | 314 | SDL_UnlockSurface(this->heightMapReference->colorMap); |
---|
[7014] | 315 | |
---|
[7467] | 316 | model->finalize(); |
---|
[7014] | 317 | |
---|
[7469] | 318 | // #undef heightMap |
---|
[7490] | 319 | #undef colors |
---|
[7014] | 320 | #undef scaleX |
---|
| 321 | #undef scaleY |
---|
| 322 | #undef scaleZ |
---|
| 323 | #undef shiftX |
---|
| 324 | #undef shiftY |
---|
| 325 | #undef shiftZ |
---|
| 326 | #undef normalVectorField |
---|
[5942] | 327 | } |
---|
| 328 | |
---|
[7469] | 329 | |
---|
[6956] | 330 | |
---|
[7469] | 331 | |
---|
[7498] | 332 | /** |
---|
| 333 | * constructor |
---|
| 334 | * @param heightMapName file name of the height map |
---|
| 335 | */ |
---|
[7497] | 336 | HeightMap::HeightMap(const std::string& heightMapName) |
---|
[7467] | 337 | : VertexArrayModel() |
---|
[6956] | 338 | { |
---|
[7497] | 339 | this->init(heightMapName); |
---|
[6956] | 340 | |
---|
[7497] | 341 | this->colorMap = NULL; |
---|
[6956] | 342 | } |
---|
| 343 | |
---|
[7498] | 344 | |
---|
| 345 | /** |
---|
| 346 | * constructor |
---|
| 347 | * @param heightMapName file name of the height map |
---|
| 348 | * @param colorMapName file name of the color map |
---|
| 349 | */ |
---|
[7497] | 350 | HeightMap::HeightMap(const std::string& heightMapName, const std::string& colorMapName) |
---|
[7467] | 351 | : VertexArrayModel() |
---|
[6956] | 352 | { |
---|
[7497] | 353 | this->init(heightMapName); |
---|
[6956] | 354 | |
---|
[7497] | 355 | this->colorMap = IMG_Load(colorMapName.c_str()); |
---|
| 356 | if( this->colorMap != NULL) |
---|
[6956] | 357 | { |
---|
[7497] | 358 | PRINTF(0)("loading Image %s\n", colorMapName.c_str()); |
---|
| 359 | PRINTF(0)("width : %i\n", this->colorMap->w); |
---|
| 360 | PRINTF(0)("height : %i\n", this->colorMap->h); |
---|
| 361 | PRINTF(0)("%i Byte(s) per Pixel \n", this->colorMap->format->BytesPerPixel); |
---|
| 362 | PRINTF(0)("Rshift : %i\n", this->colorMap->format->Rshift); |
---|
| 363 | PRINTF(0)("Bshift: %i\n", this->colorMap->format->Bshift); |
---|
| 364 | PRINTF(0)("Gshift: %i\n", this->colorMap->format->Gshift); |
---|
| 365 | PRINTF(0)("Rmask: %i\n", this->colorMap->format->Rmask); |
---|
| 366 | PRINTF(0)("Gmask: %i\n", this->colorMap->format->Gmask); |
---|
[7498] | 367 | |
---|
| 368 | this->colors = (unsigned char *) colorMap->pixels; |
---|
| 369 | this->hasColourMap = true; |
---|
[6956] | 370 | } |
---|
[7467] | 371 | else |
---|
[7498] | 372 | { |
---|
[7467] | 373 | PRINTF(0)("oops! couldn't load colorMap for some reason.\n"); |
---|
[7498] | 374 | this->hasColourMap = false; |
---|
[6956] | 375 | } |
---|
[5967] | 376 | } |
---|
| 377 | |
---|
| 378 | |
---|
[7498] | 379 | /** |
---|
| 380 | * deconstructor |
---|
| 381 | */ |
---|
[5967] | 382 | HeightMap::~HeightMap() |
---|
| 383 | { |
---|
[7526] | 384 | if( heightMap) |
---|
| 385 | delete heightMap; |
---|
| 386 | if( colorMap) |
---|
| 387 | delete colorMap; |
---|
[6956] | 388 | |
---|
[7526] | 389 | if( this->tiles) |
---|
[7467] | 390 | { |
---|
[7526] | 391 | for( int i = 0; i < heightMap->h / HM_TILE_SIZE; i++) { |
---|
| 392 | for( int j = 0; j < heightMap->w / HM_TILE_SIZE; j++) { |
---|
| 393 | delete tiles [i][j]; |
---|
| 394 | } |
---|
[7467] | 395 | } |
---|
[7526] | 396 | |
---|
| 397 | for( int i = 0; i < heightMap->h / HM_TILE_SIZE; i++) |
---|
| 398 | delete[] tiles[i]; |
---|
| 399 | delete[] tiles; |
---|
[7467] | 400 | } |
---|
[6956] | 401 | |
---|
[7526] | 402 | if( this->normalVectorField) |
---|
| 403 | { |
---|
| 404 | for(int i = 0; i < heightMap->h; i++) |
---|
| 405 | delete[] normalVectorField[i]; |
---|
| 406 | delete[] normalVectorField; |
---|
| 407 | } |
---|
[7497] | 408 | } |
---|
[6956] | 409 | |
---|
| 410 | |
---|
[7526] | 411 | /** |
---|
| 412 | * this is the init function with stuff shared between all constructors |
---|
| 413 | */ |
---|
[7497] | 414 | void HeightMap::init(const std::string& heightMapName) |
---|
| 415 | { |
---|
| 416 | this->setClassID(CL_HEIGHT_MAP, "HeightMap"); |
---|
[6956] | 417 | |
---|
[7497] | 418 | this->shiftX = 0; |
---|
| 419 | this->shiftY = 0; |
---|
| 420 | this->shiftZ = 0; |
---|
[7014] | 421 | |
---|
[7497] | 422 | heightMap = IMG_Load(heightMapName.c_str()); |
---|
| 423 | if( heightMap != NULL) |
---|
| 424 | { |
---|
| 425 | /* |
---|
| 426 | WHAT about following checks: |
---|
| 427 | - image size (rectangular?) |
---|
| 428 | - image file type (bw?) |
---|
| 429 | */ |
---|
| 430 | PRINTF(1)("loading Image %s\n", heightMapName.c_str()); |
---|
| 431 | PRINTF(1)("width : %i\n", heightMap->w); |
---|
| 432 | PRINTF(1)("height : %i\n", heightMap->h); |
---|
| 433 | PRINTF(1)("%i Byte(s) per Pixel \n", heightMap->format->BytesPerPixel); |
---|
| 434 | PRINTF(1)("Rshift : %i\n", heightMap->format->Rshift); |
---|
| 435 | PRINTF(1)("Bshift: %i\n", heightMap->format->Bshift); |
---|
| 436 | PRINTF(1)("Gshift: %i\n", heightMap->format->Gshift); |
---|
| 437 | PRINTF(1)("Rmask: %i\n", heightMap->format->Rmask); |
---|
| 438 | PRINTF(1)("Gmask: %i\n", heightMap->format->Gmask); |
---|
| 439 | } |
---|
| 440 | else |
---|
| 441 | PRINTF(1)("oops! couldn't load %s for some reason.\n", heightMapName.c_str()); |
---|
[7014] | 442 | |
---|
[7526] | 443 | this->generateNormalVectorField(); |
---|
[7498] | 444 | |
---|
| 445 | this->heights = (unsigned char*)heightMap->pixels; |
---|
[5967] | 446 | } |
---|
| 447 | |
---|
[7497] | 448 | |
---|
[7498] | 449 | /** |
---|
| 450 | * this function loads the heightmap by creatin tiles |
---|
| 451 | */ |
---|
[5942] | 452 | void HeightMap::load() |
---|
| 453 | { |
---|
[7499] | 454 | // create a dynamicly sized 2D-array for tiles |
---|
[7500] | 455 | this->tiles = new Tile**[this->heightMap->h / HM_TILE_SIZE]; |
---|
[5967] | 456 | |
---|
[7500] | 457 | for( int i = 0;i < heightMap->h / HM_TILE_SIZE; i++) |
---|
| 458 | this->tiles [i]= new Tile*[this->heightMap->w / HM_TILE_SIZE]; |
---|
[7014] | 459 | |
---|
[7499] | 460 | // setup arrays |
---|
[7500] | 461 | for( int i = 0; i < this->heightMap->h / HM_TILE_SIZE; i++) { |
---|
| 462 | for( int j = 0; j < this->heightMap->w / HM_TILE_SIZE; j++) { |
---|
| 463 | 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] | 464 | } |
---|
| 465 | } |
---|
[6956] | 466 | } |
---|
[5993] | 467 | |
---|
| 468 | |
---|
[7498] | 469 | /** |
---|
| 470 | * this function draws the height map |
---|
| 471 | */ |
---|
| 472 | void HeightMap::draw() const |
---|
[6956] | 473 | { |
---|
[7526] | 474 | Vector v = State::getCameraNode()->getAbsCoor(); |
---|
[6956] | 475 | |
---|
[7467] | 476 | int i_min = 0; |
---|
[7500] | 477 | int i_max = (heightMap->h )/ HM_TILE_SIZE; |
---|
[7467] | 478 | int j_min = 0; |
---|
[7500] | 479 | int j_max= (heightMap->w ) / HM_TILE_SIZE; |
---|
[6956] | 480 | |
---|
| 481 | |
---|
| 482 | |
---|
[7526] | 483 | /* process the draw command to the tiles, FIXME: think of something more efficient*/ |
---|
| 484 | for( int i = 0; i < i_max; i++) { |
---|
| 485 | for( int j = 0; j < j_max; j++) { |
---|
[7467] | 486 | tiles[i][j]->draw(); |
---|
| 487 | } |
---|
| 488 | } |
---|
[7498] | 489 | } |
---|
[5980] | 490 | |
---|
[7498] | 491 | |
---|
| 492 | /** |
---|
| 493 | * this function generates the normal vector field |
---|
| 494 | */ |
---|
[6956] | 495 | void HeightMap::generateNormalVectorField() |
---|
| 496 | { |
---|
[7467] | 497 | int delta = 1; |
---|
| 498 | heights = (unsigned char*) heightMap->pixels; |
---|
[6956] | 499 | |
---|
[7467] | 500 | //Create a Dynamicly sized 2D-Array to store our normals |
---|
| 501 | normalVectorField = new Vector* [heightMap->h]; |
---|
| 502 | for(int i=0;i<heightMap->h;i++) |
---|
| 503 | normalVectorField [i]= new (Vector [heightMap->w]); |
---|
[6956] | 504 | |
---|
[7467] | 505 | // Initialize |
---|
| 506 | for(int i=0; i< heightMap->h; i++) |
---|
| 507 | { |
---|
| 508 | for(int j = 0; j> heightMap->w; j++) |
---|
| 509 | { |
---|
| 510 | Vector v = Vector(0.0, 1.0, 0.0); |
---|
| 511 | normalVectorField[i][j] = v; |
---|
| 512 | } |
---|
| 513 | } |
---|
[5967] | 514 | |
---|
[7467] | 515 | // !!! Does not yet calculate the normals of some border points!!!!! |
---|
[5990] | 516 | |
---|
[7467] | 517 | if(heightMap != NULL && heightMap->format->BitsPerPixel == 8 ) |
---|
| 518 | { |
---|
| 519 | SDL_LockSurface(heightMap); |
---|
| 520 | for(int i = 0 ; i < heightMap->h - 1 ; i ++) |
---|
| 521 | { |
---|
| 522 | for(int j = 0; j < heightMap->w - 1 ; j ++) |
---|
| 523 | { |
---|
[5942] | 524 | |
---|
| 525 | |
---|
[7467] | 526 | delta = (int)heights[j + (i+1)*(heightMap->w )] - (int) heights[j + i*(heightMap->w )]; |
---|
| 527 | Vector a = Vector(-scaleX,(float)delta*scaleY ,0.0f); |
---|
[5942] | 528 | |
---|
[7467] | 529 | delta = (int)heights[j+1 + i*(heightMap->w )] - (int)heights[j + i*(heightMap->w )]; |
---|
| 530 | Vector b = Vector(0.0f,(float) delta*scaleY ,scaleZ); |
---|
[5942] | 531 | |
---|
[6956] | 532 | |
---|
[7467] | 533 | normalVectorField[i][j] = b.cross(a); |
---|
| 534 | normalVectorField[i][j].normalize(); |
---|
[5942] | 535 | |
---|
[7467] | 536 | } |
---|
| 537 | } |
---|
| 538 | SDL_UnlockSurface(heightMap); |
---|
[7014] | 539 | |
---|
[7467] | 540 | } |
---|
[5942] | 541 | } |
---|
[6956] | 542 | |
---|
| 543 | |
---|
[7498] | 544 | /** |
---|
| 545 | * scales the height map about a vector |
---|
| 546 | * @param v scaling vector |
---|
| 547 | */ |
---|
[6956] | 548 | void HeightMap::scale(Vector v) |
---|
| 549 | { |
---|
[7467] | 550 | scaleX = v.x; |
---|
| 551 | scaleY = v.y; |
---|
| 552 | scaleZ = v.z; |
---|
| 553 | generateNormalVectorField(); |
---|
[6956] | 554 | } |
---|
| 555 | |
---|
[7498] | 556 | |
---|
| 557 | /** |
---|
| 558 | * sets the absolute coordinates of the height map |
---|
| 559 | * @param v the moving vector |
---|
| 560 | */ |
---|
[6956] | 561 | void HeightMap::setAbsCoor(Vector v) |
---|
| 562 | { |
---|
[7467] | 563 | offsetX = v.x; |
---|
| 564 | offsetY = v.y; |
---|
[7498] | 565 | offsetZ = v.z; |
---|
[6956] | 566 | } |
---|
| 567 | |
---|
| 568 | |
---|
[7498] | 569 | /** |
---|
| 570 | * returns the height at a given 2D coordinate |
---|
| 571 | * @param x x coordinate of the height map (world space) |
---|
| 572 | * @param y y coordinate of the height map (world space) |
---|
| 573 | * @return the height (z) |
---|
| 574 | */ |
---|
[7014] | 575 | float HeightMap::getHeight(float x, float y) |
---|
[6956] | 576 | { |
---|
[7467] | 577 | x -= offsetX; |
---|
| 578 | y -= offsetZ; |
---|
[7014] | 579 | |
---|
[7497] | 580 | int xInt = (int)( x / scaleX); |
---|
[7467] | 581 | x -= (float)((int)x); |
---|
| 582 | xInt = heightMap->h - xInt; |
---|
[7497] | 583 | int yInt = (int)( y / scaleZ); |
---|
[7467] | 584 | y -= (float) ((int) y); /*yInt = heightMap->w - yInt;*/ |
---|
[7014] | 585 | |
---|
[7467] | 586 | //PRINTF(0)("xInt: %i, yInt: %i, x: %f, y: %f\n", xInt, yInt, x, y); |
---|
[7014] | 587 | |
---|
[7467] | 588 | if(xInt <= 0 || xInt >= heightMap->h || yInt <= 0 || yInt >= heightMap->w ) |
---|
| 589 | return 0; |
---|
| 590 | if( y >= 0.5*x) |
---|
| 591 | { |
---|
| 592 | // Check for ... |
---|
| 593 | } |
---|
[6956] | 594 | |
---|
[7467] | 595 | float height = heights[yInt + (xInt)*heightMap->w]*scaleY; |
---|
[7014] | 596 | |
---|
[7467] | 597 | float a = normalVectorField[(xInt)][yInt].x; |
---|
| 598 | float b = normalVectorField [(xInt)][yInt].z; |
---|
| 599 | float c = normalVectorField [(xInt)][yInt].y; |
---|
[7014] | 600 | |
---|
[7467] | 601 | PRINTF(0)("a: %f \n" ,a); |
---|
[6956] | 602 | PRINTF(0)("b: %f \n" ,b); |
---|
[7467] | 603 | PRINTF(0)("c: %f \n" ,c); |
---|
[7014] | 604 | |
---|
[7467] | 605 | height -= ( (a/c)*(x) + (b/c)*(y)); |
---|
[7014] | 606 | |
---|
[7467] | 607 | PRINTF(0)("height: %f \n" ,height ); |
---|
| 608 | return (height + offsetZ); |
---|
[6956] | 609 | } |
---|