Advertisement
Guest User

Mesh.java

a guest
Mar 2nd, 2015
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.73 KB | None | 0 0
  1. package de.syscy.hengine.rendering.resources;
  2.  
  3. import static org.lwjgl.opengl.GL11.GL_FLOAT;
  4. import static org.lwjgl.opengl.GL11.GL_TRIANGLES;
  5. import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT;
  6. import static org.lwjgl.opengl.GL11.glDrawElements;
  7. import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER;
  8. import static org.lwjgl.opengl.GL15.GL_ELEMENT_ARRAY_BUFFER;
  9. import static org.lwjgl.opengl.GL15.GL_STATIC_DRAW;
  10. import static org.lwjgl.opengl.GL15.glBindBuffer;
  11. import static org.lwjgl.opengl.GL15.glBufferData;
  12. import static org.lwjgl.opengl.GL15.glDeleteBuffers;
  13. import static org.lwjgl.opengl.GL15.glGenBuffers;
  14. import static org.lwjgl.opengl.GL20.glDisableVertexAttribArray;
  15. import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray;
  16. import static org.lwjgl.opengl.GL20.glVertexAttribPointer;
  17.  
  18. import java.util.ArrayList;
  19. import java.util.logging.Level;
  20.  
  21. import com.bulletphysics.collision.shapes.CollisionShape;
  22.  
  23. import de.syscy.hengine.core.Vector3f;
  24. import de.syscy.hengine.rendering.Vertex;
  25. import de.syscy.hengine.rendering.meshLoading.IndexedModel;
  26. import de.syscy.hengine.rendering.meshLoading.OBJModel;
  27. import de.syscy.hengine.util.Util;
  28.  
  29. public class Mesh extends Resource {
  30.     public static final int FROM_FILE = 0;
  31.     public static final int CUSTOM = 1;
  32.    
  33.     //The type just saves if this mesh is loaded from a file or if it is a mesh created from a vertices and an indices array
  34.     private int type = FROM_FILE;
  35.  
  36.     private int vbo;
  37.     private int ibo;
  38.  
  39.     private int size;
  40.  
  41.     private Vertex[] vertices;
  42.     private int[] indices;
  43.  
  44.     protected Mesh(String fileName) {
  45.         super(fileName);
  46.  
  47.         this.type = FROM_FILE;
  48.     }
  49.    
  50.     protected Mesh(long id) {
  51.         super("CustomMesh" + id);
  52.        
  53.         this.type = CUSTOM;
  54.     }
  55.  
  56.     protected Mesh(Vertex[] vertices, int[] indices, long id) {
  57.         super("CustomMesh" + id);
  58.  
  59.         this.type = CUSTOM;
  60.  
  61.         this.vertices = vertices;
  62.         this.indices = indices;
  63.     }
  64.    
  65.     //Loaded is called by the ResourceManager if lastUsedTime gets update, which happens when draw() is called
  66.     @Override
  67.     public void load() {
  68.         super.load();
  69.  
  70.         vbo = glGenBuffers();
  71.         ibo = glGenBuffers();
  72.  
  73.         if (type == FROM_FILE) {
  74.             loadMesh(getFileName());
  75.         } else if (type == CUSTOM) {
  76.             if(vertices != null && vertices.length > 0 && indices != null && indices.length > 0) {
  77.                 addVertices(vertices, indices, true);
  78.             }
  79.         }
  80.     }
  81.    
  82.     //Unload is called by the ResourceManager if (lastUsedTime gets updated everytime the draw method is called)        System.currentTimeMillis() - lastUsedTime is greater than a final variable, UNLOAD_TIME which is 10000 so it gets unloaded after 10secs
  83.     @Override
  84.     public void unload() {
  85.         super.unload();
  86.  
  87.         glDeleteBuffers(vbo);
  88.         glDeleteBuffers(ibo);
  89.     }
  90.  
  91.     public void setVertices(Vertex[] vertices, int[] indices) {
  92.         this.setVertices(vertices, indices, true);
  93.     }
  94.  
  95.     public void setVertices(Vertex[] vertices, int[] indices, boolean reload) {
  96.         if(type != CUSTOM) {
  97.             Util.getLogger().log(Level.WARNING, "Warning: You can only call setVertices with a custom mesh (Use the constructor with out args or the one with Vertex[] vertices and int[] indices args.");
  98.             return;
  99.         }
  100.        
  101.         this.vertices = vertices;
  102.         this.indices = indices;
  103.        
  104.         if(reload) {
  105.             if(vertices != null && vertices.length > 0 && indices != null && indices.length > 0) {
  106.                 addVertices(vertices, indices, true);
  107.             }
  108.         }
  109.     }
  110.  
  111.     private void addVertices(Vertex[] vertices, int[] indices, boolean calcNormals) {
  112.         if (calcNormals) {
  113.             calcNormals(vertices, indices);
  114.         }
  115.  
  116.         this.size = indices.length;
  117.  
  118.         glBindBuffer(GL_ARRAY_BUFFER, getVBO());
  119.         glBufferData(GL_ARRAY_BUFFER, Util.createFlippedBuffer(vertices), GL_STATIC_DRAW);
  120.         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, getIBO());
  121.         glBufferData(GL_ELEMENT_ARRAY_BUFFER, Util.createFlippedBuffer(indices), GL_STATIC_DRAW);
  122.     }
  123.  
  124.     public void draw() {
  125.         draw(GL_TRIANGLES);
  126.     }
  127.  
  128.     public void draw(int drawMode) {
  129.         updateLastUsedTime();
  130.  
  131.         if (!isLoaded()) return;
  132.  
  133.         glEnableVertexAttribArray(0);
  134.         glEnableVertexAttribArray(1);
  135.         glEnableVertexAttribArray(2);
  136.         glEnableVertexAttribArray(3);
  137.  
  138.         glBindBuffer(GL_ARRAY_BUFFER, getVBO());
  139.  
  140.         glVertexAttribPointer(0, 3, GL_FLOAT, false, Vertex.SIZE * 4, 0);
  141.         glVertexAttribPointer(1, 2, GL_FLOAT, false, Vertex.SIZE * 4, 12);
  142.         glVertexAttribPointer(2, 3, GL_FLOAT, false, Vertex.SIZE * 4, 20);
  143.         glVertexAttribPointer(3, 3, GL_FLOAT, false, Vertex.SIZE * 4, 32);
  144.  
  145.         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, getIBO());
  146.         glDrawElements(drawMode, getSize(), GL_UNSIGNED_INT, 0);
  147.  
  148.         glDisableVertexAttribArray(0);
  149.         glDisableVertexAttribArray(1);
  150.         glDisableVertexAttribArray(2);
  151.         glDisableVertexAttribArray(3);
  152.     }
  153.  
  154.     private Mesh loadMesh(String fileName) {
  155.         String[] splitArray = fileName.split("\\.");
  156.         String ext = splitArray[splitArray.length - 1];
  157.  
  158.         if (!ext.equals("obj")) {
  159.             Util.getLogger().log(Level.WARNING, "Error: '" + ext + "' file format not supported for mesh data.", new Exception());
  160.             System.exit(1);
  161.         }
  162.  
  163.         OBJModel test = new OBJModel("./data/models/" + fileName);
  164.         IndexedModel model = test.toIndexedModel();
  165.         ArrayList<Vertex> vertices = new ArrayList<Vertex>();
  166.  
  167.         for (int i = 0; i < model.getPositions().size(); i++) {
  168.             vertices.add(new Vertex(model.getPositions().get(i), model.getTexCoords().get(i), model.getNormals().get(i), model.getTangents().get(i)));
  169.         }
  170.  
  171.         Vertex[] vertexData = new Vertex[vertices.size()];
  172.         vertices.toArray(vertexData);
  173.  
  174.         Integer[] indexData = new Integer[model.getIndices().size()];
  175.         model.getIndices().toArray(indexData);
  176.  
  177.         addVertices(vertexData, Util.toIntArray(indexData), false);
  178.  
  179.         return this;
  180.     }
  181.  
  182.     public int getVBO() {
  183.         return vbo;
  184.     }
  185.  
  186.     public int getIBO() {
  187.         return ibo;
  188.     }
  189.  
  190.     public int getSize() {
  191.         return size;
  192.     }
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement