Im trying to learn opengl 3 and I started by the traidtional triangle program. But I tried to improve it to display a cube, it doesnt shows anything. Here is the relevant code:
//the shaders
const char* vertex_shader =
"#version 150\n"
"in vec3 vp;"
"void main () {"
" gl_Position = vec4 (vp, 1.0);"
"}";
const char* fragment_shader =
"#version 150\n"
"out vec4 frag_colour;"
"void main () {"
" frag_colour = vec4 (0.5, 0.0, 0.5, 1.0);"
"}";
//the cube coordinates
GLfloat box[] = {
0.0f, 0.0f, 0.0f,
1.0f, 0.0f, 0.0f,
1.0f, 1.0f, 0.0f,
0.0f, 1.0f, 0.0f,
0.0f, 0.0f, -1.0f,
1.0f, 0.0f, -1.0f,
1.0f, 1.0f, -1.0f,
0.0f, 1.0f, -1.0f
};
// Create and bind the VBO for the vertices.
glGenBuffers(1, &g_verticesVBO);
glBindBuffer(GL_ARRAY_BUFFER, g_verticesVBO);
// Transfer the vertices from CPU to GPU.
glBufferData(GL_ARRAY_BUFFER, 9 * sizeof(GLfloat), triangle, GL_STATIC_DRAW);
//glBindBuffer(GL_ARRAY_BUFFER, 0);
glGenVertexArrays (1, &g_vao);
glBindVertexArray (g_vao);
glEnableVertexAttribArray (0);
glBindBuffer (GL_ARRAY_BUFFER, g_verticesVBO);
glVertexAttribPointer (0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
//here we draw
// wipe the drawing surface clear
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram (shader_programme);
glBindVertexArray (g_verticesVBO);
// draw points 0-3 from the currently bound VAO with current in-use shader
glDrawArrays (GL_QUADS, 0, 3); //also tried glDrawArrays (GL_QUADS, 0, 8);
SDL_GL_SwapWindow(mainwindow);
Can somebody see what Im doing wrong here?