Refactor tessellation shader pipeline

This commit is contained in:
Jack Christensen 2024-08-15 13:53:52 -04:00
parent bc8d855098
commit 05ecd9b4be
6 changed files with 243 additions and 166 deletions

View File

@ -4,8 +4,8 @@ Size=400,400
Collapsed=0
[Window][Terrain Controls]
Pos=929,19
Size=351,701
Pos=933,19
Size=347,701
Collapsed=0
DockId=0x00000002,0
@ -24,12 +24,12 @@ Collapsed=0
[Window][Scene Window]
Pos=0,19
Size=927,701
Size=931,701
Collapsed=0
DockId=0x00000001,0
[Docking][Data]
DockSpace ID=0x6F42A598 Window=0xE80F322C Pos=0,19 Size=1280,701 Split=X Selected=0x9F2D9299
DockNode ID=0x00000001 Parent=0x6F42A598 SizeRef=927,701 CentralNode=1 Selected=0x9F2D9299
DockNode ID=0x00000002 Parent=0x6F42A598 SizeRef=351,701 Selected=0xF69494A7
DockNode ID=0x00000001 Parent=0x6F42A598 SizeRef=931,701 CentralNode=1 Selected=0x9F2D9299
DockNode ID=0x00000002 Parent=0x6F42A598 SizeRef=347,701 Selected=0xF69494A7

View File

