com.redthirddivision.quad.rendering.models.Model.java Source code

Java tutorial

Introduction

Here is the source code for com.redthirddivision.quad.rendering.models.Model.java

Source

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

import java.nio.FloatBuffer;
import java.nio.IntBuffer;

import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL15;
import org.lwjgl.opengl.GL20;
import org.lwjgl.opengl.GL30;

import com.redthirddivision.quad.rendering.materials.Material;
import com.redthirddivision.quad.rendering.models.data.ModelData;
import com.redthirddivision.quad.rendering.models.data.ObjFile;
import com.redthirddivision.quad.utils.Util;
import com.redthirddivision.quad.utils.managers.ResourceManager;

/**
 * <strong>Project:</strong> QuadEngine <br>
 * <strong>File:</strong> Model.java
 * 
 * <p>
 * A <code>Model</code> represents a model created with <i>in code</i>
 * data.
 * </p>
 *
 * @author <a href = "http://redthirddivision.com/team/blp"> Matthew Rogers</a>
 */
public class Model {

    private int vaoID;
    private int vertexCount;
    private Material material;

    private Model(int vaoID, int vertexCount) {
        this.vaoID = vaoID;
        this.vertexCount = vertexCount;
    }

    /**
     * Model to represent a simple quad
     * @param positions A float array of the quad's positions
     */
    public Model(float[] positions) {
        this.vaoID = createVAO();
        storeInAttribList(0, 2, positions);
        unbindVAO();
        this.vertexCount = positions.length / 2;
    }

    public Model(float[] vertices, int[] indices, float[] texCoords, float[] normals) {
        this.vaoID = createVAO();
        bindIndicesBuffer(indices);
        storeInAttribList(0, 3, vertices);
        storeInAttribList(1, 2, texCoords);
        storeInAttribList(2, 3, normals);
        unbindVAO();
        this.vertexCount = indices.length;
    }

    public Model(String file, Material material) {
        ModelData data = ObjFile.readData(file);
        this.vaoID = createVAO();
        bindIndicesBuffer(data.getIndices());
        storeInAttribList(0, 3, data.getVertices());
        storeInAttribList(1, 2, data.getTextureCoords());
        storeInAttribList(2, 3, data.getNormals());
        unbindVAO();
        this.vertexCount = data.getIndices().length;
        this.material = material;
    }

    /**
     * @return The ID of the <i>Vertex Array Object</i> associated with this
     *         model.
     */
    public int getVaoID() {
        return vaoID;
    }

    /**
     * @return The amount of vertices this model contains.
     */
    public int getVertexCount() {
        return vertexCount;
    }

    public Material getMaterial() {
        return material;
    }

    private int createVAO() {
        int vaoID = GL30.glGenVertexArrays();
        ResourceManager.addVAO(vaoID);
        GL30.glBindVertexArray(vaoID);
        return vaoID;
    }

    private void storeInAttribList(int attribNumber, int coordSize, float[] data) {
        int vboID = GL15.glGenBuffers();
        ResourceManager.addVBO(vboID);
        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboID);
        FloatBuffer buffer = Util.storeInFloatBuffer(data);
        GL15.glBufferData(GL15.GL_ARRAY_BUFFER, buffer, GL15.GL_STATIC_DRAW);
        GL20.glVertexAttribPointer(attribNumber, coordSize, GL11.GL_FLOAT, false, 0, 0);
        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
    }

    private void bindIndicesBuffer(int[] indices) {
        int vboID = GL15.glGenBuffers();
        ResourceManager.addVBO(vboID);
        GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, vboID);
        IntBuffer buffer = Util.storeInIntBuffer(indices);
        GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, buffer, GL15.GL_STATIC_DRAW);
    }

    private void unbindVAO() {
        GL30.glBindVertexArray(0);
    }

}