I have spent a good amount of time with the fixed pipeline of openGL, and I have recently began learning the programmable pipeline. I know my painter, and shader classes are not the issue because they work with fixed function pipeline stuff. I can't seem to get glDrawArrays to work for my life.
I am not sure if my error is in how i set up the vertex buffer object, in my shader or else where. I have debugged my code also and set breakpoints throughout the display function, and it seems to never get past glDrawArrays(), (i.e. it hits a breakpoint at glDrawArrays, but doesn't hit any after, not sure why.)
What gets outputted is just a white screen, nothing else.
Heres my code:
float vertices[] = { 0.75, 0.75, 0.0, 1.0,
0.75, -0.75, 0.0, 1.0,
-0.75, -0.75, 0.0, 1.0 };
GLuint vertexBufferObject;
GLuint positionLocation;
GLuint vaoObject;
void initVertexBuffer(GLuint& vertexBufferObject, float* vertexData, unsigned int size, GLenum GL_DRAW_TYPE)
{
glGenBuffers(1, &vertexBufferObject);
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject);
glBufferData(GL_ARRAY_BUFFER, size, vertexData, GL_DRAW_TYPE);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void main(int argc, char* argv[])
{
painter.initEngine(argc, argv, 500, 500, 0, 0, "2D3D");
painter.initGlutFuncs(display, resize, Input::MouseButtonClick, Input::MouseDrag, keyboard);
defaultShader.init("default.vert", "default.frag");
defaultShader.link();
initVertexBuffer(vertexBufferObject, vertices, sizeof(vertices), GL_STATIC_DRAW);
glGenVertexArrays(1, &vaoObject);
glBindVertexArray(vaoObject);
positionLocation = glGetAttribLocation(defaultShader.id(), "position");
painter.startMainLoop();
}
void display()
{
painter.clearDisplay();
defaultShader.bind();
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject);
glEnableVertexAttribArray(positionLocation);
glVertexAttribPointer(positionLocation, 4, GL_FLOAT, GL_FALSE, 0, 0);
std::cout << Framework::glErrorCheck() << std::endl;
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableVertexAttribArray(positionLocation);
glBindBuffer(GL_ARRAY_BUFFER, 0);
defaultShader.unbind();
painter.flushAndSwapBuffers();
}
Vertex Shader:
#version 140
in vec4 position;
void main()
{
gl_Position = position;
}
Fragment Shader:
#version 140
out vec4 outColor;
void main()
{
outColor = vec4(1.0, 0.0, 1.0, 1.0);
}
Edit: Code updated with Joey Dewd, keltar, and genpfault's suggestions. I'm no longer hanging at glDrawArrays, i.e. instead of a white screen I'm getting a black screen. This is leading me to think that my buffer is somehow still not setup correctly. Or possibly, I am missing something else needed for the vertex array buffer initialization (vaoObject)?