Implemented simple terrain generation

This commit is contained in:
Lorenzo Torres 2025-08-13 21:16:42 +02:00
parent fb9607b2b1
commit c0b8d021d4
16 changed files with 825 additions and 32 deletions

View file

@ -78,5 +78,6 @@ void main() {
for(int i = 0; i < pushConstants.light_count; i++)
result += calc_point_light(i, norm, FragPos, viewDir);
outColor = vec4(result, 1.0);
}

View file

@ -41,3 +41,4 @@ void main() {
TexCoords = uv;
gl_Position = out_vec;
}

View file

@ -0,0 +1,86 @@
#version 450
#define MAX_POINT_LIGHTS 1024
struct PointLight {
vec3 position;
vec3 ambient;
vec3 diffuse;
vec3 specular;
vec3 data;
};
layout(location = 0) out vec4 outColor;
layout(location = 2) in vec3 Normal;
layout(location = 3) in vec3 FragPos;
layout(location = 4) in vec2 TexCoords;
layout (binding = 2) uniform DirectionalLight {
vec3 direction;
vec3 ambient;
vec3 diffuse;
vec3 specular;
} directional_light;
layout (binding = 5) uniform PointLights {
PointLight point_lights[MAX_POINT_LIGHTS];
} point_lights;
layout (binding = 3) uniform ViewUniform {
vec3 pos;
} viewPos;
layout(push_constant) uniform pc {
int light_count;
} pushConstants;
vec3 calc_directional_light(vec3 normal, vec3 viewDir) {
vec3 lightDir = normalize(-directional_light.direction);
float diff = max(dot(normal, lightDir), 0.0);
vec3 diffuse = directional_light.diffuse * diff;
return (directional_light.ambient + diffuse);
}
vec3 calc_point_light(int index, vec3 normal, vec3 fragPos, vec3 viewDir) {
float constant = point_lights.point_lights[index].data[0];
float linear = point_lights.point_lights[index].data[1];
float quadratic = point_lights.point_lights[index].data[2];
vec3 lightDir = normalize(point_lights.point_lights[index].position - fragPos);
float diff = max(dot(normal, lightDir), 0.0);
vec3 reflectDir = reflect(-lightDir, normal);
float distance = length(point_lights.point_lights[index].position - fragPos);
float attenuation = 1.0 / (constant + linear * distance + quadratic * (distance * distance));
vec3 ambient = point_lights.point_lights[index].ambient;
vec3 diffuse = point_lights.point_lights[index].diffuse * diff;
ambient *= attenuation;
diffuse *= attenuation;
return (ambient + diffuse);
}
void main() {
vec3 norm = normalize(Normal);
vec3 viewDir = normalize(viewPos.pos - FragPos);
vec3 result = calc_directional_light(norm, viewDir);
//vec3 result = vec3(0.0, 0.0, 0.0);
for(int i = 0; i < pushConstants.light_count; i++)
result += calc_point_light(i, norm, FragPos, viewDir);
vec3 tall = vec3(1.0, 0.0, 0.0);
vec3 mid = vec3(0.0, 1.0, 0.0);
vec3 water = vec3(0.0, 0.0, 1.0);
vec3 color;
if (FragPos.y < 0.05) {
color = water;
} else if (FragPos.y > 0.3) {
color = tall;
} else {
color = mid;
}
outColor = vec4(color+result, 1.0);
}

View file

@ -0,0 +1,38 @@
#version 450
layout(location = 0) in vec3 vertPos;
layout(location = 1) in vec3 normal;
layout(location = 2) in vec2 uv;
layout (binding = 0) uniform ProjUniform {
mat4 proj;
} proj;
layout (binding = 1) uniform ViewUniform {
mat4 view;
} view;
layout(location = 2) out vec3 Normal;
layout(location = 3) out vec3 FragPos;
layout(location = 4) out vec2 TexCoords;
layout (set = 1, binding = 0) uniform sampler2D diffuseSampler;
void main() {
float texelSize = 1.0 / 200;
float hL = texture(diffuseSampler, uv - vec2(texelSize, 0.0)).r;
float hR = texture(diffuseSampler, uv + vec2(texelSize, 0.0)).r;
float hD = texture(diffuseSampler, uv - vec2(0.0, texelSize)).r;
float hU = texture(diffuseSampler, uv + vec2(0.0, texelSize)).r;
float dX = (hR - hL) * 15.0;
float dY = (hU - hD) * 15.0;
float y = texture(diffuseSampler, uv).x * 3;
vec4 out_vec = proj.proj * view.view * vec4(vec3(vertPos.x, y, vertPos.z), 1.0);
FragPos = vec3(vertPos.x, y, vertPos.z);
Normal = normalize(vec3(-dX, -dY, 1.0));
TexCoords = uv;
gl_Position = out_vec;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.3 MiB