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() : Model() |
---|
30 | { |
---|
31 | |
---|
32 | } |
---|
33 | |
---|
34 | HeightMap::HeightMap(const char* height_map_name = NULL) : Model() |
---|
35 | { |
---|
36 | this->setClassID(CL_HEIGHT_MAP, "HeightMap"); |
---|
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 | |
---|
56 | void HeightMap::load() |
---|
57 | { |
---|
58 | long height = 0; |
---|
59 | int offset = 0; |
---|
60 | char * bmp = (char*) heightMap->pixels; |
---|
61 | |
---|
62 | if(heightMap != NULL /* && heightMap->format->BitsPerPixel == 8 */) |
---|
63 | { |
---|
64 | SDL_LockSurface(heightMap); |
---|
65 | for(int i = 0; i < heightMap->h -1; i += 2) |
---|
66 | { |
---|
67 | for(int j = 0; j < heightMap->w -1; j += 2) |
---|
68 | { |
---|
69 | |
---|
70 | // get local hight from heightMap |
---|
71 | |
---|
72 | |
---|
73 | |
---|
74 | offset = bmp[j + i*heightMap->w]; |
---|
75 | height = heightMap->format->palette->colors[offset].r + heightMap->format->palette->colors[offset].g + heightMap->format->palette->colors[offset].b ; |
---|
76 | |
---|
77 | this->addVertex(heightMap->h - i , ((long)( height) / 30) - 60 ,heightMap->w - j); |
---|
78 | } |
---|
79 | } |
---|
80 | SDL_UnlockSurface(heightMap); |
---|
81 | for(int i = 0; i < (heightMap->h * heightMap->w)/4 -1 ; i ++) |
---|
82 | { |
---|
83 | this->addFace (3, VERTEX_ONLY, i,i+1 ,i + (heightMap->w / 2) ); |
---|
84 | |
---|
85 | this->addFace (3, VERTEX_ONLY,i + (heightMap->w / 2) ,i + (heightMap->w / 2) +1, i+1 ); |
---|
86 | |
---|
87 | |
---|
88 | //this->addFace (4 ,VERTEX_ONLY,i,i + (heightMap->w / 2) ,i + (heightMap->w / 2) +1, i+1 ); |
---|
89 | } |
---|
90 | |
---|
91 | |
---|
92 | } |
---|
93 | else |
---|
94 | { |
---|
95 | //this->setName("HardCore"); |
---|
96 | this->addVertex (-0.5, -0.5, 0.5); |
---|
97 | this->addVertex (0.5, -0.5, 0.5); |
---|
98 | this->addVertex (-0.5, 0.5, 0.5); |
---|
99 | this->addVertex (0.5, 0.5, 0.5); |
---|
100 | this->addVertex (-0.5, 0.5, -0.5); |
---|
101 | this->addVertex (0.5, 0.5, -0.5); |
---|
102 | this->addVertex (-0.5, -0.5, -0.5); |
---|
103 | this->addVertex (0.5, -0.5, -0.5); |
---|
104 | |
---|
105 | |
---|
106 | |
---|
107 | |
---|
108 | this->addFace (3, VERTEX_ONLY, 4, 3, 2); |
---|
109 | |
---|
110 | } |
---|
111 | this->finalize(); |
---|
112 | |
---|
113 | |
---|
114 | } |
---|