stuff which involves some rewriting

pull/1/head
hYdos 2020-02-23 08:20:31 +10:00
parent 0fcb1f7d4b
commit fe151b84cc
11 changed files with 89 additions and 151 deletions

View File

@ -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();

View File

@ -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());

View File

@ -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;
}

View File

@ -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<Vertex> vertices = new ArrayList<Vertex>();
List<Vector2f> textures = new ArrayList<Vector2f>();
List<Vector3f> normals = new ArrayList<Vector3f>();
List<Integer> indices = new ArrayList<Integer>();
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<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);
}
}
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);
}
}

View File

@ -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();

View File

@ -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) {