| 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: |
|---|
| 12 | main-programmer: |
|---|
| 13 | */ |
|---|
| 14 | |
|---|
| 15 | #include "height_map.h" |
|---|
| 16 | #include "model.h" |
|---|
| 17 | #include "texture.h" |
|---|
| 18 | #include "vector.h" |
|---|
| 19 | |
|---|
| 20 | #include "debug.h" |
|---|
| 21 | |
|---|
| 22 | // INCLUDING SDL_Image |
|---|
| 23 | #ifdef HAVE_SDL_IMAGE_H |
|---|
| 24 | #include <SDL_image.h> |
|---|
| 25 | #else |
|---|
| 26 | #include <SDL/SDL_image.h> |
|---|
| 27 | #endif |
|---|
| 28 | |
|---|
| 29 | HeightMap::HeightMap() : StaticModel() |
|---|
| 30 | { |
|---|
| 31 | |
|---|
| 32 | } |
|---|
| 33 | |
|---|
| 34 | HeightMap::HeightMap(const char* height_map_name = NULL) : StaticModel() |
|---|
| 35 | { |
|---|
| 36 | this->setClassID(CL_HEIGHT_MAP, "HeightMap"); |
|---|
| 37 | heightMap = IMG_Load(height_map_name); |
|---|
| 38 | if(heightMap!=NULL) { |
|---|
| 39 | |
|---|
| 40 | PRINTF(0)("loading Image %s\n", height_map_name); |
|---|
| 41 | PRINTF(0)("width : %i\n", heightMap->w); |
|---|
| 42 | PRINTF(0)("hight : %i\n", heightMap->h); |
|---|
| 43 | PRINTF(0)("%i Byte(s) per Pixel \n", heightMap->format->BytesPerPixel); |
|---|
| 44 | PRINTF(0)("Rshift : %i\n", heightMap->format->Rshift); |
|---|
| 45 | PRINTF(0)("Bshift: %i\n", heightMap->format->Bshift); |
|---|
| 46 | PRINTF(0)("Gshift: %i\n", heightMap->format->Gshift); |
|---|
| 47 | PRINTF(0)("Rmask: %i\n", heightMap->format->Rmask); |
|---|
| 48 | PRINTF(0)("Gmask: %i\n", heightMap->format->Gmask); |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | else PRINTF(4)("oops! couldn't load %s for some reason.\n", height_map_name); |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | HeightMap::~HeightMap() |
|---|
| 56 | { |
|---|
| 57 | delete heightMap; |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | void HeightMap::load() |
|---|
| 61 | { |
|---|
| 62 | unsigned char height = 0; |
|---|
| 63 | int offset = 0; |
|---|
| 64 | |
|---|
| 65 | char * bmp = (char*) heightMap->pixels; |
|---|
| 66 | |
|---|
| 67 | if(heightMap != NULL && heightMap->format->BitsPerPixel == 8 ) |
|---|
| 68 | { |
|---|
| 69 | SDL_LockSurface(heightMap); |
|---|
| 70 | for(int i = 0 ; i < heightMap->h ; i +=2) |
|---|
| 71 | { |
|---|
| 72 | for(int j = 0; j < heightMap->w ; j += 2) |
|---|
| 73 | { |
|---|
| 74 | // get local hight from heightMap |
|---|
| 75 | // This seems to work only on 8-Bit Grayscale-Bitmaps |
|---|
| 76 | height = bmp[j + i*(heightMap->w )]; |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | /*height = heightMap->format->palette->colors[offset].r + |
|---|
| 80 | heightMap->format->palette->colors[offset].g + |
|---|
| 81 | heightMap->format->palette->colors[offset].b ; */ |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | this->addVertex( 20*(heightMap->h - i) , (( (double)height)/0.3)-1200 ,20*j); |
|---|
| 85 | } |
|---|
| 86 | } |
|---|
| 87 | SDL_UnlockSurface(heightMap); |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | int c = (heightMap->w)/2 ; // One line |
|---|
| 92 | for(int i = 0; i < (heightMap->w)/2 -2 ; i ++) |
|---|
| 93 | { |
|---|
| 94 | for(int j = 0; j < (heightMap->h)/2 - 2; j++) |
|---|
| 95 | { |
|---|
| 96 | |
|---|
| 97 | /* Two Triangles or ...*/ |
|---|
| 98 | |
|---|
| 99 | //this->addFace (3, VERTEX_ONLY,j + (i+1)*c,j+1+i*c , j + i*c ); |
|---|
| 100 | //this->addFace (3, VERTEX_ONLY,j + (i+1)*c,j + (i+1)*c +1 ,j+i*c +1 ); |
|---|
| 101 | |
|---|
| 102 | /* ... one square*/ |
|---|
| 103 | |
|---|
| 104 | this->addFace (4 ,VERTEX_ONLY,j+i*c,j+(i+1)*c ,j + (i+1)*c +1, j +i*c+1 ); |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | }//if |
|---|
| 113 | else |
|---|
| 114 | { |
|---|
| 115 | |
|---|
| 116 | //make a cube |
|---|
| 117 | this->setName("HardCore"); |
|---|
| 118 | this->addVertex (-0.5, -0.5, 0.5); |
|---|
| 119 | this->addVertex (0.5, -0.5, 0.5); |
|---|
| 120 | this->addVertex (-0.5, 0.5, 0.5); |
|---|
| 121 | this->addVertex (0.5, 0.5, 0.5); |
|---|
| 122 | this->addVertex (-0.5, 0.5, -0.5); |
|---|
| 123 | this->addVertex (0.5, 0.5, -0.5); |
|---|
| 124 | this->addVertex (-0.5, -0.5, -0.5); |
|---|
| 125 | this->addVertex (0.5, -0.5, -0.5); |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | |
|---|
| 130 | this->addFace (3, VERTEX_ONLY, 4, 3, 2); |
|---|
| 131 | |
|---|
| 132 | } |
|---|
| 133 | this->finalize(); |
|---|
| 134 | |
|---|
| 135 | |
|---|
| 136 | } |
|---|