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