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