JOGL不会绘制2D三角形

时间:2016-12-29 01:49:41

标签: java opengl graphics 2d jogl

我最近一直在研究openGL(特别是JOGL)并且在使用简单的三角形程序时遇到了一些麻烦。我所拥有的是一个顶点数组,我将其传递给VBO以供openGL绘制。但是,三角形不会出现,也不会抛出错误。

以下是相关代码:

public class SimpleTest extends GLCanvas implements GLEventListener{

    private static int width = 600;
    private static int height = 600;

    private GLU glu;
    private IntBuffer VBO;

    public static void main(String args[]){
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                // Create the OpenGL rendering canvas
                GLCanvas canvas = new SimpleTest();
                canvas.setPreferredSize(new Dimension(width,
                        height));

                // Create a animator that drives canvas' display() at the
                // specified FPS.
                final FPSAnimator animator = new FPSAnimator(canvas, 60, true);

                // Create the top-level container
                final JFrame frame = new JFrame(); // Swing's JFrame or AWT's
                                                    // Frame
                frame.getContentPane().add(canvas);
                frame.addWindowListener(new WindowAdapter() {
                    @Override
                    public void windowClosing(WindowEvent e) {
                        // Use a dedicate thread to run the stop() to ensure
                        // that the
                        // animator stops before program exits.
                        new Thread() {
                            @Override
                            public void run() {
                                if (animator.isStarted())
                                    animator.stop();
                                System.exit(0);
                            }
                        }.start();
                    }
                });
                frame.setTitle("2D Traingle");
                frame.pack();
                frame.setVisible(true);
                animator.start(); // start the animation loop
            }
        });
    }

    SimpleTest(){
        this.addGLEventListener(this);
    }

    private void initData(GLAutoDrawable drawable){// gives data to opengl to handle
        GL2 gl = drawable.getGL().getGL2();

        float[] vertices = {
            0.0f, +1.0f,

            -1.0f, -1.0f,

            1.0f, -1.0f,
        };

        FloatBuffer vertexBuffer = GLBuffers.newDirectFloatBuffer(vertices);

        VBO = GLBuffers.newDirectIntBuffer(1);

        gl.glGenBuffers(1, VBO);
        gl.glBindBuffer(GL.GL_ARRAY_BUFFER, VBO.get(0));
        gl.glBufferData(GL.GL_ARRAY_BUFFER, (long)(vertexBuffer.capacity() * Float.BYTES), vertexBuffer, GL.GL_STATIC_DRAW);

        gl.glEnableVertexAttribArray(0);
        gl.glVertexAttribPointer(0, 2, GL.GL_FLOAT, false, 0, 0);
    }

    @Override
    public void display(GLAutoDrawable drawable) {
        GL2 gl = drawable.getGL().getGL2();

        gl.glClear(GL_COLOR_BUFFER_BIT);

        gl.glViewport(0, 0, width, height);

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

    @Override
    public void dispose(GLAutoDrawable drawable) {
        // TODO Auto-generated method stub

    }

    @Override
    public void init(GLAutoDrawable drawable) {

        GL2 gl = drawable.getGL().getGL2();

        glu = new GLU();

        gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // set background (clear) color

        initData(drawable);
    }

    @Override
    public void reshape(GLAutoDrawable drawable, int x, int y, int width,
            int height) {

        SimpleTest.width = width;
        SimpleTest.height = height;

        GL2 gl = drawable.getGL().getGL2(); // get the OpenGL 2 graphics context

        // Set the view port (display area) to cover the entire window
        gl.glViewport(0, 0, width, height);
    }
}

0 个答案:

没有答案