Java tutorial
/* Copyright (C) 2013 IFS Studios This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ package uk.co.ifs_studios.engine.geometry; import java.nio.ByteBuffer; import java.nio.FloatBuffer; import org.lwjgl.BufferUtils; import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.GL15; import org.lwjgl.opengl.GL20; import org.lwjgl.opengl.GL30; import org.lwjgl.util.vector.Matrix4f; import uk.co.ifs_studios.engine.Engine; import uk.co.ifs_studios.engine.geometry.util.Color; import uk.co.ifs_studios.engine.geometry.util.Vertex; import uk.co.ifs_studios.engine.shader.shaders.BasicShader; /** * A class that represents an OpenGL Draw Element using the in-built * BasicShader. * * @author BleedObsidian (Jesse Prescott) */ public class BasicDrawElement implements DrawElement { private final BasicShader shader; private Matrix4f modelMatrix; private final FloatBuffer matrixBuffer; private int vao; private int vbo; private int ibo; private int cbo; private int indiceCount; /** * Create new DrawElement. */ public BasicDrawElement() { this.shader = new BasicShader(); this.matrixBuffer = BufferUtils.createFloatBuffer(16); this.vao = GL30.glGenVertexArrays(); this.vbo = GL15.glGenBuffers(); this.ibo = GL15.glGenBuffers(); this.cbo = GL15.glGenBuffers(); } /** * Set verticies to DrawElement. * * @param verticies * - Verticies. */ public void setVerticies(Vertex[] verticies) { float[] vertexValues = new float[verticies.length * 4]; int vertexIndex = 0; for (int i = 0; i < vertexValues.length; i += 4) { vertexValues[i + 0] = verticies[vertexIndex].getX(); vertexValues[i + 1] = verticies[vertexIndex].getY(); vertexValues[i + 2] = verticies[vertexIndex].getZ(); vertexValues[i + 3] = verticies[vertexIndex].getW(); vertexIndex++; } FloatBuffer verticiesBuffer = BufferUtils.createFloatBuffer(vertexValues.length); verticiesBuffer.put(vertexValues); verticiesBuffer.flip(); GL30.glBindVertexArray(this.vao); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, this.vbo); GL15.glBufferData(GL15.GL_ARRAY_BUFFER, verticiesBuffer, GL15.GL_STATIC_DRAW); GL20.glVertexAttribPointer(0, 4, GL11.GL_FLOAT, false, 0, 0); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0); GL30.glBindVertexArray(0); } /** * Set colors to DrawElement. * * @param colors * - Colors. */ public void setColors(Color[] colors) { float[] colorValues = new float[colors.length * 4]; int colorIndex = 0; for (int i = 0; i < colorValues.length; i += 4) { colorValues[i + 0] = colors[colorIndex].getRed(); colorValues[i + 1] = colors[colorIndex].getGreen(); colorValues[i + 2] = colors[colorIndex].getBlue(); colorValues[i + 3] = colors[colorIndex].getAlpha(); colorIndex++; } FloatBuffer colorsBuffer = BufferUtils.createFloatBuffer(colorValues.length); colorsBuffer.put(colorValues); colorsBuffer.flip(); GL30.glBindVertexArray(this.vao); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, this.cbo); GL15.glBufferData(GL15.GL_ARRAY_BUFFER, colorsBuffer, GL15.GL_STREAM_DRAW); GL20.glVertexAttribPointer(1, 4, GL11.GL_FLOAT, false, 0, 0); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0); GL30.glBindVertexArray(0); } /** * Set indices to DrawElement. * * @param indices * - Indices. */ public void setIndices(byte[] indices) { this.indiceCount = indices.length; ByteBuffer indiciesBuffer = BufferUtils.createByteBuffer(this.indiceCount); indiciesBuffer.put(indices); indiciesBuffer.flip(); GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, this.ibo); GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, indiciesBuffer, GL15.GL_STATIC_DRAW); GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0); } /** * Set model matrix. */ public void setModelMatrix(Matrix4f matrix) { this.modelMatrix = matrix; } /** * Render DrawElement. */ public void render() { this.shader.getShaderProgram().use(); Engine.getInstance().getWindow().getProjectionMatrix().store(this.matrixBuffer); this.matrixBuffer.flip(); GL20.glUniformMatrix4(this.shader.getProjectionMatrixLocation(), false, this.matrixBuffer); Engine.getInstance().getGame().getCurrentState().getCamera().getViewMatrix().store(this.matrixBuffer); this.matrixBuffer.flip(); GL20.glUniformMatrix4(this.shader.getViewMatrixLocation(), false, this.matrixBuffer); this.modelMatrix.store(this.matrixBuffer); this.matrixBuffer.flip(); GL20.glUniformMatrix4(this.shader.getModelMatrixLocation(), false, this.matrixBuffer); GL30.glBindVertexArray(this.vao); GL20.glEnableVertexAttribArray(0); GL20.glEnableVertexAttribArray(1); GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, this.ibo); GL11.glDrawElements(GL11.GL_TRIANGLES, this.indiceCount, GL11.GL_UNSIGNED_BYTE, 0); GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0); GL20.glDisableVertexAttribArray(0); GL30.glBindVertexArray(0); this.shader.getShaderProgram().unuse(); } }