Java tutorial
/******************************************************************************** * * Copyright 2014 Theodor Angergard * * 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 se.angergard.engine.graphics; import java.nio.*; import java.util.*; import org.lwjgl.opengl.*; import se.angergard.engine.util.Util; public abstract class Mesh { public Mesh(long vertexBufferSize, long indicesBufferSize, int usage) { if (aMeshCreated) { new Exception("You can't create multiple meshes as of right now. This is due to a bug") .printStackTrace(); } vbo = GL15.glGenBuffers(); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vbo); GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vertexBufferSize, usage); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0); ibo = GL15.glGenBuffers(); GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, ibo); GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, indicesBufferSize, usage); GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0); this.VERTEX_BUFFER_SIZE = vertexBufferSize; this.INDICES_BUFFER_SIZE = indicesBufferSize; this.INDICES_LENGTH = (int) indicesBufferSize / 4; // removes bytes aMeshCreated = true; } private static boolean aMeshCreated = false; private final int vbo, ibo; public final long VERTEX_BUFFER_SIZE, INDICES_BUFFER_SIZE; public final int INDICES_LENGTH; public final void setVertices(FloatBuffer buffer) { GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vbo); GL15.glBufferSubData(GL15.GL_ARRAY_BUFFER, 0, buffer); // GL15.glBufferData(GL15.GL_ARRAY_BUFFER, buffer, GL15.GL_DYNAMIC_DRAW); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0); } public final void setIndices(IntBuffer buffer) { GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, ibo); GL15.glBufferSubData(GL15.GL_ELEMENT_ARRAY_BUFFER, 0, buffer); // GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, buffer, GL15.GL_DYNAMIC_DRAW); GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0); } public final void setIndices(ArrayList<Integer> indices) { setIndices(Util.toFlippedIntBuffer(indices)); } public final void setIndices(int[] indices) { setIndices(Util.toFlippedIntBuffer(indices)); } public void draw() { GL20.glEnableVertexAttribArray(0); GL20.glEnableVertexAttribArray(1); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vbo); GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, ibo); GL20.glVertexAttribPointer(0, 2, GL11.GL_FLOAT, false, Vertex2D.SIZE_BYTES, 0); GL20.glVertexAttribPointer(1, 2, GL11.GL_FLOAT, false, Vertex2D.SIZE_BYTES, 8); GL11.glDrawElements(GL11.GL_TRIANGLES, INDICES_LENGTH, GL11.GL_UNSIGNED_INT, 0); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0); GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0); GL20.glDisableVertexAttribArray(0); GL20.glDisableVertexAttribArray(1); } }