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.Entity; import java.util.List; import java.util.Map; import models.RawModel; import models.TextureModel; import org.lwjgl.opengl.Display; import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.GL13; import org.lwjgl.opengl.GL20; import org.lwjgl.opengl.GL30; import org.lwjgl.util.vector.Matrix4f; import shaders.StaticShader; import textures.ModelTexture; import toolBox.Maths; /** * * @author PC */ public class EntityRenderer { private StaticShader shader; public EntityRenderer(StaticShader shader, Matrix4f projectionMatrix) { this.shader = shader; shader.start(); shader.loadProjectionMatrix(projectionMatrix); shader.stop(); } public void render(Map<TextureModel, List<Entity>> entities) { for (TextureModel model : entities.keySet()) { prepareTextureModel(model); List<Entity> batch = entities.get(model); for (Entity entity : batch) { prepareInstance(entity); GL11.glDrawElements(GL11.GL_TRIANGLES, model.getRawModel().getVertexCount(), GL11.GL_UNSIGNED_INT, 0); } unbindTextureModel(); } } private void prepareTextureModel(TextureModel model) { RawModel rawModel = model.getRawModel(); GL30.glBindVertexArray(rawModel.getVaoId()); GL20.glEnableVertexAttribArray(0); GL20.glEnableVertexAttribArray(1); GL20.glEnableVertexAttribArray(2); ModelTexture texture = model.getTexture(); shader.loadShineVariables(texture.getShineDamper(), texture.getReflectivity()); GL13.glActiveTexture(GL13.GL_TEXTURE0); GL11.glBindTexture(GL11.GL_TEXTURE_2D, model.getTexture().getTextureId()); } private void prepareInstance(Entity entity) { Matrix4f transformationMatrix = Maths.createTransformationMatrix(entity.getPosition(), entity.getRotx(), entity.getRoty(), entity.getRotz(), entity.getScale()); shader.loadTransformationMatrix(transformationMatrix); } private void unbindTextureModel() { GL20.glDisableVertexAttribArray(0); GL20.glDisableVertexAttribArray(1); GL20.glDisableVertexAttribArray(2); GL30.glBindVertexArray(0); } // public void render(Entity entity, StaticShader shader) { // TextureModel textureModel = entity.getModel(); // RawModel model = textureModel.getRawModel(); // GL30.glBindVertexArray(model.getVaoId()); // GL20.glEnableVertexAttribArray(0); // GL20.glEnableVertexAttribArray(1); // GL20.glEnableVertexAttribArray(2); // Matrix4f transformationMatrix = Maths.createTransformationMatrix(entity.getPosition(), entity.getRotx(), entity.getRoty(), entity.getRotz(), entity.getScale()); // shader.loadTransformationMatrix(transformationMatrix); // ModelTexture texture = textureModel.getTexture(); // shader.loadShineVariables(texture.getShineDamper(), texture.getReflectivity()); // GL13.glActiveTexture(GL13.GL_TEXTURE0); // GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureModel.getTexture().getTextureId()); // GL11.glDrawElements(GL11.GL_TRIANGLES, model.getVertexCount(), GL11.GL_UNSIGNED_INT, 0); // GL20.glDisableVertexAttribArray(0); // GL20.glDisableVertexAttribArray(1); // GL20.glDisableVertexAttribArray(2); // GL30.glBindVertexArray(0); // } }