Login  Register

Re: nehe port - where to find the resources (images)?

Posted by rtayek on Sep 01, 2012; 1:16am
URL: https://forum.jogamp.org/nehe-port-where-to-find-the-resources-images-tp4025981p4025996.html

not sure why my code was throwing, but this code works fine:

import java.nio.FloatBuffer;
import java.nio.ShortBuffer;

import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.awt.GLJPanel;
import javax.media.opengl.fixedfunc.GLPointerFunc;
import javax.swing.JFrame;
import javax.swing.WindowConstants;

import com.jogamp.common.nio.Buffers;
public class VBOGLJPanel extends GLJPanel implements GLEventListener {
        public static void main(String[] args) {
                JFrame frame=new JFrame();
                frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
                VBOGLJPanel canvas=new VBOGLJPanel();
                canvas.addGLEventListener(canvas);
                frame.add(canvas);
                frame.setSize(640,480);
                frame.setVisible(true);
        }
        @Override public void init(GLAutoDrawable drawable) {
                vboInit(drawable);
        }
        private void vboInit(GLAutoDrawable drawable) {
                float[] vertexArray={-0.5f,0.5f,0,0.5f,0.5f,0,0.5f,-0.5f,0,-0.5f,-0.5f,0};
                FloatBuffer vertices=Buffers.newDirectFloatBuffer(vertexArray.length);
                vertices.put(vertexArray);
                vertices.flip();
                short[] indexArray={0,1,2,1/* 0 */,2,3};
                ShortBuffer indices=Buffers.newDirectShortBuffer(indexArray.length);
                indices.put(indexArray);
                indices.flip();
                GL gl=drawable.getGL();
                gl.glGenBuffers(1,VBOVertices,0);
                gl.glBindBuffer(GL.GL_ARRAY_BUFFER,VBOVertices[0]);
                gl.glBufferData(GL.GL_ARRAY_BUFFER,vertices.capacity()*Buffers.SIZEOF_FLOAT,vertices,GL.GL_STATIC_DRAW);
                gl.glBindBuffer(GL.GL_ARRAY_BUFFER,0);
                gl.glGenBuffers(1,VBOIndices,0);
                gl.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER,VBOIndices[0]);
                gl.glBufferData(GL.GL_ELEMENT_ARRAY_BUFFER,indices.capacity()*Buffers.SIZEOF_SHORT,indices,GL.GL_DYNAMIC_DRAW);
                gl.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER,0);
                indicesCapacity=indices.capacity();
        }
        private void vboRender(GLAutoDrawable drawable) {
                GL2 gl=drawable.getGL().getGL2();
                gl.glEnableClientState(GLPointerFunc.GL_VERTEX_ARRAY);
                gl.glBindBuffer(GL.GL_ARRAY_BUFFER,VBOVertices[0]);
                gl.glVertexPointer(3,GL.GL_FLOAT,0,0);
                gl.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER,VBOIndices[0]);
                gl.glDrawElements(GL.GL_TRIANGLES,indicesCapacity,GL.GL_UNSIGNED_SHORT,0);
                gl.glDisableClientState(GLPointerFunc.GL_VERTEX_ARRAY);
        }
        @Override public void display(GLAutoDrawable drawable) {
                vboRender(drawable);
        }
        @Override public void reshape(GLAutoDrawable drawable,int x,int y,int width,int height) {}
        public void displayChanged(GLAutoDrawable drawable,boolean modeChanged,boolean deviceChanged) {}
        @Override public void dispose(GLAutoDrawable drawable) {}
        private int[] VBOVertices=new int[1];
        private int[] VBOIndices=new int[1];
        int indicesCapacity;
        private static final long serialVersionUID=1L;
}