Hello Guest

Cube object only renders single triangle.

  • 2 Replies
  • 1812 Views
Cube object only renders single triangle.
« on: September 29, 2014, 22:43:26 »
I have a VBO that contains all the vertices, normals and colour data for a cube.  The display is looking at the front of the cube so it should appear as a square.  however I only get a triangle rendered in the top right corner of the display
link: http://prntscr.com/4rlmlm (prntscrDOTcomSLASH4rlmlm)

does anyone know what I am doing wrong?

vertex locations:
Code: [Select]
v 1.000000 -1.000000 -1.000000
v 1.000000 -1.000000 1.000000
v -1.000000 -1.000000 1.000000
v -1.000000 -1.000000 -1.000000
v 1.000000 1.000000 -0.999999
v 0.999999 1.000000 1.000001
v -1.000000 1.000000 1.000000
v -1.000000 1.000000 -1.000000

My render code: (vbos is an arraylist of VBO objects that each contain the handles for the vbo's vertex, normal and color data)
Code: [Select]
glBindBuffer(GL_ARRAY_BUFFER, vbos.get(i).getVertexHandle());
glVertexPointer(VERTEX_SIZE, GL_FLOAT, 0, 0l);

glBindBuffer(GL_NORMAL_ARRAY, vbos.get(i).getNormalHandle());
glVertexPointer(NORMAL_SIZE, GL_FLOAT, 0, 0l);

glBindBuffer(GL_ARRAY_BUFFER, vbos.get(i).getColourHandle());
glColorPointer(COLOUR_SIZE, GL_FLOAT, 0, 0l);

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);

glDrawArrays(GL_TRIANGLES, 0, vbos.get(i).getVertexCount());

glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);

*

Offline abcdef

  • ****
  • 322
Re: Cube object only renders single triangle.
« Reply #1 on: September 30, 2014, 08:02:54 »
You have 8 vertex, first 3 are for the first triangle, the second 3 are for the third triangle and the last 2 are probably discarded as there aren't three of them. This looks to be the reason why you are seeing triangles.

But the above is not actually what you wanted, those 8 vertex are the 8 points on a cube, but you haven't told opengl that. You only told it to draw triangles given 8 points. To fix this you either need to create 36 vertex points to represent the 12 triangles to draw in a cube (not very efficient) or you can create an index array which chooses which of the 8 vertex points to use in each triangle and then you can use the draw elements command instead

Have a look at

https://www.opengl.org/sdk/docs/man/html/glDrawElements.xhtml

Re: Cube object only renders single triangle.
« Reply #2 on: September 30, 2014, 13:57:59 »
thanks