Java tutorial
/******************************************************************************* * Copyright (C) 2015 Jordan Dalton * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *******************************************************************************/ package ovh.tgrhavoc.gameengine.core; import java.io.BufferedReader; import java.io.FileReader; import java.util.ArrayList; import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.GL15; import org.lwjgl.opengl.GL20; public class Mesh { int vbo; //Vertex Buffer Obj int size; int ibo; public Mesh(String file) { initMesh(); loadMesh(file); } public Mesh(Vertex[] verticies, int[] indecies) { this(verticies, indecies, false); } public Mesh(Vertex[] verticies, int[] indecies, boolean calcNorms) { initMesh(); addVerticies(verticies, indecies, calcNorms); } private void initMesh() { vbo = GL15.glGenBuffers(); ibo = GL15.glGenBuffers(); size = 0; } private Mesh loadMesh(String filename) { String[] splitArray = filename.split("\\."); String ex = splitArray[splitArray.length - 1]; if (!ex.equalsIgnoreCase("obj")) { System.err.println("Error: File extention '" + ex + "' is not supported for mesh data!"); new Exception().printStackTrace(); System.exit(-1); } ArrayList<Vertex> vertecies = new ArrayList<Vertex>(); ArrayList<Integer> indecies = new ArrayList<Integer>(); BufferedReader br = null; try { br = new BufferedReader(new FileReader("./res/models/" + filename)); String line; Window.setTitle("Loading.. " + filename); while ((line = br.readLine()) != null) { //System.out.println("Loaing mesh..."); String[] tokens = line.split(" "); tokens = Util.removeEmptyStrings(tokens); if (tokens.length == 0 || tokens[0].equalsIgnoreCase("#")) continue; else if (tokens[0].equals("v")) { vertecies.add(new Vertex(new Vector3f(Float.valueOf(tokens[1]), Float.valueOf(tokens[2]), Float.valueOf(tokens[3])))); } else if (tokens[0].equals("f")) { indecies.add(Integer.parseInt(tokens[1].split("/")[0]) - 1); indecies.add(Integer.parseInt(tokens[2].split("/")[0]) - 1); indecies.add(Integer.parseInt(tokens[3].split("/")[0]) - 1); if (tokens.length > 4) { indecies.add(Integer.parseInt(tokens[1].split("/")[0]) - 1); indecies.add(Integer.parseInt(tokens[3].split("/")[0]) - 1); indecies.add(Integer.parseInt(tokens[4].split("/")[0]) - 1); } } } br.close(); Vertex[] vertexData = new Vertex[vertecies.size()]; vertecies.toArray(vertexData); Integer[] indeciesData = new Integer[indecies.size()]; indecies.toArray(indeciesData); addVerticies(vertexData, Util.toIntArray(indeciesData), true); Window.setTitle("Viewing: " + filename.replace(".obj", "")); } catch (Exception e) { e.printStackTrace(); System.exit(-1); } return null; } private void calcNorms(Vertex[] data, int[] data2) { for (int i = 0; i < data2.length; i += 3) { int i0 = data2[i]; int i1 = data2[i + 1]; int i2 = data2[i + 2]; Vector3f v1 = data[i1].getPos().subtract(data[i0].getPos()); Vector3f v2 = data[i2].getPos().subtract(data[i0].getPos()); Vector3f normal = v1.cross(v2).normalized(); data[i0].setNormal(data[i0].getNormal().add(normal)); data[i1].setNormal(data[i1].getNormal().add(normal)); data[i2].setNormal(data[i2].getNormal().add(normal)); } for (int i = 0; i < data.length; i++) data[i].setNormal(data[i].getNormal().normalized()); } private void addVerticies(Vertex[] verticies, int[] indecies, boolean calcNorms) { if (calcNorms) { this.calcNorms(verticies, indecies); } size = indecies.length; GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vbo); GL15.glBufferData(GL15.GL_ARRAY_BUFFER, Util.createFlippedBuffer(verticies), GL15.GL_STATIC_DRAW); GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, ibo); GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, Util.createFlippedBuffer(indecies), GL15.GL_STATIC_DRAW); } public void draw() { GL20.glEnableVertexAttribArray(0); GL20.glEnableVertexAttribArray(1); GL20.glEnableVertexAttribArray(2); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vbo); GL20.glVertexAttribPointer(0, 3, GL11.GL_FLOAT, false, Vertex.SIZE * 4, 0); GL20.glVertexAttribPointer(1, 2, GL11.GL_FLOAT, false, Vertex.SIZE * 4, 12); GL20.glVertexAttribPointer(2, 3, GL11.GL_FLOAT, false, Vertex.SIZE * 4, 20); GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, ibo); GL11.glDrawElements(GL11.GL_TRIANGLES, size, GL11.GL_UNSIGNED_INT, 0); GL20.glDisableVertexAttribArray(0); GL20.glDisableVertexAttribArray(1); GL20.glDisableVertexAttribArray(2); } }