LWJGL Forum

Programming => Lightweight Java Gaming Library => Topic started by: Skatty on June 12, 2013, 11:19:55

Title: VBO Cube created from triangles
Post by: Skatty on June 12, 2013, 11:19:55
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);
   }
Title: Re: VBO Cube created from triangles
Post by: Fool Running on June 12, 2013, 16:45:49
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.
Title: Re: VBO Cube created from triangles
Post by: Skatty on June 12, 2013, 18:33:27
Can you help me? How should it look like?
Title: Re: VBO Cube created from triangles
Post by: Fool Running on June 14, 2013, 12:55:09
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.