-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathheightmap.cpp
50 lines (40 loc) · 949 Bytes
/
heightmap.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "heightmap.h"
#include "../common/util.h"
HeightMap::HeightMap(float heightScale)
: heightScale(heightScale), w(0), h(0)
{
}
HeightMap::~HeightMap()
{
}
int HeightMap::getWidth() const
{
return w;
}
int HeightMap::getHeight() const
{
return h;
}
const std::vector<float>& HeightMap::getData() const
{
return data;
}
bool HeightMap::load(const char* fileName)
{
unsigned char* img = SOIL_load_image(fileName, &w, &h, NULL, SOIL_LOAD_RGB);
if(!img)
{
std::cerr << "Error loading heightmap " << fileName << ": " << SOIL_last_result() << std::endl;
return false;
}
data.resize(w * h);
for(int i = 0; i < w * h; i += 3)
{
// Average the R, G and B channels
int tile = img[i] + img[i + 1] + img[i + 3];
unsigned char greyTile = tile / 3;
data[i] = (float)greyTile / 255.0f * heightScale;
}
SOIL_free_image_data(img);
return true;
}