From 4cf0dc230be536094671b6c3fd70ecbe40144b5d Mon Sep 17 00:00:00 2001 From: karl Date: Sun, 30 May 2021 20:33:10 +0200 Subject: [PATCH] Add heightmap and apply it in tessellation shader --- Shader/terrain.fs | 6 +++--- Shader/terrain.tcs | 8 ++++---- Shader/terrain.tes | 11 ++++++++++- Shader/terrain.vs | 7 +------ test/full-demo/main.cpp | 6 +++++- 5 files changed, 23 insertions(+), 15 deletions(-) diff --git a/Shader/terrain.fs b/Shader/terrain.fs index 804a588..1799ceb 100644 --- a/Shader/terrain.fs +++ b/Shader/terrain.fs @@ -5,11 +5,11 @@ in PipelineData { vec2 texture_coordinate; } fs_in; -uniform sampler2D diffuse_texture; +layout (binding = 0) uniform sampler2D heightmap; out vec4 color; void main() { - vec4 diffuse_texel = texture(diffuse_texture, fs_in.texture_coordinate); - color = vec4(1.0); // color = vec4(diffuse_texel); + vec4 diffuse_texel = texture(heightmap, fs_in.texture_coordinate); + color = vec4(diffuse_texel); } \ No newline at end of file diff --git a/Shader/terrain.tcs b/Shader/terrain.tcs index ecd980d..f1a8659 100644 --- a/Shader/terrain.tcs +++ b/Shader/terrain.tcs @@ -16,8 +16,8 @@ void main() { tc_out[gl_InvocationID].texture_coordinate = tc_in[gl_InvocationID].texture_coordinate; tc_out[gl_InvocationID].position = tc_in[gl_InvocationID].position; - gl_TessLevelInner[0] = 12; - gl_TessLevelOuter[0] = 12; - gl_TessLevelOuter[1] = 12; - gl_TessLevelOuter[2] = 12; + gl_TessLevelInner[0] = 32; + gl_TessLevelOuter[0] = 32; + gl_TessLevelOuter[1] = 32; + gl_TessLevelOuter[2] = 32; } \ No newline at end of file diff --git a/Shader/terrain.tes b/Shader/terrain.tes index 48509e3..b54b395 100644 --- a/Shader/terrain.tes +++ b/Shader/terrain.tes @@ -2,6 +2,12 @@ layout(triangles) in; +layout (binding = 0) uniform sampler2D heightmap; + +uniform mat4 model; +uniform mat4 projection; +uniform mat4 view; + in PipelineData { vec4 position; vec2 texture_coordinate; @@ -21,5 +27,8 @@ void main() { te_out.position += gl_TessCoord.y * te_in[1].position; te_out.position += gl_TessCoord.z * te_in[2].position; - gl_Position = te_out.position; + te_out.position.y += texture(heightmap, te_out.texture_coordinate).r; + + mat4 pvm = projection * view * model; + gl_Position = pvm * te_out.position; } \ No newline at end of file diff --git a/Shader/terrain.vs b/Shader/terrain.vs index a6f7e11..b2fce37 100644 --- a/Shader/terrain.vs +++ b/Shader/terrain.vs @@ -6,20 +6,15 @@ layout (location = 2) in vec2 texture_coordinate; layout (location = 3) in vec3 aTangent; layout (location = 4) in vec3 aBitangent; -uniform mat4 model; -uniform mat4 projection; -uniform mat4 view; - out PipelineData { vec4 position; vec2 texture_coordinate; } vs_out; void main() { - mat4 pvm = projection * view * model; vs_out.texture_coordinate = texture_coordinate; vec4 homogenous_position = vec4(position, 1.0); - vs_out.position = pvm * homogenous_position; + vs_out.position = homogenous_position; gl_Position = vs_out.position; } \ No newline at end of file diff --git a/test/full-demo/main.cpp b/test/full-demo/main.cpp index 15b2102..09f2980 100644 --- a/test/full-demo/main.cpp +++ b/test/full-demo/main.cpp @@ -26,7 +26,8 @@ class FullDemo : public Gedeng::Application { particle_tex1("Resources/Textures/Particles/circle.png", Gedeng::Texture::Settings()), particle_tex2("Resources/Textures/Particles/magic.png", Gedeng::Texture::Settings()), particle_tex3("Resources/Textures/Particles/smoke.png", Gedeng::Texture::Settings()), - quad_mesh(Gedeng::QuadMesh(10.0)), light(glm::vec3(0.57735, -0.57735, 0.57735)), + heightmap("Resources/Textures/terrain.png", Gedeng::Texture::Settings()), quad_mesh(Gedeng::QuadMesh(10.0)), + light(glm::vec3(0.57735, -0.57735, 0.57735)), mesh1("Resources/Meshes/Monkey.obj", Gedeng::ObjMesh::Settings()), mesh2("Resources/Meshes/bench.obj", Gedeng::ObjMesh::Settings()), mesh3("Resources/Meshes/ring.obj", Gedeng::ObjMesh::Settings()) { @@ -126,6 +127,7 @@ class FullDemo : public Gedeng::Application { // Terrain terrain_shader.use(); + heightmap.bind_to(0); terrain_shader.setMat4("projection", camera.get_projection()); terrain_shader.setMat4("view", camera.get_view()); terrain_mesh.render_patches(terrain_shader); @@ -157,6 +159,8 @@ class FullDemo : public Gedeng::Application { Gedeng::Texture particle_tex2; Gedeng::Texture particle_tex3; + Gedeng::Texture heightmap; + Gedeng::QuadMesh quad_mesh; Gedeng::QuadMesh terrain_mesh;