1 | |
---|
2 | |
---|
3 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_HEIGHTMAP |
---|
4 | |
---|
5 | |
---|
6 | #include "heightmap.h" |
---|
7 | |
---|
8 | #include <stdio.h> |
---|
9 | #include <string.h> |
---|
10 | #include <stdlib.h> |
---|
11 | |
---|
12 | #include "debug.h" |
---|
13 | #include "compiler.h" |
---|
14 | |
---|
15 | |
---|
16 | Heightmap::Heightmap(const char* fileName, float scaling, int displaylistResolution, int vertexResolution) |
---|
17 | { |
---|
18 | this->scaleFactor = scaling; |
---|
19 | this->setName(fileName); |
---|
20 | |
---|
21 | this->import(fileName, displaylistResolution, vertexResolution); |
---|
22 | |
---|
23 | this->finalize(); |
---|
24 | } |
---|
25 | |
---|
26 | Heightmap::~Heightmap () |
---|
27 | { |
---|
28 | } |
---|
29 | |
---|
30 | bool Heightmap::import (const char* fileName, int displaylistResolution, int vertexResolution) |
---|
31 | { |
---|
32 | PRINTF(4)("preparing to read in file: %s\n", fileName); |
---|
33 | |
---|
34 | // using SDL-image to load the bitmap |
---|
35 | this->bitmap = IMG_Load(fileName); |
---|
36 | |
---|
37 | // how many pixels wide is a display list? |
---|
38 | int pixPerDLSide = displaylistResolution * vertexResolution; |
---|
39 | |
---|
40 | int howManyListsWide = bitmap->w / pixPerDLSide + 1; |
---|
41 | int howManyListsHigh = bitmap->h / pixPerDLSide + 1; |
---|
42 | |
---|
43 | // calculated pixel dimensions of one display list. usually equals pixPerDLSide, but at the end of the picture |
---|
44 | // this needs to be shorter, that we do not make a segfault |
---|
45 | int width = pixPerDLSide; |
---|
46 | int height = pixPerDLSide; |
---|
47 | |
---|
48 | for (int i=0; i<howManyListsWide; i++) |
---|
49 | { |
---|
50 | for (int j=0; j<howManyListsHigh; j++) |
---|
51 | { |
---|
52 | // catch out of image access in x-direction |
---|
53 | if ( (j+1)*pixPerDLSide - 1 >= bitmap->w ) width = bitmap->w - j*pixPerDLSide; |
---|
54 | else width = pixPerDLSide; |
---|
55 | // y-direction |
---|
56 | if ( (i+1)*pixPerDLSide - 1 >= bitmap->h ) height = bitmap->h - i*pixPerDLSide; |
---|
57 | else height = pixPerDLSide; |
---|
58 | |
---|
59 | |
---|
60 | |
---|
61 | this->readIntoGroup(j*pixPerDLSide,i*pixPerDLSide,width,height,vertexResolution); |
---|
62 | } |
---|
63 | } |
---|
64 | |
---|
65 | return true; |
---|
66 | } |
---|
67 | |
---|
68 | void Heightmap::readIntoGroup(int left, int top,int width, int height, int vertexResolution) |
---|
69 | { |
---|
70 | // add all vertices first |
---|
71 | for (int z=top; z < top+height; z++) |
---|
72 | { |
---|
73 | for (int x=left; x < left+width; x++) |
---|
74 | { |
---|
75 | this->addVertex(x,this->getHeightAt(x,z),z); |
---|
76 | } |
---|
77 | } |
---|
78 | |
---|
79 | |
---|
80 | // code readability |
---|
81 | int thisVertex = 0; |
---|
82 | int bottomVertex = width; |
---|
83 | |
---|
84 | // create all faces |
---|
85 | for (int z=top; z < top+height - 1; z++) |
---|
86 | { |
---|
87 | for (int x=left; x < left+width - 1; x++) |
---|
88 | { |
---|
89 | thisVertex++; |
---|
90 | bottomVertex++; |
---|
91 | |
---|
92 | this->addFace(3, VERTEX_ONLY, thisVertex, bottomVertex, thisVertex+1); |
---|
93 | this->addFace(3, VERTEX_ONLY, thisVertex+1, bottomVertex, bottomVertex+1); |
---|
94 | |
---|
95 | } |
---|
96 | } |
---|
97 | |
---|
98 | |
---|
99 | |
---|
100 | } |
---|
101 | |
---|
102 | float Heightmap::getHeightAt(int x, int z) |
---|
103 | { |
---|
104 | Uint8 index; |
---|
105 | SDL_Color color; |
---|
106 | |
---|
107 | SDL_LockSurface(bitmap); |
---|
108 | |
---|
109 | index = *((Uint8*)bitmap->pixels + z * bitmap->pitch + x * bitmap->format->BytesPerPixel); |
---|
110 | color = bitmap->format->palette->colors[index]; |
---|
111 | |
---|
112 | SDL_UnlockSurface(bitmap); |
---|
113 | |
---|
114 | // return the red component, because in a grayscale pic, r,g and b are all the same value |
---|
115 | return (GLint)color.r; |
---|
116 | } |
---|