@ -1,68 +1,78 @@
#version 430
precision highp float;
#version 450 core
layout(binding = 0) uniform sampler2D heightTexture;
layout(location = 1) uniform float time;
in vec3 fragPosition;
out vec4 FragColor;
layout(std140, binding = 0) uniform SceneUniforms
{
mat4 PV; //camera projection * view matrix
vec4 eye_w; //world-space eye position
};
layout(std140, binding = 1) uniform LightUniforms
{
vec4 La; //ambient light color
vec4 Ld; //diffuse light color
vec4 Ls; //specular light color
vec4 light_w; //world-space light position
};
layout(std140, binding = 2) uniform MaterialUniforms
{
vec4 ka; //ambient material color
vec4 kd; //diffuse material color
vec4 ks; //specular material color
float shininess; //specular exponent
};
in vec3 frag_position;
in VertexData {
vec2 tex_coord;
} inData;
out vec4 frag_color; //the output color for this fragment
void main(void)
{
//Compute per-fragment Phong lighting
// vec4 ktex = texture(diffuse_tex, inData.tex_coord);
// vec4 ambient_term = ka*ktex*La;
// const float eps = 1e-8; //small value to avoid division by 0
// float d = distance(light_w.xyz, inData.pw.xyz);
// float atten = 1.0/(d*d+eps); //d-squared attenuation
// vec3 nw = normalize(inData.nw); //world-space unit normal vector
// vec3 lw = normalize(light_w.xyz - inData.pw.xyz); //world-space unit light vector
// vec4 diffuse_term = atten*kd*ktex*Ld*max(0.0, dot(nw, lw));
// vec3 vw = normalize(eye_w.xyz - inData.pw.xyz); //world-space unit view vector
// vec3 rw = reflect(-lw, nw); //world-space unit reflection vector
// vec4 specular_term = atten*ks*Ls*pow(max(0.0, dot(rw, vw)), shininess);
// fragcolor = ambient_term + diffuse_term + specular_term;
float height = texture(heightTexture, inData.tex_coord).r;
//frag_color = vec4(inData.tex_coord, 0.0f, 1.0f);
// if (height > 0.8) frag_color = vec4(vec3(1.0f-height), 1.0f);
// else frag_color = vec4(vec3(height), 1.0f);
frag_color = vec4(height, height, height, 1.0);
void main() {
FragColor = vec4(fragPosition * 0.5f + 0.5f, 1.0f);
}
// layout(binding = 0) uniform sampler2D heightTexture;
// layout(location = 1) uniform float time;
// layout(std140, binding = 0) uniform SceneUniforms
// {
// mat4 PV; //camera projection * view matrix
// vec4 eye_w; //world-space eye position
// };
// layout(std140, binding = 1) uniform LightUniforms
// {
// vec4 La; //ambient light color
// vec4 Ld; //diffuse light color
// vec4 Ls; //specular light color
// vec4 light_w; //world-space light position
// };
// layout(std140, binding = 2) uniform MaterialUniforms
// {
// vec4 ka; //ambient material color
// vec4 kd; //diffuse material color
// vec4 ks; //specular material color
// float shininess; //specular exponent
// };
// in vec3 frag_position;
// in VertexData {
// vec2 tex_coord;
// } inData;
// out vec4 frag_color; //the output color for this fragment
// void main(void)
// {
// //Compute per-fragment Phong lighting
// // vec4 ktex = vec4(vec3(texture(heightTexture, inData.tex_coord).r, 1.0);
// // vec4 ambient_term = ka*ktex*La;
// // const float eps = 1e-8; //small value to avoid division by 0
// // float d = distance(light_w.xyz, inData.pw.xyz);
// // float atten = 1.0/(d*d+eps); //d-squared attenuation
// // vec3 nw = normalize(inData.nw); //world-space unit normal vector
// // vec3 lw = normalize(light_w.xyz - inData.pw.xyz); //world-space unit light vector
// // vec4 diffuse_term = atten*kd*ktex*Ld*max(0.0, dot(nw, lw));
// // vec3 vw = normalize(eye_w.xyz - inData.pw.xyz); //world-space unit view vector
// // vec3 rw = reflect(-lw, nw); //world-space unit reflection vector
// // vec4 specular_term = atten*ks*Ls*pow(max(0.0, dot(rw, vw)), shininess);
// // frag_color = ambient_term + diffuse_term + specular_term;
// float height = texture(heightTexture, inData.tex_coord).r;
// //frag_color = vec4(inData.tex_coord, 0.0f, 1.0f);
// // if (height > 0.8) frag_color = vec4(vec3(1.0f-height), 1.0f);
// // else frag_color = vec4(vec3(height), 1.0f);
// frag_color = vec4(height, height, height, 1.0);
// }

View File

@ -1,28 +1,31 @@
#version 430
#version 450 core
layout(vertices = 4) out; // Define the number of control points per patch (e.g., 4 for a quad)
layout(vertices = 4) out;
in VertexData {
vec2 tex_coord;
} inData[];
in VS_OUT {
vec3 position;
vec2 texCoord;
} tc_in[];
out VertexData {
vec2 tex_coord;
} outData[];
out TC_OUT {
vec3 position;
vec2 texCoord;
} tc_out[];
void main()
{
// Pass through control points to the tessellation evaluation shader
gl_out[gl_InvocationID].gl_Position = gl_in[gl_InvocationID].gl_Position;
outData[gl_InvocationID].tex_coord = inData[gl_InvocationID].tex_coord;
uniform float tessellationFactor = 8.0f;
// Set the tessellation levels (outer and inner)
void main() {
// Pass through position and texture coordinates to the tessellation evaluation shader
tc_out[gl_InvocationID].position = tc_in[gl_InvocationID].position;
tc_out[gl_InvocationID].texCoord = tc_in[gl_InvocationID].texCoord;
// Set tessellation levels
if (gl_InvocationID == 0) {
gl_TessLevelOuter[0] = 5.0; // Level of tessellation along one edge
gl_TessLevelOuter[1] = 5.0; // Level of tessellation along another edge
gl_TessLevelOuter[2] = 5.0; // Level of tessellation along the other edge
gl_TessLevelOuter[3] = 5.0; // Level of tessellation along the other edge
gl_TessLevelInner[0] = 5.0; // Level of tessellation for the inner part
gl_TessLevelInner[1] = 5.0; // Level of tessellation for the inner part
gl_TessLevelOuter[0] = tessellationFactor; // Level of tessellation along one edge
gl_TessLevelOuter[1] = tessellationFactor; // Level of tessellation along another edge
gl_TessLevelOuter[2] = tessellationFactor; // Level of tessellation along the other edge
gl_TessLevelOuter[3] = tessellationFactor; // Level of tessellation along the other edge
gl_TessLevelInner[0] = tessellationFactor; // Level of tessellation for the inner part
gl_TessLevelInner[1] = tessellationFactor; // Level of tessellation for the inner part
}
}
}

View File

@ -1,6 +1,11 @@
#version 430
#version 450 core
layout(quads, equal_spacing, ccw) in; // Define the type of patch (e.g., quads)
layout(quads, fractional_even_spacing, cw) in;
in TC_OUT {
vec3 position;
vec2 texCoord;
} te_in[];
layout(location = 0) uniform mat4 M;
@ -10,28 +15,122 @@ layout(std140, binding = 0) uniform SceneUniforms
vec4 eye_w; //world-space eye position
};
in VertexData {
vec2 tex_coord;
} inData[];
out vec3 fragPosition;
out vec3 frag_position; // Ensure this matches the input in fragment.glsl
layout(binding = 0) uniform sampler2D heightTexture;
uniform float displacementScale = 0.1;
out VertexData {
vec2 tex_coord;
} outData;
void main() {
// Interpolate texture coordinates
vec2 texCoord = mix(
mix(te_in[0].texCoord, te_in[1].texCoord, gl_TessCoord[0]),
mix(te_in[3].texCoord, te_in[2].texCoord, gl_TessCoord[0]),
gl_TessCoord[1]
);
void main()
{
// Interpolate the position using the barycentric coordinates from tessellation
vec3 pos = mix(mix(gl_in[0].gl_Position.xyz, gl_in[1].gl_Position.xyz, gl_TessCoord.x),
mix(gl_in[3].gl_Position.xyz, gl_in[2].gl_Position.xyz, gl_TessCoord.x),
gl_TessCoord.y);
frag_position = pos;
// Interpolate the original position
vec3 position = mix(
mix(te_in[0].position, te_in[1].position, gl_TessCoord[0]),
mix(te_in[3].position, te_in[2].position, gl_TessCoord[0]),
gl_TessCoord[1]
);
outData.tex_coord = mix(mix(inData[0].tex_coord, inData[1].tex_coord, gl_TessCoord.x),
mix(inData[3].tex_coord, inData[2].tex_coord, gl_TessCoord.x),
gl_TessCoord.y);
gl_Position = PV*M*vec4(pos, 1.0);
fragPosition = (PV*M*vec4(position, 1.0)).xyz;
gl_Position = PV*M*vec4(position, 1.0f);
}
// layout(quads, equal_spacing, ccw) in; // Define the type of patch (e.g., quads)
// in vec2 tcs_uv[];
// layout(binding = 0) uniform sampler2D heightTexture;
// layout(location = 0) uniform mat4 M;
// layout(std140, binding = 0) uniform SceneUniforms
// {
// mat4 PV; //camera projection * view matrix
// vec4 eye_w; //world-space eye position
// };
// in VertexData {
// vec2 tex_coord;
// } inData[];
// out vec3 frag_position; // Ensure this matches the input in fragment.glsl
// out VertexData {
// vec2 tex_coord;
// } outData;
// float getHeight(vec2 uv) {
// return texture(heightTexture, uv).r;
// }
// void main()
// {
// const float u = gl_TessCoord[0];
// const float v = gl_TessCoord[1];
// vec2 tess_coord = gl_TessCoord.xy;
// vec2 base_uv = mix(tcs_uv[0], tcs_uv[1], tess_coord.x);
// base_uv = mix(base_uv, mix(tcs_uv[3], tcs_uv[2], tess_coord.x), tess_coord.y);
// vec4 htex = texture(heightTexture, base_uv);
// const vec4 p0 = PV*M*gl_in[0].gl_Position;
// const vec4 p1 = PV*M*gl_in[1].gl_Position;
// const vec4 p2 = PV*M*gl_in[2].gl_Position;
// const vec4 p3 = PV*M*gl_in[3].gl_Position;
// vec4 a = mix(p0, p1, u);
// vec4 b = mix(p3, p2, u);
// vec4 c = mix(a, b, v);
// c.y = htex.r * 0.5f;
// float eps = 0.0002;
// float hCenter = getHeight(base_uv);
// float hRight = getHeight(base_uv + vec2(eps, 0.0));
// float hLeft = getHeight(base_uv - vec2(eps, 0.0));
// float hUp = getHeight(base_uv + vec2(0.0, eps));
// float hDown = getHeight(base_uv - vec2(0.0, eps));
// vec3 tangentX = normalize(vec3(0.002, hRight - hLeft, 0.0));
// vec3 tangentZ = normalize(vec3(0.0, hUp - hDown, 0.002));
// vec3 d0 = gl_in[0].gl_Position.xyz;
// vec3 d1 = gl_in[1].gl_Position.xyz;
// vec3 d2 = gl_in[2].gl_Position.xyz;
// vec3 n_u = d1 - d0;
// vec3 v_u = d2 - d0;
// vec3 v_normal = normalize(cross(n_u, v_u));
// vec3 normal = cross(tangentZ, tangentX);
// gl_Position = c;
// outData.tex_coord = base_uv;
// // outData.pw = vec3(c);
// // outData.nw = normal;
// // Interpolate the position using the barycentric coordinates from tessellation
// // vec3 pos = mix(mix(gl_in[0].gl_Position.xyz, gl_in[1].gl_Position.xyz, gl_TessCoord.x),
// // mix(gl_in[3].gl_Position.xyz, gl_in[2].gl_Position.xyz, gl_TessCoord.x),
// // gl_TessCoord.y);
// // frag_position = pos;
// // outData.tex_coord = mix(mix(inData[0].tex_coord, inData[1].tex_coord, gl_TessCoord.x),
// // mix(inData[3].tex_coord, inData[2].tex_coord, gl_TessCoord.x),
// // gl_TessCoord.y);
// // gl_Position = PV*M*vec4(pos, 1.0);
// }

View File

@ -1,55 +1,14 @@
#version 430
layout(location = 0) uniform mat4 M;
#version 450 core
layout(std140, binding = 0) uniform SceneUniforms
{
mat4 PV; //camera projection * view matrix
vec4 eye_w; //world-space eye position
};
layout(location = 0) in vec3 inPosition;
layout(location = 1) in vec2 inTexCoord;
layout(location = 0) in vec3 pos_attrib;
layout(location = 1) in vec2 tex_coord_attrib;
layout(location = 2) in vec3 normal_attrib;
out VertexData
{
vec2 tex_coord;
} outData;
out VS_OUT {
vec3 position;
vec2 texCoord;
} vs_out;
void main() {
gl_Position = vec4(pos_attrib, 1.0);
outData.tex_coord = tex_coord_attrib; //send tex_coord to fragment shader
}
// #version 400 core
// layout(location = 0) uniform mat4 M;
// layout(location = 1) uniform float time;
// layout(std140, binding = 0) uniform SceneUniforms
// {
// mat4 PV; //camera projection * view matrix
// vec4 eye_w; //world-space eye position
// };
// layout(location = 0) in vec3 pos_attrib; //this variable holds the position of mesh vertices
// layout(location = 1) in vec2 tex_coord_attrib;
// layout(location = 2) in vec3 normal_attrib;
// out VertexData
// {
// vec2 tex_coord;
// vec3 pw; //world-space vertex position
// vec3 nw; //world-space normal vector
// } outData;
// void main(void)
// {
// gl_Position = PV*M*vec4(pos_attrib, 1.0); //transform vertices and send result into pipeline
// //Use dot notation to access members of the interface block
// outData.tex_coord = tex_coord_attrib; //send tex_coord to fragment shader
// outData.pw = vec3(M*vec4(pos_attrib, 1.0)); //world-space vertex position
// outData.nw = vec3(M*vec4(normal_attrib, 0.0)); //world-space normal vector
// }
vs_out.position = (vec4(inPosition, 1.0f)).xyz;
vs_out.texCoord = inTexCoord;
}

