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

Java tutorial

Introduction

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

Source

/*   Copyright 2014 - 2015 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 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.Vector2f;

import com.redthirddivision.quad.rendering.gui.GuiTexture;
import com.redthirddivision.quad.rendering.models.Model;
import com.redthirddivision.quad.rendering.shaders.GuiShader;
import com.redthirddivision.quad.utils.MathHelper;

/**
 * <strong>Project:</strong> QuadEngine <br>
 * <strong>File:</strong> GuiRenderer.java
 *
 * @author <a href = "http://redthirddivision.com/team/blp"> Matthew Rogers</a>
 */
public class GuiRenderer implements SimplifiedRenderer<GuiTexture> {

    private final Model quad;
    private GuiShader shader;

    public GuiRenderer() {
        quad = new Model(new float[] { -1, 1, -1, -1, 1, 1, 1, -1 });
        shader = new GuiShader();
    }

    @Override
    public void render(ArrayList<GuiTexture> elements) {
        shader.start();

        GL30.glBindVertexArray(quad.getVaoID());
        GL20.glEnableVertexAttribArray(0);
        GL11.glEnable(GL11.GL_BLEND);
        GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
        GL11.glDisable(GL11.GL_DEPTH_TEST);
        for (GuiTexture gui : elements) {
            GL13.glActiveTexture(GL13.GL_TEXTURE0);
            GL11.glBindTexture(GL11.GL_TEXTURE_2D, gui.getTexture());
            Matrix4f matrix = MathHelper.createTransformationMatrix(gui.getPosition(), gui.getScale(),
                    new Vector2f(0, 0));
            shader.loadTransformation(matrix);
            GL11.glDrawArrays(GL11.GL_TRIANGLE_STRIP, 0, quad.getVertexCount());
        }

        GL11.glDisable(GL11.GL_BLEND);
        GL11.glEnable(GL11.GL_DEPTH_TEST);
        GL20.glDisableVertexAttribArray(0);
        GL30.glBindVertexArray(0);

        shader.stop();
    }

    public void cleanUp() {
        shader.cleanUp();
    }
}