Skip to content

Commit 6525d49

Browse files
author
Cheng Xie
committed
Implemented fog for terrain and particles
1 parent 65fe9b2 commit 6525d49

18 files changed

+94
-21
lines changed

src/Material.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <GL/glew.h>
2+
#include "shader.h"
3+
#include "shadercache.h"
4+
#include "drawcontext.h"
5+
#include "scene.h"
6+
7+
void Material::apply(DrawContext &context)
8+
{
9+
shader_->bind();
10+
context.shader = shader_;
11+
12+
shader_->setUniformValue("fogColor", context.scene->fogColor());
13+
shader_->setUniformValue("fogDensity", context.scene->fogDensity());
14+
}

src/camera.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
Camera::Camera()
66
{
7-
near_ = 1;
8-
far_ = 10000;
7+
near_ = 0.1;
8+
far_ = 1000;
99
aspectRatio_ = 1;
1010
heightAngle_ = 60;
1111

@@ -18,6 +18,8 @@ void Camera::apply(DrawContext &context)
1818
context.shader->setUniformValue("view", viewMatrix());
1919
context.shader->setUniformValue("projection", projectionMatrix());
2020
context.shader->setUniformValue("eyePositionW", position());
21+
context.shader->setUniformValue("near", near());
22+
context.shader->setUniformValue("far", far());
2123
}
2224

2325
const mat4& Camera::projectionMatrix() const

src/final.pro

+2-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ SOURCES += main.cpp \
4545
fpscameracontroller.cpp \
4646
Tornado.cpp \
4747
TornadoParticleSystem.cpp \
48-
terrainmaterial.cpp
48+
terrainmaterial.cpp \
49+
material.cpp
4950

5051
HEADERS += mainwindow.h \
5152
view.h \

src/material.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class Material
88
{
99
public:
1010
virtual ~Material() {}
11-
virtual void apply(DrawContext& context) = 0;
11+
virtual void apply(DrawContext& context);
1212
virtual void endRender() {}
1313

1414
protected:

src/particlematerial.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@ ParticleMaterial::ParticleMaterial()
1111

1212
void ParticleMaterial::apply(DrawContext &context)
1313
{
14-
shader_->bind();
15-
context.shader = shader_;
16-
1714
assert(texture_);
1815

16+
Material::apply(context);
17+
1918
texture_->apply(context, "map", 0);
2019

2120
//glDisable(GL_DEPTH_TEST);

src/phongmaterial.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ PhongMaterial::~PhongMaterial()
1717

1818
void PhongMaterial::apply(DrawContext &context)
1919
{
20-
shader_->bind();
21-
context.shader = shader_;
20+
Material::apply(context);
2221

2322
shader_->setUniformValue("ambient", ambient_);
2423
shader_->setUniformValue("diffuse", diffuse_);

src/scene.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ Scene::~Scene()
3333

3434
void Scene::initialize()
3535
{
36+
setFogColor({0.45, 0.47, 0.5});
37+
setFogDensity(0.008);
38+
3639
// Materials are owned (and deleted) by SceneObject
3740
PhongMaterial* material1 = new PhongMaterial;
3841
material1->setAmbient({0.2, 0.2, 0.2});
@@ -98,4 +101,25 @@ void Scene::render(DrawContext &context)
98101
}
99102
}
100103

104+
const Color &Scene::fogColor() const
105+
{
106+
return fogColor_;
107+
}
108+
109+
void Scene::setFogColor(const Color &fogColor)
110+
{
111+
fogColor_ = fogColor;
112+
}
113+
float Scene::fogDensity() const
114+
{
115+
return fogDensity_;
116+
}
117+
118+
void Scene::setFogDensity(float fogDensity)
119+
{
120+
fogDensity_ = fogDensity;
121+
}
122+
123+
124+
101125

src/scene.h

+9
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,21 @@ class Scene
2020

2121
void render(DrawContext& context);
2222

23+
const Color& fogColor() const;
24+
void setFogColor(const Color &fogColor);
25+
26+
float fogDensity() const;
27+
void setFogDensity(float fogDensity);
28+
2329
private:
2430
SceneObjects sceneObjects_;
2531
Lights lights_;
2632

2733
Skybox* skybox_;
2834
Tornado* tornado_;
35+
36+
Color fogColor_;
37+
float fogDensity_;
2938
};
3039

3140
#endif // SCENE_H

src/shaders/particle.fs

+6-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@
22

33
uniform sampler2D map;
44

5+
uniform vec3 fogColor;
6+
57
in vec2 uv;
8+
in float fogFactor;
9+
610
out vec4 fragColor;
711

812
void main() {
9-
fragColor = texture(map, uv);
13+
vec4 color = texture(map, uv);
14+
fragColor = vec4(mix(color.xyz, fogColor, fogFactor), color.a);
1015
}

src/shaders/particle.gs

+6
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ uniform mat4 projection;
77

88
in float dim[];
99
in float rot[];
10+
in float fog_factor[];
1011

1112
out vec2 uv;
13+
out float fogFactor;
1214

1315
void main() {
1416
float size = dim[0];
@@ -23,21 +25,25 @@ void main() {
2325
vec4 offset = rotMat * vec4(-size, -size, 0.0, 0.0);
2426
gl_Position = projection * (offset + gl_in[0].gl_Position);
2527
uv = vec2(0.0, 0.0);
28+
fogFactor = fog_factor[0];
2629
EmitVertex();
2730

2831
offset = rotMat * vec4(size, -size, 0.0, 0.0);
2932
gl_Position = projection * (offset + gl_in[0].gl_Position);
3033
uv = vec2(1.0, 0.0);
34+
fogFactor = fog_factor[0];
3135
EmitVertex();
3236

3337
offset = rotMat * vec4(-size, size, 0.0, 0.0);
3438
gl_Position = projection * (offset + gl_in[0].gl_Position);
3539
uv = vec2(0.0, 1.0);
40+
fogFactor = fog_factor[0];
3641
EmitVertex();
3742

3843
offset = rotMat * vec4(size, size, 0.0, 0.0);
3944
gl_Position = projection * (offset + gl_in[0].gl_Position);
4045
uv = vec2(1.0, 1.0);
46+
fogFactor = fog_factor[0];
4147
EmitVertex();
4248

4349
EndPrimitive();

src/shaders/particle.vs

+7
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,17 @@ uniform mat4 worldView;
99
out float dim;
1010
out float rot;
1111

12+
uniform float fogDensity;
13+
14+
out float fog_factor;
15+
1216
void main() {
1317
dim = size;
1418
rot = rotation / 180 * 3.1415926;
1519
gl_Position = worldView * vec4(position, 1);
20+
21+
vec4 positionV = worldView * vec4(position, 1);
22+
fog_factor = 1 - 1 / pow(exp(-positionV.z * fogDensity), 2);
1623
}
1724

1825

src/shaders/terrain.fs

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
#version 330
22

33
in vec3 normalW;
4-
in vec3 positionW;
54
in vec2 uv;
5+
in float fogFactor;
6+
7+
uniform float near;
8+
uniform float far;
69

710
uniform vec3 lightDir;
811
uniform vec3 lightColor;
912

1013
uniform vec3 ambient;
1114

15+
uniform vec3 fogColor;
16+
1217
uniform sampler2D diffuseMap;
1318

1419
out vec4 fragColor;
@@ -25,6 +30,8 @@ void main()
2530

2631
vec3 color = Li * brdf_diffuse;
2732
color += ambient * brdf_diffuse;
33+
34+
color = mix(color, fogColor, fogFactor);
2835

2936
fragColor = vec4(color, 1);
3037
}

src/shaders/terrain.vs

+7-2
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,18 @@ layout (location = 0) in vec3 positionL;
44
layout (location = 2) in vec2 uvL;
55

66
uniform mat4 world;
7+
uniform mat4 worldView;
78
uniform mat4 worldViewProjection;
89

910
uniform float heightScale;
1011

1112
uniform sampler2D heightMap;
1213

14+
uniform float fogDensity;
15+
1316
out vec3 normalW;
14-
out vec3 positionW;
1517
out vec2 uv;
18+
out float fogFactor;
1619

1720
void main()
1821
{
@@ -33,9 +36,11 @@ void main()
3336
vec3 dv = normalize(vec3(0, h_pos_v - h_neg_v, -turbulence));
3437
normal = cross(du, dv);
3538

36-
positionW = (world * vec4(position, 1)).xyz;
3739
normalW = (world * vec4(normal, 0)).xyz;
3840
uv = uvL;
3941

4042
gl_Position = worldViewProjection * vec4(position, 1);
43+
44+
vec4 positionV = worldView * vec4(position, 1);
45+
fogFactor = 1 - 1 / pow(exp(-positionV.z * fogDensity), 2);
4146
}

src/skybox.cpp

+1-4
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,5 @@ Skybox::Skybox(string name)
1515
material->setTexture(texture);
1616
setMaterial(material);
1717

18-
transform().translate(0, -50, 0);
19-
transform().scale(1000);
20-
21-
18+
transform().translate(0, 0, 0);
2219
}

src/skyboxmaterial.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ void SkyboxMaterial::setTexture(Texture *texture)
1717

1818
void SkyboxMaterial::apply(DrawContext &context)
1919
{
20-
shader_->bind();
21-
context.shader = shader_;
20+
Material::apply(context);
2221

2322
assert(texture_);
2423

src/terrainmaterial.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ void TerrainMaterial::apply(DrawContext &context)
1313
assert(heightMap_);
1414
assert(diffuseMap_);
1515

16-
shader_->bind();
17-
context.shader = shader_;
16+
Material::apply(context);
1817

1918
shader_->setUniformValue("ambient", ambient_);
2019
shader_->setUniformValue("heightScale", heightScale_);

src/textures/bonehead.jpg

-31.5 KB
Binary file not shown.

src/textures/grass.jpg

485 KB
Loading

0 commit comments

Comments
 (0)