stuart6854

RenderModel.java

Jun 1st, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.61 KB | None | 0 0
  1. package org.bearengine.graphics.types;
  2.  
  3. import com.sun.deploy.net.proxy.StaticProxyManager;
  4. import com.sun.media.sound.ModelSource;
  5. import com.sun.org.apache.xpath.internal.functions.FuncFalse;
  6. import org.bearengine.debug.Debug;
  7. import org.bearengine.objects.Object;
  8. import org.lwjgl.BufferUtils;
  9. import sun.nio.cs.ext.TIS_620;
  10.  
  11. import java.nio.FloatBuffer;
  12. import java.nio.IntBuffer;
  13. import java.util.ArrayList;
  14. import java.util.List;
  15.  
  16. import static org.lwjgl.opengl.GL11.GL_FALSE;
  17. import static org.lwjgl.opengl.GL11.GL_FLOAT;
  18. import static org.lwjgl.opengl.GL15.*;
  19. import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray;
  20. import static org.lwjgl.opengl.GL20.glVertexAttribPointer;
  21. import static org.lwjgl.opengl.GL30.*;
  22.  
  23. /**
  24.  * Created by Stuart on 29/05/2016.
  25.  */
  26. public class RenderModel {
  27.  
  28.     public static List<RenderModel> Models = new ArrayList<>();
  29.  
  30.     private int VAO_ID;
  31.     private int Vertex_Count;
  32.     private int INDEX_VBO_ID;
  33.     private int[] VBO_IDs;
  34.  
  35.     private ArrayList<Object> Transforms;
  36.  
  37.     public RenderModel(int vao_id, int index_vbo_id, int[] vbos){
  38.         this.VAO_ID = vao_id;
  39.         this.INDEX_VBO_ID = index_vbo_id;
  40.         this.VBO_IDs = vbos;
  41.         this.Transforms = new ArrayList<>();
  42.     }
  43.  
  44.     public void AddTransform(Object object){
  45.         this.Transforms.add(object);
  46.     }
  47.  
  48.     public void RemoveTransform(Object object){
  49.         this.Transforms.remove(object);
  50.     }
  51.  
  52.     public ArrayList<Object> GetTransforms() {
  53.         return Transforms;
  54.     }
  55.  
  56.     public void PrepareRender(){
  57.         for(int vbo : Current_VBOs)
  58.             glEnableVertexAttribArray(vbo);
  59.  
  60.         glBindVertexArray(VAO_ID);
  61.     }
  62.  
  63.     public void ReleaseModel(){
  64.         glDeleteVertexArrays(VAO_ID);
  65.         for(int vbo : VBO_IDs)
  66.             glDeleteBuffers(vbo);
  67.  
  68.         glDeleteBuffers(INDEX_VBO_ID);
  69.     }
  70.  
  71.     ///RENDER-MODEL FACTORY///
  72.  
  73.     private static boolean CREATING = false;
  74.  
  75.     private static int Current_Model = -1;
  76.     private static int Current_VBO_Index = -1;
  77.     private static int[] Current_VBOs = null;
  78.     private static int Current_Index_VBO = -1;
  79.  
  80.  
  81.     public static RenderModel Create2DModel(float[] vertices, float[] uvs, int[] indices){
  82.         if(CREATING){
  83.             Debug.error("RenderModel -> Already Creating RenderModel!");
  84.             return null;
  85.         }
  86.  
  87.         CreateModel(3);
  88.         SetupAttributeBuffer(0, 3, vertices);
  89.         SetupAttributeBuffer(1, 2, uvs);
  90.         SetupIndexBuffer(indices);
  91.         FinaliseModel();
  92.         return StoreModel();
  93.     }
  94.  
  95.     public static RenderModel Create3DModel(float[] vertices, float[] uvs, float[] normals, int[] indices){
  96.         if(CREATING){
  97.             Debug.error("RenderModel -> Already Creating RenderModel!");
  98.             return null;
  99.         }
  100.  
  101.         CreateModel(3);
  102.         SetupAttributeBuffer(0, 3, vertices);
  103.         SetupAttributeBuffer(1, 2, uvs);
  104.         SetupAttributeBuffer(2, 3, normals);
  105.         SetupIndexBuffer(indices);
  106.         FinaliseModel();
  107.         return StoreModel();
  108.     }
  109.  
  110.     private static void CreateModel(int vbos){
  111.         CREATING = true;
  112.         Current_Model = glGenVertexArrays();
  113.         Current_VBO_Index = 0;
  114.         Current_VBOs = new int[vbos];
  115.         glBindVertexArray(Current_Model);
  116.     }
  117.  
  118.     private static void SetupAttributeBuffer(int location, int components, float[] data){
  119.         int vbo_id = glGenBuffers();
  120.         FloatBuffer buffer = BufferUtils.createFloatBuffer(data.length);
  121.         glBindBuffer(GL_ARRAY_BUFFER, vbo_id);
  122.         buffer.put(data);
  123.         buffer.flip();
  124.         glBufferData(GL_ARRAY_BUFFER, buffer, GL_STATIC_DRAW);
  125.         glVertexAttribPointer(location, components, GL_FLOAT, false, 0, 0);
  126.         Current_VBOs[Current_VBO_Index++] = vbo_id;
  127.     }
  128.  
  129.     private static void SetupIndexBuffer(int[] indices){
  130.         int id = glGenBuffers();
  131.         IntBuffer buffer = BufferUtils.createIntBuffer(indices.length);
  132.         buffer.put(indices).flip();
  133.         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, id);
  134.         glBufferData(GL_ELEMENT_ARRAY_BUFFER, buffer, GL_STATIC_DRAW);
  135.         Current_Index_VBO = id;
  136.     }
  137.  
  138.     private static void FinaliseModel(){
  139.         for(int vbo : Current_VBOs)
  140.             glEnableVertexAttribArray(vbo);
  141.  
  142.         glBindVertexArray(0);
  143.     }
  144.  
  145.     private static RenderModel StoreModel(){
  146.         RenderModel model = new RenderModel(Current_Model, Current_Index_VBO, Current_VBOs);
  147.         return model;
  148.     }
  149.  
  150.     private static void ReleaseModels(){
  151.         for(RenderModel model : Models)
  152.             model.ReleaseModel();
  153.     }
  154.  
  155. }
Advertisement
Add Comment
Please, Sign In to add comment