Jump to content

View more

Image of the Day

Boxes as reward for our ranking mode. ヾ(☆▽☆)
#indiedev #gamedev #gameart #screenshotsaturday https://t.co/ALF1InmM7K
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

Sign up now

my first gl 3 app: displaying a cube

4: Adsense

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 rogerdv   Members   


Posted 20 May 2014 - 09:45 AM

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
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); 

Can somebody see what Im doing wrong here?

#2 h3xl3r   Members   


Posted 20 May 2014 - 10:25 AM

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.




Take a look at:



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

Edited by h3xl3r, 20 May 2014 - 10:40 AM.

#3 deftware   Prime Members   


Posted 20 May 2014 - 11:45 AM

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 ?

#4 rogerdv   Members   


Posted 20 May 2014 - 01:16 PM

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

#5 Irlan Robson   Members   


Posted 20 May 2014 - 02:00 PM

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






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




It increases the readability this way.

#6 Gooey   Members   


Posted 20 May 2014 - 02:07 PM

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

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.