Java tutorial
/** The MIT License (MIT) Copyright (c) 2015 Chris Aubert | GeekyAubergine Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ package com.geekyaubergine.geekyjgameutil.model; import java.nio.FloatBuffer; import java.nio.IntBuffer; import java.util.ArrayList; import java.util.List; 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 com.geekyaubergine.geekyjgameutil.texture.Texture; import com.geekyaubergine.geekyjutil.validate.Validate; /** * A class responsible for the loading and memory management of models. * * @author Chris Aubert * */ public class ModelLoader { private static final int TEXTURE_COORD_SIZE = 2; /** Stored to allow for deletion later */ private static List<Integer> vaos = new ArrayList<Integer>(); private static List<Integer> vbos = new ArrayList<Integer>(); /** * Loads a textured model * @param vertices Vertices of model * @param textureCoordinates Texture uv/st coordinates * @param indicies Index map * @param vertexSize Size of vertices * @param geometryType Geometry type * @param texure Texture to render on model * @return Textured model */ public static ModelTextured loadTexturedModel(float[] vertices, float[] textureCoordinates, int[] indicies, int vertexSize, int geometryType, Texture texure) { Validate.notNull(texure, "Texture must not be null"); int vaoID = createVAO(); bindIndicesBuffer(indicies); storeDataInAttributeList(Model.VAO_ATTRIBUTE_VERTEX_DATA_INDEX, vertices, vertexSize); storeDataInAttributeList(Model.VAO_ATTRIBUTE_TEXTURE_COORDINATES_INDEX, textureCoordinates, TEXTURE_COORD_SIZE); unbindVAO(); return new ModelTextured(vaoID, indicies.length, geometryType, texure); } /** * Creates VAO and returns its ID * @return Created VAO ID */ private static int createVAO() { int vaoId = GL30.glGenVertexArrays(); vaos.add(vaoId); GL30.glBindVertexArray(vaoId); return vaoId; } /** * Stores data in attribute list * @param attributeNumber Index in the attribute list to store data * @param data Data to store in attribute * @param dataSize Size of the data being stored */ private static void storeDataInAttributeList(int attributeNumber, float[] data, int dataSize) { Validate.isTrue(attributeNumber >= 0, "Attribute index must be a positive number"); Validate.isTrue(dataSize >= 0, "Data size must be a positive number"); int vboID = GL15.glGenBuffers(); vbos.add(vboID); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboID); FloatBuffer buffer = storeDataInFloatBuffer(data); GL15.glBufferData(GL15.GL_ARRAY_BUFFER, buffer, GL15.GL_STATIC_DRAW); GL20.glVertexAttribPointer(attributeNumber, dataSize, GL11.GL_FLOAT, false, 0, 0); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0); } /** * Unbinds current VAO */ private static void unbindVAO() { GL30.glBindVertexArray(0); } /** * Binds indices buffer for model * @param indicies Indices */ private static void bindIndicesBuffer(int[] indicies) { int vboID = GL15.glGenBuffers(); vbos.add(vboID); GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, vboID); IntBuffer buffer = storeDataInIntBuffer(indicies); GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, buffer, GL15.GL_STATIC_DRAW); } /** * Stores array in <tt>IntBuffer</tt> * @param data Data to store in int buffer * @return Data as IntBuffer * @see IntBuffer */ private static IntBuffer storeDataInIntBuffer(int[] data) { IntBuffer buffer = BufferUtils.createIntBuffer(data.length); buffer.put(data); buffer.flip(); return buffer; } /** * Stores array in <tt>FloatBuffer</tt> * @param data Data to store in float buffer * @return Data as FloatBuffer * @see FloatBuffer */ private static FloatBuffer storeDataInFloatBuffer(float[] data) { FloatBuffer buffer = BufferUtils.createFloatBuffer(data.length); buffer.put(data); buffer.flip(); return buffer; } /** * Deletes data stored in VAO's and VBO's */ public static void deleteData() { for (int vao : vaos) { GL30.glDeleteVertexArrays(vao); } for (int vbo : vbos) { GL15.glDeleteBuffers(vbo); } } }