-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce 'terrain' using tessellation shader
- Loading branch information
Showing
15 changed files
with
233 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#ifndef TERRAIN_H | ||
#define TERRAIN_H | ||
|
||
#include "mesh.hpp" | ||
#include <glad/glad.h> | ||
|
||
namespace renderer { | ||
namespace mesh { | ||
|
||
struct TerrainVertex { | ||
float x, y; | ||
float r, g, b; | ||
}; | ||
|
||
class Terrain : public Mesh { | ||
public: | ||
explicit Terrain() noexcept; | ||
|
||
void render() const; | ||
|
||
private: | ||
GLuint vao; | ||
GLuint program; | ||
}; | ||
} // namespace mesh | ||
} // namespace renderer | ||
#endif // TERRAIN_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#ifndef TERRAINMATERIAL_H | ||
#define TERRAINMATERIAL_H | ||
|
||
#include "material.hpp" | ||
|
||
namespace renderer { | ||
namespace material { | ||
|
||
class TerrainMaterial : public Material { | ||
public: | ||
explicit TerrainMaterial() noexcept; | ||
}; | ||
|
||
} // namespace material | ||
} // namespace renderer | ||
|
||
#endif // TERRAINMATERIAL_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#include "terrain.hpp" | ||
|
||
using namespace renderer::mesh; | ||
|
||
Terrain::Terrain() noexcept { | ||
const TerrainVertex vertices[4] = {{-1.0f, -1.0f, 1.f, 0.f, 0.f}, | ||
{-1.0f, 1.0f, 0.f, 1.f, 0.f}, | ||
{1.0f, 1.0f, 0.f, 0.f, 1.f}, | ||
{1.0f, -1.0f, 1.f, 1.f, 1.f}}; | ||
|
||
GLuint vbo; | ||
glCreateBuffers(1, &vbo); | ||
glNamedBufferStorage(vbo, sizeof(vertices), vertices, GL_DYNAMIC_STORAGE_BIT); | ||
|
||
glCreateVertexArrays(1, &vao); | ||
|
||
glVertexArrayVertexBuffer(vao, 0, vbo, 0, sizeof(TerrainVertex)); | ||
|
||
glEnableVertexArrayAttrib(vao, 0); | ||
glVertexArrayAttribFormat(vao, 0, 2, GL_FLOAT, GL_FALSE, 0); | ||
glVertexArrayAttribBinding(vao, 0, 0); | ||
|
||
glEnableVertexArrayAttrib(vao, 1); | ||
glVertexArrayAttribFormat(vao, 1, 3, GL_FLOAT, GL_FALSE, (sizeof(float) * 2)); | ||
glVertexArrayAttribBinding(vao, 1, 0); | ||
} | ||
|
||
void Terrain::render() const { | ||
glBindVertexArray(vao); | ||
glPatchParameteri(GL_PATCH_VERTICES, 4); | ||
glDrawArrays(GL_PATCHES, 0, 4); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#include "terrainmaterial.hpp" | ||
|
||
using namespace renderer::material; | ||
|
||
TerrainMaterial::TerrainMaterial() noexcept | ||
: Material{"terrain.vs.glsl", "terrain.tcs.glsl", "terrain.tes.glsl", | ||
"terrain.fs.glsl"} {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#version 460 | ||
|
||
in TE_OUT { | ||
vec3 color; | ||
} fs_in; | ||
|
||
out vec4 color; | ||
|
||
void main() { | ||
color = vec4(fs_in.color, 1.0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#version 460 | ||
|
||
layout(vertices = 4) out; | ||
|
||
in V_OUT { | ||
vec3 color; | ||
} tcs_in[]; | ||
|
||
out TC_OUT { | ||
vec3 color; | ||
} tcs_out[]; | ||
|
||
void main(void) { | ||
gl_TessLevelOuter[0] = 2.0; | ||
gl_TessLevelOuter[1] = 4.0; | ||
gl_TessLevelOuter[2] = 6.0; | ||
gl_TessLevelOuter[3] = 8.0; | ||
|
||
gl_TessLevelInner[0] = 8.0; | ||
gl_TessLevelInner[1] = 8.0; | ||
|
||
gl_out[gl_InvocationID].gl_Position = gl_in[gl_InvocationID].gl_Position; | ||
|
||
tcs_out[gl_InvocationID].color = tcs_in[gl_InvocationID].color; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#version 460 | ||
|
||
layout(quads, equal_spacing, ccw) in; | ||
|
||
in TC_OUT { | ||
vec3 color; | ||
} tes_in[]; | ||
|
||
out TE_OUT { | ||
vec3 color; | ||
} tes_out; | ||
|
||
vec4 interpolate4(in vec4 v0, in vec4 v1, in vec4 v2, in vec4 v3) { | ||
vec4 a = mix(v0, v1, gl_TessCoord.x); | ||
vec4 b = mix(v3, v2, gl_TessCoord.x); | ||
return mix(a, b, gl_TessCoord.y); | ||
} | ||
|
||
vec3 interpolate3(in vec3 v0, in vec3 v1, in vec3 v2, in vec3 v3) { | ||
vec3 a = mix(v0, v1, gl_TessCoord.x); | ||
vec3 b = mix(v3, v2, gl_TessCoord.x); | ||
return mix(a, b, gl_TessCoord.y); | ||
} | ||
|
||
void main() { | ||
gl_Position = interpolate4(gl_in[0].gl_Position, gl_in[1].gl_Position, gl_in[2].gl_Position, gl_in[3].gl_Position); | ||
tes_out.color = interpolate3(tes_in[0].color, tes_in[1].color,tes_in[2].color, tes_in[3].color).xyz; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#version 460 | ||
|
||
layout (location = 0) in vec2 position; | ||
layout (location = 1) in vec3 color; | ||
|
||
layout (location = 0) uniform mat4 model; | ||
layout (location = 1) uniform mat4 view; | ||
layout (location = 2) uniform mat4 projection; | ||
|
||
out VS_OUT { | ||
vec3 color; | ||
} vs_out; | ||
|
||
void main() { | ||
gl_Position = projection * view * model * vec4(position, 0.0, 1.0); | ||
vs_out.color = color; | ||
} |