Commit 41b60730 authored by Bastian Krayer's avatar Bastian Krayer

added isoline example to BasicTessellation

parent f2df84b5
......@@ -9,6 +9,9 @@ const int HEIGHT = 800;
glm::vec4 tessOuter = glm::vec4(8.0f);
glm::vec2 tessInner = glm::vec2(5.0f, 3.0f);
// Specifies the curvature of the parabola computed in the isolines shader
float isoCurvature = 1.0f;
float maxTessLevel = 64.0f;
// Window
......@@ -30,6 +33,7 @@ int initWindow() {
// Shaders
CVK::ShaderSet* shaderTessTri = nullptr;
CVK::ShaderSet* shaderTessQuad = nullptr;
CVK::ShaderSet* shaderTessIsolines = nullptr;
void initOpenGL() {
......@@ -51,6 +55,14 @@ void initOpenGL() {
sPath + "/BasicTessellation/basicTessellationQuad.tes",
sPath + "/BasicTessellation/basicTessellation.frag"});
shaderTessIsolines = new CVK::ShaderSet(
VERTEX_SHADER_BIT | FRAGMENT_SHADER_BIT | TESS_CONTROL_BIT |
TESS_EVAL_BIT,
{sPath + "/BasicTessellation/basicTessellation.vert",
sPath + "/BasicTessellation/basicTessellation.tcs",
sPath + "/BasicTessellation/basicTessellationIsolines.tes",
sPath + "/BasicTessellation/basicTessellation.frag"});
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
// We want to draw a wireframe mesh
......@@ -84,11 +96,21 @@ void cleanup() {
delete triangle;
delete shaderTessTri;
delete shaderTessQuad;
delete shaderTessIsolines;
}
// Assuming "qwertz" keyboard layout:
// q-w: Increment inner tessellation levels
// e-z: Increment outer tessellation levels
// u: Increment isoline curvature
//
// a-s: Decrement inner tessellation levels
// d-h: Decrement outer tessellation levels
// j: Decrement isoline curvature
void charCallback(GLFWwindow* window, unsigned int key) {
const float incr = 1.0f;
const float incrCurv = 0.1f;
switch (key) {
case 'q':
tessInner.x += incr;
......@@ -108,6 +130,9 @@ void charCallback(GLFWwindow* window, unsigned int key) {
case 'z':
tessOuter.w += incr;
break;
case 'u':
isoCurvature += incrCurv;
break;
case 'a':
tessInner.x -= incr;
break;
......@@ -126,6 +151,9 @@ void charCallback(GLFWwindow* window, unsigned int key) {
case 'h':
tessOuter.w -= incr;
break;
case 'j':
isoCurvature -= incrCurv;
break;
default:
break;
}
......@@ -183,7 +211,7 @@ int main(int argc, const char* argv[]) {
shaderTessQuad->useProgram();
glm::mat4 M = glm::identity<glm::mat4>();
M = glm::translate(M, glm::vec3(-0.15f, -0.25f, 0.0f));
M = glm::translate(M, glm::vec3(-0.15f, -0.75f, 0.0f));
M = glm::scale(M, glm::vec3(0.5f));
glUniformMatrix4fv(
glGetUniformLocation(shaderTessQuad->getProgramID(),
......@@ -204,6 +232,41 @@ int main(int argc, const char* argv[]) {
glDrawArrays(GL_PATCHES, 0, 3);
}
// Draw isoline based tessellation
// Important!
// We can use the same number of patch vertices (3), as the tessellation
// is independant of the patch vertices. These are defined by us and can
// be used in the tessellation evaluation shader to produce the final
// positions and attributes
{
shaderTessIsolines->useProgram();
glm::mat4 M = glm::identity<glm::mat4>();
M = glm::translate(M, glm::vec3(-0.15f, 0.25f, 0.0f));
M = glm::scale(M, glm::vec3(0.5f));
glUniformMatrix4fv(
glGetUniformLocation(shaderTessIsolines->getProgramID(),
"modelMatrix"),
1, GL_FALSE, glm::value_ptr(M));
glUniform2fv(glGetUniformLocation(
shaderTessIsolines->getProgramID(), "tessInner"),
1, glm::value_ptr(tessInner));
glUniform4fv(glGetUniformLocation(
shaderTessIsolines->getProgramID(), "tessOuter"),
1, glm::value_ptr(tessOuter));
glUniform1f(glGetUniformLocation(shaderTessIsolines->getProgramID(),
"isoCurvature"),
isoCurvature);
// Important!
// We have to use GL_PATCHES as topology mode to use the
// tessellation shader
glBindVertexArray(triangle->getVAO());
glDrawArrays(GL_PATCHES, 0, 3);
}
glfwSwapBuffers(window);
glfwPollEvents();
}
......
#version 400 core
layout(isolines) in;
uniform float isoCurvature;
void main() {
// The tessellation is generated from a quad, but we only need
// 3 vertices to define a parallelogram, so these are our control points
// Read them in for easier reading
vec4 a = gl_in[0].gl_Position;
vec4 b = gl_in[1].gl_Position;
vec4 c = gl_in[2].gl_Position;
// Side vectors
vec4 ab = b - a;
vec4 ac = c - a;
// Compute the final position
// For isolines primitives, gl_TessCoord contains uv coordinates in xy
float x = gl_TessCoord.x;
float y = gl_TessCoord.y;
// Compute a parabola with vertex at x=0.5 and a curvature of isoCurvature
// We offset each line by this value in the y direction
float f = isoCurvature * (x - 0.5) * (x - 0.5);
// Each line has multiple vertices in the x direction and there are multiple
// lines stacked in the y direction Just the lines are computed as a + ab *
// x + ac * y
gl_Position = a + ab * x + ac * (y + f);
}
\ No newline at end of file
......@@ -7,7 +7,7 @@ void main()
// The tessellation is generated from a quad, but we only need
// 3 vertices to define a parallelogram, so these are our control points
// Read them in for easer reading
// Read them in for easier reading
vec4 a = gl_in[0].gl_Position;
vec4 b = gl_in[1].gl_Position;
vec4 c = gl_in[2].gl_Position;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment