my first gl 3 app: displaying a cube

Started by
4 comments, last by Gooey 9 years, 11 months ago

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?

Advertisement

Only time for a quick look, if there's time later I'll try to post some code as well, but here's some remarks:

- You're using the deprecated (in OpenGL 3) GL_QUADS primitive type. You should be using two triangles really to draw a "quad". Your driver might still support this primitive type of course as it's only depreacted and might not be removed completley (yet), but you'll have to switch sooner or later and it's the way to go in general.

- Your vertex data does not have enough vertices to draw a cube without using an index buffer. Eight points are enough for drawing an indexed cube as the indexes can reference each vertex multiple times. If you're using glDrawArrays ( = non-indexed) you'll have to specify each vertex(-position) multiple times to get the same result. Take a look at glDrawElements for indexed drawing.

My suggestion would be to not go for the cube immediately but try to draw one quad composed of two triangles first, preferably using an index buffer, as that's what you'll need extending the code for the full cube later.

Edit:

Take a look at:

http://www.open.gl/drawing

The example there is also starting with a triangle and extending it to a quad with glDrawArrays first, and then adding indexing with glDrawElements.

I see you have your 'box[]' array, but your glBufferData call references 'triangle' instead. And why is it trying to put 9 floats in there ?

Yep, wrong number, is 24. 9 is for the triangle, and should be box. I just copied the wrong code.

Don't use mixed bindings for creation. Just do:

glBind(foo)

glUnbind(foo)

glBind(foo1)

glBind(foo) // if uses foo and ind more than once

glUnbind(foo)

glUnbind(foo1)

It increases the readability this way.

After quickly flicking through as radioteeth said you glBufferData is pointing at the wrong array also i noticed with your draw call


glDrawArrays (GL_QUADS, 0, 3)

you are only trying to draw using 3 vertices the call is

glDrawArrays(mode, first, last);

where mode can be GL_POINTS, GL_LINES, GL_LINE_STRIP, GL_LINE_LOOP, GL_TRIANGLES, GL_TRIANGLE_STRIP or GL_TRIANGLE_FAN. first and last point at the places in your enabled array.

to draw a cube with 8 vertices you would need to use an ebo but you will learn about them with whatever tutorial your using

This topic is closed to new replies.

Advertisement