1

I've been looking into openGL recently (specifically JOGL) and have had some trouble with a simple triangle program. All I have is an array of vertices which I pass to a VBO for openGL to draw. However, the triangles do not appear and no errors are thrown.

Here is the code in question:

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);
    }
}
Everthings
  • 11
  • 2
  • You've got a valid question, but I'm not sure it's [minimal](http://stackoverflow.com/help/mcve)... What have you found using your IDE's debugger? – Travis Dec 29 '16 at 03:02
  • You should choose a title that doesn't imply that you're sure JOGL is to blame as obviously JOGL can draw triangles. You should replace "new GLU()" by "GLU.createGLU(gl)" as the generic GLU instance isn't very capable, you risk to get an UnsupportedOperationException. – gouessej Dec 29 '16 at 16:16
  • I advise you to look at this example: http://jogamp.org/git/?p=jogl-demos.git;a=blob;f=src/demos/es2/RawGL2ES2demo.java;hb=HEAD If you need a rudimentary example, look at those of our official wiki: http://jogamp.org/wiki/index.php/Jogl_Tutorial#JogAmp.27s_Static – gouessej Dec 29 '16 at 16:20

0 Answers0