Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenGL Java VBO

Tags:

java

opengl

vbo

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

I want to draw cubes of different sizes and positions (no rotation), and I think I should use VBOs for this.

Can anyone could give me some example code or insight on how to use VBOs with Java or even if VBOs are the best choice?

like image 972
Matt Avatar asked Feb 25 '23 01:02

Matt


1 Answers

This is the code I wrote to test VBOs with Java. It uses JOGL instead of LWJGL, but that's 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) {}
}
like image 189
msell Avatar answered Feb 27 '23 09:02

msell