简体   繁体   English

OpenGL Java VBO

[英]OpenGL Java 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.我正在使用LWJGL并使用glBegin/glEnd绘制立方体,但我听说这种方法效率很低,我应该开始使用 VBO。 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 VBO s for this.我想绘制不同大小和位置的立方体(无旋转),我认为我应该为此使用VBO

Can anyone could give me some example code or insight on how to use VBO s with Java or even if VBO s are the best choice?谁能给我一些示例代码或有关如何将VBOJava一起使用的示例代码或见解,或者即使VBO是最佳选择?

This is the code I wrote to test VBOs with Java.这是我用 Java 测试 VBO 编写的代码。 It uses JOGL instead of LWJGL, but that's a minor thing.它使用 JOGL 而不是 LWJGL,但这是一件小事。

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).除了 glVertexPointer,您还可以使用 glTexCoordPointer 和 glNormalPointer 指定纹理坐标和法线的数据,并使用 glEnableClientState(GL.GL_TEXTURE_COORD_ARRAY) 和 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) {}
}

Jzy3d allows drawing VBO easily in Java based on JOGL. Jzy3d 允许在基于 JOGL 的 Java 中轻松绘制 VBO。

Here is an example I wrote showing how to create a VBO scatter plot.是我写的一个例子,展示了如何创建一个 VBO scatter plot。 This scatter is based on a generic DrawableVBO which can be extended to set your own geometry/mesh.这个分散基于一个通用的DrawableVBO ,它可以被扩展来设置你自己的几何/网格。

Googling for OpenGL VBO gives useful results like this one谷歌搜索 OpenGL VBO 提供了类似这样的有用结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM