-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterrain.cpp
53 lines (37 loc) · 1.17 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
#include "terrain.h"
#include "meshutility.h"
#include "terrainmaterial.h"
#include "mesh.h"
#include "texturecache.h"
#include "texture.h"
Terrain::Terrain(const vec3& center, float size)
{
size_ = size;
center_ = center;
heightScale_ = 30;
mesh_ = MeshUtility::createPlane(200, size);
TerrainMaterial* material = new TerrainMaterial;
material->setAmbient({0.4, 0.4, 0.4});
Texture* heightMap = TextureCache::getInstance()->acquire("heightmap2", TextureType::Texture2D);
Texture* diffuseMap = TextureCache::getInstance()->acquire("terrain", TextureType::Texture2D);
material->setHeightMap(heightMap);
material->setDiffuseMap(diffuseMap);
heightMap_ = heightMap;
material->setHeightScale(heightScale_);
setMaterial(material);
transform().translate(center);
}
Terrain::~Terrain()
{
delete mesh_;
}
float Terrain::height(float x, float z)
{
assert(heightMap_);
vec3 uvw;
float width = size_ * 2;
uvw.setX(max(0.f, (x + size_) / width));
uvw.setY(max(0.f, (z + size_) / width));
uvw.setZ(0);
return heightMap_->sample(uvw).x() * heightScale_;
}