[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 | |
---|
[6227] | 32 | HeightMap::HeightMap() |
---|
[5942] | 33 | { |
---|
[6227] | 34 | |
---|
[5942] | 35 | } |
---|
| 36 | |
---|
[6267] | 37 | HeightMap::HeightMap(const char* height_map_name = NULL) |
---|
[5967] | 38 | { |
---|
[5976] | 39 | this->setClassID(CL_HEIGHT_MAP, "HeightMap"); |
---|
[5990] | 40 | heightMap = IMG_Load(height_map_name); |
---|
| 41 | if(heightMap!=NULL) { |
---|
[6267] | 42 | |
---|
[5967] | 43 | PRINTF(0)("loading Image %s\n", height_map_name); |
---|
| 44 | PRINTF(0)("width : %i\n", heightMap->w); |
---|
| 45 | PRINTF(0)("hight : %i\n", heightMap->h); |
---|
| 46 | PRINTF(0)("%i Byte(s) per Pixel \n", heightMap->format->BytesPerPixel); |
---|
[5990] | 47 | PRINTF(0)("Rshift : %i\n", heightMap->format->Rshift); |
---|
| 48 | PRINTF(0)("Bshift: %i\n", heightMap->format->Bshift); |
---|
| 49 | PRINTF(0)("Gshift: %i\n", heightMap->format->Gshift); |
---|
| 50 | PRINTF(0)("Rmask: %i\n", heightMap->format->Rmask); |
---|
[6267] | 51 | PRINTF(0)("Gmask: %i\n", heightMap->format->Gmask); |
---|
[5967] | 52 | } |
---|
[6267] | 53 | |
---|
[5990] | 54 | else PRINTF(4)("oops! couldn't load %s for some reason.\n", height_map_name); |
---|
[6227] | 55 | |
---|
[6267] | 56 | |
---|
[6227] | 57 | generateNormalVectorField(); |
---|
[6267] | 58 | |
---|
| 59 | |
---|
[6227] | 60 | //Defines 2 Materials (for testing only!) |
---|
[6267] | 61 | this->tmp_mat = new Material(); |
---|
[6227] | 62 | tmp_mat->setTransparency(1.0); |
---|
| 63 | tmp_mat->setIllum(0.3); |
---|
| 64 | tmp_mat->setDiffuse(0.55,0.4,0.2); |
---|
| 65 | tmp_mat->setAmbient(0.6,0.4,0.2); |
---|
| 66 | tmp_mat->setSpecular(0.02,0.02,0.02); |
---|
| 67 | tmp_mat->setShininess(.1); |
---|
| 68 | tmp_mat->setTransparency(1.0); |
---|
| 69 | |
---|
| 70 | tmp_mat->diffuseTexture = NULL; |
---|
| 71 | tmp_mat->ambientTexture = NULL; |
---|
| 72 | tmp_mat->specularTexture = NULL; |
---|
| 73 | |
---|
[6267] | 74 | this->red_mat = new Material(); |
---|
[6227] | 75 | red_mat->setTransparency(1.0); |
---|
| 76 | red_mat->setIllum(0.3); |
---|
| 77 | red_mat->setDiffuse(0.55,0.1,0.1); |
---|
| 78 | red_mat->setAmbient(0.55,0.1,0.1); |
---|
| 79 | red_mat->setSpecular(0.02,0.02,0.02); |
---|
| 80 | red_mat->setShininess(.1); |
---|
| 81 | red_mat->setTransparency(1.0); |
---|
| 82 | |
---|
| 83 | red_mat->diffuseTexture = NULL; |
---|
| 84 | red_mat->ambientTexture = NULL; |
---|
| 85 | red_mat->specularTexture = NULL; |
---|
| 86 | |
---|
[5967] | 87 | } |
---|
| 88 | |
---|
[6249] | 89 | HeightMap::HeightMap(const char* height_map_name = NULL, const char* colour_map_name = NULL) : VertexArrayModel() |
---|
[6227] | 90 | { |
---|
| 91 | this->setClassID(CL_HEIGHT_MAP, "HeightMap"); |
---|
[5967] | 92 | |
---|
[6227] | 93 | heightMap = IMG_Load(height_map_name); |
---|
| 94 | if(heightMap!=NULL) { |
---|
[6267] | 95 | |
---|
[6227] | 96 | PRINTF(0)("loading Image %s\n", height_map_name); |
---|
| 97 | PRINTF(0)("width : %i\n", heightMap->w); |
---|
| 98 | PRINTF(0)("hight : %i\n", heightMap->h); |
---|
| 99 | PRINTF(0)("%i Byte(s) per Pixel \n", heightMap->format->BytesPerPixel); |
---|
| 100 | PRINTF(0)("Rshift : %i\n", heightMap->format->Rshift); |
---|
| 101 | PRINTF(0)("Bshift: %i\n", heightMap->format->Bshift); |
---|
| 102 | PRINTF(0)("Gshift: %i\n", heightMap->format->Gshift); |
---|
| 103 | PRINTF(0)("Rmask: %i\n", heightMap->format->Rmask); |
---|
[6267] | 104 | PRINTF(0)("Gmask: %i\n", heightMap->format->Gmask); |
---|
[6227] | 105 | } |
---|
[6267] | 106 | |
---|
[6227] | 107 | else PRINTF(4)("oops! couldn't load %s for some reason.\n", height_map_name); |
---|
| 108 | |
---|
[6267] | 109 | |
---|
[6227] | 110 | generateNormalVectorField(); |
---|
[6267] | 111 | |
---|
| 112 | |
---|
[6227] | 113 | //Defines 2 Materials (for testing only!) |
---|
[6267] | 114 | this->tmp_mat = new Material("tmp"); |
---|
[6227] | 115 | tmp_mat->setTransparency(1.0); |
---|
| 116 | tmp_mat->setIllum(0.3); |
---|
| 117 | tmp_mat->setDiffuse(1.0,1.0,1.0); |
---|
| 118 | tmp_mat->setAmbient(1.0,1.0,1.0); |
---|
| 119 | |
---|
| 120 | tmp_mat->setSpecular(0.0,0.00,0.00); |
---|
| 121 | tmp_mat->setShininess(1.0); |
---|
| 122 | |
---|
| 123 | |
---|
| 124 | |
---|
[6267] | 125 | |
---|
| 126 | this->red_mat = new Material("red"); |
---|
[6227] | 127 | red_mat->setTransparency(1.0); |
---|
| 128 | red_mat->setIllum(3); |
---|
| 129 | red_mat->setDiffuse(0.55,0.1,0.1); |
---|
| 130 | red_mat->setAmbient(0.55,0.1,0.1); |
---|
| 131 | red_mat->setSpecular(0.0,0.0,0.0); |
---|
| 132 | red_mat->setShininess(1.0); |
---|
| 133 | red_mat->setTransparency(1.0); |
---|
| 134 | |
---|
| 135 | // red_mat->diffuseTexture = NULL; |
---|
| 136 | // red_mat->ambientTexture = NULL; |
---|
| 137 | //red_mat->specularTexture = NULL; |
---|
[6267] | 138 | |
---|
[6227] | 139 | if(colour_map_name != NULL) |
---|
| 140 | { |
---|
| 141 | colourMap = IMG_Load(colour_map_name); |
---|
| 142 | if(colourMap != NULL) |
---|
| 143 | { |
---|
| 144 | PRINTF(0)("loading Image %s\n", colour_map_name); |
---|
| 145 | PRINTF(0)("width : %i\n", colourMap->w); |
---|
| 146 | PRINTF(0)("hight : %i\n", colourMap->h); |
---|
| 147 | PRINTF(0)("%i Byte(s) per Pixel \n", colourMap->format->BytesPerPixel); |
---|
| 148 | PRINTF(0)("Rshift : %i\n", colourMap->format->Rshift); |
---|
| 149 | PRINTF(0)("Bshift: %i\n", colourMap->format->Bshift); |
---|
| 150 | PRINTF(0)("Gshift: %i\n", colourMap->format->Gshift); |
---|
| 151 | PRINTF(0)("Rmask: %i\n", colourMap->format->Rmask); |
---|
| 152 | PRINTF(0)("Gmask: %i\n", colourMap->format->Gmask); |
---|
| 153 | } |
---|
| 154 | else PRINTF(4)("oops! couldn't load %s for some reason.\n", colour_map_name); |
---|
[6267] | 155 | |
---|
[6227] | 156 | const char* texture_name = "pictures/ground1.tga"; |
---|
| 157 | tmp_mat->setDiffuseMap(texture_name); |
---|
| 158 | if(colourMap != NULL) |
---|
| 159 | { |
---|
| 160 | colours = (unsigned char *) colourMap->pixels; |
---|
| 161 | hasColourMap = true; |
---|
| 162 | } |
---|
| 163 | else |
---|
| 164 | { |
---|
| 165 | hasColourMap = false; |
---|
[6267] | 166 | |
---|
| 167 | |
---|
[6227] | 168 | } |
---|
[6267] | 169 | |
---|
[6227] | 170 | // tmp_mat->setAmbientMap(texture_name); |
---|
| 171 | //tmp_mat->setSpecularMap(texture_name); |
---|
[6267] | 172 | |
---|
[6227] | 173 | // red_mat->setDiffuseMap(texture_name); |
---|
| 174 | // red_mat->setAmbientMap(texture_name); |
---|
[6267] | 175 | //red_mat->setSpecularMap(texture_name); |
---|
[6227] | 176 | } |
---|
[6267] | 177 | |
---|
| 178 | |
---|
[6227] | 179 | } |
---|
[5967] | 180 | HeightMap::~HeightMap() |
---|
| 181 | { |
---|
| 182 | delete heightMap; |
---|
| 183 | } |
---|
| 184 | |
---|
[5942] | 185 | void HeightMap::load() |
---|
| 186 | { |
---|
[6227] | 187 | |
---|
[6249] | 188 | this->camera = State::getCamera(); |
---|
| 189 | this->camCoords = this->camera->getAbsCoor(); |
---|
| 190 | float x = this->camCoords.x + 300; |
---|
| 191 | float y = this->camCoords.z + 100; |
---|
| 192 | |
---|
| 193 | tmp_mat->select(); |
---|
| 194 | |
---|
[6267] | 195 | unsigned char height = 0; |
---|
[6249] | 196 | int offset = 0; |
---|
| 197 | int g = 0; |
---|
| 198 | float old_r = 0.0f; |
---|
| 199 | float old_g = 0.0f; |
---|
| 200 | float old_b = 0.0f; |
---|
| 201 | heights = (unsigned char*) heightMap->pixels; |
---|
| 202 | bool colourChanged = true; |
---|
[6267] | 203 | |
---|
| 204 | |
---|
[6249] | 205 | /* |
---|
| 206 | if(heightMap != NULL && heightMap->format->BitsPerPixel == 8 ) |
---|
| 207 | {} |
---|
| 208 | SDL_LockSurface(heightMap); |
---|
[6267] | 209 | |
---|
| 210 | |
---|
| 211 | |
---|
[6249] | 212 | for(int i = 0 ; i < heightMap->h -sampleRate ; i +=sampleRate) |
---|
| 213 | { |
---|
[6267] | 214 | |
---|
[6249] | 215 | tmp_mat->select(); |
---|
| 216 | |
---|
| 217 | |
---|
[6267] | 218 | |
---|
[6249] | 219 | int j = 0; |
---|
| 220 | |
---|
[6267] | 221 | |
---|
[6249] | 222 | for(int j = 0 ; j < heightMap->w -sampleRate ; j += sampleRate) |
---|
| 223 | { |
---|
| 224 | if(true) |
---|
| 225 | { |
---|
| 226 | float r = (float)colours[3*j+2 + 3*i*(heightMap->w )]; |
---|
| 227 | float g = (float)colours[3*j+1 + 3*i*(heightMap->w)]; |
---|
| 228 | float b = (float)colours[3*j+0 + 3*i*(heightMap->w)]; |
---|
| 229 | colourChanged = old_r != r || old_g != g || old_b != b; |
---|
| 230 | old_r = r; |
---|
| 231 | old_g = g; |
---|
| 232 | old_b = b; |
---|
| 233 | if(colourChanged) |
---|
| 234 | { |
---|
| 235 | tmp_mat->setAmbient(r/255.0,g/255.0,b/255.0); |
---|
| 236 | tmp_mat->setDiffuse(r/255.0,g/255.0,b/255.0); |
---|
[6267] | 237 | tmp_mat->select(); |
---|
[6249] | 238 | } |
---|
| 239 | } |
---|
[6267] | 240 | |
---|
| 241 | |
---|
[6249] | 242 | height = heights[j+sampleRate + i*(heightMap->w )]; |
---|
| 243 | this->addNormal(normalVectorField[i][j+sampleRate].y,normalVectorField[i][j+sampleRate].z,normalVectorField[i][j+2].x); |
---|
| 244 | this->addTexCoor(((j/sampleRate)%4)/4.0,((i/sampleRate)%4)/4.0); |
---|
[6267] | 245 | |
---|
| 246 | this->addVertex(scaleX*(heightMap->h -i),(double)((double)(height)*scaleY),scaleZ*(j+sampleRate)); // Top Right |
---|
| 247 | |
---|
| 248 | |
---|
| 249 | |
---|
[6249] | 250 | } |
---|
| 251 | |
---|
| 252 | |
---|
[6267] | 253 | |
---|
| 254 | |
---|
| 255 | |
---|
| 256 | |
---|
| 257 | |
---|
| 258 | |
---|
| 259 | |
---|
[6249] | 260 | } |
---|
[6267] | 261 | |
---|
| 262 | |
---|
| 263 | |
---|
[6249] | 264 | SDL_UnlockSurface(heightMap); |
---|
[6267] | 265 | |
---|
| 266 | |
---|
[6249] | 267 | int cnt = 0; |
---|
| 268 | for(int i = 0 ; i < heightMap->h -sampleRate ; i +=sampleRate) |
---|
| 269 | { |
---|
| 270 | |
---|
| 271 | for(int j = 0 ; j < heightMap->w -sampleRate ; j += sampleRate) |
---|
| 272 | { |
---|
[6267] | 273 | |
---|
| 274 | this->addIndice(cnt); |
---|
[6249] | 275 | cnt ++; |
---|
| 276 | } |
---|
| 277 | if(i%2 == 1) |
---|
| 278 | { |
---|
| 279 | this->newStripe(); |
---|
| 280 | } |
---|
[6267] | 281 | } |
---|
| 282 | |
---|
| 283 | |
---|
| 284 | |
---|
| 285 | |
---|
[6249] | 286 | */ |
---|
| 287 | |
---|
[6267] | 288 | this->planeModel(10.0, 10.0, 20, 20); |
---|
| 289 | |
---|
[6249] | 290 | this->finalize(); |
---|
| 291 | |
---|
| 292 | |
---|
[6227] | 293 | } |
---|
| 294 | |
---|
[6249] | 295 | /* |
---|
[6267] | 296 | void HeightMap::draw() |
---|
[6227] | 297 | { |
---|
| 298 | |
---|
| 299 | this->camera = State::getCamera(); |
---|
| 300 | this->camCoords = this->camera->getAbsCoor(); |
---|
| 301 | float x = this->camCoords.x + 300; |
---|
| 302 | float y = this->camCoords.z + 100; |
---|
| 303 | |
---|
| 304 | // Draw a red rectangle to test getHeight |
---|
| 305 | |
---|
| 306 | red_mat->select(); |
---|
| 307 | glBegin(GL_TRIANGLE_STRIP); |
---|
| 308 | glNormal3f(0,1,0); |
---|
| 309 | glTexCoord2d(1.0,0.0); |
---|
| 310 | glVertex3f(x-10,getHeight(x-10,y+10)+5.0f ,y+10); // Top Left |
---|
| 311 | |
---|
[6267] | 312 | |
---|
[6227] | 313 | glNormal3f(0,1,0); |
---|
| 314 | glTexCoord2d(1.0,1.0); |
---|
| 315 | glVertex3f(x+10,getHeight(x+10,y+10)+5.0f ,y+10); // Top Left |
---|
| 316 | |
---|
[6267] | 317 | |
---|
[6227] | 318 | glNormal3f(0,1,0); |
---|
| 319 | glTexCoord2d(0.0,0.0); |
---|
| 320 | glVertex3f(x-10,getHeight(x-10,y-10)+5.0f ,y-10); // Top Left |
---|
| 321 | |
---|
[6267] | 322 | |
---|
[6227] | 323 | glNormal3f(0,1,0); |
---|
| 324 | glTexCoord2d(0.0,1.0); |
---|
| 325 | glVertex3f(x+10,getHeight(x+10,y-10)+5.0f ,y-10); // Top Left |
---|
[6267] | 326 | |
---|
[6227] | 327 | glEnd(); |
---|
| 328 | |
---|
| 329 | tmp_mat->select(); |
---|
| 330 | |
---|
[6267] | 331 | unsigned char height = 0; |
---|
[5967] | 332 | int offset = 0; |
---|
[6227] | 333 | int g = 0; |
---|
| 334 | float old_r = 0.0f; |
---|
| 335 | float old_g = 0.0f; |
---|
| 336 | float old_b = 0.0f; |
---|
| 337 | heights = (unsigned char*) heightMap->pixels; |
---|
[5993] | 338 | |
---|
[6227] | 339 | bool colourChanged = true; |
---|
[6267] | 340 | |
---|
[5990] | 341 | if(heightMap != NULL && heightMap->format->BitsPerPixel == 8 ) |
---|
[5967] | 342 | { |
---|
[5990] | 343 | SDL_LockSurface(heightMap); |
---|
[6227] | 344 | |
---|
[6267] | 345 | glBegin(GL_TRIANGLE_STRIP); |
---|
[6227] | 346 | for(int i = 0 ; i < heightMap->h -sampleRate ; i +=sampleRate) |
---|
[5990] | 347 | { |
---|
[6227] | 348 | |
---|
| 349 | |
---|
| 350 | |
---|
[6267] | 351 | |
---|
[6227] | 352 | int j = 0; |
---|
[6267] | 353 | |
---|
[6227] | 354 | /* |
---|
| 355 | height = heights[j + i*(heightMap->w )]; |
---|
| 356 | glNormal3f(normalVectorField[i][j].y,normalVectorField[i][j].z,normalVectorField[i][j].x); |
---|
| 357 | glVertex3f(20*(heightMap->h -(i))-1000,(double)((double)(height)/1-300),20*(j)-1000); // Top Left |
---|
| 358 | |
---|
| 359 | height = heights[j + (i+2)*(heightMap->w )]; |
---|
| 360 | glNormal3f(normalVectorField[i+2][j].y,normalVectorField[i+2][j].z,normalVectorField[i+2][j].x); |
---|
| 361 | glVertex3f(20*(heightMap->h -(i+2))-1000,(double)((double)(height)/1-300),20*(j)-1000); // Bottom Left |
---|
| 362 | |
---|
[6267] | 363 | */ |
---|
[6249] | 364 | /* |
---|
[6227] | 365 | for(int j = 0 ; j < heightMap->w -sampleRate ; j += sampleRate) |
---|
[5990] | 366 | { |
---|
[6227] | 367 | if(hasColourMap) |
---|
| 368 | { |
---|
| 369 | float r = (float)colours[3*j+2 + 3*i*(heightMap->w )]; |
---|
| 370 | float g = (float)colours[3*j+1 + 3*i*(heightMap->w)]; |
---|
| 371 | float b = (float)colours[3*j+0 + 3*i*(heightMap->w)]; |
---|
| 372 | colourChanged = old_r != r || old_g != g || old_b != b; |
---|
| 373 | old_r = r; |
---|
| 374 | old_g = g; |
---|
| 375 | old_b = b; |
---|
| 376 | if(colourChanged) |
---|
| 377 | { |
---|
| 378 | tmp_mat->setAmbient(r/255.0,g/255.0,b/255.0); |
---|
| 379 | tmp_mat->setDiffuse(r/255.0,g/255.0,b/255.0); |
---|
[6267] | 380 | tmp_mat->select(); |
---|
[6227] | 381 | } |
---|
| 382 | } |
---|
[6249] | 383 | */ |
---|
| 384 | //if(true/*(abs(-scaleX*i-x+5000) > 1100 || abs(scaleZ*j-y-2000) > 1100 )*/){ //subdivide?? |
---|
| 385 | /* |
---|
[6227] | 386 | height = heights[j+sampleRate + i*(heightMap->w )]; |
---|
| 387 | glNormal3f(normalVectorField[i][j+sampleRate].y,normalVectorField[i][j+sampleRate].z,normalVectorField[i][j+2].x); |
---|
| 388 | glTexCoord2f(((j/sampleRate)%4)/4.0,((i/sampleRate)%4)/4.0); |
---|
[6267] | 389 | |
---|
| 390 | glVertex3f(scaleX*(heightMap->h -i),(double)((double)(height)*scaleY),scaleZ*(j+sampleRate)); // Top Right |
---|
| 391 | |
---|
[6227] | 392 | height = heights[j+sampleRate + (i+sampleRate)*(heightMap->w )]; |
---|
| 393 | glNormal3f(normalVectorField[i+sampleRate][j+sampleRate].y,normalVectorField[i+sampleRate][j+sampleRate].z,normalVectorField[i+sampleRate][j+sampleRate].x); |
---|
| 394 | glTexCoord2f(((j/sampleRate)%4)/4.0,1/4.0+((i/sampleRate)%4)/4.0); |
---|
| 395 | //glTexCoord2f(0.0,0.0); |
---|
| 396 | glVertex3f(scaleX*(heightMap->h -(i+sampleRate)),(double)((double)(height)*scaleY),scaleZ*(j+sampleRate)); // Bottom Right |
---|
[6267] | 397 | |
---|
[6227] | 398 | } |
---|
| 399 | else |
---|
| 400 | { |
---|
| 401 | //red_mat->select(); |
---|
| 402 | //glEnd(); |
---|
[6267] | 403 | |
---|
| 404 | |
---|
| 405 | |
---|
| 406 | glBegin(GL_TRIANGLE_STRIP); |
---|
| 407 | |
---|
[6227] | 408 | drawRect(j,i+sampleRate/2,j+sampleRate/2,i); |
---|
| 409 | glEnd(); |
---|
| 410 | glBegin(GL_TRIANGLE_STRIP); |
---|
[6267] | 411 | |
---|
| 412 | |
---|
[6227] | 413 | drawRect(j+sampleRate/2,i+sampleRate/2,j+sampleRate,i); |
---|
| 414 | glEnd(); |
---|
| 415 | glBegin(GL_TRIANGLE_STRIP); |
---|
[6267] | 416 | |
---|
| 417 | |
---|
[6227] | 418 | drawRect(j,i+sampleRate,j+sampleRate/2,i+sampleRate/2); |
---|
| 419 | glEnd(); |
---|
| 420 | glBegin(GL_TRIANGLE_STRIP); |
---|
| 421 | drawRect(j+sampleRate/2,i+sampleRate,j+sampleRate,i+sampleRate/2); |
---|
[6267] | 422 | |
---|
[6227] | 423 | glEnd(); |
---|
| 424 | |
---|
[6267] | 425 | |
---|
| 426 | |
---|
| 427 | |
---|
| 428 | |
---|
[6227] | 429 | //subdivide |
---|
| 430 | } |
---|
| 431 | |
---|
[6267] | 432 | |
---|
[6249] | 433 | } |
---|
| 434 | j = heightMap->w -sampleRate; |
---|
[6267] | 435 | |
---|
| 436 | |
---|
[6249] | 437 | glNormal3f(normalVectorField[i+sampleRate][j+sampleRate].y,normalVectorField[i+sampleRate][j+sampleRate].z,normalVectorField[i+sampleRate][j+sampleRate].x); |
---|
| 438 | //glTexCoord2f(((j/sampleRate)%4)/4.0,1/4.0+((i/sampleRate)%4)/4.0); |
---|
| 439 | //glTexCoord2f(0.0,0.0); |
---|
| 440 | glTexCoord2f(((j/sampleRate)%4)/4.0,((i/sampleRate)%4)/4.0); |
---|
| 441 | glVertex3f(scaleX*(heightMap->h -i),(double)((double)-1000.0),scaleZ*(j+sampleRate)); // Top Right |
---|
[6267] | 442 | |
---|
[6249] | 443 | glNormal3f(normalVectorField[i+sampleRate][j+sampleRate].y,normalVectorField[i+sampleRate][j+sampleRate].z,normalVectorField[i+sampleRate][j+sampleRate].x); |
---|
| 444 | glTexCoord2f(((j/sampleRate)%4)/4.0,1/4.0+((i/sampleRate)%4)/4.0); |
---|
[6267] | 445 | |
---|
[6249] | 446 | glVertex3f(scaleX*(heightMap->h -(i+sampleRate)),(double)-1000.0,scaleZ*(j+sampleRate)); // Bottom Right |
---|
[6227] | 447 | |
---|
[6267] | 448 | |
---|
| 449 | |
---|
| 450 | |
---|
| 451 | |
---|
| 452 | //glEnd(); |
---|
| 453 | |
---|
[5990] | 454 | } |
---|
| 455 | SDL_UnlockSurface(heightMap); |
---|
[6249] | 456 | glEnd(); |
---|
[6227] | 457 | } |
---|
[5980] | 458 | |
---|
[6249] | 459 | } |
---|
[6227] | 460 | */ |
---|
| 461 | void HeightMap::generateNormalVectorField() |
---|
| 462 | { |
---|
| 463 | int delta = 1; |
---|
| 464 | heights = (unsigned char*) heightMap->pixels; |
---|
| 465 | |
---|
| 466 | //Create a Dynamicly sized 2D-Array to store our normals |
---|
| 467 | normalVectorField = new Vector* [heightMap->h]; |
---|
| 468 | for(int i=0;i<heightMap->h;i++) |
---|
| 469 | normalVectorField [i]= new (Vector [heightMap->w]); |
---|
| 470 | |
---|
| 471 | |
---|
| 472 | |
---|
| 473 | // !!! Does not yet calculate any normals for some border points!!!!! |
---|
| 474 | |
---|
| 475 | if(heightMap != NULL && heightMap->format->BitsPerPixel == 8 ) |
---|
| 476 | { |
---|
| 477 | SDL_LockSurface(heightMap); |
---|
| 478 | for(int i = 0 ; i < heightMap->h -1 ; i ++) |
---|
| 479 | { |
---|
| 480 | for(int j = 0; j < heightMap->w -1 ; j ++) |
---|
| 481 | { |
---|
[5967] | 482 | |
---|
[6267] | 483 | |
---|
[6227] | 484 | delta = (int)heights[j + (i+1)*(heightMap->w )] - (int) heights[j + i*(heightMap->w )]; |
---|
| 485 | Vector a = Vector(-20.0,(float)delta ,0.0f); |
---|
[6267] | 486 | |
---|
[6227] | 487 | delta = (int)heights[j+1 + i*(heightMap->w )] - (int)heights[j + i*(heightMap->w )]; |
---|
| 488 | Vector b = Vector(0.0f,(float) delta ,20.0); |
---|
[6267] | 489 | |
---|
| 490 | |
---|
[6227] | 491 | normalVectorField[i][j] = b.cross(a); |
---|
| 492 | normalVectorField[i][j].normalize(); |
---|
[6267] | 493 | |
---|
| 494 | } |
---|
[6227] | 495 | } |
---|
| 496 | SDL_UnlockSurface(heightMap); |
---|
[5942] | 497 | |
---|
[6227] | 498 | } |
---|
[5942] | 499 | |
---|
[6227] | 500 | } |
---|
[5942] | 501 | |
---|
[6227] | 502 | void HeightMap::drawRect(int xBottom, int yBottom, int xTop, int yTop ) |
---|
| 503 | { |
---|
| 504 | int height = 0; |
---|
| 505 | if(true) |
---|
| 506 | { |
---|
[6267] | 507 | |
---|
[6227] | 508 | height = heights[xTop + yTop*(heightMap->w )]; |
---|
| 509 | glNormal3f(normalVectorField[yTop][xTop].y,normalVectorField[yTop][xTop].z,normalVectorField[yTop][xTop].x); |
---|
| 510 | glTexCoord2f(((yBottom/sampleRate)%8)/8.0+0.125,((xBottom/sampleRate)%8)/8.0+0.125); |
---|
| 511 | glVertex3f(scaleX*(heightMap->h - yTop),(double)((double)(height)*scaleY),scaleZ*(xTop)); // Top Right |
---|
[5942] | 512 | |
---|
[6227] | 513 | height = heights[xBottom + yTop*(heightMap->w )]; |
---|
| 514 | glNormal3f(normalVectorField[yTop][xBottom].y,normalVectorField[yTop][xBottom].z,normalVectorField[yTop][xBottom].x); |
---|
| 515 | glTexCoord2f(((yBottom/sampleRate)%8)/8.0,((xBottom/sampleRate)%8)/8.0+0.125); |
---|
| 516 | glVertex3f(scaleX*(heightMap->h -(yTop)),(double)((double)(height)*scaleY),scaleZ*(xBottom)); // Top Left |
---|
[6267] | 517 | |
---|
[6227] | 518 | height = heights[xTop + (yBottom)*(heightMap->w )]; |
---|
| 519 | glNormal3f(normalVectorField[yBottom][xTop].y,normalVectorField[yBottom][xTop].z,normalVectorField[yBottom][xTop].x); |
---|
| 520 | glTexCoord2f(((yBottom/sampleRate)%8)/8.0+0.125,((yBottom/sampleRate)%8)/8.0); |
---|
| 521 | glVertex3f(scaleX*(heightMap->h -(yBottom)),(double)((double)(height)*scaleY),scaleZ*(xTop)); // Bottom Right |
---|
[6267] | 522 | |
---|
[6227] | 523 | height = heights[xBottom + (yBottom)*(heightMap->w )]; |
---|
| 524 | glNormal3f(normalVectorField[yBottom][xBottom].y,normalVectorField[yBottom][xBottom].z,normalVectorField[yBottom][xBottom].x); |
---|
| 525 | glTexCoord2f(((yBottom/sampleRate)%8)/8.0,((yBottom/sampleRate)%8)/8.0); |
---|
| 526 | glVertex3f(scaleX*(heightMap->h -(yBottom)),(double)((double)(height)*scaleY),scaleZ*(xBottom)); // Bottom Left |
---|
| 527 | |
---|
| 528 | |
---|
[6267] | 529 | |
---|
[5967] | 530 | } |
---|
[6227] | 531 | else |
---|
| 532 | { |
---|
| 533 | // subdivide |
---|
| 534 | } |
---|
[6267] | 535 | |
---|
[6227] | 536 | } |
---|
[5942] | 537 | |
---|
| 538 | |
---|
[6227] | 539 | void HeightMap::fixBoarder(int xBottomLeft, int yBottomLeft, int xTopRight, int yTopRight) |
---|
| 540 | { |
---|
| 541 | |
---|
| 542 | int height = 0; |
---|
[6267] | 543 | |
---|
[6227] | 544 | for(int i= xTopRight; i < xBottomLeft ; i+= sampleRate) |
---|
| 545 | { |
---|
[6267] | 546 | |
---|
[6227] | 547 | glBegin(GL_TRIANGLES); |
---|
[6267] | 548 | |
---|
[6227] | 549 | height = heights[yBottomLeft + (i + sampleRate)*(heightMap->w )]; |
---|
| 550 | glNormal3f(0,1,0); |
---|
| 551 | glVertex3f(scaleX*(heightMap->h -i-sampleRate),scaleY*height,scaleZ*(yBottomLeft)); |
---|
[6267] | 552 | |
---|
[6227] | 553 | height = heights[yBottomLeft + i*(heightMap->w )]; |
---|
| 554 | glNormal3f(0,1,0); |
---|
[6267] | 555 | glVertex3f(scaleX*(heightMap->h -i),scaleY*height,scaleZ*(yBottomLeft)); |
---|
| 556 | |
---|
[6227] | 557 | height = heights[yBottomLeft + (i + sampleRate/2)*(heightMap->w )]; |
---|
| 558 | glNormal3f(0,1,0); |
---|
[6267] | 559 | glVertex3f(scaleX*(heightMap->h -i - sampleRate/2),scaleY*height,scaleZ*(yBottomLeft)); |
---|
| 560 | |
---|
| 561 | |
---|
| 562 | |
---|
[6227] | 563 | glEnd(); |
---|
[6267] | 564 | |
---|
| 565 | |
---|
| 566 | |
---|
| 567 | |
---|
[6227] | 568 | } |
---|
| 569 | for(int j= yBottomLeft; j < yTopRight; j+= sampleRate) |
---|
| 570 | { |
---|
| 571 | glBegin(GL_TRIANGLES); |
---|
| 572 | height = heights[j + (xBottomLeft+sampleRate)*(heightMap->w )]; |
---|
| 573 | glNormal3f(-1,0,0); |
---|
[6267] | 574 | glVertex3f(scaleX*(heightMap->h -xBottomLeft -sampleRate),scaleY*height,scaleZ*(j)); |
---|
| 575 | |
---|
[6227] | 576 | height = heights[j + sampleRate/2 + (xBottomLeft + sampleRate)*(heightMap->w )]; |
---|
| 577 | glNormal3f(-1,0,0); |
---|
[6267] | 578 | glVertex3f(scaleX*(heightMap->h -xBottomLeft - sampleRate),scaleY*height,scaleZ*(j+sampleRate/2)); |
---|
| 579 | |
---|
[6227] | 580 | height = heights[j + sampleRate + (xBottomLeft + sampleRate)*(heightMap->w )]; |
---|
| 581 | glNormal3f(-1,0,0); |
---|
[6267] | 582 | glVertex3f(scaleX*(heightMap->h -xBottomLeft-sampleRate),scaleY*height,scaleZ*(j+sampleRate)); |
---|
| 583 | |
---|
[6227] | 584 | glEnd(); |
---|
| 585 | } |
---|
[5942] | 586 | } |
---|
[6227] | 587 | |
---|
| 588 | void HeightMap::scale(Vector v) |
---|
| 589 | { |
---|
| 590 | scaleX = v.x; |
---|
| 591 | scaleY = v.y; |
---|
| 592 | scaleZ = v.z; |
---|
| 593 | } |
---|
| 594 | |
---|
| 595 | // Accepts Coordinates relative to HeightMap |
---|
| 596 | float HeightMap::getHeight(float x, float y) |
---|
| 597 | { |
---|
| 598 | int xInt = (int)x / scaleX; x -= (float)xInt*scaleX; xInt = heightMap->h - xInt; |
---|
[6267] | 599 | int yInt = (int)y / scaleZ; y -= (float)yInt*scaleZ; |
---|
[6227] | 600 | if(xInt <= 0 || xInt >= heightMap->h || yInt <= 0 || yInt >= heightMap->w ) return 0.0f; |
---|
| 601 | float height = heights[yInt + (xInt)*heightMap->w]*scaleY; |
---|
| 602 | float a = normalVectorField[(xInt)][yInt].x; |
---|
| 603 | float b = normalVectorField [(xInt)][yInt].z; |
---|
| 604 | float c = normalVectorField [(xInt)][yInt].y; |
---|
| 605 | height -= ( (a/c)*(x) + (b/c)*(y))*scaleY; |
---|
| 606 | return height; |
---|
| 607 | } |
---|