[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: |
---|
| 12 | main-programmer: |
---|
| 13 | */ |
---|
| 14 | |
---|
| 15 | #include "height_map.h" |
---|
| 16 | #include "model.h" |
---|
[5967] | 17 | #include "texture.h" |
---|
| 18 | #include "vector.h" |
---|
[5942] | 19 | |
---|
[5967] | 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 | |
---|
[5942] | 29 | HeightMap::HeightMap() : Model() |
---|
| 30 | { |
---|
[5967] | 31 | |
---|
[5942] | 32 | } |
---|
| 33 | |
---|
[5967] | 34 | HeightMap::HeightMap(const char* height_map_name = NULL) : Model() |
---|
| 35 | { |
---|
[5976] | 36 | this->setClassID(CL_HEIGHT_MAP, "HeightMap"); |
---|
[5967] | 37 | heightMap = IMG_Load(height_map_name); |
---|
| 38 | if(heightMap!=NULL) { |
---|
| 39 | PRINTF(0)("loading Image %s\n", height_map_name); |
---|
| 40 | PRINTF(0)("width : %i\n", heightMap->w); |
---|
| 41 | PRINTF(0)("hight : %i\n", heightMap->h); |
---|
| 42 | PRINTF(0)("%i Byte(s) per Pixel \n", heightMap->format->BytesPerPixel); |
---|
| 43 | |
---|
| 44 | |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | else PRINTF(4)("oops! couldn't load %s for some reason.\n", height_map_name); |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | |
---|
| 51 | HeightMap::~HeightMap() |
---|
| 52 | { |
---|
| 53 | delete heightMap; |
---|
| 54 | } |
---|
| 55 | |
---|
[5942] | 56 | void HeightMap::load() |
---|
| 57 | { |
---|
[5976] | 58 | long height = 0; |
---|
[5967] | 59 | int offset = 0; |
---|
| 60 | char * bmp = (char*) heightMap->pixels; |
---|
| 61 | |
---|
| 62 | if(heightMap != NULL /* && heightMap->format->BitsPerPixel == 8 */) |
---|
| 63 | { |
---|
[5980] | 64 | SDL_LockSurface(heightMap); |
---|
[5989] | 65 | for(int i = 0 ; i < heightMap->h ; i +=1) |
---|
[5967] | 66 | { |
---|
[5980] | 67 | for(int j = 0; j < heightMap->w ; j += 1) |
---|
[5967] | 68 | { |
---|
| 69 | |
---|
| 70 | // get local hight from heightMap |
---|
| 71 | |
---|
| 72 | |
---|
[5976] | 73 | |
---|
[5980] | 74 | offset = bmp[j + i*(heightMap->w )]; |
---|
| 75 | height = (unsigned int)heightMap->format->palette->colors[offset].r + |
---|
| 76 | (unsigned int)heightMap->format->palette->colors[offset].g + |
---|
| 77 | (unsigned int) heightMap->format->palette->colors[offset].b ; |
---|
[5976] | 78 | |
---|
[5980] | 79 | |
---|
[5989] | 80 | this->addVertex( 20*(heightMap->h - i) , (( height)/15) - 30 ,20*j); |
---|
[5967] | 81 | } |
---|
| 82 | } |
---|
[5976] | 83 | SDL_UnlockSurface(heightMap); |
---|
[5980] | 84 | |
---|
[5989] | 85 | int c = (heightMap->w) ; |
---|
[5980] | 86 | int g = 0; |
---|
| 87 | |
---|
[5989] | 88 | for(int i = 0; i < (heightMap->w) -1 ; i += 1) |
---|
[5967] | 89 | { |
---|
[5980] | 90 | for(int j = 0; j < (heightMap->h)/1 - 1; j++) |
---|
| 91 | { |
---|
| 92 | //this->addFace (3, VERTEX_ONLY,j+1+i*c ,j + (i+1)*c, j + i*c ); |
---|
[5967] | 93 | |
---|
[5980] | 94 | //this->addFace (3, VERTEX_ONLY,j + (i+1)*c +1 ,j + (i+1)*c,j+i*c +1 ); |
---|
| 95 | this->addFace (4 ,VERTEX_ONLY,j+i*c,j+(i+1)*c ,j + (i+1)*c +1, j +i*c+1 ); |
---|
| 96 | } |
---|
[5976] | 97 | |
---|
[5980] | 98 | |
---|
| 99 | |
---|
[5967] | 100 | } |
---|
| 101 | |
---|
| 102 | |
---|
| 103 | } |
---|
| 104 | else |
---|
| 105 | { |
---|
| 106 | //this->setName("HardCore"); |
---|
[5942] | 107 | this->addVertex (-0.5, -0.5, 0.5); |
---|
| 108 | this->addVertex (0.5, -0.5, 0.5); |
---|
| 109 | this->addVertex (-0.5, 0.5, 0.5); |
---|
| 110 | this->addVertex (0.5, 0.5, 0.5); |
---|
| 111 | this->addVertex (-0.5, 0.5, -0.5); |
---|
| 112 | this->addVertex (0.5, 0.5, -0.5); |
---|
| 113 | this->addVertex (-0.5, -0.5, -0.5); |
---|
| 114 | this->addVertex (0.5, -0.5, -0.5); |
---|
[5967] | 115 | |
---|
[5942] | 116 | |
---|
| 117 | |
---|
| 118 | |
---|
[5967] | 119 | this->addFace (3, VERTEX_ONLY, 4, 3, 2); |
---|
[5942] | 120 | |
---|
[5967] | 121 | } |
---|
[5942] | 122 | this->finalize(); |
---|
| 123 | |
---|
| 124 | |
---|
| 125 | } |
---|