pull/1/head
hYdos 2020-02-23 10:18:11 +10:00
parent 775638ed73
commit 9047d03883
11 changed files with 105 additions and 99 deletions

View File

@ -54,7 +54,7 @@ public class Example {
public void main(String[] args) { public void main(String[] args) {
Window.create(800, 1200, "Ginger Example", 60); Window.create(1200, 800, "Ginger Example", 60);
GingerMain.init(); GingerMain.init();
@ -75,7 +75,7 @@ public class Example {
FontType font = new FontType(Loader.loadFontAtlas("candara.png"), "candara.fnt"); FontType font = new FontType(Loader.loadFontAtlas("candara.png"), "candara.fnt");
GUIText text = new GUIText("hi, this is some sample text", 3, font, new Vector2f(0,0), 1f, true); GUIText text = new GUIText("german", 3, font, new Vector2f(0,0), 1f, true);
text.setColour(0, 1, 0); text.setColour(0, 1, 0);
text.setBorderWidth(0.7f); text.setBorderWidth(0.7f);
text.setBorderEdge(0.4f); text.setBorderEdge(0.4f);

View File

@ -0,0 +1,5 @@
package io.github.hydos.ginger.engine.obj;
public class Material {
}

View File

@ -1,7 +1,5 @@
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;
@ -19,9 +17,6 @@ public class ModelData {
this.furthestPoint = furthestPoint; this.furthestPoint = furthestPoint;
} }
public ModelData(AIScene scene) {
}
public float[] getVertices() { public float[] getVertices() {
return vertices; return vertices;

View File

@ -7,7 +7,7 @@ import io.github.hydos.ginger.engine.utils.Loader;
public class ModelLoader { public class ModelLoader {
public static TexturedModel loadModel(String objPath, String texturePath) { public static TexturedModel loadModel(String objPath, String texturePath) {
ModelData data = OBJFileLoader.loadOBJ(objPath); ModelData data = OBJFileLoader.loadModel(objPath, texturePath);
TexturedModel tm = new TexturedModel(Loader.loadToVAO(data.getVertices(), data.getIndices(), data.getNormals(), data.getTextureCoords()), new ModelTexture(texturePath)); TexturedModel tm = new TexturedModel(Loader.loadToVAO(data.getVertices(), data.getIndices(), data.getNormals(), data.getTextureCoords()), new ModelTexture(texturePath));
return tm; return tm;
} }

View File

@ -1,93 +1,94 @@
package io.github.hydos.ginger.engine.obj; package io.github.hydos.ginger.engine.obj;
import static org.lwjgl.assimp.Assimp.aiGetErrorString; import org.lwjgl.assimp.AIFace;
import static org.lwjgl.assimp.Assimp.aiImportFileEx; import org.lwjgl.assimp.AIMesh;
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.AIScene;
import org.lwjgl.assimp.AIVector3D;
import org.lwjgl.assimp.AIVector3D.Buffer;
import org.lwjgl.assimp.Assimp; 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/"; public static String resourceLocation = "/models/";
public static ModelData loadOBJ(String objFileName) { public static ModelData loadModel(String filePath, String texturePath) {
AIFileIO fileIo = AIFileIO.create(); AIScene scene = null;
AIFileOpenProcI fileOpenProc = new AIFileOpenProc() { try {
public long invoke(long pFileIO, long fileName, long openMode) { scene = Assimp.aiImportFile(resourceLocation + filePath, Assimp.aiProcess_JoinIdenticalVertices | Assimp.aiProcess_Triangulate);
AIFile aiFile = AIFile.create();
final ByteBuffer data; AIMesh mesh = AIMesh.create(scene.mMeshes().get(0));
String fileNameUtf8 = memUTF8(fileName); int vertexCount = mesh.mNumVertices();
try {
data = IOUtil.ioResourceToByteBuffer(fileNameUtf8, 8192); AIVector3D.Buffer vertices = mesh.mVertices();
} catch (IOException e) { AIVector3D.Buffer normals = mesh.mNormals();
throw new RuntimeException("Could not open file: " + fileNameUtf8);
} Vertex[] vertexList = new Vertex[vertexCount];
AIFileReadProcI fileReadProc = new AIFileReadProc() {
public long invoke(long pFile, long pBuffer, long size, long count) { for (int i = 0; i < vertexCount; i++) {
long max = Math.min(data.remaining(), size * count); AIVector3D vertex = vertices.get(i);
memCopy(memAddress(data) + data.position(), pBuffer, max); Vector3f meshVertex = new Vector3f(vertex.x(), vertex.y(), vertex.z());
return max;
} AIVector3D normal = normals.get(i);
}; Vector3f meshNormal = new Vector3f(normal.x(), normal.y(), normal.z());
AIFileSeekI fileSeekProc = new AIFileSeek() {
public int invoke(long pFile, long offset, int origin) { Vector2f meshTextureCoord = new Vector2f(0, 0);
if (origin == Assimp.aiOrigin_CUR) { if (mesh.mNumUVComponents().get(0) != 0) {
data.position(data.position() + (int) offset); AIVector3D texture = mesh.mTextureCoords(0).get(i);
} else if (origin == Assimp.aiOrigin_SET) { meshTextureCoord.setX(texture.x());
data.position((int) offset); meshTextureCoord.setY(texture.y());
} else if (origin == Assimp.aiOrigin_END) { }
data.position(data.limit() + (int) offset);
} vertexList[i] = new Vertex(meshVertex, meshNormal, meshTextureCoord);
return 0; }
}
}; int faceCount = mesh.mNumFaces();
AIFileTellProcI fileTellProc = new AIFileTellProc() { AIFace.Buffer indices = mesh.mFaces();
public long invoke(long pFile) { int[] indicesList = new int[faceCount * 3];
return data.limit();
} for (int i = 0; i < faceCount; i++) {
}; AIFace face = indices.get(i);
aiFile.ReadProc(fileReadProc); indicesList[i * 3 + 0] = face.mIndices().get(0);
aiFile.SeekProc(fileSeekProc); indicesList[i * 3 + 1] = face.mIndices().get(1);
aiFile.FileSizeProc(fileTellProc); indicesList[i * 3 + 2] = face.mIndices().get(2);
return aiFile.address(); }
}
}; return parseMeshData(vertexList, indicesList, normals);
AIFileCloseProcI fileCloseProc = new AIFileCloseProc() { }catch(Exception e) {
public void invoke(long pFileIO, long pFile) { System.err.println("Couldnt load scene file!");
/* Nothing to do */ e.printStackTrace();
} }
};
fileIo.set(fileOpenProc, fileCloseProc, NULL);
AIScene scene = aiImportFileEx(RES_LOC+objFileName, return new ModelData(new float[0], new float[0], new float[0], new int[0], 1F);
aiProcess_JoinIdenticalVertices | aiProcess_Triangulate, fileIo); }
if (scene == null) {
throw new IllegalStateException(aiGetErrorString()); private static ModelData parseMeshData(Vertex[] vertexList, int[] indicesList, Buffer normals) {
} float[] verticies = new float[vertexList.length];
return new ModelData(scene); float[] textureCoords = new float[vertexList.length];
//texture coords where stored in the vertices so there should be as many as there are vertices
int j = 0;
int i = 0;
for(Vertex vertex: vertexList) {
float x = vertex.getPosition().x;
float y = vertex.getPosition().y;
float z = vertex.getPosition().z;
verticies[i] = x;
i++;
verticies[i] = y;
i++;
verticies[i] = z;
i++;
textureCoords[j] = vertex.getTextureIndex().x;
j++;
textureCoords[j] = vertex.getTextureIndex().y;
}
return new ModelData(verticies, textureCoords, null, indicesList, i);
} }
} }

View File

@ -1,14 +1,13 @@
package io.github.hydos.ginger.engine.obj; package io.github.hydos.ginger.engine.obj;
import io.github.hydos.ginger.engine.math.vectors.Vector2f;
import io.github.hydos.ginger.engine.math.vectors.Vector3f; import io.github.hydos.ginger.engine.math.vectors.Vector3f;
public class Vertex { public class Vertex {
private static final int NO_INDEX = -1;
private Vector3f position; private Vector3f position;
private int textureIndex = NO_INDEX; private Vector2f textureIndex = null;
private int normalIndex = NO_INDEX; private Vector3f normalIndex = null;
private Vertex duplicateVertex = null; private Vertex duplicateVertex = null;
private int index; private int index;
private float length; private float length;
@ -19,6 +18,12 @@ public class Vertex {
this.length = position.length(); this.length = position.length();
} }
public Vertex(Vector3f position, Vector3f normal, Vector2f textureCoord) {
this.position = position;
this.normalIndex = normal;
this.textureIndex = textureCoord;
}
public int getIndex(){ public int getIndex(){
return index; return index;
} }
@ -28,18 +33,18 @@ public class Vertex {
} }
public boolean isSet(){ public boolean isSet(){
return textureIndex!=NO_INDEX && normalIndex!=NO_INDEX; return textureIndex!=null && normalIndex!=null;
} }
public boolean hasSameTextureAndNormal(int textureIndexOther,int normalIndexOther){ public boolean hasSameTextureAndNormal(Vector2f textureIndexOther,Vector3f normalIndexOther){
return textureIndexOther==textureIndex && normalIndexOther==normalIndex; return textureIndexOther==textureIndex && normalIndexOther==normalIndex;
} }
public void setTextureIndex(int textureIndex){ public void setTextureIndex(Vector2f textureIndex){
this.textureIndex = textureIndex; this.textureIndex = textureIndex;
} }
public void setNormalIndex(int normalIndex){ public void setNormalIndex(Vector3f normalIndex){
this.normalIndex = normalIndex; this.normalIndex = normalIndex;
} }
@ -47,11 +52,11 @@ public class Vertex {
return position; return position;
} }
public int getTextureIndex() { public Vector2f getTextureIndex() {
return textureIndex; return textureIndex;
} }
public int getNormalIndex() { public Vector3f getNormalIndex() {
return normalIndex; return normalIndex;
} }