jogamp简单三角形没有显示在屏幕上

avkwfej4  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(289)

我想在Windows上画个三角形。我得到窗口出现,但窗口是黑色的,没有三角形出现在窗口中。着色器文件可以打开(如果没有,我会得到一个filenotfoundexception),但是三角形不会显示在屏幕上。以下是我的代码:

public class GoodbyeSimpleWorld implements GLEventListener, KeyListener {
    private static GLWindow window;
    private static Animator animator;

    float vertices[] = {
            -0.5f, -0.5f, 0.0f,
            0.5f, -0.5f, 0.0f,
            0.0f,  0.5f, 0.0f
    };

    public static void main(String[] args) {
        new GoodbyeSimpleWorld().setup();
    }

    private void setup() {

        GLProfile glProfile = GLProfile.get(GLProfile.GL3);
        GLCapabilities glCapabilities = new GLCapabilities(glProfile);

        window = GLWindow.create(glCapabilities);

        window.setTitle("Goodbye Simple World");
        window.setSize(1024, 768);

        window.addGLEventListener(this);
        window.addKeyListener(this);

        window.setVisible(true);

        window.addGLEventListener(this);
        window.addKeyListener(this);

        window.addWindowListener(new WindowAdapter(){
            @Override
            public void windowDestroyed(WindowEvent e) {
                animator.stop();
                System.exit(0);
            }
        });

        animator = new Animator();
        animator.start();

    }

    @Override
    public void init(GLAutoDrawable drawable) {
        GL4 gl = drawable.getGL().getGL4();
        IntBuffer VBO = GLBuffers.newDirectIntBuffer(1);
        IntBuffer VAO = GLBuffers.newDirectIntBuffer(1);
        gl.glGenVertexArrays(1, VAO);
        gl.glGenBuffers(1, VBO);

        ShaderCode vertexShader = ShaderCode.create(gl, GL_VERTEX_SHADER, this.getClass(),
                "shaders/gl3", null, "goodbye", "vert", null, true);
        vertexShader.compile(gl);

        ShaderCode fragmentShader = ShaderCode.create(gl, GL_FRAGMENT_SHADER, this.getClass(),
                "shaders/gl3", null, "goodbye", "frag", null, true);
        fragmentShader.compile(gl);

        int shaderProgram = gl.glCreateProgram();
        gl.glAttachShader(shaderProgram, vertexShader.id());
        gl.glAttachShader(shaderProgram, fragmentShader.id());
        gl.glLinkProgram(shaderProgram);

        gl.glBindVertexArray(VAO.get());

        gl.glBindBuffer(GL.GL_ARRAY_BUFFER, VBO.get());
        gl.glBufferData(GL.GL_ARRAY_BUFFER, vertices.length, VBO, gl.GL_STATIC_DRAW);

        gl.glVertexAttribPointer(0, 3, GL_FLOAT, false, 3 * Float.BYTES, Long.MIN_VALUE);
        gl.glEnableVertexAttribArray(0);

        gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);

        gl.glBindVertexArray(0);

        gl.glUseProgram(shaderProgram);
        gl.glDeleteShader(vertexShader.id());
        gl.glDeleteShader(fragmentShader.id());

        gl.glDrawArrays(GL_TRIANGLES, 0, 3);
    }
...
}
osh3o9ms

osh3o9ms1#

我看到了几个问题: VBO 是一个包含vbo名称的缓冲区,但您将其传递给 glBufferData 如果希望将数据存储在vbo中,则从不复制 vertices 将数组放入缓冲区
你打电话来 glVertexAttribPointerLong.MIN_VALUE ,大约是-2^63,但是你应该放在那里的值是 0 在执行实际的draw调用时,没有vao绑定
你从来没有 SwapBuffers 随便打电话。请注意,gleventlistener可以设置为 setAutoSwapBufferMode 模式,但这会影响 dispay 方法,让您的绘图调用 init 没什么意义。

相关问题