stuff which involves some rewriting
parent
0fcb1f7d4b
commit
fe151b84cc
|
@ -54,7 +54,7 @@ public class Example {
|
||||||
public void main(String[] args) {
|
public void main(String[] args) {
|
||||||
|
|
||||||
|
|
||||||
Window.create(2000, 1200, "Ginger Example", 60);
|
Window.create(800, 1200, "Ginger Example", 60);
|
||||||
|
|
||||||
GingerMain.init();
|
GingerMain.init();
|
||||||
|
|
||||||
|
|
|
@ -58,7 +58,7 @@ public class Window {
|
||||||
GLFW.glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MINOR, 3);
|
GLFW.glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||||
GLFW.glfwWindowHint(GLFW.GLFW_OPENGL_PROFILE, GLFW.GLFW_OPENGL_CORE_PROFILE);
|
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_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());
|
GLFWVidMode vidmode = GLFW.glfwGetVideoMode(GLFW.glfwGetPrimaryMonitor());
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package io.github.hydos.ginger.engine.obj;
|
package io.github.hydos.ginger.engine.obj;
|
||||||
|
|
||||||
|
import org.lwjgl.assimp.AIScene;
|
||||||
|
|
||||||
public class ModelData {
|
public class ModelData {
|
||||||
|
|
||||||
private float[] vertices;
|
private float[] vertices;
|
||||||
|
@ -17,6 +19,10 @@ public class ModelData {
|
||||||
this.furthestPoint = furthestPoint;
|
this.furthestPoint = furthestPoint;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ModelData(AIScene scene) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
public float[] getVertices() {
|
public float[] getVertices() {
|
||||||
return vertices;
|
return vertices;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,155 +1,93 @@
|
||||||
package io.github.hydos.ginger.engine.obj;
|
package io.github.hydos.ginger.engine.obj;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import static org.lwjgl.assimp.Assimp.aiGetErrorString;
|
||||||
import java.io.IOException;
|
import static org.lwjgl.assimp.Assimp.aiImportFileEx;
|
||||||
import java.io.InputStreamReader;
|
import static org.lwjgl.assimp.Assimp.aiProcess_JoinIdenticalVertices;
|
||||||
import java.util.ArrayList;
|
import static org.lwjgl.assimp.Assimp.aiProcess_Triangulate;
|
||||||
import java.util.List;
|
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 {
|
public class OBJFileLoader {
|
||||||
|
|
||||||
private static final String RES_LOC = "/models/";
|
private static final String RES_LOC = "/models/";
|
||||||
|
|
||||||
public static ModelData loadOBJ(String objFileName) {
|
public static ModelData loadOBJ(String objFileName) {
|
||||||
String objFile = RES_LOC + objFileName;
|
AIFileIO fileIo = AIFileIO.create();
|
||||||
System.out.println(objFile);
|
AIFileOpenProcI fileOpenProc = new AIFileOpenProc() {
|
||||||
InputStreamReader isr = new InputStreamReader(Class.class.getResourceAsStream(objFile));
|
public long invoke(long pFileIO, long fileName, long openMode) {
|
||||||
BufferedReader reader = new BufferedReader(isr);
|
AIFile aiFile = AIFile.create();
|
||||||
String line;
|
final ByteBuffer data;
|
||||||
List<Vertex> vertices = new ArrayList<Vertex>();
|
String fileNameUtf8 = memUTF8(fileName);
|
||||||
List<Vector2f> textures = new ArrayList<Vector2f>();
|
try {
|
||||||
List<Vector3f> normals = new ArrayList<Vector3f>();
|
data = IOUtil.ioResourceToByteBuffer(fileNameUtf8, 8192);
|
||||||
List<Integer> indices = new ArrayList<Integer>();
|
} catch (IOException e) {
|
||||||
try {
|
throw new RuntimeException("Could not open file: " + fileNameUtf8);
|
||||||
while (true) {
|
}
|
||||||
line = reader.readLine();
|
AIFileReadProcI fileReadProc = new AIFileReadProc() {
|
||||||
if (line.startsWith("v ")) {
|
public long invoke(long pFile, long pBuffer, long size, long count) {
|
||||||
String[] currentLine = line.split(" ");
|
long max = Math.min(data.remaining(), size * count);
|
||||||
Vector3f vertex = new Vector3f(Float.valueOf(currentLine[1]),
|
memCopy(memAddress(data) + data.position(), pBuffer, max);
|
||||||
Float.valueOf(currentLine[2]),
|
return max;
|
||||||
Float.valueOf(currentLine[3]));
|
}
|
||||||
Vertex newVertex = new Vertex(vertices.size(), vertex);
|
};
|
||||||
vertices.add(newVertex);
|
AIFileSeekI fileSeekProc = new AIFileSeek() {
|
||||||
|
public int invoke(long pFile, long offset, int origin) {
|
||||||
} else if (line.startsWith("vt ")) {
|
if (origin == Assimp.aiOrigin_CUR) {
|
||||||
String[] currentLine = line.split(" ");
|
data.position(data.position() + (int) offset);
|
||||||
Vector2f texture = new Vector2f(Float.valueOf(currentLine[1]),
|
} else if (origin == Assimp.aiOrigin_SET) {
|
||||||
Float.valueOf(currentLine[2]));
|
data.position((int) offset);
|
||||||
textures.add(texture);
|
} else if (origin == Assimp.aiOrigin_END) {
|
||||||
} else if (line.startsWith("vn ")) {
|
data.position(data.limit() + (int) offset);
|
||||||
String[] currentLine = line.split(" ");
|
}
|
||||||
Vector3f normal = new Vector3f(Float.valueOf(currentLine[1]),
|
return 0;
|
||||||
Float.valueOf(currentLine[2]),
|
}
|
||||||
Float.valueOf(currentLine[3]));
|
};
|
||||||
normals.add(normal);
|
AIFileTellProcI fileTellProc = new AIFileTellProc() {
|
||||||
} else if (line.startsWith("f ")) {
|
public long invoke(long pFile) {
|
||||||
break;
|
return data.limit();
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
while (line != null && line.startsWith("f ")) {
|
aiFile.ReadProc(fileReadProc);
|
||||||
String[] currentLine = line.split(" ");
|
aiFile.SeekProc(fileSeekProc);
|
||||||
String[] vertex1 = currentLine[1].split("/");
|
aiFile.FileSizeProc(fileTellProc);
|
||||||
String[] vertex2 = currentLine[2].split("/");
|
return aiFile.address();
|
||||||
String[] vertex3 = currentLine[3].split("/");
|
}
|
||||||
processVertex(vertex1, vertices, indices);
|
};
|
||||||
processVertex(vertex2, vertices, indices);
|
AIFileCloseProcI fileCloseProc = new AIFileCloseProc() {
|
||||||
processVertex(vertex3, vertices, indices);
|
public void invoke(long pFileIO, long pFile) {
|
||||||
line = reader.readLine();
|
/* Nothing to do */
|
||||||
}
|
}
|
||||||
reader.close();
|
};
|
||||||
} catch (IOException e) {
|
fileIo.set(fileOpenProc, fileCloseProc, NULL);
|
||||||
System.err.println("Error reading the file");
|
AIScene scene = aiImportFileEx(RES_LOC+objFileName,
|
||||||
}
|
aiProcess_JoinIdenticalVertices | aiProcess_Triangulate, fileIo);
|
||||||
removeUnusedVertices(vertices);
|
if (scene == null) {
|
||||||
float[] verticesArray = new float[vertices.size() * 3];
|
throw new IllegalStateException(aiGetErrorString());
|
||||||
float[] texturesArray = new float[vertices.size() * 2];
|
}
|
||||||
float[] normalsArray = new float[vertices.size() * 3];
|
return new ModelData(scene);
|
||||||
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<Vertex> vertices, List<Integer> 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<Integer> 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<Vertex> vertices, List<Vector2f> textures,
|
|
||||||
List<Vector3f> 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<Integer> indices, List<Vertex> 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<Vertex> vertices){
|
|
||||||
for(Vertex vertex:vertices){
|
|
||||||
if(!vertex.isSet()){
|
|
||||||
vertex.setTextureIndex(0);
|
|
||||||
vertex.setNormalIndex(0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -16,7 +16,6 @@ public class ContrastChanger {
|
||||||
public void render(int texture) {
|
public void render(int texture) {
|
||||||
shader.start();
|
shader.start();
|
||||||
GL13.glActiveTexture(GL13.GL_TEXTURE0);
|
GL13.glActiveTexture(GL13.GL_TEXTURE0);
|
||||||
System.out.println(texture);
|
|
||||||
GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture);
|
GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture);
|
||||||
renderer.renderQuad();
|
renderer.renderQuad();
|
||||||
shader.stop();
|
shader.stop();
|
||||||
|
|
|
@ -35,11 +35,6 @@ public class Image {
|
||||||
throw new RuntimeException("Failed to read image information: " + stbi_failure_reason());
|
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
|
// Decode the image
|
||||||
img = stbi_load_from_memory(imageBuffer, w, h, comp, 0);
|
img = stbi_load_from_memory(imageBuffer, w, h, comp, 0);
|
||||||
if (img == null) {
|
if (img == null) {
|
||||||
|
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue