package Hexel.rendering;
import java.util.ArrayList;
import java.util.HashMap;
import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import javax.media.opengl.GL2ES1;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.fixedfunc.GLMatrixFunc;
import javax.media.opengl.glu.GLU;
import Hexel.Engine;
import Hexel.PCInput;
import Hexel.Stage;
import Hexel.chunk.Chunks;
import Hexel.math.Vector3i;
import Hexel.rendering.overlays.GameOverlay;
import Hexel.things.types.Controllable;
import Hexel.things.types.Thing;
import Hexel.util.TickHandler;
import com.jogamp.opengl.util.FPSAnimator;
public class Renderer implements GLEventListener {
private Camera camera;
public static Camera dbgCamera;
public void setCamera(Camera camera) {
dbgCamera = camera;
this.camera = camera;
}
public Window window;
private GLU glu;
private TickHandler tickHandler;
public FPSAnimator animator;
private Engine engine;
public ChunkRenderer chunkRenderer;
private ThingRenderer thingRenderer;
public void loadChunks(ArrayList<Vector3i> points) {
this.chunkRenderer.loadChunks(points);
}
public boolean hasChunk(Vector3i point) {
return this.chunkRenderer.hasChunk(point);
}
public void unloadGLChunk(Vector3i points) {
this.chunkRenderer.unloadGLChunk(points);
}
public HashMap<Vector3i, GLChunk> getGLChunkTable() {
return this.chunkRenderer.getGLChunkTable();
}
public void sendInputToCurrentOverlay(PCInput input){
this.overlay.sendInputToCurrentOverlay(input);
}
public GameOverlay overlay;
public void setControllable(Controllable controllable) {
this.overlay.setControllable(controllable);
}
public void setStage(Stage stage) {
this.overlay.setStage(stage);
}
public Renderer(Engine engine, Chunks chunks) {
this.engine = engine;
this.window = new Window();
this.chunkRenderer = new ChunkRenderer(engine, chunks);
this.thingRenderer = new ThingRenderer(engine.thingSimulator);
this.overlay = new GameOverlay(engine);
this.window.getCanvas().addGLEventListener(this);
}
public void start(TickHandler tickHandler) {
this.tickHandler = tickHandler;
this.animator = new FPSAnimator(this.window.getCanvas(), 60);
this.animator.start();
}
@Override
public void display(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2();
if (this.tickHandler != null) {
this.tickHandler.onTick();
update(gl);
render(gl);
this.overlay.display();
}
}
@Override
public void dispose(GLAutoDrawable drawable) {
}
@Override
public void init(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2();
this.glu = GLU.createGLU();
gl.glClearColor(0x00 / 255f, 0x7d / 255f, 0xdf / 255f, 0f);
gl.glClearDepthf(1.0f);
gl.glEnable(GL.GL_DEPTH_TEST);
gl.glDepthFunc(GL.GL_LEQUAL);
gl.glHint(GL2ES1.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);
this.chunkRenderer.init(drawable, gl);
this.overlay.init(drawable);
}
@Override
public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
GL2 gl = drawable.getGL().getGL2();
gl.glViewport(0, 0, w, h);
gl.glMatrixMode(GLMatrixFunc.GL_PROJECTION);
gl.glLoadIdentity();
this.overlay.updateSize(w, h);
this.glu.gluPerspective(60.0f, w * 1.0f / h, .01f, 1000.0f); // FOV,
// AspectRatio,
// NearClip,
// FarClip
}
private void update(GL2 gl) {
this.chunkRenderer.update(gl);
}
private void render(GL2 gl) {
float light = (float)engine.getAmbientLight();
gl.glClearColor(0x00 / 255f * light, 0x7d / 255f * light, 0xdf / 255f * light, 0f);
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
gl.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
if (this.camera == null)
return;
gl.glLoadIdentity();
gl.glRotatef((float) -this.camera.getCameraXRot(), 1, 0, 0);
gl.glRotatef((float) -this.camera.getCameraZRot(), 0, 0, 1);
gl.glTranslatef((float) -this.camera.getCameraX(),
(float) -this.camera.getCameraY(),
(float) -this.camera.getCameraZ());
this.chunkRenderer.render(gl);
this.thingRenderer.render(gl);
gl.glEnd();
}
}