Skip to content

Commit 2ef6743

Browse files
author
Cheng Xie
committed
Seperated terrain shader from phong shader to make it more flexible
1 parent eaf94e6 commit 2ef6743

20 files changed

+235
-90
lines changed

src/final.pro

+4-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ SOURCES += main.cpp \
4444
orbitcameracontroller.cpp \
4545
fpscameracontroller.cpp \
4646
Tornado.cpp \
47-
TornadoParticleSystem.cpp
47+
TornadoParticleSystem.cpp \
48+
terrainmaterial.cpp
4849

4950
HEADERS += mainwindow.h \
5051
view.h \
@@ -78,6 +79,7 @@ HEADERS += mainwindow.h \
7879
orbitcameracontroller.h \
7980
fpscameracontroller.h \
8081
Tornado.h \
81-
TornadoParticleSystem.h
82+
TornadoParticleSystem.h \
83+
terrainmaterial.h
8284

8385
FORMS += mainwindow.ui

src/fpscameracontroller.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#include <QWheelEvent>
55
#include "camera.h"
66

7+
static const float rotate_factor = 0.3;
8+
79
FPSCameraController::FPSCameraController(Camera *camera) : CameraController(camera)
810
{
911

@@ -32,11 +34,11 @@ void FPSCameraController::mouseMoveEvent(QMouseEvent *event)
3234
transform = mat * transform;
3335

3436
mat.setToIdentity();
35-
mat.rotate(-delta.x() * 0.5, {0, 1, 0});
37+
mat.rotate(-delta.x() * rotate_factor, {0, 1, 0});
3638
transform = mat * transform;
3739

3840
mat.setToIdentity();
39-
mat.rotate(-delta.y() * 0.5, camera_->right());
41+
mat.rotate(-delta.y() * rotate_factor, camera_->right());
4042
transform = mat * transform;
4143

4244
mat.setToIdentity();

src/fpscameracontroller.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class FPSCameraController : public CameraController
2424
bool dPressed_ = false;
2525
bool rbuttonDown_ = false;
2626
QPoint oldMousePos_;
27-
float speed_ = 50.f;
27+
float speed_ = 10.f;
2828
};
2929

3030
#endif // FPSCAMERACONTROLLER_H

src/meshutility.cpp

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

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

101+
cout << dy << endl;
102+
101103
float width = (lowerright - lowerleft).length();
102104
float height = (upperleft - lowerleft).length();
103105

@@ -152,6 +154,7 @@ void MeshUtility::tessellate(vec3 p1, vec3 p2, int level, vector<vec3> &result)
152154
assert(level > 0);
153155

154156
auto delta = (p2 - p1) / level;
157+
155158
for (int i = 0; i <= level; i++)
156159
{
157160
result.push_back(p1 + delta * i);

src/phongmaterial.cpp

+1-16
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,6 @@ void PhongMaterial::apply(DrawContext &context)
3232
else {
3333
shader_->setUniformValue("useDiffuseMap", false);
3434
}
35-
36-
// TODO
37-
if (heightMap_) {
38-
shader_->setUniformValue("useHeightMap", true);
39-
heightMap_->apply(context, "heightMap", 1);
40-
}
41-
else {
42-
shader_->setUniformValue("useHeightMap", false);
43-
}
44-
4535
}
4636

4737
const Color& PhongMaterial::ambient() const
@@ -72,6 +62,7 @@ void PhongMaterial::setSpecular(const Color &specular)
7262
{
7363
specular_ = specular;
7464
}
65+
7566
float PhongMaterial::shiness() const
7667
{
7768
return shiness_;
@@ -86,9 +77,3 @@ void PhongMaterial::setDiffuseMap(Texture *diffuseMap)
8677
{
8778
diffuseMap_ = diffuseMap;
8879
}
89-
90-
void PhongMaterial::setHeightMap(Texture *heightMap)
91-
{
92-
heightMap_ = heightMap;
93-
}
94-

src/phongmaterial.h

-8
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,6 @@ class PhongMaterial : public Material
2424
void setShiness(float shiness);
2525

2626
void setDiffuseMap(Texture *diffuseMap);
27-
Texture* getDiffuseMap(){return diffuseMap_;}
28-
29-
// TODO
30-
void setHeightMap(Texture *heightMap);
31-
Texture* getHeightMap(){return heightMap_;}
3227

3328
private:
3429
Color ambient_;
@@ -37,9 +32,6 @@ class PhongMaterial : public Material
3732
float shiness_ = 0;
3833

3934
Texture* diffuseMap_ = NULL;
40-
41-
// TODO
42-
Texture* heightMap_ = NULL;
4335
};
4436

