3D models

This commit is contained in:
2020-05-29 19:34:51 -04:00
parent 3192682853
commit 68720e1d1f
9 changed files with 200103 additions and 32 deletions
+4
View File
@@ -151,4 +151,8 @@ public class Mesh {
public boolean isType() {
return type;
}
public void setMaterial(Material material) {
this.material = material;
}
}
@@ -0,0 +1,61 @@
package org.hl.engine.graphics;
import org.hl.engine.math.lalg.Vector2f;
import org.hl.engine.math.lalg.Vector3f;
import org.lwjgl.assimp.*;
public class ModelLoader {
public static Mesh loadModel(String path, String texture) throws Exception {
AIScene scene = Assimp.aiImportFile(path, Assimp.aiProcess_JoinIdenticalVertices | Assimp.aiProcess_Triangulate);
if (scene == null) {
throw new Exception("Couldn't load model at " + path);
}
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 engineVertex = new Vector3f(vertex.x(), vertex.y(), vertex.z());
AIVector3D normal = normals.get(i);
Vector3f engineNormal = new Vector3f(normal.x(), normal.y(), normal.z());
Vector2f meshTextureCoord = new Vector2f(0, 0);
if (mesh.mNumUVComponents().get(0) != 0) {
AIVector3D tex = mesh.mTextureCoords(0).get(i);
meshTextureCoord.setX(tex.x());
meshTextureCoord.setY(tex.y());
}
vertexList[i] = new Vertex(engineVertex, meshTextureCoord, engineNormal);
}
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 new Mesh(vertexList, indicesList, new Material(new Texture(texture)), "texture");
}
}
+12 -1
View File
@@ -8,7 +8,7 @@ public class Vertex {
// Just a vertex
private Vector3f position;
private Vector3f position, normal;
private Vector3f color;
private Vector2f textureCoords;
private boolean type;
@@ -19,6 +19,13 @@ public class Vertex {
this.color = new Vector3f(1, 1, 1);
}
public Vertex(Vector3f position, Vector2f textureCoords, Vector3f normal) {
this.position = position;
this.textureCoords = textureCoords;
this.normal = normal;
this.color = new Vector3f(1, 1, 1);
}
public Vertex(Vector3f position, Vector3f color) {
this.position = position;
this.color = color;
@@ -47,4 +54,8 @@ public class Vertex {
public boolean isType() {
return type;
}
public Vector3f getNormal() {
return normal;
}
}
+4
View File
@@ -254,4 +254,8 @@ public class Display {
public void setBackgroundColor(float r, float g, float b) {
background.setVector(r, g, b);
}
public void reset() {
swapBuffers();
}
}
+2 -9
View File
@@ -28,9 +28,7 @@ public class GameObject {
}
public GameObject(String meshFileName, Vector3f position, Vector3f rotation, Vector3f scale) throws Exception {
if (!meshFileName.endsWith(".mesh")) {
throw new Exception("Wrong file type! ");
}
if (!meshFileName.endsWith(".mesh")) { throw new Exception("Wrong file type! "); }
Yaml yaml = new Yaml();
FileInputStream inputStream = new FileInputStream(meshFileName);
YMesh yMesh = yaml.loadAs(inputStream, YMesh.class);
@@ -54,22 +52,17 @@ public class GameObject {
if (!type.equals("texture") && !type.equals("color")) {
throw new Exception("Incorrect type. Type can only be texture or color. ");
}
for (int i = 0; i < vertices.length; i ++) {
Vertex value = new Vertex(
new Vector3f(vertices[i].getVertex().get(0), vertices[i].getVertex().get(1), vertices[i].getVertex().get(2)),
new Vector3f(vertices[i].getColor().get(0), vertices[i].getColor().get(1), vertices[i].getColor().get(2)),
new Vector2f(vertices[i].getTexture().get(0), vertices[i].getTexture().get(1))
);
new Vector2f(vertices[i].getTexture().get(0), vertices[i].getTexture().get(1)));
meshFormat[i] = value;
}
int[] indices = new int[cull.length];
for (int j = 0; j < cull.length; j ++) {
indices[j] = cull[j];
}
this.mesh = new Mesh(meshFormat, indices, new Material(new Texture(texture)), type);
this.position = position;
this.scale = scale;