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

Java tutorial

Introduction

Here is the source code for com.redthirddivision.quad.rendering.renderers.TerrainRenderer.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 org.lwjgl.util.vector.Vector3f;

import com.redthirddivision.quad.rendering.materials.TerrainTexturePack;
import com.redthirddivision.quad.rendering.models.Model;
import com.redthirddivision.quad.rendering.shaders.TerrainShader;
import com.redthirddivision.quad.utils.MathHelper;
import com.redthirddivision.quad.world.Terrain;

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

    private TerrainShader shader;

    public TerrainRenderer(TerrainShader shader, Matrix4f projection) {
        this.shader = shader;
        shader.start();
        shader.loadProjection(projection);
        shader.connectTextureUnits();
        shader.stop();
    }

    @Override
    public void render(ArrayList<Terrain> terrains) {
        for (Terrain terrain : terrains) {
            prepare(terrain);
            prepareInstance(terrain);
            GL11.glDrawElements(GL11.GL_TRIANGLES, terrain.getModel().getVertexCount(), GL11.GL_UNSIGNED_INT, 0);
            unbindTexturedModel();
        }
    }

    @Override
    protected void prepare(Terrain terrain) {
        Model prim = terrain.getModel();
        GL30.glBindVertexArray(prim.getVaoID());
        GL20.glEnableVertexAttribArray(0);
        GL20.glEnableVertexAttribArray(1);
        GL20.glEnableVertexAttribArray(2);

        bindTextures(terrain);
        shader.loadShine(1, 0);
    }

    private void bindTextures(Terrain terrain) {
        TerrainTexturePack texturePack = terrain.getTexturePack();
        GL13.glActiveTexture(GL13.GL_TEXTURE0);
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, texturePack.getBackgroundTexture().getTextureID());

        GL13.glActiveTexture(GL13.GL_TEXTURE1);
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, texturePack.getrTexture().getTextureID());

        GL13.glActiveTexture(GL13.GL_TEXTURE2);
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, texturePack.getgTexture().getTextureID());

        GL13.glActiveTexture(GL13.GL_TEXTURE3);
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, texturePack.getbTexture().getTextureID());

        GL13.glActiveTexture(GL13.GL_TEXTURE4);
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, terrain.getBlendMap().getTextureID());
    }

    @Override
    protected void prepareInstance(Terrain terrain) {
        Matrix4f transformation = MathHelper.transformationMatrix(new Vector3f(terrain.getX(), 0, terrain.getZ()),
                new Vector3f(0, 0, 0), 1);
        shader.loadTransformation(transformation);
    }

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

    @Override
    public void render(Map<Terrain, ArrayList<Terrain>> elements) {
    }

}