Reputation: 55
I am trying to use OpenGL shaders to draw a simple triangle. I have tried to split up the code into separate methods inside one overarching rendering class, however, I cannot seem to get my triangle to render on the screen. This is my compileShaders() method which compiles and loads my shader source code:
static const GLchar* vertexShaderSource[] = {
"#version 430 core \n"
"in vec4 vPosition; \n"
"in vec4 vColor; \n"
"out vec4 color; \n"
" \n"
"void main() { \n"
" color = vColor; \n"
" gl_Position = vPosition; \n"
"} \n"
};
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, vertexShaderSource, NULL);
glCompileShader(vertexShader);
program = glCreateProgram();
glAttachShader(program, vertexShader);
glLinkProgram(program);
glDeleteShader(vertexShader);
return program;
In a different method I am calling glewInit(), compileShaders() and am then setting up the vertex array and vertex buffer objects. In this method triangleVertices[] and triangleColors[] are two arrays containing the position (x,y,z) and colour values (r,g,b,a) for each vertex the triangle.
glewInit();
renderingProgram = compileShaders();
glEnable(GL_ARRAY_BUFFER);
glGenVertexArrays(1, &vertexArrayObject);
glBindVertexArray(vertexArrayObject);
glGenBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, numberOfVertices*7*sizeof(GLfloat), NULL, GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, numberOfVertices*3*sizeof(GLfloat), triangleVertices);
glBufferSubData(GL_ARRAY_BUFFER, numberOfVertices*3*sizeof(GLfloat), numberOfVertices*4*sizeof(GLfloat), triangleColors);
GLuint vpos = glGetAttribLocation(renderingProgram, "vPosition");
glVertexAttribPointer(vpos, 3, GL_FLOAT, GL_FALSE, 0, 0);
GLuint cpos = glGetAttribLocation(renderingProgram, "vColor");
glVertexAttribPointer(cpos, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(numberOfVertices*3*sizeof(GLfloat)));
glUseProgram(renderingProgram);
glEnableVertexAttribArray(vpos);
glEnableVertexAttribArray(cpos);
Finally my render() method in which I try and draw the Triangle loaded into the Array Buffer:
const GLfloat color[] = {0.0f, 0.0f, 1.0f, 1.0f};
glClearBufferfv(GL_COLOR, 0, color);
glUseProgram(renderingProgram);
glDrawArrays(GL_TRIANGLES, 0, 3);
SwapBuffers(hDC_);
I am rendering to a Win32 window which is setup in a different class. After rendering I can see a blue screen, so it appears as if the glClearBufferfv
call is working correctly. However my triangle is nowhere to be seen. If I change the rendering mode to glDrawArrays(GL_POINTS, 0, 1)
I see one white pixel in the middle of the screen.
What am I doing wrong?
Upvotes: 0
Views: 530
Reputation: 22348
As others have pointed out, you need a fragment shader too, e.g.,
#version 430
in vec4 color;
out vec4 frag_rgba; // default index (0) (glFragBindDataLocation)
void main (void) {
frag_rgba = color;
}
Attach this shader to the program along with the vertex shader prior to linking. You can detach the shaders from the program after linking. You might also consider using explicit attribute indices in the vertex shader.
Upvotes: 1