render.Quad.java Source code

Java tutorial

Introduction

Here is the source code for render.Quad.java

Source

/*
 * 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 render;

import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import opengl.GL;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL20;
import org.lwjgl.util.glu.GLU;

import utils.ShaderHandler;

/**
 * TODO Vernnftige Funktionalitt einbauen!
 *
 * @author andreas
 */
public class Quad {

    //Quad
    private int vaoId = 0;
    private int vboId = 0;

    private int vbindexId = 0;
    private int indexCnt = 0;
    //shader
    private int vsId = 0;
    private int fsId = 0;
    private int programId = 0;

    public void draw() {

        VertexData v0 = new VertexData();
        v0.setVertexPos(-0.5f, 0.5f, 0f);
        v0.setVertexColor(1f, 0f, 0f);
        VertexData v1 = new VertexData();
        v1.setVertexPos(-0.5f, -0.5f, 0f);
        v1.setVertexColor(0f, 1f, 0f);
        VertexData v2 = new VertexData();
        v2.setVertexPos(0.5f, -0.5f, 0f);
        v2.setVertexColor(0f, 0f, 1f);
        VertexData v3 = new VertexData();
        v3.setVertexPos(0.5f, 0.5f, 0f);
        v3.setVertexColor(1f, 1f, 1f);

        VertexData[] vertices = new VertexData[] { v0, v1, v2, v3 };
        FloatBuffer vertexBuff = BufferUtils.createFloatBuffer(vertices.length * VertexData.ELEMENTCNT);

        for (int i = 0; i < vertices.length; i++) {
            vertexBuff.put(vertices[i].getPos());
            vertexBuff.put(vertices[i].getColor());
        }

        vertexBuff.flip();

        int[] indices = { 0, 1, 2, 2, 3, 0 };

        indexCnt = indices.length;
        IntBuffer indexBuff = BufferUtils.createIntBuffer(indexCnt);
        indexBuff.put(indices);
        indexBuff.flip();

        //Vertexarray
        vaoId = GL.glGenVertexArrays();
        GL.glBindVertexArray(vaoId);

        //Vertexbuffer (vertices)
        vboId = GL.glGenBuffers();
        GL.glBindBuffer(GL.GL_ARRAY_BUFFER, vboId);
        GL.glBufferData(GL.GL_ARRAY_BUFFER, vertexBuff, GL.GL_STATIC_DRAW);
        GL.glVertexAttribPointer(0, 4, GL.GL_FLOAT, false, VertexData.VERTEXSIZE, 0);
        GL.glVertexAttribPointer(1, 4, GL.GL_FLOAT, false, VertexData.VERTEXSIZE, VertexData.BYTESIZE * 4);
        GL.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);

        GL.glBindVertexArray(0);

        //Indexbuffer
        vbindexId = GL.glGenBuffers();
        GL.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, vbindexId);
        GL.glBufferData(GL.GL_ELEMENT_ARRAY_BUFFER, indexBuff, GL.GL_STATIC_DRAW);
        GL.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, 0);

    }

    public void quadLoop() {
        GL.glClear(GL.GL_COLOR_BUFFER_BIT);

        GL.glUseProgram(programId);

        GL.glBindVertexArray(vaoId);
        GL.glEnableVertexAttribArray(0);
        GL.glEnableVertexAttribArray(1);

        GL.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, vbindexId);
        GL.glDrawElements(GL.GL_TRIANGLES, indexCnt, GL.GL_UNSIGNED_INT, 0);

        GL.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, 0);
        GL20.glDisableVertexAttribArray(0);
        GL20.glDisableVertexAttribArray(1);
        GL.glBindVertexArray(0);
        GL.glUseProgram(0);

    }

    public void initShaders() {

        int errValue;
        ShaderHandler help = new ShaderHandler();

        vsId = help.readShader("./shader/VertexShader.glsl", GL.GL_VERTEX_SHADER);
        fsId = help.readShader("./shader/FragmentShader.glsl", GL.GL_FRAGMENT_SHADER);

        programId = GL.glCreateProgram();
        GL.glAttachShader(programId, vsId);
        GL.glAttachShader(programId, fsId);

        GL.glBindAttribLocation(programId, 0, "inPos");
        GL.glBindAttribLocation(programId, 1, "inColor");

        GL.glLinkProgram(programId);

        GL20.glValidateProgram(programId);

        errValue = GL.glGetError();

        if (errValue != GL11.GL_NO_ERROR) {
            System.out.println("Shader creation failed" + GLU.gluErrorString(errValue));
            System.exit(-1);
        }
    }

    public void quadDelete() {
        //TODO
    }
}