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) {
Window.create(800, 1200, "Ginger Example", 60);
Window.create(1200, 800, "Ginger Example", 60);
GingerMain.init();
@ -75,7 +75,7 @@ public class Example {
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.setBorderWidth(0.7f);
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;
import org.lwjgl.assimp.AIScene;
public class ModelData {
private float[] vertices;
@ -19,9 +17,6 @@ public class ModelData {
this.furthestPoint = furthestPoint;
}
public ModelData(AIScene scene) {
}
public float[] getVertices() {
return vertices;

View File

@ -7,7 +7,7 @@ import io.github.hydos.ginger.engine.utils.Loader;
public class ModelLoader {
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));
return tm;
}

View File

@ -1,93 +1,94 @@
package io.github.hydos.ginger.engine.obj;
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.AIFace;
import org.lwjgl.assimp.AIMesh;
import org.lwjgl.assimp.AIScene;
import org.lwjgl.assimp.AIVector3D;
import org.lwjgl.assimp.AIVector3D.Buffer;
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 String resourceLocation = "/models/";
public static ModelData loadModel(String filePath, String texturePath) {
AIScene scene = null;
try {
scene = Assimp.aiImportFile(resourceLocation + filePath, Assimp.aiProcess_JoinIdenticalVertices | Assimp.aiProcess_Triangulate);
AIMesh mesh = AIMesh.create(scene.mMeshes().get(0));
int vertexCount = mesh.mNumVertices();
AIVector3D.Buffer vertices = mesh.mVertices();
AIVector3D.Buffer normals = mesh.mNormals();
Vertex[] vertexList = new Vertex[vertexCount];
for (int i = 0; i < vertexCount; i++) {
AIVector3D vertex = vertices.get(i);
Vector3f meshVertex = new Vector3f(vertex.x(), vertex.y(), vertex.z());
AIVector3D normal = normals.get(i);
Vector3f meshNormal = new Vector3f(normal.x(), normal.y(), normal.z());
Vector2f meshTextureCoord = new Vector2f(0, 0);
if (mesh.mNumUVComponents().get(0) != 0) {
AIVector3D texture = mesh.mTextureCoords(0).get(i);
meshTextureCoord.setX(texture.x());
meshTextureCoord.setY(texture.y());
}
vertexList[i] = new Vertex(meshVertex, meshNormal, meshTextureCoord);
}
int faceCount = mesh.mNumFaces();
AIFace.Buffer indices = mesh.mFaces();
int[] indicesList = new int[faceCount * 3];
for (int i = 0; i < faceCount; i++) {
AIFace face = indices.get(i);
indicesList[i * 3 + 0] = face.mIndices().get(0);
indicesList[i * 3 + 1] = face.mIndices().get(1);
indicesList[i * 3 + 2] = face.mIndices().get(2);
}
return parseMeshData(vertexList, indicesList, normals);
}catch(Exception e) {
System.err.println("Couldnt load scene file!");
e.printStackTrace();
}
return new ModelData(new float[0], new float[0], new float[0], new int[0], 1F);
}
public static ModelData loadOBJ(String objFileName) {
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);
private static ModelData parseMeshData(Vertex[] vertexList, int[] indicesList, Buffer normals) {
float[] verticies = new float[vertexList.length];
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;
import io.github.hydos.ginger.engine.math.vectors.Vector2f;
import io.github.hydos.ginger.engine.math.vectors.Vector3f;
public class Vertex {
private static final int NO_INDEX = -1;
private Vector3f position;
private int textureIndex = NO_INDEX;
private int normalIndex = NO_INDEX;
private Vector2f textureIndex = null;
private Vector3f normalIndex = null;
private Vertex duplicateVertex = null;
private int index;
private float length;
@ -19,6 +18,12 @@ public class Vertex {
this.length = position.length();
}
public Vertex(Vector3f position, Vector3f normal, Vector2f textureCoord) {
this.position = position;
this.normalIndex = normal;
this.textureIndex = textureCoord;
}
public int getIndex(){
return index;
}
@ -28,18 +33,18 @@ public class Vertex {
}
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;
}
public void setTextureIndex(int textureIndex){
public void setTextureIndex(Vector2f textureIndex){
this.textureIndex = textureIndex;
}
public void setNormalIndex(int normalIndex){
public void setNormalIndex(Vector3f normalIndex){
this.normalIndex = normalIndex;
}
@ -47,11 +52,11 @@ public class Vertex {
return position;
}
public int getTextureIndex() {
public Vector2f getTextureIndex() {
return textureIndex;
}
public int getNormalIndex() {
public Vector3f getNormalIndex() {
return normalIndex;
}