All pastes #1612623 Raw Edit

LWJGL Test

public java v1 · immutable
#1612623 ·published 2009-10-11 15:19 UTC
rendered paste body
import java.nio.*;import org.lwjgl.*;import org.lwjgl.opengl.*;import static org.lwjgl.opengl.ARBBufferObject.*;import static org.lwjgl.opengl.GL11.*;import static org.lwjgl.util.glu.GLU.*;import static org.lwjgl.opengl.ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB;public class TestGLCube {	private static final long serialVersionUID = 2L;	public static void main(String[] args) {		new TestGLCube();	}			public TestGLCube() {				try {			createDisplay();		} catch (LWJGLException e) {			System.out.println("oh no");			e.printStackTrace();		}				init();				        while (!Display.isCloseRequested()) {        	// Rendercode	, HIERZO wordt het beeld getekend... en dat gebeurd nu niet        	        	this.display();        	            Display.update();        }                Display.destroy();        System.exit(0);			}		private void createDisplay() throws LWJGLException {		DisplayMode[] modes = Display.getAvailableDisplayModes();		DisplayMode picked = null;		for(DisplayMode mode: modes)		   if(mode.getWidth() == 800 && mode.getHeight() == 600)		      picked = mode;		Display.setDisplayMode(picked);        Display.setFullscreen(false);        Display.create();	}	public void init() {		vertices = BufferUtils.createFloatBuffer(vertexCount*3);		float[] coords = {-100, 100, 100,						  -100, 100,-100,						  -100,-100,-100,						  -100,-100, 100,						   100, 100, 100,						   100, 100,-100,						   100,-100,-100,						   100,-100, 100};		vertices.put(coords);		byte[] indData = {1,4,3,3,4,6,6,3,2,2,7,6,6,4,5,5,7,6,7,5,2,2,5,1,1,5,4,4,0,1,1,2,3,3,1,0};		indices = BufferUtils.createByteBuffer(indCount);		indices.put(indData);			vertices.flip();		indices.flip();					buildVBOs();	}		public void display() {        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);        setCamera(new float[]{250,250,-500,0,0,0,0,1,0});        draw();                glFlush();			}		private void draw() {		for (int i = 0; i < 10000; ++i) {	        glPushMatrix(); {	            setClientStates(true);	            	            glBindBufferARB(GL_ARRAY_BUFFER_ARB, vboHandle);	            glVertexPointer(3,GL_FLOAT,0,0);	            	            glDrawElements(GL_TRIANGLES,indices);	            	            setClientStates(false);	        } glPopMatrix();		}    }		private void setCamera(float[] c) {        if (c.length != 9)    // Should never occur, since it's only called from the inside            throw new IllegalArgumentException("Invalid camData length");                // Change to projection matrix.        GL11.glMatrixMode(GL11.GL_PROJECTION);        glLoadIdentity();        // Perspective.                float widthHeightRatio = (float)1440/(float)900;        gluPerspective(45,widthHeightRatio,.001f,1000);        gluLookAt(c[0],c[1],c[2],c[3],c[4],c[5],c[6],c[7],c[8]);        // Change back to model view matrix.        glMatrixMode(GL11.GL_MODELVIEW);        glLoadIdentity();    }		private void setClientStates(final boolean enable) {        if (enable) {        	//System.out.println("Enabling client states");        	glEnableClientState(GL11.GL_VERTEX_ARRAY);        }        else {        	//System.out.println("Disabling client states");        	glDisableClientState(GL11.GL_VERTEX_ARRAY);        }            }		private void buildVBOs() {			    glGenBuffersARB(HANDLE_HOLDER);	    vboHandle = HANDLE_HOLDER.get(0);		        glBindBufferARB(GL_ARRAY_BUFFER_ARB,vboHandle);        glBufferDataARB(GL_ARRAY_BUFFER_ARB,vertices,GL_STATIC_DRAW_ARB);	}		private static final IntBuffer HANDLE_HOLDER;	int vboHandle;        private int vertexCount = 8;	// Cube, 8 vertices    private int indCount = 36;	// Cube, 36 indices    private FloatBuffer vertices;    private ByteBuffer indices;            static {    	HANDLE_HOLDER = BufferUtils.createIntBuffer(1);    }}