Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package de.syscy.hengine.rendering.resources;
- import static org.lwjgl.opengl.GL11.GL_FLOAT;
- import static org.lwjgl.opengl.GL11.GL_TRIANGLES;
- import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT;
- import static org.lwjgl.opengl.GL11.glDrawElements;
- import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER;
- import static org.lwjgl.opengl.GL15.GL_ELEMENT_ARRAY_BUFFER;
- import static org.lwjgl.opengl.GL15.GL_STATIC_DRAW;
- import static org.lwjgl.opengl.GL15.glBindBuffer;
- import static org.lwjgl.opengl.GL15.glBufferData;
- import static org.lwjgl.opengl.GL15.glDeleteBuffers;
- import static org.lwjgl.opengl.GL15.glGenBuffers;
- import static org.lwjgl.opengl.GL20.glDisableVertexAttribArray;
- import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray;
- import static org.lwjgl.opengl.GL20.glVertexAttribPointer;
- import java.util.ArrayList;
- import java.util.logging.Level;
- import com.bulletphysics.collision.shapes.CollisionShape;
- import de.syscy.hengine.core.Vector3f;
- import de.syscy.hengine.rendering.Vertex;
- import de.syscy.hengine.rendering.meshLoading.IndexedModel;
- import de.syscy.hengine.rendering.meshLoading.OBJModel;
- import de.syscy.hengine.util.Util;
- public class Mesh extends Resource {
- public static final int FROM_FILE = 0;
- public static final int CUSTOM = 1;
- //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
- private int type = FROM_FILE;
- private int vbo;
- private int ibo;
- private int size;
- private Vertex[] vertices;
- private int[] indices;
- protected Mesh(String fileName) {
- super(fileName);
- this.type = FROM_FILE;
- }
- protected Mesh(long id) {
- super("CustomMesh" + id);
- this.type = CUSTOM;
- }
- protected Mesh(Vertex[] vertices, int[] indices, long id) {
- super("CustomMesh" + id);
- this.type = CUSTOM;
- this.vertices = vertices;
- this.indices = indices;
- }
- //Loaded is called by the ResourceManager if lastUsedTime gets update, which happens when draw() is called
- @Override
- public void load() {
- super.load();
- vbo = glGenBuffers();
- ibo = glGenBuffers();
- if (type == FROM_FILE) {
- loadMesh(getFileName());
- } else if (type == CUSTOM) {
- if(vertices != null && vertices.length > 0 && indices != null && indices.length > 0) {
- addVertices(vertices, indices, true);
- }
- }
- }
- //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
- @Override
- public void unload() {
- super.unload();
- glDeleteBuffers(vbo);
- glDeleteBuffers(ibo);
- }
- public void setVertices(Vertex[] vertices, int[] indices) {
- this.setVertices(vertices, indices, true);
- }
- public void setVertices(Vertex[] vertices, int[] indices, boolean reload) {
- if(type != CUSTOM) {
- 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.");
- return;
- }
- this.vertices = vertices;
- this.indices = indices;
- if(reload) {
- if(vertices != null && vertices.length > 0 && indices != null && indices.length > 0) {
- addVertices(vertices, indices, true);
- }
- }
- }
- private void addVertices(Vertex[] vertices, int[] indices, boolean calcNormals) {
- if (calcNormals) {
- calcNormals(vertices, indices);
- }
- this.size = indices.length;
- glBindBuffer(GL_ARRAY_BUFFER, getVBO());
- glBufferData(GL_ARRAY_BUFFER, Util.createFlippedBuffer(vertices), GL_STATIC_DRAW);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, getIBO());
- glBufferData(GL_ELEMENT_ARRAY_BUFFER, Util.createFlippedBuffer(indices), GL_STATIC_DRAW);
- }
- public void draw() {
- draw(GL_TRIANGLES);
- }
- public void draw(int drawMode) {
- updateLastUsedTime();
- if (!isLoaded()) return;
- glEnableVertexAttribArray(0);
- glEnableVertexAttribArray(1);
- glEnableVertexAttribArray(2);
- glEnableVertexAttribArray(3);
- glBindBuffer(GL_ARRAY_BUFFER, getVBO());
- glVertexAttribPointer(0, 3, GL_FLOAT, false, Vertex.SIZE * 4, 0);
- glVertexAttribPointer(1, 2, GL_FLOAT, false, Vertex.SIZE * 4, 12);
- glVertexAttribPointer(2, 3, GL_FLOAT, false, Vertex.SIZE * 4, 20);
- glVertexAttribPointer(3, 3, GL_FLOAT, false, Vertex.SIZE * 4, 32);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, getIBO());
- glDrawElements(drawMode, getSize(), GL_UNSIGNED_INT, 0);
- glDisableVertexAttribArray(0);
- glDisableVertexAttribArray(1);
- glDisableVertexAttribArray(2);
- glDisableVertexAttribArray(3);
- }
- private Mesh loadMesh(String fileName) {
- String[] splitArray = fileName.split("\\.");
- String ext = splitArray[splitArray.length - 1];
- if (!ext.equals("obj")) {
- Util.getLogger().log(Level.WARNING, "Error: '" + ext + "' file format not supported for mesh data.", new Exception());
- System.exit(1);
- }
- OBJModel test = new OBJModel("./data/models/" + fileName);
- IndexedModel model = test.toIndexedModel();
- ArrayList<Vertex> vertices = new ArrayList<Vertex>();
- for (int i = 0; i < model.getPositions().size(); i++) {
- vertices.add(new Vertex(model.getPositions().get(i), model.getTexCoords().get(i), model.getNormals().get(i), model.getTangents().get(i)));
- }
- Vertex[] vertexData = new Vertex[vertices.size()];
- vertices.toArray(vertexData);
- Integer[] indexData = new Integer[model.getIndices().size()];
- model.getIndices().toArray(indexData);
- addVertices(vertexData, Util.toIntArray(indexData), false);
- return this;
- }
- public int getVBO() {
- return vbo;
- }
- public int getIBO() {
- return ibo;
- }
- public int getSize() {
- return size;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement