Skip to content
This repository was archived by the owner on Jun 14, 2025. It is now read-only.

Commit 6052c54

Browse files
committed
Connected heightmaps, please read the readme.
There is many features in this commit, one of more most important: 1- Connected heightmaps. All heightmaps are connected using global UV position (chunk tile position) plus image UV as constant noise value in FBM. 2- Fixed mouse crashing? 3- PBR lighting not working in long distances. 4- Performance related issue. Known issues: 1- Chunking performance. 2- GPU memory leaks or something else. 3- Many buffer gen & delete calls. Thank you for reading.
1 parent 72e3627 commit 6052c54

39 files changed

+425
-360
lines changed

README.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# AGK
22

3-
Anubis Graphics Kit (AGK) is a photo-realism rendering engine made in OpenGL 4 for SDL2, the purpose of this engine is to do rendering tests and implement impirical rendering equations, techniques, concetps & specifically the umbrealla PBR.
3+
Anubis Graphics Kit (AGK) is a photo-realism rendering engine made in OpenGL 4 for SDL2, the purpose of this engine is to do rendering tests and implement empirical rendering equations, techniques, concepts & specifically the umbrella PBR.
44
There is an official researcher article being write together with this project to investigate a way to connect heightmaps. The research is not linked to any institution, perhaps I will publish here at the ends of article project,
55
but VokeGpu will keep update this engine project.
66

@@ -24,7 +24,7 @@ The location of executables is in `./build/`, choose your system and run Anubis
2424
* Dynamic input system.
2525
* ~~Motion blur~~.
2626
* ~~Screen space occlusion ambient (SSAO)~~.
27-
* ~~Connected heightmaps.~~
27+
* Connected heightmaps.
2828
* ~~Subsurface scattering~~.
2929
* ~~Depth of field~~.
3030
* ~~Animation.~~
@@ -37,11 +37,14 @@ One of most important rendering technique used here is the umbrella of physicall
3737
The BRDF make the terrain and world objects looks soft and realistic at some point, the visual effect also do subsurface scattering or known as BSSDF (bidirectional subsurface scattering distribution function) effect.
3838
![Alt text](/splash/splash-brdf-2.png?raw=true)
3939

40-
The terrain is procedural generated using compute shaders and FBM equation with perlin noise from Ken Perlin, the volume of terrain processed from grey-scale is real-time computed using tessellation shader with 4 patches, credits to [learnopengl](https://learnopengl.com) which I have used as book to learn about terrain generator.
41-
For rendering all terrain AGK come with a chunking system.
42-
![Alt text](/splash/splash-terrain-1.png)
43-
40+
The terrain is procedural generated using compute shaders and FBM equation with perlin noise from Ken Perlin, the volume of terrain processed from grey-scale is real-time computed using tessellation shader with 4 patches, credits to [learnopengl](https://learnopengl.com) which I have used as book to learn about terrain rendering.
41+
For rendering all terrain AGK come with a chunking system.
4442
The GUI is powered by VokeGpu [ekg ui library](https://github.com/vokegpu/ekg-ui-library) a high-performance library made in OpenGL for SDL2 written in C++.
43+
All detailed process are include in the article writen for this project, called Real-Time Terrain Generator by Rina.
44+
45+
![Alt text](/splash/splash-terrain-6.png)
46+
![Alt text](/splash/splash-terrain-rochoso.png)
47+
![Alt text](/splash/splash-terrain-arenoso.png)
4548

4649
# License & credits
4750
The project is under license MIT.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#version 450
2+
3+
layout (location = 0) out vec4 vFragColor;
4+
in vec3 vColor;
5+
6+
void main() {
7+
vFragColor = vec4(vColor, 1.0f);
8+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#version 450
2+
3+
layout (location = 0) in vec3 aPos;
4+
layout (location = 1) in vec3 aColor;
5+
6+
uniform mat4 uMVP;
7+
out vec3 vColor;
8+
9+
void main() {
10+
gl_Position = uMVP * vec4(aPos, 1.0f);
11+
vColor = aColor;
12+
}
Lines changed: 66 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,103 @@
11
#version 450 core
22
#define PI 3.1415926535897932384626433832795
33

4-
layout (location = 0) out vec4 FragColor;
4+
layout (location = 0) out vec4 vFragColor;
55

6-
in vec3 Pos;
7-
in vec2 Texcoord;
8-
in vec3 Normal;
6+
in vec3 vPos;
7+
in vec2 vTexCoord;
8+
in vec3 vNormal;
9+
10+
uniform vec3 uCameraPos;
11+
uniform float uAmbientColor;
12+
uniform int uLightAmount;
13+
uniform float uGamma = 1.0f;
14+
15+
vec3 mMaterialColor;
916

10-
uniform vec3 CameraPosition;
1117
uniform struct {
12-
vec3 Color;
13-
bool Metal;
14-
float Rough;
15-
} Material;
18+
vec3 uColor;
19+
bool uMetal;
20+
float uRough;
21+
} uMaterial;
1622

17-
uniform int LoadedLightLen;
1823
uniform struct {
19-
vec3 Intensity;
20-
bool Directional;
21-
vec3 POD;
22-
} Light[100];
23-
24-
vec3 SchlickFresnel(float lDotH) {
25-
vec3 f0 = vec3(0.04);
26-
if (Material.Metal) {
27-
f0 = Material.Color;
28-
}
24+
vec3 uIntensity;
25+
bool uDirectional;
26+
vec3 uVector;
27+
} uLight[100];
2928

30-
return f0 + (1 - f0) * pow(clamp(1.0 - lDotH, 0.0f, 1.0), 5.0);
29+
uniform struct {
30+
vec2 uDistance;
31+
vec3 uColor;
32+
} uFog;
33+
34+
vec3 shlickFresnel(float lDotH) {
35+
vec3 f0 = vec3(0.04f);
36+
if (uMaterial.uMetal) {
37+
f0 = mMaterialColor;
38+
}
39+
return f0 + (1 - f0) * pow(clamp(1.0f - lDotH, 0.0f, 1.0f), 5.0f);
3140
}
3241

33-
float GeometrySmith(float dotProduct) {
34-
float k = (Material.Rough + 1.0) * (Material.Rough + 1.0) / 8.0;
35-
float denom = dotProduct * (1 - k) + k;
36-
return 1.0 / denom;
42+
float geometrySmith(float dotProd) {
43+
float k = (uMaterial.uRough + 1.0f) * (uMaterial.uRough + 1.0f) / 8.0f;
44+
float denom = dotProd * (1.0f - k) + k;
45+
return 1.0f / denom;
3746
}
3847

39-
float GGxDistribution(float nDotH) {
40-
float alpha2 = Material.Rough * Material.Rough * Material.Rough * Material.Rough;
41-
float d = (nDotH * nDotH) * (alpha2 - 1) + 1;
42-
return alpha2 / (PI * d * d);
48+
float ggxDistribution(float nDotH) {
49+
float a2 = uMaterial.uRough * uMaterial.uRough * uMaterial.uRough * uMaterial.uRough;
50+
float d = (nDotH * nDotH) * (a2 - 1.0f) + 1.0f;
51+
return a2 / (PI * d * d);
4352
}
4453

45-
vec3 BRDFunction(vec3 n, vec3 v, int lightIndex) {
46-
vec3 diffuse = vec3(0.0);
47-
if (!Material.Metal) {
48-
diffuse = Material.Color;
54+
vec3 bidirecionalReflectanceDistributionFunc(vec3 n, vec3 v, int index) {
55+
vec3 diffuse = vec3(0.0f);
56+
if (!uMaterial.uMetal) {
57+
diffuse = mMaterialColor;
4958
}
5059

51-
vec3 intensity = Light[lightIndex].Intensity;
52-
vec3 l = vec3(0.0);
60+
vec3 intensity = uLight[index].uIntensity;
61+
vec3 l = vec3(0.0f);
5362

54-
if (Light[lightIndex].Directional) {
55-
l = normalize(Light[lightIndex].POD);
63+
if (uLight[index].uDirectional) {
64+
l = normalize(uLight[index].uVector);
5665
} else {
57-
l = Light[lightIndex].POD - Pos;
58-
float distance = length(l);
66+
l = uLight[index].uVector - vPos;
67+
float dist = length(l);
5968
l = normalize(l);
60-
intensity /= (distance * distance);
69+
intensity /= (dist * dist);
6170
}
6271

63-
float Wrap = 0.0;
64-
6572
vec3 h = normalize(v + l);
6673
float nDotH = dot(n, h);
6774
float lDotH = dot(l, h);
68-
float nDotL = max(dot(l, n), 0.0);
75+
float nDotL = max(dot(n, l), 1.0f);
6976
float nDotV = dot(n, v);
70-
vec3 spec = 0.25 * GGxDistribution(nDotH) * SchlickFresnel(lDotH) * GeometrySmith(nDotL) * GeometrySmith(nDotV);
7177

72-
return (diffuse + PI * spec) * intensity * nDotL;
78+
/* 0.25f == fracion of 4 */
79+
vec3 specular = 0.25f * ggxDistribution(nDotH) * shlickFresnel(lDotH) * geometrySmith(nDotL) * geometrySmith(nDotV);
80+
return (diffuse + PI * specular) * intensity * nDotL;
7381
}
7482

7583
void main() {
76-
vec3 color = vec3(0.0f);
77-
78-
if (Material.Rough == -1) {
79-
color = Material.Color;
84+
float dist = length(vPos);
85+
float fogFactor = clamp((uFog.uDistance.y - dist) / (uFog.uDistance.y - uFog.uDistance.x), 0.0, 1.0);
86+
mMaterialColor = uMaterial.uColor;
87+
vec3 sum = mMaterialColor * uAmbientColor;
88+
89+
if (uMaterial.uRough == -1) {
90+
sum = mMaterialColor;
8091
} else {
81-
vec3 n = normalize(Normal);
82-
vec3 v = normalize(CameraPosition - Pos);
92+
vec3 n = normalize(vNormal);
93+
vec3 v = normalize(uCameraPos - vPos);
8394

8495
if (!gl_FrontFacing) n = -n;
85-
for (int it = 0; it <= LoadedLightLen; it++) {
86-
color += BRDFunction(n, v, it);
96+
for (int index = 0; index <= uLightAmount; index++) {
97+
sum += bidirecionalReflectanceDistributionFunc(n, v, index);
8798
}
88-
89-
color = pow(color, vec3(1.0 / 2.0));
9099
}
91100

92-
FragColor = vec4(color, 1.0f);
101+
sum = mix(uFog.uColor, sum, fogFactor);
102+
vFragColor = vec4(sum, 1.0f);
93103
}
Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
#version 450 core
22

3-
layout (location = 0) in vec3 VertexPosition;
4-
layout (location = 1) in vec2 TextureCoordinate;
5-
layout (location = 2) in vec3 FaceNormal;
3+
layout (location = 0) in vec3 aPos;
4+
layout (location = 1) in vec2 aTexCoord;
5+
layout (location = 2) in vec3 aNormal;
66

7-
out vec3 Pos;
8-
out vec2 Texcoord;
9-
out vec3 Normal;
7+
out vec3 vPos;
8+
out vec2 vTexCoord;
9+
out vec3 vNormal;
1010

11-
uniform mat4 MVP;
12-
uniform mat4 MatrixModel;
13-
uniform mat3 MatrixNormal;
11+
uniform mat4 uMVP;
12+
uniform mat4 uModelMatrix;
13+
uniform mat3 uNormalMatrix;
1414

1515
void main() {
16-
Pos = (MatrixModel * vec4(VertexPosition, 1.0f)).xyz;
17-
Texcoord = TextureCoordinate;
18-
Normal = normalize(MatrixNormal * FaceNormal);
16+
gl_Position = uMVP * vec4(aPos, 1.0f);
17+
vec4 modelPos = uModelMatrix * vec4(aPos, 1.0f);
1918

20-
gl_Position = MVP * vec4(VertexPosition, 1.0f);
19+
vPos = modelPos.xyz;
20+
vTexCoord = aTexCoord;
21+
vNormal = normalize(uNormalMatrix * aNormal);
2122
}
Lines changed: 57 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,82 @@
1-
#version 450 core
1+
#version 450
22

3-
layout (location = 0) out vec4 FragColor;
4-
layout (binding = 0) uniform sampler2D FramebufferTexture;
5-
layout (binding = 1) uniform sampler2D DepthbufferTexture;
3+
layout (location = 0) out vec4 vFragColor;
4+
layout (binding = 0) uniform sampler2D uTexture;
5+
layout (binding = 1) uniform sampler2D uDepthSampler;
66

7-
in vec4 Rect;
8-
in vec2 Texcoord;
7+
in vec4 vRect;
8+
uniform vec4 uColor;
99

10-
uniform vec4 Color;
11-
12-
uniform mat3 RGBtoXYZ = mat3(
13-
0.4124564, 0.2126729, 0.0193339,
14-
0.3575761, 0.7151522, 0.1191920,
15-
0.1804375, 0.0721750, 0.9503041
10+
const mat3 RGBtoXYZ = mat3(
11+
0.4124564f, 0.2126729f, 0.0193339f,
12+
0.3575761f, 0.7151522f, 0.1191920f,
13+
0.1804375f, 0.0721750f, 0.9503041f
1614
);
1715

18-
uniform mat3 XYZtoRGB = mat3(
19-
3.2404542, -0.9692660, 0.0556434,
20-
-1.5371385, 1.8760108, -0.2040259,
21-
-0.4985314, 0.0415560, 1.0572252
16+
const mat3 XYZtoRGB = mat3(
17+
3.2404542f, -0.9692660f, 0.0556434f,
18+
-1.5371385f, 1.8760108f, -0.2040259f,
19+
-0.4985314f, 0.0415560f, 1.0572252f
2220
);
2321

24-
uniform float Exposure = 0.35;
25-
uniform float White = 0.928;
26-
uniform float AveLuminance;
27-
uniform mat4 PreviousMVP;
28-
uniform mat4 InverseMVP;
22+
uniform mat4 uMVP;
23+
uniform mat4 uInverseMVP;
2924

3025
uniform struct {
31-
bool HighDynamicRange;
32-
float AveLuminanceHDR;
33-
} Effects;
26+
bool uEnabled;
27+
float uAverageLuminance;
28+
float uExposure;
29+
float uWhite;
30+
} uHDR;
3431

3532
void main() {
36-
vec4 sum = texture(FramebufferTexture, Texcoord);
37-
vec4 depw = texture(DepthbufferTexture, Texcoord);
38-
vec4 c = sum;
39-
float dep = depw.w;
40-
41-
vec4 h = vec4(Texcoord.x * 2 - 1.0f, (1.0f - Texcoord.y) * 2 - 1, dep, 1);
42-
vec4 d = h * InverseMVP;
43-
vec4 worldPos = vec4(d.xyz / d.w, 1);
44-
vec4 currentPos = h;
45-
46-
vec4 previousPos = worldPos * PreviousMVP;
47-
previousPos /= previousPos.w;
48-
49-
vec2 texcoord = Texcoord;
50-
vec2 velocity = (currentPos.xy - previousPos.xy);
51-
52-
for (int i = 1; i < 5; ++i) {
53-
texcoord += velocity;
54-
vec4 color = texture(FramebufferTexture, texcoord);
55-
sum += color;
33+
vec2 fragCoord = gl_FragCoord.xy / textureSize(uTexture, 0);
34+
vec4 framebufferColor = texture(uTexture, fragCoord);
35+
vec4 framebufferDepth = texture(uDepthSampler, fragCoord);
36+
37+
float dep = framebufferDepth.r;
38+
vec4 h = vec4(fragCoord.x * 2 - 1, (1.0f - fragCoord.y) * 2 - 1, dep, 1.0f);
39+
vec4 d = uInverseMVP * h;
40+
vec4 w = d / d.w;
41+
42+
vec4 currPos = h;
43+
vec4 prevPos = uMVP * w;
44+
prevPos = prevPos / prevPos.w;
45+
46+
vec2 velocity = (currPos.xy - prevPos.xy);
47+
vec2 texCoord = fragCoord + velocity;
48+
int numSamples = 1;
49+
if (velocity.x == 0 && velocity.y == 0) {
50+
numSamples = 1;
51+
}
52+
53+
for (int i = 1; i < numSamples; ++i) {
54+
texCoord += velocity;
55+
vec4 currColor = texture(uTexture, texCoord);
56+
framebufferColor += currColor;
5657
}
5758

58-
sum = vec4(sum.xyz / 5, 1.0f);
59+
vec4 sum = framebufferColor / numSamples;
60+
61+
if (uHDR.uEnabled) {
62+
float white = 0.928f;
63+
float exposure = 0.35f;
5964

60-
if (Effects.HighDynamicRange) {
6165
vec3 xyzCol = RGBtoXYZ * vec3(sum);
6266

6367
float xyzSum = xyzCol.x + xyzCol.y + xyzCol.z;
64-
vec3 xyYCol = vec3(0);
65-
if (xyzSum > 0.0) xyYCol = vec3(xyzCol.x / xyzSum, xyzCol.y / xyzSum, xyzCol.y);
68+
vec3 xyYCol = vec3(0.0f);
69+
if (xyzSum > 0.0f) xyYCol = vec3(xyzCol.x / xyzSum, xyzCol.y / xyzSum, xyzCol.y);
6670

67-
float l = (Exposure * xyYCol.z) / AveLuminance;
68-
l = (l * (1 + l / (White * White))) / (1 + l);
71+
float l = (exposure * xyYCol.z) / uHDR.uAverageLuminance;
72+
l = (l * (1 + l / (white * white))) / (1.0f + l);
6973

7074
xyzCol.x = (l * xyYCol.x) / (xyYCol.y);
7175
xyzCol.y = l;
72-
xyzCol.z = (l * (1 - xyYCol.x - xyYCol.y)) / xyYCol.y;
76+
xyzCol.z = (l * (1.0f - xyYCol.x - xyYCol.y)) / xyYCol.y;
7377

74-
sum = vec4(XYZtoRGB * xyzCol, 1.0);
78+
sum = vec4(XYZtoRGB * xyzCol, 1.0f);
7579
}
7680

77-
FragColor = sum;
81+
vFragColor = sum;
7882
}

0 commit comments

Comments
 (0)