com.redthirddivision.quad.rendering.renderers.EntityRenderer.java Source code

Java tutorial

Introduction

Here is the source code for com.redthirddivision.quad.rendering.renderers.EntityRenderer.java

Source

/*   Copyright 2014 Matthew Rogers "BossLetsPlays"
*
*   Licensed under the Apache License, Version 2.0 (the "License");
*   you may not use this file except in compliance with the License.
*   You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
*   Unless required by applicable law or agreed to in writing, software
*   distributed under the License is distributed on an "AS IS" BASIS,
*   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*   See the License for the specific language governing permissions and
*   limitations under the License.
*/
package com.redthirddivision.quad.rendering.renderers;

import java.util.ArrayList;
import java.util.Map;

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 com.redthirddivision.quad.core.entities.Entity;
import com.redthirddivision.quad.rendering.materials.Material;
import com.redthirddivision.quad.rendering.models.Model;
import com.redthirddivision.quad.rendering.shaders.StaticShader;
import com.redthirddivision.quad.utils.MathHelper;
import com.redthirddivision.quad.utils.Util;

/**
 * <strong>Project:</strong> QuadEngine <br>
 * <strong>File:</strong> EntityRenderer.java
 *
 * @author <a href = "http://redthirddivision.com/team/blp"> Matthew Rogers</a>
 */
public class EntityRenderer extends BaseRenderer<Model, Entity> {

    private StaticShader shader;

    public EntityRenderer(StaticShader shader, Matrix4f projection) {
        this.shader = shader;
        shader.start();
        shader.loadProjection(projection);
        shader.stop();
    }

    public void render(Map<Model, ArrayList<Entity>> entities) {
        for (Model model : entities.keySet()) {
            prepare(model);
            ArrayList<Entity> batch = entities.get(model);
            for (Entity e : batch) {
                prepareInstance(e);
                GL11.glDrawElements(GL11.GL_TRIANGLES, model.getVertexCount(), GL11.GL_UNSIGNED_INT, 0);
            }

            unbindTexturedModel();
        }
    }

    protected void prepare(Model model) {
        GL30.glBindVertexArray(model.getVaoID());
        GL20.glEnableVertexAttribArray(0);
        GL20.glEnableVertexAttribArray(1);
        GL20.glEnableVertexAttribArray(2);

        Material material = model.getMaterial();
        shader.loadNumRows(material.getNumRows());
        if (material.hasTransparency())
            Util.disableCulling();
        shader.loadFakeLighting(material.isUseingFakeLighting());
        shader.loadShine(material.getShineDamper(), material.getReflectivity());

        GL13.glActiveTexture(GL13.GL_TEXTURE0);
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, model.getMaterial().getTextureID());
    }

    @Override
    protected void prepareInstance(Entity element) {
        Matrix4f transformation = MathHelper.transformationMatrix(element.getPosition(), element.getRotation(),
                element.getScale());
        shader.loadTransformation(transformation);
        shader.loadOffset(element.getTextureOffsetX(), element.getTextureOffsetY());
    }

    @Override
    protected void unbindTexturedModel() {
        Util.enableCulling();
        GL20.glDisableVertexAttribArray(0);
        GL20.glDisableVertexAttribArray(1);
        GL20.glDisableVertexAttribArray(2);
        GL30.glBindVertexArray(0);
    }

    @Override
    public void render(ArrayList<Entity> elements) {
    }

}