OpenGL Java VBO

I use LWJGL and drawing cubes with glBegin/glEnd , but I heard that this method is very inefficient and I should start using VBOs. I have no idea how this works.

I want to draw cubes of different sizes and positions (without rotation), and I think that for this I need to use VBO .

Can anyone give me an example of code or understand how to use VBO with Java or even if VBO - the best choice?

+4
source share
3 answers

This is the code I wrote to test VBOs with Java. It uses JOGL instead of LWJGL, but this is a minor thing.

In addition to glVertexPointer, you can also use glTexCoordPointer and glNormalPointer to specify data for texture coordinates and normals and enable them with glEnableClientState (GL.GL_TEXTURE_COORD_ARRAY) and glEnableClientState (GL.GL_NORMAL_ARRAY).

 import com.sun.opengl.util.*; import javax.media.opengl.*; import javax.swing.*; import java.nio.*; public class VBOTest implements GLEventListener { public static void main(String[] args) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); GLCanvas canvas = new GLCanvas(); canvas.addGLEventListener(new VBOTest()); frame.add(canvas); frame.setSize(640, 480); frame.setVisible(true); } private FloatBuffer vertices; private ShortBuffer indices; private int VBOVertices; private int VBOIndices; public void init(GLAutoDrawable drawable) { float[] vertexArray = {-0.5f, 0.5f, 0, 0.5f, 0.5f, 0, 0.5f, -0.5f, 0, -0.5f, -0.5f, 0}; vertices = BufferUtil.newFloatBuffer(vertexArray.length); vertices.put(vertexArray); vertices.flip(); short[] indexArray = {0, 1, 2, 0, 2, 3}; indices = BufferUtil.newShortBuffer(indexArray.length); indices.put(indexArray); indices.flip(); GL gl = drawable.getGL(); int[] temp = new int[2]; gl.glGenBuffers(2, temp, 0); VBOVertices = temp[0]; gl.glBindBuffer(GL.GL_ARRAY_BUFFER, VBOVertices); gl.glBufferData(GL.GL_ARRAY_BUFFER, vertices.capacity() * BufferUtil.SIZEOF_FLOAT, vertices, GL.GL_STATIC_DRAW); gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0); VBOIndices = temp[1]; gl.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, VBOIndices); gl.glBufferData(GL.GL_ELEMENT_ARRAY_BUFFER, indices.capacity() * BufferUtil.SIZEOF_SHORT, indices, GL.GL_STATIC_DRAW); gl.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, 0); } public void display(GLAutoDrawable drawable) { GL gl = drawable.getGL(); gl.glEnableClientState(GL.GL_VERTEX_ARRAY); gl.glBindBuffer(GL.GL_ARRAY_BUFFER, VBOVertices); gl.glVertexPointer(3, GL.GL_FLOAT, 0, 0); gl.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, VBOIndices); gl.glDrawElements(GL.GL_TRIANGLES, indices.capacity(), GL.GL_UNSIGNED_SHORT, 0); gl.glDisableClientState(GL.GL_VERTEX_ARRAY); } public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {} public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {} } 
+7
source

Googling for OpenGL VBO gives useful results like this

0
source

Jzy3d makes it easy to draw VBOs in Java based on JOGL.

Here is an example . I wrote how to create a VBO scatter chart. This spread is based on the common DrawableVBO , which can be expanded to set your own geometry / mesh.

0
source

All Articles