3D models

This commit is contained in:
EvilMuffinHa 2020-05-29 19:34:51 -04:00
parent 3192682853
commit 68720e1d1f
9 changed files with 200103 additions and 32 deletions

View File

@ -4,6 +4,7 @@
<exclude-output /> <exclude-output />
<content url="file://$MODULE_DIR$"> <content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/resources" isTestSource="false" />
</content> </content>
<orderEntry type="inheritedJdk" /> <orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />

BIN
resources/models/bunny.stl Normal file

Binary file not shown.

200007
resources/models/dragon.obj Executable file

File diff suppressed because it is too large Load Diff

View File

@ -3,6 +3,7 @@ import org.hl.engine.io.Display;
import org.hl.engine.io.Input; import org.hl.engine.io.Input;
import org.hl.engine.math.lalg.Vector3f; import org.hl.engine.math.lalg.Vector3f;
import org.hl.engine.math.lalg.Vector2f; import org.hl.engine.math.lalg.Vector2f;
import org.hl.engine.objects.FirstPersonCamera;
import org.hl.engine.objects.GameObject; import org.hl.engine.objects.GameObject;
import org.hl.engine.objects.ThirdPersonCamera; import org.hl.engine.objects.ThirdPersonCamera;
import org.hl.engine.utils.FileUtils; import org.hl.engine.utils.FileUtils;
@ -20,6 +21,8 @@ public class Test implements Game {
public Renderer renderer; public Renderer renderer;
public Shader shader; public Shader shader;
public Mesh dragon = ModelLoader.loadModel("resources/models/dragon.obj", "resources/textures/b.png");
public Mesh cube = new Mesh(new Vertex[] { public Mesh cube = new Mesh(new Vertex[] {
//Back face //Back face
new Vertex(new Vector3f(-0.5f, 0.5f, -0.5f), new Vector2f(0.0f, 0.0f)), new Vertex(new Vector3f(-0.5f, 0.5f, -0.5f), new Vector2f(0.0f, 0.0f)),
@ -84,16 +87,15 @@ public class Test implements Game {
public boolean lockToggle = false; public boolean lockToggle = false;
public GameObject[] gameObjects = new GameObject[500];
public GameObject cubeObject = new GameObject(cube, new Vector3f(0, 0, 0 ), new Vector3f(0, 0, 0), new Vector3f(1, 1, 1)); public GameObject cubeObject = new GameObject(cube, new Vector3f(0, 0, 0 ), new Vector3f(0, 0, 0), new Vector3f(1, 1, 1));
//public GameObject planeObject = new GameObject(plane, new Vector3f(0, 0, 0 ), new Vector3f(0, 0, 0), new Vector3f(1, 1, 1));
public GameObject planeObject = new GameObject("resources/objects/plane.mesh", new Vector3f(0, 0, 0 ), new Vector3f(0, 0, 0), new Vector3f(1, 1, 1)); public GameObject planeObject = new GameObject("resources/objects/plane.mesh", new Vector3f(0, 0, 0 ), new Vector3f(0, 0, 0), new Vector3f(1, 1, 1));
public GameObject dragonObject = new GameObject(dragon, new Vector3f(0, 0, 0), new Vector3f(0, 0, 0), new Vector3f(1, 1, 1));
public ThirdPersonCamera camera = new ThirdPersonCamera(new Vector3f(0, 0, 5), new Vector3f(0, 0, 0), cubeObject, 0.5f, 5, 0.1f, 20f, true, true, true); // public ThirdPersonCamera camera = new ThirdPersonCamera(new Vector3f(0, 0, 5), new Vector3f(0, 0, 0), cubeObject, 0.5f, 5, 0.1f, 20f, false, true, true);
public FirstPersonCamera camera = new FirstPersonCamera(new Vector3f(0, 0, 5), new Vector3f(0, 0, 0), 0.5f, 0.15f);
public Test() throws Exception { public Test() throws Exception {
} }
@ -138,13 +140,10 @@ public class Test implements Game {
// rendering the cube and plane // rendering the cube and plane
renderer.renderMesh(cubeObject, camera); renderer.renderMesh(cubeObject, camera);
renderer.renderMesh(planeObject, camera); renderer.renderMesh(planeObject, camera);
renderer.renderMesh(dragonObject, camera);
//swap buffers so the new one will appear //swap buffers so the new one will appear
for (GameObject gameObject : gameObjects) { display.reset();
renderer.renderMesh(gameObject, camera);
}
display.swapBuffers();
} }
@ -167,19 +166,12 @@ public class Test implements Game {
// Creating / displaying the cube and plane // Creating / displaying the cube and plane
planeObject.create(); planeObject.create();
cubeObject.create();
dragonObject.create();
// Creating the shader // Creating the shader
shader.create(); shader.create();
for (int i = 0; i < gameObjects.length; i ++) {
gameObjects[i] = new GameObject(cube, new Vector3f( (float)Math.random()*50 - 25 , (float)Math.random()*50, (float)Math.random()*50 - 25), new Vector3f(0, 0, 0), new Vector3f(1, 1, 1));
}
for (GameObject gameObject : gameObjects) {
gameObject.create();
}
// Creating the input // Creating the input
i = new Input(display); i = new Input(display);
@ -195,9 +187,7 @@ public class Test implements Game {
shader.destroy(); shader.destroy();
cubeObject.destroy(); cubeObject.destroy();
planeObject.destroy(); planeObject.destroy();
for (GameObject gameObject : gameObjects) { dragonObject.destroy();
gameObject.destroy();
}
} }
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {

View File

@ -151,4 +151,8 @@ public class Mesh {
public boolean isType() { public boolean isType() {
return type; return type;
} }
public void setMaterial(Material material) {
this.material = material;
}
} }

View File

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

View File

@ -8,7 +8,7 @@ public class Vertex {
// Just a vertex // Just a vertex
private Vector3f position; private Vector3f position, normal;
private Vector3f color; private Vector3f color;
private Vector2f textureCoords; private Vector2f textureCoords;
private boolean type; private boolean type;
@ -19,6 +19,13 @@ public class Vertex {
this.color = new Vector3f(1, 1, 1); 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) { public Vertex(Vector3f position, Vector3f color) {
this.position = position; this.position = position;
this.color = color; this.color = color;
@ -47,4 +54,8 @@ public class Vertex {
public boolean isType() { public boolean isType() {
return type; return type;
} }
public Vector3f getNormal() {
return normal;
}
} }

View File

@ -254,4 +254,8 @@ public class Display {
public void setBackgroundColor(float r, float g, float b) { public void setBackgroundColor(float r, float g, float b) {
background.setVector(r, g, b); background.setVector(r, g, b);
} }
public void reset() {
swapBuffers();
}
} }

View File

@ -28,9 +28,7 @@ public class GameObject {
} }
public GameObject(String meshFileName, Vector3f position, Vector3f rotation, Vector3f scale) throws Exception { public GameObject(String meshFileName, Vector3f position, Vector3f rotation, Vector3f scale) throws Exception {
if (!meshFileName.endsWith(".mesh")) { if (!meshFileName.endsWith(".mesh")) { throw new Exception("Wrong file type! "); }
throw new Exception("Wrong file type! ");
}
Yaml yaml = new Yaml(); Yaml yaml = new Yaml();
FileInputStream inputStream = new FileInputStream(meshFileName); FileInputStream inputStream = new FileInputStream(meshFileName);
YMesh yMesh = yaml.loadAs(inputStream, YMesh.class); YMesh yMesh = yaml.loadAs(inputStream, YMesh.class);
@ -54,22 +52,17 @@ public class GameObject {
if (!type.equals("texture") && !type.equals("color")) { if (!type.equals("texture") && !type.equals("color")) {
throw new Exception("Incorrect type. Type can only be texture or color. "); throw new Exception("Incorrect type. Type can only be texture or color. ");
} }
for (int i = 0; i < vertices.length; i ++) { for (int i = 0; i < vertices.length; i ++) {
Vertex value = new Vertex( 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].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 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; meshFormat[i] = value;
} }
int[] indices = new int[cull.length]; int[] indices = new int[cull.length];
for (int j = 0; j < cull.length; j ++) { for (int j = 0; j < cull.length; j ++) {
indices[j] = cull[j]; indices[j] = cull[j];
} }
this.mesh = new Mesh(meshFormat, indices, new Material(new Texture(texture)), type); this.mesh = new Mesh(meshFormat, indices, new Material(new Texture(texture)), type);
this.position = position; this.position = position;
this.scale = scale; this.scale = scale;