View File

@ -53,7 +53,7 @@ Scene::Scene(int width, int height)
aspect_(static_cast<float>(width) / static_cast<float>(height)),
near_z_(0.01f),
far_z_(100.0f),
fov_(glm::pi<float>() / 8.0f)
fov_(glm::pi<float>() / 4.0f)
{}
Scene::~Scene() {
@ -70,7 +70,7 @@ void Scene::Init() {
// GL_DEPTH_TEST causes artifacts in meshes made up of multiple triangles
// disabling for now
//glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
//glEnable(GL_CULL_FACE);
fbo_.Init(window_width, window_height);
post_fbo_.Init(window_width, window_height);
@ -99,6 +99,12 @@ void Scene::InitBuffers() {
-0.5f, 0.5f, 0.0f, 0.0f, 1.0f // Top-left
};
float vertices[] = {
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
0.0f, 0.5f, 0.0f
};
glGenVertexArrays(1, &vao_);
glGenBuffers(1, &patch_vbo);
glBindVertexArray(vao_);
@ -169,7 +175,7 @@ void Scene::Display(GLFWwindow* window) {
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
view_matrix_ = glm::lookAt(glm::vec3(1.2f, 1.2f, 1.2f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f));
view_matrix_ = glm::lookAt(glm::vec3(1.4f, 1.4f, 1.4f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f));
Uniforms::SceneData.PV = projection_matrix_ * view_matrix_; // Projection-View matrix
Uniforms::BufferSceneData();