Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package lwjglapp; import com.mati.logger.Log; import java.nio.FloatBuffer; import java.nio.IntBuffer; import java.util.ArrayList; import model.Camera; import model.Renderable; import org.lwjgl.BufferUtils; import org.lwjgl.LWJGLException; import static org.lwjgl.opengl.ARBBufferObject.*; import static org.lwjgl.opengl.ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB; import org.lwjgl.opengl.Display; import org.lwjgl.opengl.DisplayMode; import org.lwjgl.opengl.GL11; import org.lwjgl.util.glu.GLU; /** * * @author mati */ class Renderer extends ArrayList<Renderable> implements Renderable { private final int width, height; private Camera camera; public Renderer(int width, int height) { this.width = width; this.height = (height > 0 ? height : 1); initDisplay(); initGL(); } private void initDisplay() { try { Display.setDisplayMode(new DisplayMode(width, height)); Display.setFullscreen(true); Display.create(); } catch (LWJGLException e) { Log.i(e.getLocalizedMessage()); System.exit(0); } } private void initGL() { GL11.glMatrixMode(GL11.GL_PROJECTION); GL11.glLoadIdentity(); GLU.gluPerspective(45.0f, (float) (width / height), 0.1f, 100.0f); GL11.glMatrixMode(GL11.GL_MODELVIEW); // GL11.glLoadIdentity(); // // GL11.glEnable(GL11.GL_TEXTURE_2D); // Enable Texture Mapping ( NEW ) // GL11.glShadeModel(GL11.GL_SMOOTH); GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); GL11.glClearDepth(1.0f); GL11.glEnable(GL11.GL_DEPTH_TEST); GL11.glDepthFunc(GL11.GL_LEQUAL); GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST); } public void preRender() { GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT | GL11.GL_STENCIL_BUFFER_BIT); GL11.glMatrixMode(GL11.GL_PROJECTION); GL11.glLoadIdentity(); GL11.glMatrixMode(GL11.GL_MODELVIEW); GL11.glLoadIdentity(); } @Override public void render() { camera.render(); this.stream().forEach(r -> r.render()); drawVBO(); } public void setCamera(Camera camera) { this.camera = camera; } void drawVBO() { FloatBuffer cBuffer = BufferUtils.createFloatBuffer(9); cBuffer.put(1).put(0).put(0); cBuffer.put(0).put(1).put(0); cBuffer.put(0).put(0).put(1); cBuffer.flip(); FloatBuffer vBuffer = BufferUtils.createFloatBuffer(9); vBuffer.put(-0.5f).put(-0.5f).put(0.0f); vBuffer.put(+0.5f).put(-0.5f).put(0.0f); vBuffer.put(+0.5f).put(+0.5f).put(0.0f); vBuffer.flip(); IntBuffer ib = BufferUtils.createIntBuffer(2); glGenBuffersARB(ib); int vHandle = ib.get(0); int cHandle = ib.get(1); GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY); GL11.glEnableClientState(GL11.GL_COLOR_ARRAY); glBindBufferARB(GL_ARRAY_BUFFER_ARB, vHandle); glBufferDataARB(GL_ARRAY_BUFFER_ARB, vBuffer, GL_STATIC_DRAW_ARB); GL11.glVertexPointer(3, GL11.GL_FLOAT, /* stride */ 3 << 2, 0L); glBindBufferARB(GL_ARRAY_BUFFER_ARB, cHandle); glBufferDataARB(GL_ARRAY_BUFFER_ARB, cBuffer, GL_STATIC_DRAW_ARB); GL11.glColorPointer(3, GL11.GL_FLOAT, /* stride */ 3 << 2, 0L); GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, 3 /* elements */); glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0); GL11.glDisableClientState(GL11.GL_COLOR_ARRAY); GL11.glDisableClientState(GL11.GL_VERTEX_ARRAY); // cleanup VBO handles ib.put(0, vHandle); ib.put(1, cHandle); glDeleteBuffersARB(ib); } }