diff --git a/src/main/java/io/github/hydos/ginger/Example.java b/src/main/java/io/github/hydos/ginger/Example.java index 5b41d7f..469e07e 100644 --- a/src/main/java/io/github/hydos/ginger/Example.java +++ b/src/main/java/io/github/hydos/ginger/Example.java @@ -54,7 +54,7 @@ public class Example { public void main(String[] args) { - Window.create(2000, 1200, "Ginger Example", 60); + Window.create(800, 1200, "Ginger Example", 60); GingerMain.init(); diff --git a/src/main/java/io/github/hydos/ginger/engine/io/Window.java b/src/main/java/io/github/hydos/ginger/engine/io/Window.java index e44bd09..74f5ad0 100644 --- a/src/main/java/io/github/hydos/ginger/engine/io/Window.java +++ b/src/main/java/io/github/hydos/ginger/engine/io/Window.java @@ -58,7 +58,7 @@ public class Window { GLFW.glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MINOR, 3); GLFW.glfwWindowHint(GLFW.GLFW_OPENGL_PROFILE, GLFW.GLFW_OPENGL_CORE_PROFILE); GLFW.glfwWindowHint(GLFW.GLFW_OPENGL_FORWARD_COMPAT, GL11.GL_TRUE); - GLFW.glfwWindowHint(GLFW.GLFW_RESIZABLE, GLFW.GLFW_FALSE); + GLFW.glfwWindowHint(GLFW.GLFW_RESIZABLE, GLFW.GLFW_TRUE); GLFWVidMode vidmode = GLFW.glfwGetVideoMode(GLFW.glfwGetPrimaryMonitor()); diff --git a/src/main/java/io/github/hydos/ginger/engine/obj/ModelData.java b/src/main/java/io/github/hydos/ginger/engine/obj/ModelData.java index a492d9d..bea9569 100644 --- a/src/main/java/io/github/hydos/ginger/engine/obj/ModelData.java +++ b/src/main/java/io/github/hydos/ginger/engine/obj/ModelData.java @@ -1,5 +1,7 @@ package io.github.hydos.ginger.engine.obj; +import org.lwjgl.assimp.AIScene; + public class ModelData { private float[] vertices; @@ -17,6 +19,10 @@ public class ModelData { this.furthestPoint = furthestPoint; } + public ModelData(AIScene scene) { + + } + public float[] getVertices() { return vertices; } diff --git a/src/main/java/io/github/hydos/ginger/engine/obj/OBJFileLoader.java b/src/main/java/io/github/hydos/ginger/engine/obj/OBJFileLoader.java index 7999480..31ef57d 100644 --- a/src/main/java/io/github/hydos/ginger/engine/obj/OBJFileLoader.java +++ b/src/main/java/io/github/hydos/ginger/engine/obj/OBJFileLoader.java @@ -1,155 +1,93 @@ package io.github.hydos.ginger.engine.obj; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.util.ArrayList; -import java.util.List; +import static org.lwjgl.assimp.Assimp.aiGetErrorString; +import static org.lwjgl.assimp.Assimp.aiImportFileEx; +import static org.lwjgl.assimp.Assimp.aiProcess_JoinIdenticalVertices; +import static org.lwjgl.assimp.Assimp.aiProcess_Triangulate; +import static org.lwjgl.system.MemoryUtil.NULL; +import static org.lwjgl.system.MemoryUtil.memAddress; +import static org.lwjgl.system.MemoryUtil.memCopy; +import static org.lwjgl.system.MemoryUtil.memUTF8; + +import java.io.IOException; +import java.nio.ByteBuffer; + +import org.lwjgl.assimp.AIFile; +import org.lwjgl.assimp.AIFileCloseProc; +import org.lwjgl.assimp.AIFileCloseProcI; +import org.lwjgl.assimp.AIFileIO; +import org.lwjgl.assimp.AIFileOpenProc; +import org.lwjgl.assimp.AIFileOpenProcI; +import org.lwjgl.assimp.AIFileReadProc; +import org.lwjgl.assimp.AIFileReadProcI; +import org.lwjgl.assimp.AIFileSeek; +import org.lwjgl.assimp.AIFileSeekI; +import org.lwjgl.assimp.AIFileTellProc; +import org.lwjgl.assimp.AIFileTellProcI; +import org.lwjgl.assimp.AIScene; +import org.lwjgl.assimp.Assimp; + +import io.github.hydos.ginger.engine.render.tools.IOUtil; -import io.github.hydos.ginger.engine.math.vectors.Vector2f; -import io.github.hydos.ginger.engine.math.vectors.Vector3f; public class OBJFileLoader { private static final String RES_LOC = "/models/"; public static ModelData loadOBJ(String objFileName) { - String objFile = RES_LOC + objFileName; - System.out.println(objFile); - InputStreamReader isr = new InputStreamReader(Class.class.getResourceAsStream(objFile)); - BufferedReader reader = new BufferedReader(isr); - String line; - List vertices = new ArrayList(); - List textures = new ArrayList(); - List normals = new ArrayList(); - List indices = new ArrayList(); - try { - while (true) { - line = reader.readLine(); - if (line.startsWith("v ")) { - String[] currentLine = line.split(" "); - Vector3f vertex = new Vector3f(Float.valueOf(currentLine[1]), - Float.valueOf(currentLine[2]), - Float.valueOf(currentLine[3])); - Vertex newVertex = new Vertex(vertices.size(), vertex); - vertices.add(newVertex); - - } else if (line.startsWith("vt ")) { - String[] currentLine = line.split(" "); - Vector2f texture = new Vector2f(Float.valueOf(currentLine[1]), - Float.valueOf(currentLine[2])); - textures.add(texture); - } else if (line.startsWith("vn ")) { - String[] currentLine = line.split(" "); - Vector3f normal = new Vector3f(Float.valueOf(currentLine[1]), - Float.valueOf(currentLine[2]), - Float.valueOf(currentLine[3])); - normals.add(normal); - } else if (line.startsWith("f ")) { - break; - } - } - while (line != null && line.startsWith("f ")) { - String[] currentLine = line.split(" "); - String[] vertex1 = currentLine[1].split("/"); - String[] vertex2 = currentLine[2].split("/"); - String[] vertex3 = currentLine[3].split("/"); - processVertex(vertex1, vertices, indices); - processVertex(vertex2, vertices, indices); - processVertex(vertex3, vertices, indices); - line = reader.readLine(); - } - reader.close(); - } catch (IOException e) { - System.err.println("Error reading the file"); - } - removeUnusedVertices(vertices); - float[] verticesArray = new float[vertices.size() * 3]; - float[] texturesArray = new float[vertices.size() * 2]; - float[] normalsArray = new float[vertices.size() * 3]; - float furthest = convertDataToArrays(vertices, textures, normals, verticesArray, - texturesArray, normalsArray); - int[] indicesArray = convertIndicesListToArray(indices); - ModelData data = new ModelData(verticesArray, texturesArray, normalsArray, indicesArray, - furthest); - return data; - } - - private static void processVertex(String[] vertex, List vertices, List indices) { - int index = Integer.parseInt(vertex[0]) - 1; - Vertex currentVertex = vertices.get(index); - int textureIndex = Integer.parseInt(vertex[1]) - 1; - int normalIndex = Integer.parseInt(vertex[2]) - 1; - if (!currentVertex.isSet()) { - currentVertex.setTextureIndex(textureIndex); - currentVertex.setNormalIndex(normalIndex); - indices.add(index); - } else { - dealWithAlreadyProcessedVertex(currentVertex, textureIndex, normalIndex, indices, - vertices); - } - } - - private static int[] convertIndicesListToArray(List indices) { - int[] indicesArray = new int[indices.size()]; - for (int i = 0; i < indicesArray.length; i++) { - indicesArray[i] = indices.get(i); - } - return indicesArray; - } - - private static float convertDataToArrays(List vertices, List textures, - List normals, float[] verticesArray, float[] texturesArray, - float[] normalsArray) { - float furthestPoint = 0; - for (int i = 0; i < vertices.size(); i++) { - Vertex currentVertex = vertices.get(i); - if (currentVertex.getLength() > furthestPoint) { - furthestPoint = currentVertex.getLength(); - } - Vector3f position = currentVertex.getPosition(); - Vector2f textureCoord = textures.get(currentVertex.getTextureIndex()); - Vector3f normalVector = normals.get(currentVertex.getNormalIndex()); - verticesArray[i * 3] = position.x; - verticesArray[i * 3 + 1] = position.y; - verticesArray[i * 3 + 2] = position.z; - texturesArray[i * 2] = textureCoord.x; - texturesArray[i * 2 + 1] = 1 - textureCoord.y; - normalsArray[i * 3] = normalVector.x; - normalsArray[i * 3 + 1] = normalVector.y; - normalsArray[i * 3 + 2] = normalVector.z; - } - return furthestPoint; - } - - private static void dealWithAlreadyProcessedVertex(Vertex previousVertex, int newTextureIndex, - int newNormalIndex, List indices, List vertices) { - if (previousVertex.hasSameTextureAndNormal(newTextureIndex, newNormalIndex)) { - indices.add(previousVertex.getIndex()); - } else { - Vertex anotherVertex = previousVertex.getDuplicateVertex(); - if (anotherVertex != null) { - dealWithAlreadyProcessedVertex(anotherVertex, newTextureIndex, newNormalIndex, - indices, vertices); - } else { - Vertex duplicateVertex = new Vertex(vertices.size(), previousVertex.getPosition()); - duplicateVertex.setTextureIndex(newTextureIndex); - duplicateVertex.setNormalIndex(newNormalIndex); - previousVertex.setDuplicateVertex(duplicateVertex); - vertices.add(duplicateVertex); - indices.add(duplicateVertex.getIndex()); - } - - } - } - - private static void removeUnusedVertices(List vertices){ - for(Vertex vertex:vertices){ - if(!vertex.isSet()){ - vertex.setTextureIndex(0); - vertex.setNormalIndex(0); - } - } + AIFileIO fileIo = AIFileIO.create(); + AIFileOpenProcI fileOpenProc = new AIFileOpenProc() { + public long invoke(long pFileIO, long fileName, long openMode) { + AIFile aiFile = AIFile.create(); + final ByteBuffer data; + String fileNameUtf8 = memUTF8(fileName); + try { + data = IOUtil.ioResourceToByteBuffer(fileNameUtf8, 8192); + } catch (IOException e) { + throw new RuntimeException("Could not open file: " + fileNameUtf8); + } + AIFileReadProcI fileReadProc = new AIFileReadProc() { + public long invoke(long pFile, long pBuffer, long size, long count) { + long max = Math.min(data.remaining(), size * count); + memCopy(memAddress(data) + data.position(), pBuffer, max); + return max; + } + }; + AIFileSeekI fileSeekProc = new AIFileSeek() { + public int invoke(long pFile, long offset, int origin) { + if (origin == Assimp.aiOrigin_CUR) { + data.position(data.position() + (int) offset); + } else if (origin == Assimp.aiOrigin_SET) { + data.position((int) offset); + } else if (origin == Assimp.aiOrigin_END) { + data.position(data.limit() + (int) offset); + } + return 0; + } + }; + AIFileTellProcI fileTellProc = new AIFileTellProc() { + public long invoke(long pFile) { + return data.limit(); + } + }; + aiFile.ReadProc(fileReadProc); + aiFile.SeekProc(fileSeekProc); + aiFile.FileSizeProc(fileTellProc); + return aiFile.address(); + } + }; + AIFileCloseProcI fileCloseProc = new AIFileCloseProc() { + public void invoke(long pFileIO, long pFile) { + /* Nothing to do */ + } + }; + fileIo.set(fileOpenProc, fileCloseProc, NULL); + AIScene scene = aiImportFileEx(RES_LOC+objFileName, + aiProcess_JoinIdenticalVertices | aiProcess_Triangulate, fileIo); + if (scene == null) { + throw new IllegalStateException(aiGetErrorString()); + } + return new ModelData(scene); } } \ No newline at end of file diff --git a/src/main/java/io/github/hydos/ginger/engine/postprocessing/ContrastChanger.java b/src/main/java/io/github/hydos/ginger/engine/postprocessing/ContrastChanger.java index 943306f..da891d0 100644 --- a/src/main/java/io/github/hydos/ginger/engine/postprocessing/ContrastChanger.java +++ b/src/main/java/io/github/hydos/ginger/engine/postprocessing/ContrastChanger.java @@ -16,7 +16,6 @@ public class ContrastChanger { public void render(int texture) { shader.start(); GL13.glActiveTexture(GL13.GL_TEXTURE0); - System.out.println(texture); GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture); renderer.renderQuad(); shader.stop(); diff --git a/src/main/java/io/github/hydos/ginger/engine/render/texture/Image.java b/src/main/java/io/github/hydos/ginger/engine/render/texture/Image.java index 0e302e9..318eeac 100644 --- a/src/main/java/io/github/hydos/ginger/engine/render/texture/Image.java +++ b/src/main/java/io/github/hydos/ginger/engine/render/texture/Image.java @@ -35,11 +35,6 @@ public class Image { throw new RuntimeException("Failed to read image information: " + stbi_failure_reason()); } -// System.out.println("Image width: " + w.get(0)); -// System.out.println("Image height: " + h.get(0)); -// System.out.println("Image components: " + comp.get(0)); -// System.out.println("Image HDR: " + stbi_is_hdr_from_memory(imageBuffer)); - // Decode the image img = stbi_load_from_memory(imageBuffer, w, h, comp, 0); if (img == null) { diff --git a/target/classes/io/github/hydos/ginger/Example.class b/target/classes/io/github/hydos/ginger/Example.class index efb3044..09cbe87 100644 Binary files a/target/classes/io/github/hydos/ginger/Example.class and b/target/classes/io/github/hydos/ginger/Example.class differ diff --git a/target/classes/io/github/hydos/ginger/engine/io/Window.class b/target/classes/io/github/hydos/ginger/engine/io/Window.class index efd9fc2..e44d3a9 100644 Binary files a/target/classes/io/github/hydos/ginger/engine/io/Window.class and b/target/classes/io/github/hydos/ginger/engine/io/Window.class differ diff --git a/target/classes/io/github/hydos/ginger/engine/obj/ModelData.class b/target/classes/io/github/hydos/ginger/engine/obj/ModelData.class index b1cd759..ad34bf9 100644 Binary files a/target/classes/io/github/hydos/ginger/engine/obj/ModelData.class and b/target/classes/io/github/hydos/ginger/engine/obj/ModelData.class differ diff --git a/target/classes/io/github/hydos/ginger/engine/obj/OBJFileLoader.class b/target/classes/io/github/hydos/ginger/engine/obj/OBJFileLoader.class index 917714b..d1d330f 100644 Binary files a/target/classes/io/github/hydos/ginger/engine/obj/OBJFileLoader.class and b/target/classes/io/github/hydos/ginger/engine/obj/OBJFileLoader.class differ diff --git a/target/classes/io/github/hydos/ginger/engine/postProcessing/ContrastChanger.class b/target/classes/io/github/hydos/ginger/engine/postProcessing/ContrastChanger.class index 09a9125..48531c4 100644 Binary files a/target/classes/io/github/hydos/ginger/engine/postProcessing/ContrastChanger.class and b/target/classes/io/github/hydos/ginger/engine/postProcessing/ContrastChanger.class differ