VBO Cube created from triangles

Started by Skatty, June 12, 2013, 11:19:55

Previous topic - Next topic

Skatty

Hello

I found a code for VBO indexed. It draw colored triangle. My question is: Is it possible to use that VBO for cube created from textured triangles? (2 triangles per quad).

Here's the code:
  static void drawVertexBufferObjectIndexed()
   {
      // create geometry buffers
      FloatBuffer cBuffer = BufferUtils.createFloatBuffer(9);
      cBuffer.put(1).put(0).put(0);
      cBuffer.put(0).put(1).put(0);
      cBuffer.put(0).put(0).put(1);
      cBuffer.flip();

      FloatBuffer vBuffer = BufferUtils.createFloatBuffer(9);
      vBuffer.put(-0.5f).put(-0.5f).put(0.0f);
      vBuffer.put(+0.5f).put(-0.5f).put(0.0f);
      vBuffer.put(+0.5f).put(+0.5f).put(0.0f);
      vBuffer.flip();

      // create index buffer
      ShortBuffer iBuffer = BufferUtils.createShortBuffer(3);
      iBuffer.put((short) 0);
      iBuffer.put((short) 1);
      iBuffer.put((short) 2);
      iBuffer.flip();

      //

      IntBuffer ib = BufferUtils.createIntBuffer(3);

      glGenBuffersARB(ib);
      int vHandle = ib.get(0);
      int cHandle = ib.get(1);
      int iHandle = ib.get(2);

      glEnableClientState(GL_VERTEX_ARRAY);
      glEnableClientState(GL_COLOR_ARRAY);

      glBindBufferARB(GL_ARRAY_BUFFER_ARB, vHandle);
      glBufferDataARB(GL_ARRAY_BUFFER_ARB, vBuffer, GL_STATIC_DRAW_ARB);
      glVertexPointer(3, GL_FLOAT, /* stride */3 << 2, 0L);

      glBindBufferARB(GL_ARRAY_BUFFER_ARB, cHandle);
      glBufferDataARB(GL_ARRAY_BUFFER_ARB, cBuffer, GL_STATIC_DRAW_ARB);
      glColorPointer(3, GL_FLOAT, /* stride */3 << 2, 0L);

      glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, iHandle);
      glBufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, iBuffer, GL_STATIC_DRAW_ARB);

      glDrawElements(GL_TRIANGLES, /* elements */3, GL_UNSIGNED_SHORT, 0L);

      glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
      glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);

      glDisableClientState(GL_COLOR_ARRAY);
      glDisableClientState(GL_VERTEX_ARRAY);

      // cleanup VBO handles
      ib.put(0, vHandle);
      ib.put(1, cHandle);
      ib.put(2, cHandle);
      glDeleteBuffersARB(ib);
   }

Fool Running

Yes, but you'll have to change the code a little to add the other polygons needed for a cube and also add code to store the texture coordinates.
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D

Skatty

Can you help me? How should it look like?

Fool Running

Create a new buffer for the texture coordinates and add the texture coordinates to the buffer (and then create a VBO with that buffer).
For the other triangles, just add the information for the other vertexes of the cube to your color VBO and your vertex VBO. Then add the new indexes to the index VBO.
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D