Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ecumene.opengl;
- import java.nio.FloatBuffer;
- import org.lwjgl.BufferUtils;
- import org.lwjgl.opengl.GL11;
- import org.lwjgl.opengl.GL15;
- import org.lwjgl.opengl.GL30;
- /**
- * Links many vertices together and bind their attributes
- * into one vertex buffer object
- *
- * @author Ecumene
- */
- public class Model {
- /*The model information*/
- private Vertex[] model;
- /**The model's per-vertex positions attribute*/
- private VertexAttrib positions;
- /**The model's per-vertex texture uv attribute*/
- private VertexAttrib textureUVs;
- /**The model's per-vertex normal attribute*/
- private VertexAttrib normals;
- /**The model's per-vertex color attribute*/
- private VertexAttrib colors;
- /**The model's drawing order*/
- private float[] indices;
- /**The vertex attribute array buffer*/
- private int va;
- /**The vertex indices id*/
- private int vi;
- /**
- * Initializes the model with an array of vertices
- *
- * @param model The model information
- */
- public Model(Vertex ... model){
- this.model = model;
- }
- /**
- * Initializes the model with a size
- *
- * @param size The amount of vertices the model is intended
- * to hold
- */
- public Model(int size){
- model = new Vertex[size];
- }
- /**
- * Adds a vertex to the current model
- *
- * @param index The index to add the vertex at
- * @param vertices The vertex to add
- */
- public void add(int index, Vertex vertex){
- model[index] = vertex;
- }
- /**
- * Sets the drawing order of the mode's vertices
- * (Sets indices)
- * @param values The drawing order to set
- */
- public void setDrawingOrder(float...values){
- indices = values;
- }
- /**
- * Links the model's vertex data with the vertex attribute objects
- *
- * @param positions The vertex positions vertex attribute ID
- * @param textures The vertex texture coordinate vertex attribute ID
- * @param colors The vertex color vertex attribute ID
- * @param normals The vertex normal vertex attribute ID
- */
- public void link(int positions, int textures, int colors, int normals){
- float[] vertexPos = new float[model.length * 4];
- float[] textureUV = new float[model.length * 2];
- float[] color = new float[model.length * 4];
- float[] normal = new float[model.length * 3];
- for(int i = 0; i < model.length; i++){
- vertexPos[4*i+0] = model[i].position.x;
- vertexPos[4*i+1] = model[i].position.y;
- vertexPos[4*i+2] = model[i].position.z;
- vertexPos[4*i+3] = model[i].position.w;
- textureUV[2*i+0] = model[i].textureUV.x;
- textureUV[2*i+1] = model[i].textureUV.x;
- color[4*i+0] = model[i].color.getRed();
- color[4*i+1] = model[i].color.getGreen();
- color[4*i+2] = model[i].color.getBlue();
- color[4*i+3] = model[i].color.getAlpha();
- normal[3*i+0] = model[i].normal.x;
- normal[3*i+1] = model[i].normal.y;
- normal[3*i+2] = model[i].normal.z;
- }
- FloatBuffer indicesBuffer = BufferUtils.createFloatBuffer(indices.length);
- indicesBuffer.put(indices);
- indicesBuffer.flip();
- va = GL15.glGenBuffers();
- GL30.glBindVertexArray(va);
- this.positions = new VertexAttrib(positions, 4, vertexPos);
- this.textureUVs = new VertexAttrib(textures, 2, textureUV);
- this.colors = new VertexAttrib(colors, 4, color);
- this.normals = new VertexAttrib(normals, 3, normal);
- GL30.glBindVertexArray(0);
- vi = GL15.glGenBuffers();
- GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, vi);
- GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, indicesBuffer, GL15.GL_STATIC_DRAW);
- GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
- System.out.println("linkeed");
- }
- /**
- * Draws the vertex attribute object
- * @param mode The mode to draw
- */
- public void render(int mode){
- GL30.glBindVertexArray(va);
- positions.bind();
- textureUVs.bind();
- colors.bind();
- normals.bind();
- GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, vi);
- GL11.glDrawElements(mode, indices.length, GL11.GL_UNSIGNED_BYTE, 0);
- GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
- System.out.println("rendered");
- positions.unbind();
- textureUVs.unbind();
- colors.unbind();
- normals.unbind();
- System.out.println("\n");
- GL30.glBindVertexArray(0);
- }
- /**
- * @return The model's vertices
- */
- public Vertex[] getVertices(){
- return model;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment