/* * File: Renderer.java * Author: Joe Phillips * Date: 2/6/08 */ package edu.msoe.phillijw.main; import javax.media.opengl.*; import javax.media.opengl.glu.*; import java.util.*; import java.io.*; import edu.msoe.phillijw.helper.*; import edu.msoe.phillijw.entity.*; public class Renderer implements GLEventListener { private GLU glu = new GLU(); private LinkedList entities = new LinkedList(); private int texture; public Renderer() { entities.add(new FloorEntity()); entities.add(new BlockEntity()); } /** * Called by the drawable to initiate OpenGL rendering by the client. * After all GLEventListeners have been notified of a display event, the * drawable will swap its buffers if necessary. * @author Joe Phillips * @param gLDrawable The GLAutoDrawable object. */ public void display(GLAutoDrawable gLDrawable) { final GL gl = gLDrawable.getGL(); gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); gl.glLoadIdentity(); glu.gluLookAt(5.0, 6.0, 25.0, //eye position 0.0, 5.0, 0.0, //look at 0.0, 1.0, 0.0); //y-axis is up for (Entity e : entities) { gl.glColor3f(0.0f, 0.8f, 0.0f); e.render(gl); } //render hud gl.glBindTexture(GL.GL_TEXTURE_2D, texture); gl.glDisable(GL.GL_DEPTH_TEST); gl.glPushMatrix(); gl.glLoadIdentity(); glu.gluOrtho2D(-1, 1, -1, 1); gl.glBindTexture(GL.GL_TEXTURE_2D, texture); gl.glBegin(GL.GL_QUADS); gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f(-1.0f, -1.0f, 1.0f); gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f( 1.0f, -1.0f, 1.0f); gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f( 1.0f, 1.0f, 1.0f); gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f(-1.0f, 1.0f, 1.0f); gl.glEnd(); gl.glEnable(GL.GL_BLEND); gl.glPopMatrix(); gl.glEnable(GL.GL_DEPTH_TEST); gl.glDisable(GL.GL_BLEND); } /** * Called by the drawable immediately after the OpenGL context is * initialized for the first time. Can be used to perform one-time OpenGL * initialization such as setup of lights and display lists. * @param gLDrawable The GLAutoDrawable object. */ public void init(GLAutoDrawable gLDrawable) { GL gl = gLDrawable.getGL(); gl.glEnable(GL.GL_DEPTH_TEST); gl.glShadeModel(GL.GL_SMOOTH); // Enable Smooth Shading gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Black Background gl.glEnable(GL.GL_LIGHTING); gl.glEnable(GL.GL_LIGHT1); float[] diffuse = new float[] {1.0f, 1.0f, 1.0f, 0.5f}; float[] position = new float[] {2.0f, 2.0f, 10.0f, 1.0f}; gl.glLightfv(GL.GL_LIGHT1, GL.GL_DIFFUSE, diffuse, 0); gl.glLightfv(GL.GL_LIGHT1, GL.GL_POSITION, position, 0); gl.glEnable(GL.GL_TEXTURE_2D); texture = genTexture(gl); gl.glBindTexture(GL.GL_TEXTURE_2D, texture); TextureReader.Texture texture = null; try { texture = TextureReader.readTexture("OMSBerry.png"); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException(e); } makeRGBTexture(gl, glu, texture, GL.GL_TEXTURE_2D, false); gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR); gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR); } /** * Called by the drawable during the first repaint after the component has * been resized. The client can update the viewport and view volume of the * window appropriately, for example by a call to * GL.glViewport(int, int, int, int); note that for convenience the component * has already called GL.glViewport(int, int, int, int)(x, y, width, height) * when this method is called, so the client may not have to do anything in * this method. * @param gLDrawable The GLAutoDrawable object. * @param x The X Coordinate of the viewport rectangle. * @param y The Y coordinate of the viewport rectangle. * @param width The new width of the window. * @param height The new height of the window. */ public void reshape(GLAutoDrawable gLDrawable, int x, int y, int width, int height) { final GL gl = gLDrawable.getGL(); if (height <= 0) { height = 1; } // avoid a divide by zero error! final float h = (float) width / (float) height; gl.glViewport(0, 0, width, height); gl.glMatrixMode(GL.GL_PROJECTION); gl.glLoadIdentity(); glu.gluPerspective(30.0, h, 1.0, 100.0); gl.glMatrixMode(GL.GL_MODELVIEW); gl.glLoadIdentity(); } private void makeRGBTexture(GL gl, GLU glu, TextureReader.Texture img, int target, boolean mipmapped) { if (mipmapped) { glu.gluBuild2DMipmaps(target, GL.GL_RGB8, img.getWidth(), img.getHeight(), GL.GL_RGB, GL.GL_UNSIGNED_BYTE, img.getPixels()); } else { gl.glTexImage2D(target, 0, GL.GL_RGB, img.getWidth(), img.getHeight(), 0, GL.GL_RGB, GL.GL_UNSIGNED_BYTE, img.getPixels()); } } private int genTexture(GL gl) { final int[] tmp = new int[1]; gl.glGenTextures(1, tmp, 0); return tmp[0]; } /** * Called when the display mode has been changed. * !! CURRENTLY UNIMPLEMENTED IN JOGL! * @param gLDrawable The GLAutoDrawable object. * @param modeChanged Indicates if the video mode has changed. * @param deviceChanged Indicates if the video device has changed. */ public void displayChanged(GLAutoDrawable gLDrawable, boolean modeChanged, boolean deviceChanged) { //unavailable } }