How to use index buffers in JOGL?

Can someone show me how to use index buffers in JOGL? Just a simple square made of two triangles will do. If you want you could show how to bind colors to the vertecies also.
Thanks

Moin,

i’m not quite sure whether this is what you wanted, but maybe it helps:

package fancy.jg.vertexarray;

import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;

import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCanvas;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.glu.GLU;

import com.sun.opengl.util.Animator;
import com.sun.opengl.util.BufferUtil;

public class VertexArray implements GLEventListener {
    
    
    private static final float[] staticVertices = new float[] {
        -1.0f, -1.0f, 0.0f,
         1.0f, -1.0f, 0.0f,
         1.0f,  1.0f, 0.0f,
        -1.0f,  1.0f, 0.0f
    };
    
    
    private static final float[] staticColors = new float[] {
        1.0f, 0.0f, 0.0f,
        0.0f, 1.0f, 0.0f,
        0.0f, 0.0f, 1.0f,
        1.0f, 1.0f, 1.0f
    };
    
    
    private static final int[] staticIndices = new int[] {
        0, 1, 2, 0, 2, 3
    };

    
    private GL                   gl             = null;
    private GLU                  glu            = null;

    private FloatBuffer          vaVertices     = null;
    private FloatBuffer          vaColors       = null;
    private IntBuffer            vaIndices      = null;


    @Override
    public void init(final GLAutoDrawable drawable) {

        gl = drawable.getGL();
        glu = new GLU();

        vaVertices = BufferUtil.newFloatBuffer(staticVertices.length);
        vaVertices.put(staticVertices, 0, staticVertices.length);
        vaVertices.rewind();

        vaColors = BufferUtil.newFloatBuffer(staticColors.length);
        vaColors.put(staticColors, 0, staticColors.length);
        vaColors.rewind();

        vaIndices = BufferUtil.newIntBuffer(staticIndices.length);
        vaIndices.put(staticIndices, 0, staticIndices.length);
        vaIndices.rewind();

        gl.glEnableClientState(GL.GL_VERTEX_ARRAY);
        gl.glEnableClientState(GL.GL_COLOR_ARRAY);


    }


    @Override
    public void display(final GLAutoDrawable drawable) {

        gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
        gl.glLoadIdentity();

        glu.gluLookAt(0, 0, 10, 0, 0, 0, 0, 1, 0);

        gl.glVertexPointer(3, GL.GL_FLOAT, 0, vaVertices);
        gl.glColorPointer(3, GL.GL_FLOAT, 0, vaColors);
        gl.glDrawElements(GL.GL_TRIANGLES, staticIndices.length, GL.GL_UNSIGNED_INT, vaIndices);
    }


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

        if (height <= 0) height = 1;

        gl.glMatrixMode(GL.GL_PROJECTION);
        gl.glLoadIdentity();
        glu.gluPerspective(45.0f, (float) width / (float) height, 1.0, 50.0);
        gl.glMatrixMode(GL.GL_MODELVIEW);
        gl.glLoadIdentity();
    }


    @Override
    public void displayChanged(final GLAutoDrawable drawable, final boolean modeChanged, final boolean deviceChanged) {

    }


    public static void main(final String[] args) {

        final VertexArray main = new VertexArray();
        final Frame frame = new Frame();
        final GLCanvas canvas = new GLCanvas();
        final Animator animator = new Animator(canvas);
        canvas.addGLEventListener(main);
        frame.add(canvas);
        frame.setSize(500, 500);
        frame.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(final WindowEvent e) {

                new Thread(new Runnable() {

                    public void run() {

                        animator.stop();
                        System.exit(0);
                        
                    }
                    
                }).start();
                
            }
        });
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        animator.start();
    }

}

regards,
Michael

Thank you so much, looks like this is it. I’ll have to add this to my project and test it out.

Great it works and shows the Square. I apprecate it, now I have to learn how to apply textures, but I haven’t read much about them so I’ll post if I need help. Thanks again.