-
Notifications
You must be signed in to change notification settings - Fork 0
/
Terrain.cpp
55 lines (38 loc) · 1.18 KB
/
Terrain.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
51
52
53
54
55
#include "Terrain.h"
//#include <stdlib.h>
//#include "System.h"
#include <mastdlib.h>
using namespace MAUtil;
void Terrain::init(MAHandle heightMap) {
int imageWidth = EXTENT_X(maGetImageSize(heightMap));
int imageHeight = EXTENT_Y(maGetImageSize(heightMap));
int *data = new int[imageWidth*imageHeight];
mHeightMap = heightMap;
MARect srcRect = {0,0,imageWidth,imageHeight};
maGetImageData(heightMap, data, &srcRect, imageWidth);
MAUtil::Vector<TriangleStrip> strips;
for(int j = 0; j < imageHeight-1; j++)
{
strips.add(TriangleStrip());
TriangleStrip& strip = strips[strips.size()-1];
for(int i = 0; i < imageWidth; i++)
{
float h1 = (float)(data[i+j*imageWidth]>>24)/255.0;
float h2 = (float)(data[i+(j+1)*imageWidth]>>24)/255.0;
float u = (float)i/(float)imageWidth;
float v1 = (float)j/(float)imageHeight;
float v2 = (float)(j+1)/(float)imageHeight;
vec3 pos_a = vec3(u, h1, v1);
vec3 pos_b = vec3(u, h2, v2);
strip.vertices.add(pos_a);
strip.vertices.add(pos_b);
strip.textureCoordinates.add(vec2(u, v1));
strip.textureCoordinates.add(vec2(u, v2));
}
}
delete data;
initVBO(strips);
}
void Terrain::render() {
renderVBO();
}