[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: |
---|
[6494] | 12 | main-programmer: bottac@ee.ethz.ch |
---|
[5942] | 13 | */ |
---|
| 14 | |
---|
| 15 | #include "height_map.h" |
---|
| 16 | #include "model.h" |
---|
[5967] | 17 | #include "texture.h" |
---|
| 18 | #include "vector.h" |
---|
[6494] | 19 | #include "material.h" |
---|
| 20 | #include "p_node.h" |
---|
| 21 | #include "state.h" |
---|
| 22 | #include "resource_manager.h" |
---|
[5967] | 23 | #include "debug.h" |
---|
| 24 | |
---|
| 25 | // INCLUDING SDL_Image |
---|
| 26 | #ifdef HAVE_SDL_IMAGE_H |
---|
| 27 | #include <SDL_image.h> |
---|
| 28 | #else |
---|
| 29 | #include <SDL/SDL_image.h> |
---|
| 30 | #endif |
---|
| 31 | |
---|
[6579] | 32 | Tile::Tile(int i1, int j1, int i2, int j2, HeightMap* hm ) |
---|
[5942] | 33 | { |
---|
[6494] | 34 | |
---|
| 35 | PRINTF(0)("Tile Constructor\n"); |
---|
| 36 | highResModel = new VertexArrayModel(); |
---|
| 37 | lowResModel = new VertexArrayModel(); |
---|
| 38 | |
---|
[6579] | 39 | this->load(i1,j1,i2,j2,hm,highResModel,2); |
---|
| 40 | this->load(i1,j1,i2,j2,hm,lowResModel, 8); |
---|
[5942] | 41 | } |
---|
| 42 | |
---|
[6494] | 43 | Tile::Tile() |
---|
[5967] | 44 | { |
---|
[6494] | 45 | PRINTF(0)("ooops *********************************************************************************************************************************************************************************************************** \n"); |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | Tile::~Tile() |
---|
| 49 | { |
---|
| 50 | delete highResModel; |
---|
| 51 | delete lowResModel; |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | void Tile::draw() |
---|
| 55 | { |
---|
[6603] | 56 | // This Draws the LOD's |
---|
| 57 | //FIXME |
---|
| 58 | float cameraDistance = (State::getCamera()->getAbsCoor() - Vector(0.0,100.0,0.0)).len(); |
---|
| 59 | if (cameraDistance > 5000 ) |
---|
| 60 | { |
---|
| 61 | |
---|
| 62 | } |
---|
| 63 | else if (cameraDistance > 1000 ) |
---|
| 64 | { |
---|
| 65 | this->drawLowRes(); |
---|
| 66 | } |
---|
| 67 | else |
---|
| 68 | { |
---|
| 69 | this->drawHighRes(); |
---|
| 70 | } |
---|
[6494] | 71 | } |
---|
| 72 | |
---|
| 73 | void Tile::drawHighRes() |
---|
| 74 | { |
---|
| 75 | highResModel->draw(); |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | void Tile::drawLowRes() |
---|
| 79 | { |
---|
| 80 | lowResModel->draw(); |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | /* |
---|
| 84 | */ |
---|
| 85 | void Tile::load(int i1, int j1, int i2, int j2, HeightMap* hm, VertexArrayModel* model, int Res) |
---|
| 86 | { |
---|
[6579] | 87 | |
---|
| 88 | #define heightMap hm->heightMap |
---|
| 89 | #define colours hm->colours |
---|
| 90 | #define scaleX hm->scaleX |
---|
| 91 | #define scaleY hm->scaleY |
---|
| 92 | #define scaleZ hm->scaleZ |
---|
| 93 | #define shiftX hm->shiftX |
---|
| 94 | #define shiftY hm->shiftY |
---|
| 95 | #define shiftZ hm->shiftZ |
---|
| 96 | #define normalVectorField hm->normalVectorField |
---|
| 97 | |
---|
| 98 | int sampleRate = Res; |
---|
| 99 | |
---|
| 100 | float height = 0; |
---|
| 101 | int offset = 0; |
---|
| 102 | |
---|
| 103 | float r = 0.0; |
---|
| 104 | float g = 0.0; |
---|
| 105 | float b = 0.0; |
---|
[6494] | 106 | |
---|
[6579] | 107 | |
---|
[6494] | 108 | PRINTF(0)("loadin * \n"); |
---|
[6579] | 109 | |
---|
[6494] | 110 | if(heightMap != NULL && heightMap->format->BitsPerPixel == 8 ) |
---|
[6579] | 111 | |
---|
| 112 | SDL_LockSurface(heightMap); |
---|
[6494] | 113 | |
---|
[6579] | 114 | for(int i = i1 ; i <= i2 ; i +=sampleRate) |
---|
| 115 | { |
---|
| 116 | int w = 0; |
---|
[6494] | 117 | |
---|
[6579] | 118 | |
---|
| 119 | |
---|
| 120 | if(hm->hasColourMap) |
---|
| 121 | { |
---|
| 122 | r = (float)colours[3*w+2 + 3*i*(heightMap->w )]; |
---|
| 123 | g = (float)colours[3*w+1 + 3*i*(heightMap->w)]; |
---|
| 124 | b = (float)colours[3*w+0 + 3*i*(heightMap->w)]; |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | w = j1; |
---|
| 128 | model->addVertex(scaleX*(heightMap->h -i)+ shiftX,shiftY,scaleZ*(w)+ shiftZ); // Top Right |
---|
| 129 | model->addNormal(normalVectorField[i][w].y,normalVectorField[i][w].z,normalVectorField[i][w].x); |
---|
| 130 | model->addTexCoor((((w -sampleRate)/sampleRate)%4)/4.0,(((i-sampleRate)/sampleRate)%4)/4.0); |
---|
| 131 | model->addColor(r/255.0,g/255.0,b/255.0); |
---|
| 132 | |
---|
| 133 | for(int j = j1 ; j <= j2 ; j += sampleRate) |
---|
| 134 | { |
---|
| 135 | |
---|
| 136 | |
---|
| 137 | // To be fixed |
---|
| 138 | if(hm->hasColourMap) |
---|
| 139 | { |
---|
| 140 | r = (float)colours[3*j+2 + 3*i*(heightMap->w )]; |
---|
| 141 | g = (float)colours[3*j+1 + 3*i*(heightMap->w)]; |
---|
| 142 | b = (float)colours[3*j+0 + 3*i*(heightMap->w)]; |
---|
| 143 | } |
---|
| 144 | height = (float)(unsigned char) hm->heights[j +sampleRate+ i*(heightMap->w )]; |
---|
| 145 | height += (float)(unsigned char) hm->heights[j+ 1 + sampleRate + (i+1)*(heightMap->w )]; |
---|
| 146 | height += (float) (unsigned char) hm->heights[j -1+ sampleRate + (i+1)*(heightMap->w )]; |
---|
| 147 | height += (float)(unsigned char)hm->heights[j +sampleRate+ (i+2)*(heightMap->w )]; |
---|
| 148 | height += (float)(unsigned char)hm->heights[j+sampleRate + (i)*(heightMap->w )]; |
---|
| 149 | |
---|
| 150 | height=height/5.0; |
---|
| 151 | |
---|
| 152 | model->addVertex(scaleX*(heightMap->h -i) + shiftX ,((double)(height)*scaleY) + shiftY ,scaleZ*(j) + shiftZ); // Top Right |
---|
| 153 | model->addNormal(normalVectorField[i][j].y,normalVectorField[i][j].z,normalVectorField[i][j].x); |
---|
| 154 | model->addTexCoor((float)j /(texRate), (float)(i %heightMap->h)/(texRate)); |
---|
| 155 | |
---|
| 156 | //PRINTF(0)("TexCoord: %f %f \n",(float)j / 100.0, (float)(i %heightMap->h)/100.0); |
---|
| 157 | |
---|
| 158 | model->addColor(r/255.0,g/255.0,b/255.0); |
---|
| 159 | w = j; |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | |
---|
| 163 | model->addVertex(scaleX*(heightMap->h -i)+ shiftX,shiftY,scaleZ*(w)+ shiftZ); // Top Right |
---|
| 164 | model->addNormal(normalVectorField[i][w].y,normalVectorField[i][w].z,normalVectorField[i][w].x); |
---|
| 165 | model->addTexCoor((((w+sampleRate)/sampleRate))/4.0,(((i+ sampleRate)/sampleRate))/4.0); |
---|
| 166 | model->addColor(r/255.0,g/255.0,b/255.0); |
---|
| 167 | |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | |
---|
| 171 | |
---|
[6494] | 172 | |
---|
| 173 | |
---|
[6579] | 174 | SDL_UnlockSurface(heightMap); |
---|
| 175 | int cnt = 0; |
---|
| 176 | for(int i = i1 ; i < i2 ; i +=sampleRate) |
---|
| 177 | { |
---|
| 178 | |
---|
| 179 | for(int j = j1-sampleRate ; j < j2 + 2*sampleRate ; j += sampleRate) |
---|
| 180 | { |
---|
| 181 | |
---|
| 182 | model->addIndice(cnt); |
---|
| 183 | model->addIndice(cnt + (j2 -j1 + 3* sampleRate )/ sampleRate ); |
---|
| 184 | cnt++; |
---|
| 185 | |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | |
---|
| 189 | |
---|
| 190 | model->newStripe(); |
---|
| 191 | |
---|
| 192 | |
---|
| 193 | } |
---|
| 194 | cnt += (j2 -j1 + 3* sampleRate)/ sampleRate; |
---|
| 195 | |
---|
| 196 | for(int j = j1 ; j <= j2 ; j += sampleRate) |
---|
| 197 | { |
---|
| 198 | int i = i1; |
---|
| 199 | |
---|
| 200 | // To be fixed |
---|
| 201 | if(hm->hasColourMap) |
---|
| 202 | { |
---|
| 203 | r = (float)colours[3*j+2 + 3*i*(heightMap->w )]; |
---|
| 204 | g = (float)colours[3*j+1 + 3*i*(heightMap->w)]; |
---|
| 205 | b = (float)colours[3*j+0 + 3*i*(heightMap->w)]; |
---|
| 206 | } |
---|
| 207 | |
---|
| 208 | model->addVertex(scaleX*(heightMap->h -i) + shiftX , shiftY ,scaleZ*(j) + shiftZ); // Top Right |
---|
| 209 | model->addNormal(normalVectorField[i][j].y,normalVectorField[i][j].z,normalVectorField[i][j].x); |
---|
| 210 | model->addTexCoor(((j/sampleRate))/texRatef,(((i - sampleRate)/sampleRate))/texRatef); |
---|
| 211 | model->addColor(r/255.0,g/255.0,b/255.0); |
---|
| 212 | |
---|
| 213 | } |
---|
[6494] | 214 | |
---|
[6579] | 215 | for(int j = j1 ; j <= j2 ; j += sampleRate) |
---|
| 216 | { |
---|
| 217 | int i = i1; |
---|
| 218 | height = (float)(unsigned char) hm->heights[j +sampleRate+ i*(heightMap->w )]; |
---|
| 219 | height += (float)(unsigned char) hm->heights[j+ 1 + sampleRate + (i+1)*(heightMap->w )]; |
---|
| 220 | height += (float) (unsigned char) hm->heights[j -1+ sampleRate + (i+1)*(heightMap->w )]; |
---|
| 221 | height += (float)(unsigned char)hm->heights[j +sampleRate+ (i+2)*(heightMap->w )]; |
---|
| 222 | height += (float)(unsigned char)hm->heights[j+sampleRate + (i)*(heightMap->w )]; |
---|
| 223 | height=height/5.0; |
---|
| 224 | |
---|
| 225 | model->addVertex(scaleX*(heightMap->h -i) + shiftX , ((double)(height)*scaleY) +shiftY ,scaleZ*(j) + shiftZ); // Top Right |
---|
| 226 | model->addNormal(normalVectorField[i][j].y,normalVectorField[i][j].z,normalVectorField[i][j].x); |
---|
| 227 | model->addTexCoor(((j/sampleRate))/texRatef,(((i)/sampleRate))/texRatef); |
---|
| 228 | model->addColor(r/255.0,g/255.0,b/255.0); |
---|
| 229 | |
---|
| 230 | } |
---|
| 231 | |
---|
| 232 | |
---|
| 233 | |
---|
| 234 | for(int j = j1 ; j <= j2 ; j += sampleRate) |
---|
| 235 | { |
---|
| 236 | int i = i2; |
---|
| 237 | |
---|
| 238 | // To be fixed |
---|
| 239 | if(hm->hasColourMap) |
---|
| 240 | { |
---|
| 241 | r = (float)colours[3*j+2 + 3*i*(heightMap->w )]; |
---|
| 242 | g = (float)colours[3*j+1 + 3*i*(heightMap->w)]; |
---|
| 243 | b = (float)colours[3*j+0 + 3*i*(heightMap->w)]; |
---|
| 244 | } |
---|
| 245 | |
---|
| 246 | model->addVertex(scaleX*(heightMap->h -i) + shiftX , shiftY ,scaleZ*(j) + shiftZ); // Top Right |
---|
| 247 | model->addNormal(normalVectorField[i][j].y,normalVectorField[i][j].z,normalVectorField[i][j].x); |
---|
| 248 | model->addTexCoor(((j/sampleRate)%texRate)/texRatef,(((i- sampleRate)/sampleRate)%texRate)/texRatef); |
---|
| 249 | model->addColor(r/255.0,g/255.0,b/255.0); |
---|
| 250 | |
---|
| 251 | } |
---|
| 252 | |
---|
| 253 | |
---|
| 254 | for(int j = j1 ; j <= j2 ; j += sampleRate) |
---|
| 255 | { |
---|
| 256 | int i = i2; |
---|
| 257 | height = (float)(unsigned char) hm->heights[j +sampleRate+ i*(heightMap->w )]; |
---|
| 258 | height += (float)(unsigned char) hm->heights[j+ 1 + sampleRate + (i+1)*(heightMap->w )]; |
---|
| 259 | height += (float) (unsigned char) hm->heights[j -1+ sampleRate + (i+1)*(heightMap->w )]; |
---|
| 260 | height += (float)(unsigned char)hm->heights[j +sampleRate+ (i+2)*(heightMap->w )]; |
---|
| 261 | height += (float)(unsigned char)hm->heights[j+sampleRate + (i)*(heightMap->w )]; |
---|
| 262 | height=height/5.0; |
---|
| 263 | model->addVertex(scaleX*(heightMap->h -i) + shiftX , ((double)(height)*scaleY) +shiftY ,scaleZ*(j) + shiftZ); // Top Right |
---|
| 264 | model->addNormal(normalVectorField[i][j].y,normalVectorField[i][j].z,normalVectorField[i][j].x); |
---|
| 265 | model->addTexCoor(((j/sampleRate)%texRate)/texRatef,((i/sampleRate)%texRate)/texRatef); |
---|
| 266 | model->addColor(r/255.0,g/255.0,b/255.0); |
---|
| 267 | |
---|
| 268 | } |
---|
| 269 | |
---|
[6494] | 270 | |
---|
[6579] | 271 | |
---|
| 272 | |
---|
| 273 | // link Boarder Stripe |
---|
| 274 | for(int j = j1-sampleRate ; j < j2 ; j += sampleRate) |
---|
| 275 | { |
---|
| 276 | |
---|
| 277 | model->addIndice(cnt); |
---|
| 278 | model->addIndice(cnt + (j2 -j1 + sampleRate )/ sampleRate ); |
---|
| 279 | cnt++; |
---|
| 280 | |
---|
| 281 | } |
---|
| 282 | |
---|
| 283 | cnt++; |
---|
| 284 | |
---|
| 285 | model->newStripe(); |
---|
| 286 | |
---|
| 287 | |
---|
| 288 | |
---|
| 289 | |
---|
| 290 | |
---|
| 291 | cnt += (j2-j1)/ sampleRate; |
---|
| 292 | |
---|
| 293 | // link 2nd BoarderStripe |
---|
| 294 | for(int j = j1-sampleRate ; j < j2 ; j += sampleRate) |
---|
| 295 | { |
---|
| 296 | |
---|
| 297 | model->addIndice(cnt); |
---|
| 298 | model->addIndice(cnt + (j2 -j1 + sampleRate )/ sampleRate ); |
---|
| 299 | cnt++; |
---|
| 300 | |
---|
| 301 | } |
---|
| 302 | |
---|
| 303 | |
---|
| 304 | |
---|
| 305 | model->finalize(); |
---|
| 306 | |
---|
| 307 | |
---|
| 308 | |
---|
| 309 | |
---|
| 310 | #undef heightMap |
---|
| 311 | #undef colours |
---|
| 312 | #undef scaleX |
---|
| 313 | #undef scaleY |
---|
| 314 | #undef scaleZ |
---|
| 315 | #undef shiftX |
---|
| 316 | #undef shiftY |
---|
| 317 | #undef shiftZ |
---|
| 318 | #undef normalVectorField |
---|
| 319 | |
---|
[6494] | 320 | } |
---|
| 321 | |
---|
| 322 | HeightMap::HeightMap() |
---|
| 323 | { |
---|
| 324 | |
---|
| 325 | } |
---|
| 326 | |
---|
| 327 | HeightMap::HeightMap(const char* height_map_name = NULL) : VertexArrayModel() |
---|
| 328 | { |
---|
[5976] | 329 | this->setClassID(CL_HEIGHT_MAP, "HeightMap"); |
---|
[5990] | 330 | heightMap = IMG_Load(height_map_name); |
---|
| 331 | if(heightMap!=NULL) { |
---|
[6494] | 332 | |
---|
[6579] | 333 | PRINTF(0)("loading Image %s\n", height_map_name); |
---|
| 334 | PRINTF(0)("width : %i\n", heightMap->w); |
---|
| 335 | PRINTF(0)("height : %i\n", heightMap->h); |
---|
| 336 | PRINTF(0)("%i Byte(s) per Pixel \n", heightMap->format->BytesPerPixel); |
---|
| 337 | PRINTF(0)("Rshift : %i\n", heightMap->format->Rshift); |
---|
| 338 | PRINTF(0)("Bshift: %i\n", heightMap->format->Bshift); |
---|
| 339 | PRINTF(0)("Gshift: %i\n", heightMap->format->Gshift); |
---|
| 340 | PRINTF(0)("Rmask: %i\n", heightMap->format->Rmask); |
---|
| 341 | PRINTF(0)("Gmask: %i\n", heightMap->format->Gmask); |
---|
| 342 | } |
---|
[6494] | 343 | |
---|
[5990] | 344 | else PRINTF(4)("oops! couldn't load %s for some reason.\n", height_map_name); |
---|
[6494] | 345 | |
---|
[6579] | 346 | |
---|
[6494] | 347 | generateNormalVectorField(); |
---|
| 348 | |
---|
| 349 | shiftX = 0; |
---|
| 350 | shiftY = 0; |
---|
| 351 | shiftZ = 0; |
---|
| 352 | |
---|
[5967] | 353 | } |
---|
| 354 | |
---|
[6494] | 355 | HeightMap::HeightMap(const char* height_map_name = NULL, const char* colour_map_name = NULL) : VertexArrayModel() |
---|
| 356 | { |
---|
| 357 | this->setClassID(CL_HEIGHT_MAP, "HeightMap"); |
---|
[5967] | 358 | |
---|
[6494] | 359 | heightMap = IMG_Load(height_map_name); |
---|
| 360 | if(heightMap!=NULL) { |
---|
| 361 | |
---|
[6579] | 362 | PRINTF(0)("loading Image %s\n", height_map_name); |
---|
| 363 | PRINTF(0)("width : %i\n", heightMap->w); |
---|
| 364 | PRINTF(0)("height : %i\n", heightMap->h); |
---|
| 365 | PRINTF(0)("%i Byte(s) per Pixel \n", heightMap->format->BytesPerPixel); |
---|
| 366 | PRINTF(0)("Rshift : %i\n", heightMap->format->Rshift); |
---|
| 367 | PRINTF(0)("Bshift: %i\n", heightMap->format->Bshift); |
---|
| 368 | PRINTF(0)("Gshift: %i\n", heightMap->format->Gshift); |
---|
| 369 | PRINTF(0)("Rmask: %i\n", heightMap->format->Rmask); |
---|
| 370 | PRINTF(0)("Gmask: %i\n", heightMap->format->Gmask); |
---|
| 371 | } |
---|
[6494] | 372 | |
---|
| 373 | else PRINTF(4)("oops! couldn't load %s for some reason.\n", height_map_name); |
---|
| 374 | |
---|
| 375 | |
---|
| 376 | generateNormalVectorField(); |
---|
| 377 | |
---|
| 378 | colourMap=NULL; |
---|
| 379 | if(colour_map_name != NULL) |
---|
| 380 | { |
---|
| 381 | colourMap = IMG_Load(colour_map_name); |
---|
| 382 | } |
---|
[6579] | 383 | |
---|
[6494] | 384 | if(colourMap != NULL) |
---|
[6579] | 385 | { |
---|
| 386 | PRINTF(0)("loading Image %s\n", colour_map_name); |
---|
| 387 | PRINTF(0)("width : %i\n", colourMap->w); |
---|
| 388 | PRINTF(0)("height : %i\n", colourMap->h); |
---|
| 389 | PRINTF(0)("%i Byte(s) per Pixel \n", colourMap->format->BytesPerPixel); |
---|
| 390 | PRINTF(0)("Rshift : %i\n", colourMap->format->Rshift); |
---|
| 391 | PRINTF(0)("Bshift: %i\n", colourMap->format->Bshift); |
---|
| 392 | PRINTF(0)("Gshift: %i\n", colourMap->format->Gshift); |
---|
| 393 | PRINTF(0)("Rmask: %i\n", colourMap->format->Rmask); |
---|
| 394 | PRINTF(0)("Gmask: %i\n", colourMap->format->Gmask); |
---|
| 395 | } |
---|
| 396 | else PRINTF(0)("oops! couldn't load colorMap for some reason.\n"); |
---|
[6494] | 397 | |
---|
[6579] | 398 | |
---|
| 399 | |
---|
[6494] | 400 | if(colourMap != NULL) |
---|
| 401 | { |
---|
| 402 | colours = (unsigned char *) colourMap->pixels; |
---|
| 403 | hasColourMap = true; |
---|
| 404 | } |
---|
| 405 | else hasColourMap = false; |
---|
[6579] | 406 | |
---|
| 407 | |
---|
| 408 | heights = (unsigned char*) heightMap->pixels; |
---|
[6494] | 409 | |
---|
| 410 | |
---|
| 411 | } |
---|
[5967] | 412 | HeightMap::~HeightMap() |
---|
| 413 | { |
---|
[6579] | 414 | delete heightMap; |
---|
[5967] | 415 | } |
---|
| 416 | |
---|
[5942] | 417 | void HeightMap::load() |
---|
| 418 | { |
---|
[5993] | 419 | |
---|
[6494] | 420 | //Create a Dynamicly sized 2D-Array for Tiles |
---|
| 421 | tiles = new Tile** [heightMap->h/tileSize]; |
---|
| 422 | for(int i=0;i < heightMap->h/tileSize ; i++) |
---|
| 423 | tiles [i]= new (Tile* [heightMap->w /tileSize ]); |
---|
[5967] | 424 | |
---|
[6494] | 425 | //SetUp Arrays |
---|
| 426 | for(int i = 0; i < (heightMap->h - tileSize )/ tileSize; i ++) |
---|
| 427 | { |
---|
[6579] | 428 | for(int j = 0; j < (heightMap->w - tileSize )/ tileSize; j ++) |
---|
| 429 | { |
---|
| 430 | |
---|
| 431 | tiles[i][j] = new Tile( i*tileSize , j*tileSize , (i+1)*tileSize, (j+1)*tileSize , this ) ; |
---|
| 432 | } |
---|
[6494] | 433 | } |
---|
[5993] | 434 | |
---|
[6494] | 435 | } |
---|
[5993] | 436 | |
---|
[5980] | 437 | |
---|
[6579] | 438 | void HeightMap::draw() |
---|
[6494] | 439 | { |
---|
| 440 | const PNode* camera = State::getCamera(); |
---|
| 441 | Vector v = camera->getAbsCoor(); |
---|
[5967] | 442 | |
---|
[6494] | 443 | int i_min = 1; |
---|
[6579] | 444 | int i_max = (heightMap->h - tileSize )/ tileSize; |
---|
[6494] | 445 | int j_min = 1; |
---|
| 446 | int j_max= (heightMap->w - tileSize) / tileSize; |
---|
[5990] | 447 | |
---|
[5942] | 448 | |
---|
| 449 | |
---|
[6603] | 450 | for(int i = 1; i < i_max ; i ++) |
---|
[6494] | 451 | { |
---|
[6579] | 452 | for(int j = 1; j < j_max ; j++) |
---|
| 453 | { |
---|
| 454 | |
---|
[6603] | 455 | tiles[i][j]->draw(); |
---|
[6579] | 456 | } |
---|
| 457 | } |
---|
[6494] | 458 | |
---|
| 459 | } |
---|
| 460 | void HeightMap::generateNormalVectorField() |
---|
| 461 | { |
---|
| 462 | int delta = 1; |
---|
| 463 | heights = (unsigned char*) heightMap->pixels; |
---|
| 464 | |
---|
| 465 | //Create a Dynamicly sized 2D-Array to store our normals |
---|
| 466 | normalVectorField = new Vector* [heightMap->h]; |
---|
| 467 | for(int i=0;i<heightMap->h;i++) |
---|
| 468 | normalVectorField [i]= new (Vector [heightMap->w]); |
---|
| 469 | |
---|
| 470 | |
---|
| 471 | |
---|
| 472 | // !!! Does not yet calculate the normals of some border points!!!!! |
---|
| 473 | |
---|
| 474 | if(heightMap != NULL && heightMap->format->BitsPerPixel == 8 ) |
---|
[6579] | 475 | { |
---|
| 476 | SDL_LockSurface(heightMap); |
---|
| 477 | for(int i = 0 ; i < heightMap->h - 1 ; i ++) |
---|
| 478 | { |
---|
| 479 | for(int j = 0; j < heightMap->w - 1 ; j ++) |
---|
| 480 | { |
---|
[6494] | 481 | |
---|
| 482 | |
---|
[6579] | 483 | delta = (int)heights[j + (i+1)*(heightMap->w )] - (int) heights[j + i*(heightMap->w )]; |
---|
| 484 | Vector a = Vector(-scaleX,(float)delta*scaleY ,0.0f); |
---|
[6494] | 485 | |
---|
[6579] | 486 | delta = (int)heights[j+1 + i*(heightMap->w )] - (int)heights[j + i*(heightMap->w )]; |
---|
| 487 | Vector b = Vector(0.0f,(float) delta*scaleY ,scaleZ); |
---|
[6494] | 488 | |
---|
| 489 | |
---|
[6579] | 490 | normalVectorField[i][j] = b.cross(a); |
---|
| 491 | normalVectorField[i][j].normalize(); |
---|
| 492 | |
---|
| 493 | } |
---|
| 494 | } |
---|
| 495 | SDL_UnlockSurface(heightMap); |
---|
[6494] | 496 | |
---|
| 497 | } |
---|
| 498 | |
---|
| 499 | } |
---|
| 500 | |
---|
| 501 | |
---|
| 502 | |
---|
| 503 | |
---|
| 504 | |
---|
| 505 | void HeightMap::scale(Vector v) |
---|
| 506 | { |
---|
| 507 | scaleX = v.x; |
---|
| 508 | scaleY = v.y; |
---|
| 509 | scaleZ = v.z; |
---|
| 510 | } |
---|
| 511 | |
---|
| 512 | void HeightMap::shift(Vector v) |
---|
| 513 | { |
---|
| 514 | shiftX = v.x; |
---|
| 515 | shiftY = v.y; |
---|
| 516 | shiftZ = v.z; |
---|
| 517 | } |
---|
| 518 | |
---|
| 519 | |
---|
| 520 | float HeightMap::getHeight(float x, float y) |
---|
| 521 | { |
---|
| 522 | int xInt = (int)x / scaleX; x -= (float)xInt*scaleX; xInt = heightMap->h - xInt; |
---|
| 523 | int yInt = (int)y / scaleZ; y -= (float)yInt*scaleZ; |
---|
| 524 | if(xInt <= 0 || xInt >= heightMap->h || yInt <= 0 || yInt >= heightMap->w ) return 0.0f; |
---|
| 525 | float height = heights[yInt + (xInt)*heightMap->w]*scaleY; |
---|
| 526 | float a = normalVectorField[(xInt)][yInt].x; |
---|
| 527 | float b = normalVectorField [(xInt)][yInt].z; |
---|
| 528 | float c = normalVectorField [(xInt)][yInt].y; |
---|
| 529 | height -= ( (a/c)*(x) + (b/c)*(y))*scaleY; |
---|
| 530 | return height; |
---|
| 531 | } |
---|