Skip to content

Commit 65fe9b2

Browse files
author
Cheng Xie
committed
height function for terrain
1 parent 2ef6743 commit 65fe9b2

7 files changed

+72
-17
lines changed

src/camera.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Camera::Camera()
1010
heightAngle_ = 60;
1111

1212
updateProjection();
13-
lookAt({3, 3, 3}, {0, 0, 0}, {0, 1, 0});
13+
lookAt({-100, 20, 100}, {0, 0, 0}, {0, 1, 0});
1414
}
1515

1616
void Camera::apply(DrawContext &context)

src/meshutility.cpp

-2
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,6 @@ Mesh *MeshUtility::createPlane(int tessellationLevel, float size)
9898

9999
vec3 dy = (upperleft - lowerleft) / tessellationLevel;
100100

101-
cout << dy << endl;
102-
103101
float width = (lowerright - lowerleft).length();
104102
float height = (upperleft - lowerleft).length();
105103

src/scene.cpp

+10-7
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,13 @@ void Scene::initialize()
4141
material1->setShiness(100);
4242

4343
// Textures are owned by cache
44-
Texture* diffuseMap = TextureCache::getInstance()->acquire("frostmourne", TextureType::Texture2D);
44+
Texture* diffuseMap = TextureCache::getInstance()->acquire("cheese", TextureType::Texture2D);
4545
material1->setDiffuseMap(diffuseMap);
4646

4747
SceneObject* obj1 = new SceneObject;
48-
Mesh* mesh1 = MeshCache::getInstance()->acquire("frostmourne");
48+
Mesh* mesh1 = MeshCache::getInstance()->acquire("cube");
4949
obj1->setMesh(mesh1);
5050
obj1->setMaterial(material1);
51-
52-
obj1->transform().translate(0, 50, 0);
53-
obj1->transform().scale(20, 20, 20);
5451

5552
sceneObjects_.push_back(obj1);
5653

@@ -66,11 +63,17 @@ void Scene::initialize()
6663

6764
Terrain* terrain = new Terrain({0, 0, 0}, size);
6865
sceneObjects_.push_back(terrain);
66+
67+
float x = -100;
68+
float z = 100;
69+
float y = terrain->height(x, z);
70+
71+
obj1->transform().translate(x, y, z);
6972

7073
// Get a tornado in here!
7174
vec3 tornadoStart;
7275
tornadoStart.setX(0.0);
73-
tornadoStart.setY(50);
76+
tornadoStart.setY(0);
7477
tornadoStart.setZ(0.0);
7578
tornado_ = new Tornado(tornadoStart);
7679
tornadoStart.setX(8.0);
@@ -88,7 +91,7 @@ void Scene::render(DrawContext &context)
8891
context.sceneObjects = &sceneObjects_;
8992
context.scene = this;
9093

91-
//skybox_->render(context);
94+
skybox_->render(context);
9295

9396
for (SceneObject* object : sceneObjects_) {
9497
object->render(context);

src/sceneobject.h

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ class SceneObject
1515

1616
const Material *material() const;
1717
void setMaterial(Material *material);
18-
Material* getMaterial(){return material_;}
1918

2019
mat4& transform();
2120
const mat4& transform() const;

src/terrain.cpp

+26-3
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,16 @@
33
#include "terrainmaterial.h"
44
#include "mesh.h"
55
#include "texturecache.h"
6+
#include "texture.h"
67

7-
Terrain::Terrain(const vec3& center, int size)
8+
Terrain::Terrain(const vec3& center, float size)
89
{
9-
mesh_ = MeshUtility::createPlane(200, size / 2);
10+
size_ = size;
11+
center_ = center;
12+
13+
heightScale_ = 20;
14+
15+
mesh_ = MeshUtility::createPlane(200, size);
1016

1117
TerrainMaterial* material = new TerrainMaterial;
1218
material->setAmbient({0.4, 0.4, 0.4});
@@ -17,7 +23,9 @@ Terrain::Terrain(const vec3& center, int size)
1723
material->setHeightMap(heightMap);
1824
material->setDiffuseMap(diffuseMap);
1925

20-
material->setHeightScale(10);
26+
heightMap_ = heightMap;
27+
28+
material->setHeightScale(heightScale_);
2129

2230
setMaterial(material);
2331

@@ -28,3 +36,18 @@ Terrain::~Terrain()
2836
{
2937
delete mesh_;
3038
}
39+
40+
float Terrain::height(float x, float z)
41+
{
42+
assert(heightMap_);
43+
44+
vec3 uvw;
45+
46+
float width = size_ * 2;
47+
48+
uvw.setX((x + size_) / width);
49+
uvw.setY((z + size_) / width);
50+
uvw.setZ(0);
51+
52+
return heightMap_->sample(uvw).x() * heightScale_;
53+
}

src/terrain.h

+15-1
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,22 @@
66
class Terrain : public SceneObject
77
{
88
public:
9-
Terrain(const vec3& center, int size);
9+
Terrain(const vec3& center, float size);
1010
virtual ~Terrain();
11+
12+
float height(float x, float z);
13+
14+
private:
15+
// half width of terrain
16+
float size_ = 10;
17+
18+
// weak reference
19+
Texture* heightMap_ = NULL;
20+
21+
// TODO temp
22+
vec3 center_;
23+
24+
float heightScale_ = 1;
1125
};
1226

1327
#endif // TERRAIN_H

src/texture2d.cpp

+20-2
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,26 @@ void Texture2D::load(string filename)
5454

5555
vec4 Texture2D::sample(const vec3 &uvw)
5656
{
57-
cout << "1" << endl;
58-
return vec4();
57+
struct Color32 {
58+
uint8_t r;
59+
uint8_t g;
60+
uint8_t b;
61+
uint8_t a;
62+
};
63+
64+
float u = uvw.x();
65+
float v = uvw.y();
66+
67+
int x = (int)(u * image_->width()) % image_->width();
68+
int y = image_->height() - (int)(v * image_->height()) % image_->height() - 1;
69+
70+
Color32 *data = (Color32 *)image_->bits();
71+
72+
assert(x < image_->width() && x >= 0);
73+
assert(y < image_->height() && y >= 0);
74+
75+
Color32 color = data[y * image_->width() + x];
76+
return vec4(color.r / 255.f, color.g / 255.f, color.b / 255.f, color.a / 255.f);
5977
}
6078

6179
void Texture2D::apply(DrawContext &context, string name, int binding) const

0 commit comments

Comments
 (0)