Hello I have been following a tutorial and have been trying to get my tessellation to render but for some reason its not working. I have been looking at the code for a while but just cant seem to find out what is going on. If some one could help me out that would be great thanks.
#include <iostream>
using namespace std;
// GLEW
#define GLEW_STATIC
#include <GL/glew.h>
// GLFW
#include <GLFW/glfw3.h>
// Window dimensions
const GLuint WIDTH = 800, HEIGHT = 600;
GLuint compile_shaders(void) {
GLuint vertext_shader;
GLuint tesslation_shader;
GLuint evaluation_shader;
GLuint fragment_shader;
GLuint program;
//This sets the vertext shader for rendring the square on the screen
static const GLchar * vertex_shader_source[] = {
"#version 450 core
" // This defines the core gl version to use
"layout (location = 0) in vec4 offset;
"//Sets the location of where to retreive attribute
"layout( location = 1) in vec4 color;
"
"out VS_OUT
"
"{
"
"vec4 color;
"
"} vs_out;
"
"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;
"
"vs_out.color = color;
"
"}
"
};
static const GLchar * tess_shader_source[] = {
"#version 450 core
" // This defines the core gl version to use
"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; "
};
static const GLchar * eval_shader_source[] = {
"#version 450 core
" // This defines the core gl version to use
"layout(triangles, equal_spacing, cw) in;
"
"void main(void)
"
"{
"
"gl_Position = (gl_TessCoord.x * gl_in[0].gl_Position +
"
"gl_TessCoord.y * gl_in[1].gl_Position +
"
"gl_TessCoord.z * gl_in[2].gl_Position);
"
"}
"
};
static const GLchar * fragment_shader_source[] = {
"#version 450 core
" // This defines the core gl version to use
"in VS_OUT
"
"{
"
"vec4 color;
"
"}fs_in;
"
"out vec4 color;
"
"void main(void)
"
"{
"
"color = fs_in.color;
"
"}
"
};
vertext_shader = glCreateShader(GL_VERTEX_SHADER); //Creates a empty shader object wating for the source to be placed in it
glShaderSource(vertext_shader, 1, vertex_shader_source, NULL);//creates the shader source code to be passed to the shader object to create a coppy of it
glCompileShader(vertext_shader);//compiles the shader source code from the shader object
tesslation_shader = glCreateShader(GL_TESS_CONTROL_SHADER); //Creates a empty shader object wating for the source to be placed in it
glShaderSource(tesslation_shader, 1, tess_shader_source, NULL);//creates the shader source code to be passed to the shader object to create a coppy of it
glCompileShader(tesslation_shader);//compiles the shader source code from the shader object
evaluation_shader = glCreateShader(GL_TESS_EVALUATION_SHADER); //Creates a empty shader object wating for the source to be placed in it
glShaderSource(evaluation_shader, 1, eval_shader_source, NULL);//creates the shader source code to be passed to the shader object to create a coppy of it
glCompileShader(evaluation_shader);//compiles the shader source code from the shader object
fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment_shader, 1, fragment_shader_source, NULL);
glCompileShader(fragment_shader);
program = glCreateProgram(); //Creates a program object to attach shader objects
glAttachShader(program, vertext_shader); //attches shader object to the program object
glAttachShader(program, tesslation_shader); //attches shader object to the program object
glAttachShader(program, evaluation_shader);
glAttachShader(program, fragment_shader);
glLinkProgram(program);//links all the shader objects attached to the program object and links them
glDeleteShader(vertext_shader);
glDeleteShader(tesslation_shader);
glDeleteShader(evaluation_shader);
glDeleteShader(fragment_shader);//Once the shader object was linked into the program object it
//Deletes the shader object from meomry
return program;// Returns the final shader from the program object
}
// The MAIN function, from here we start the application and run the game loop
int main(double currentTime){
GLuint rendering_program;
GLuint vertex_array_object;
glfwInit();//Initlizes the glfw init
// Create a GLFWwindow object that we can use for GLFW's functions
GLFWwindow *window;
window = glfwCreateWindow(WIDTH, HEIGHT, "Project Blarple", nullptr, nullptr);
if (window == nullptr){
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glewExperimental = true;
GLenum glewError = glewInit();
if (glewError != GLEW_OK) {
printf("glew init error
%s
", glewGetErrorString(glewError));
}
int errorFlag = glGetError();
if (errorFlag != GL_NO_ERROR) {
cout<<"error";
}
rendering_program = compile_shaders();
glCreateVertexArrays(1, &vertex_array_object);// creates a input for the pipeline sets it to one vertex
glBindVertexArray(vertex_array_object);//binds it to the context
//Main Loop
while (!glfwWindowShouldClose(window)) {
glViewport(0, 0, WIDTH, HEIGHT); //This sets the view port inside the window in pixles
glClearColor(0, 0, 0, 0); //Sets the windows background color
glClear(GL_COLOR_BUFFER_BIT); //Exectutes the color buffering for the screen
glUseProgram(rendering_program);// tells the gpu to use the rendring program object
GLfloat attrib[] = {
(float)sin(currentTime)* 0.5f,
(float)cos(currentTime)* 0.6,
0.0f, 0.0f
};// Moves the postion of the
GLfloat rgbc[] = {
1.0f, 0.5f, 0.2f, 1.0f
};// Moves the postion of the
glVertexAttrib4fv(0, attrib);//Call the location of the attribute wich is one and then call the attribute that i want to set
glVertexAttrib4fv(1, rgbc);
// Draws the single point on the screen since its only drawing one vertex the last set is three
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glDrawArrays(GL_PATCHES, 0, 3);
glDeleteVertexArrays(1, &vertex_array_object);
glDeleteProgram(rendering_program);
glfwSwapBuffers(window); //swaps frames
glfwPollEvents();//Checks for keyboard events on the window
}
glfwDestroyWindow(window);//gets rid of the window object
glfwTerminate(); //Cloeses the window out
return 0;
}