4537
#endif // PHONGMATERIAL_H

src/scene.cpp

+6-30
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ void Scene::initialize()
3838
material1->setAmbient({0.2, 0.2, 0.2});
3939
material1->setDiffuse({0.7, 0.7, 0.7});
4040
material1->setSpecular({0.7, 0.7, 0.7});
41-
material1->setShiness(50);
41+
material1->setShiness(100);
4242

4343
// Textures are owned by cache
4444
Texture* diffuseMap = TextureCache::getInstance()->acquire("frostmourne", TextureType::Texture2D);
@@ -49,28 +49,10 @@ void Scene::initialize()
4949
obj1->setMesh(mesh1);
5050
obj1->setMaterial(material1);
5151

52-
obj1->transform().translate(20, 20, 0);
52+
obj1->transform().translate(0, 50, 0);
5353
obj1->transform().scale(20, 20, 20);
5454

55-
sceneObjects_.push_back(obj1);
56-
57-
//
58-
PhongMaterial* material2 = new PhongMaterial;
59-
material2->setAmbient({0.2, 0.2, 0.2});
60-
material2->setDiffuse({0.7, 0.7, 0.7});
61-
material2->setSpecular({0.7, 0.7, 0.7});
62-
material2->setShiness(50);
63-
64-
SceneObject *obj2 = new SceneObject;
65-
Mesh* mesh2 = MeshCache::getInstance()->acquire("bunny");
66-
obj2->setMesh(mesh2);
67-
68-
obj2->setMaterial(material2);
69-
70-
obj2->transform().translate(-20, 0, 0);
71-
obj2->transform().scale(20, 20, 20);
72-
73-
sceneObjects_.push_back(obj2);
55+
sceneObjects_.push_back(obj1);
7456

7557
// Lights owned by the scene
7658
DirectLight* light = new DirectLight({-1, -0.5, -1}, {1.0, 1.0, 1.0});
@@ -80,16 +62,10 @@ void Scene::initialize()
8062
skybox_ = new Skybox("cloudy");
8163

8264
// Terrain
83-
int size = 800;
65+
int size = 200;
8466

85-
Terrain* terrain = new Terrain({0, 0, 0}, size+2);
67+
Terrain* terrain = new Terrain({0, 0, 0}, size);
8668
sceneObjects_.push_back(terrain);
87-
88-
// terrain = new Terrain({size, 0, size}, size+2);
89-
// sceneObjects_.push_back(terrain);
90-
91-
// terrain = new Terrain({size, 0, 0}, size+2);
92-
// sceneObjects_.push_back(terrain);
9369

9470
// Get a tornado in here!
9571
vec3 tornadoStart;
@@ -112,7 +88,7 @@ void Scene::render(DrawContext &context)
11288
context.sceneObjects = &sceneObjects_;
11389
context.scene = this;
11490

115-
skybox_->render(context);
91+
//skybox_->render(context);
11692

11793
for (SceneObject* object : sceneObjects_) {
11894
object->render(context);

src/shaders/phong.fs

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ void main()
3434
vec3 Li = lightColor * cos_factor;
3535

3636
vec3 color = Li * brdf_phong;
37-
color += ambient;
37+
color += ambient * brdf_diffuse;
3838

3939
fragColor = vec4(color, 1);
4040
}

src/shaders/phong.vs

-19
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ layout (location = 2) in vec2 uvL;
77
uniform mat4 world;
88
uniform mat4 worldViewProjection;
99

10-
uniform bool useHeightMap;
11-
uniform sampler2D heightMap;
12-
1310
out vec3 normalW;
1411
out vec3 positionW;
1512
out vec2 uv;
@@ -19,22 +16,6 @@ void main()
1916
vec3 position = positionL;
2017
vec3 normal = normalL;
2118

22-
if (useHeightMap) {
23-
float height = texture(heightMap, uvL).x;
24-
position += normalL * height * 50;
25-
26-
float turbulence = 0.1;
27-
const ivec3 off = ivec3(-1, 0, 1);
28-
29-
float h_pos_u = textureOffset(heightMap, uvL, off.zy).x;
30-
float h_neg_u = textureOffset(heightMap, uvL, off.xy).x;
31-
float h_pos_v = textureOffset(heightMap, uvL, off.yz).x;
32-
float h_neg_v = textureOffset(heightMap, uvL, off.yx).x;
33-
vec3 du = normalize(vec3(turbulence, h_pos_u - h_neg_u, 0));
34-
vec3 dv = normalize(vec3(0, h_pos_v - h_neg_v, -turbulence));
35-
normal = cross(du, dv);
36-
}
37-
3819
positionW = (world * vec4(position, 1)).xyz;
3920
normalW = (world * vec4(normal, 0)).xyz;
4021
uv = uvL;

src/shaders/terrain.fs

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#version 330
2+
3+
in vec3 normalW;
4+
in vec3 positionW;
5+
in vec2 uv;
6+
7+
uniform vec3 lightDir;
8+
uniform vec3 lightColor;
9+
10+
uniform vec3 ambient;
11+
12+
uniform sampler2D diffuseMap;
13+
14+
out vec4 fragColor;
15+
16+
void main()
17+
{
18+
vec3 n = normalize(normalW);
19+
vec3 l = normalize(-lightDir);
20+
21+
vec3 brdf_diffuse = texture(diffuseMap, uv * 15).xyz;
22+
23+
float cos_factor = max(0, dot(l, n));
24+
vec3 Li = lightColor * cos_factor;
25+
26+
vec3 color = Li * brdf_diffuse;
27+
color += ambient * brdf_diffuse;
28+
29+
fragColor = vec4(color, 1);
30+
}

src/shaders/terrain.vs

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#version 330
2+
3+
layout (location = 0) in vec3 positionL;
4+
layout (location = 2) in vec2 uvL;
5+
6+
uniform mat4 world;
7+
uniform mat4 worldViewProjection;
8+
9+
uniform float heightScale;
10+
11+
uniform sampler2D heightMap;
12+
13+
out vec3 normalW;
14+
out vec3 positionW;
15+
out vec2 uv;
16+
17+
void main()
18+
{
19+
vec3 position = positionL;
20+
vec3 normal = vec3(0, 1, 0);
21+
22+
float height = texture(heightMap, uvL).x;
23+
position += normal * height * heightScale;
24+
25+
float turbulence = 0.1;
26+
const ivec3 off = ivec3(-1, 0, 1);
27+
28+
float h_pos_u = textureOffset(heightMap, uvL, off.zy).x;
29+
float h_neg_u = textureOffset(heightMap, uvL, off.xy).x;
30+
float h_pos_v = textureOffset(heightMap, uvL, off.yz).x;
31+
float h_neg_v = textureOffset(heightMap, uvL, off.yx).x;
32+
vec3 du = normalize(vec3(turbulence, h_pos_u - h_neg_u, 0));
33+
vec3 dv = normalize(vec3(0, h_pos_v - h_neg_v, -turbulence));
34+
normal = cross(du, dv);
35+
36+
positionW = (world * vec4(position, 1)).xyz;
37+
normalW = (world * vec4(normal, 0)).xyz;
38+
uv = uvL;
39+
40+
gl_Position = worldViewProjection * vec4(position, 1);
41+
}

src/terrain.cpp

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
#include "terrain.h"
22
#include "meshutility.h"
3-
#include "phongmaterial.h"
3+
#include "terrainmaterial.h"
44
#include "mesh.h"
55
#include "texturecache.h"
66

77
Terrain::Terrain(const vec3& center, int size)
88
{
99
mesh_ = MeshUtility::createPlane(200, size / 2);
1010

11-
PhongMaterial* material = new PhongMaterial;
12-
material->setAmbient({0.2, 0.2, 0.2});
13-
material->setDiffuse({0.7, 0.7, 0.2});
11+
TerrainMaterial* material = new TerrainMaterial;
12+
material->setAmbient({0.4, 0.4, 0.4});
1413

15-
Texture* heightMap = TextureCache::getInstance()->acquire("heightmap3", TextureType::Texture2D);
14+
Texture* heightMap = TextureCache::getInstance()->acquire("heightmap2", TextureType::Texture2D);
1615
Texture* diffuseMap = TextureCache::getInstance()->acquire("terrain", TextureType::Texture2D);
1716

1817
material->setHeightMap(heightMap);
1918
material->setDiffuseMap(diffuseMap);
2019

20+
material->setHeightScale(10);
21+
2122
setMaterial(material);
2223

2324
transform().translate(center);

0 commit comments

Comments
 (0)