Hi, I am trying to create a simple Tesselation shader (TCS + TES). I have the following code:
#include "sb6.h"
#include <math.h>
class my_application : public sb6::application
{
private:
GLuint rendering_program;
GLuint vertex_array_object;
public:
void startup(void)
{
rendering_program = compile_shaders();
glGenVertexArrays(1, &vertex_array_object);
glBindVertexArray(vertex_array_object);
}
void shutdown(void)
{
glDeleteVertexArrays(1, &vertex_array_object);
glDeleteProgram(rendering_program);
}
void render(double currentTime)
{
const GLfloat clear_color[] = { 0.0f, 0.2f, 0.0f, 1.0f };
glClearBufferfv(GL_COLOR, 0, clear_color);
// Use the program object for rendering
glUseProgram(rendering_program);
GLfloat attrib_offset[] = { (float)sin(currentTime) * 0.5f,
(float)cos(currentTime) * 0.6f,
0.0f, 0.0f };
// Update the value of input attribute 0
glVertexAttrib4fv(0, attrib_offset);
// Draw one point
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glDrawArrays(GL_TRIANGLES, 0, 3);
}
GLuint compile_shaders(void)
{
GLuint vertex_shader;
GLuint tesselation_control_shader;
GLuint tesselation_evaluation_shader;
GLuint fragment_shader;
GLuint program;
// Source code for the vertex shader
static const GLchar *vertex_shader_source[] =
{
"#version 430 core
"
"
"
"layout (location = 0) in vec4 offset;
"
"
"
"void main(void)
"
"{
"
" const vec4 vertices[3] = vec4[3](vec4( 0.25, -0.25, 0.5, 1.0),
"
" vec4(-0.25, -0.25, 0.5, 1.0),
"
" vec4( 0.25, 0.25, 0.5, 1.0));
"
"
"
" gl_Position = vertices[gl_VertexID] + offset;
"
"}
"
};
// Source code for the tesselation control shader
static const GLchar *tesselation_control_shader_source[] =
{
"#version 430 core
"
"
"
"layout (vertices = 3) out;
"
"
"
"void main(void)
"
"{
"
" if(gl_InvocationID == 0)
"
" {
"
" gl_TessLevelInner[0] = 5.0;
"
" gl_TessLevelOuter[0] = 5.0;
"
" gl_TessLevelOuter[1] = 5.0;
"
" gl_TessLevelOuter[2] = 5.0;
"
" }
"
" gl_out[gl_InvocationID].gl_Position = gl_in[gl_InvocationID].gl_Position;
"
"}
"
};
// Source code for the tesselation evaluation shader
static const GLchar *tesselation_evaluation_shader_source[] =
{
"#version 430 core
"
"
"
"layout (triangles, equal_spacing, cw) in;
"
"
"
"void main(void)
"
"{
"
" gl_Position = (gl_TessCord.x * gl_in[0].gl_Position +
"
" gl_TessCord.y * gl_in[1].gl_Position +
"
" gl_TessCord.z * gl_in[2].gl_Position);
"
"}
"
};
// Source code for the fragment shader
static const GLchar *fragment_shader_source[] =
{
"#version 430 core
"
"
"
"out vec4 color;
"
"
"
"void main(void)
"
"{
"
" color = vec4(1.0, 1.0, 1.0, 1.0);
"
"}
"
};
// Create and compile the vertex shader
vertex_shader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex_shader, 1, vertex_shader_source, NULL);
glCompileShader(vertex_shader);
// Create and compile the tesselation control shader
tesselation_control_shader = glCreateShader(GL_TESS_CONTROL_SHADER);
glShaderSource(tesselation_control_shader, 1, tesselation_control_shader_source, NULL);
glCompileShader(tesselation_control_shader);
// Create and compile the tesselation evaluation shader
tesselation_evaluation_shader = glCreateShader(GL_TESS_EVALUATION_SHADER);
glShaderSource(tesselation_evaluation_shader, 1, tesselation_evaluation_shader_source, NULL);
glCompileShader(tesselation_evaluation_shader);
// Create and compile the fragment shader
fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment_shader, 1, fragment_shader_source, NULL);
glCompileShader(fragment_shader);
// Create program, attach shaders to it, and link it
program = glCreateProgram();
glAttachShader(program, vertex_shader);
glAttachShader(program, tesselation_control_shader);
glAttachShader(program, tesselation_evaluation_shader);
glAttachShader(program, fragment_shader);
glLinkProgram(program);
// Delete the shaders as the program has them now
glDeleteShader(vertex_shader);
glDeleteShader(tesselation_control_shader);
glDeleteShader(tesselation_evaluation_shader);
glDeleteShader(fragment_shader);
return program;
}
};
DECLARE_MAIN(my_application);
However the triangle is not displayed on screen. If I comment out the lines that attaches the TCS and TES, then the triangle is drawn correctly which must mean there is something wrong with my tesselation shaders. However I can not understand what it is!
PS. The forum screws up the formatting of my code in some places…
Thanks/
Robin