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 main; import entities.Camera; import entities.Entity; import entities.Light; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import models.TextureModel; import org.lwjgl.opengl.Display; import org.lwjgl.opengl.GL11; import org.lwjgl.util.vector.Matrix4f; import shaders.StaticShader; import shaders.TerrainShader; import terrains.Terrain; /** * * @author PC */ public class MasterRenderer { private static final float FOV = 70; private static final float NEAR_PLANE = 0.1f; private static final float FAR_PLANE = 1000; private Matrix4f projectionMatrix; private StaticShader shader = new StaticShader(); private EntityRenderer render; private TerrainRenderer terrainRenderer; private TerrainShader terrainShader = new TerrainShader(); private Map<TextureModel, List<Entity>> entities = new HashMap<TextureModel, List<Entity>>(); private List<Terrain> terrains = new ArrayList<Terrain>(); public MasterRenderer() { GL11.glEnable(GL11.GL_CULL_FACE); GL11.glCullFace(GL11.GL_BACK); createProjetionMatrix(); render = new EntityRenderer(shader, projectionMatrix); terrainRenderer = new TerrainRenderer(terrainShader, projectionMatrix); } public void render(Light sun, Camera camera) { prepare(); shader.start(); shader.loadLigt(sun); shader.loadViewMatrix(camera); render.render(entities); shader.stop(); terrainShader.start(); terrainShader.loadLigt(sun); terrainShader.loadViewMatrix(camera); terrainRenderer.render(terrains); terrainShader.stop(); terrains.clear(); entities.clear(); } public void prepare() { GL11.glEnable(GL11.GL_DEPTH_TEST); GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); GL11.glClearColor(0.3f, 0.8f, 0.8f, 1); } public void processTerrain(Terrain terrain) { terrains.add(terrain); } public void processEntity(Entity entity) { TextureModel entityModel = entity.getModel(); List<Entity> batch = entities.get(entityModel); if (batch != null) { batch.add(entity); } else { List<Entity> newBatch = new ArrayList<>(); newBatch.add(entity); entities.put(entityModel, newBatch); } } public void cleanUp() { shader.cleanUp(); terrainShader.cleanUp(); } private void createProjetionMatrix() { float aspectRatio = (float) Display.getWidth() / (float) Display.getHeight(); float y_scale = (float) ((1f / Math.tan(Math.toRadians(FOV / 2f))) * aspectRatio); float x_scale = y_scale / aspectRatio; float frustum_length = FAR_PLANE - NEAR_PLANE; projectionMatrix = new Matrix4f(); projectionMatrix.m00 = x_scale; projectionMatrix.m11 = y_scale; projectionMatrix.m22 = -((FAR_PLANE + NEAR_PLANE) / frustum_length); projectionMatrix.m23 = -1; projectionMatrix.m32 = -((2 * NEAR_PLANE * FAR_PLANE) / frustum_length); projectionMatrix.m33 = 0; } }