meshes and colors

This commit is contained in:
2020-05-29 15:32:24 -04:00
parent 8d8a90f29a
commit 3192682853
19 changed files with 492 additions and 54 deletions
+12 -1
View File
@@ -15,12 +15,19 @@ public class Mesh {
private int[] indices;
private int vertexArrayObject, positionBufferObject, indicesBufferObject, colorBufferObject, textureBufferObject;
private Material material;
private boolean type;
// A group of vertices combined based on the indexes
public Mesh(Vertex[] vertices, int[] indices, Material material) {
public Mesh(Vertex[] vertices, int[] indices, Material material, String type) throws Exception {
this.vertices = vertices;
this.indices = indices;
this.material = material;
if (!type.equals("texture") && !type.equals("color")) {
throw new Exception("Type must be either texture or color. ");
}
this.type = type.equals("texture");
}
@@ -140,4 +147,8 @@ public class Mesh {
return bufferID;
}
public boolean isType() {
return type;
}
}
+1
View File
@@ -34,6 +34,7 @@ public class Renderer {
shader.bind();
shader.setUniform("type", object.getMesh().isType());
shader.setUniform("projection", display.getProjectionMatrix());
shader.setUniform("view", Matrix4f.view(camera.getPosition(), camera.getRotation()));
shader.setUniform("model", Matrix4f.transform(object.getPosition(), object.getRotation(), object.getScale()));
+17 -8
View File
@@ -11,17 +11,23 @@ public class Vertex {
private Vector3f position;
private Vector3f color;
private Vector2f textureCoords;
public Vertex (Vector3f position, Vector3f color, Vector2f textureCoords) {
this.position = position;
this.color = color;
this.textureCoords = textureCoords;
}
private boolean type;
public Vertex(Vector3f position, Vector2f textureCoords) {
this.position = position;
this.color = new Vector3f(1.0F, 1.0F, 1.0F);
this.textureCoords = textureCoords;
this.color = new Vector3f(1, 1, 1);
}
public Vertex(Vector3f position, Vector3f color) {
this.position = position;
this.color = color;
this.textureCoords = new Vector2f(0, 0);
}
public Vertex(Vector3f position, Vector3f color, Vector2f textureCoords) {
this.position = position;
this.color = color;
this.textureCoords = textureCoords;
}
@@ -38,4 +44,7 @@ public class Vertex {
return textureCoords;
}
public boolean isType() {
return type;
}
}
+7 -3
View File
@@ -138,9 +138,13 @@ public class Display {
//System.exit(1);
throw new Exception("Failed to initialize GLFW! ");
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
if(System.getProperty("os.name").contains("Mac")) {
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
} else {
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
}
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL11.GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
+2 -2
View File
@@ -16,7 +16,7 @@ public class Camera {
return position;
}
public void setPosition(Vector3f position) {
public void setPosition(Vector3f position) throws Exception {
this.position = position;
}
@@ -24,7 +24,7 @@ public class Camera {
return rotation;
}
public void setRotation(Vector3f rotation) {
public void setRotation(Vector3f rotation) throws Exception {
this.rotation = rotation;
}
@@ -25,7 +25,7 @@ public class FirstPersonCamera extends Camera {
this.i = i;
}
public void update () {
public void update () throws Exception {
newMouseX = i.getMouseX();
newMouseY = i.getMouseY();
@@ -0,0 +1,19 @@
package org.hl.engine.objects;
import org.hl.engine.math.lalg.Vector3f;
public class FixedCamera extends Camera {
public FixedCamera(Vector3f position, Vector3f rotation) {
super(position, rotation);
}
@Override
public void setRotation(Vector3f rotation) throws Exception {
throw new Exception("You cannot rotate a fixed camera! ");
}
@Override
public void setPosition(Vector3f rotation) throws Exception {
throw new Exception("You cannot move a fixed camera! ");
}
}
+88
View File
@@ -1,7 +1,20 @@
package org.hl.engine.objects;
import org.hl.engine.graphics.Material;
import org.hl.engine.graphics.Mesh;
import org.hl.engine.graphics.Texture;
import org.hl.engine.graphics.Vertex;
import org.hl.engine.math.lalg.Vector2f;
import org.hl.engine.math.lalg.Vector3f;
import org.hl.engine.objects.yloaders.YMesh;
import org.hl.engine.objects.yloaders.YPoint;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Objects;
public class GameObject {
private Vector3f position, rotation, scale;
@@ -14,6 +27,65 @@ public class GameObject {
this.mesh = mesh;
}
public GameObject(String meshFileName, Vector3f position, Vector3f rotation, Vector3f scale) throws Exception {
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);
Integer[] cull = yMesh.getCull().toArray(new Integer[yMesh.getCull().size()]);
String type = yMesh.getType();
String texture = yMesh.getTexture();
YPoint[] vertices = yMesh.getVertices().toArray(new YPoint[yMesh.getVertices().size()]);
Vertex[] meshFormat = new Vertex[vertices.length];
for (YPoint vertex : vertices) {
if (vertex.getVertex().size() != 3) {
throw new Exception("Incorrect number of coordinates. ");
}
if (vertex.getColor().size() != 3) {
throw new Exception("Incorrect number of color values. ");
}
if (vertex.getTexture().size() != 2) {
throw new Exception("Incorrect number of texture coordinates. ");
}
}
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))
);
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;
this.rotation = rotation;
}
public void create() {
mesh.create();
}
public void destroy() {
mesh.destroy();
}
public Vector3f getPosition() {
return position;
}
@@ -41,4 +113,20 @@ public class GameObject {
public void setScale(Vector3f scale) {
this.scale = scale;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
GameObject that = (GameObject) o;
return Objects.equals(getPosition(), that.getPosition()) &&
Objects.equals(getRotation(), that.getRotation()) &&
Objects.equals(getScale(), that.getScale()) &&
Objects.equals(getMesh(), that.getMesh());
}
@Override
public int hashCode() {
return Objects.hash(getPosition(), getRotation(), getScale(), getMesh());
}
}
@@ -0,0 +1,128 @@
package org.hl.engine.objects;
import org.hl.engine.io.Input;
import org.hl.engine.math.lalg.Vector3f;
import org.lwjgl.glfw.GLFW;
public class ThirdPersonCamera extends Camera {
private GameObject object;
private Input i;
private float distance;
private float angle = 0;
private float horizAngle = 0;
private float vertAngle = 0;
private float moveSpeed;
private float sensitivity;
private float near;
private float far;
private boolean clickToMove;
private boolean clickToZoom;
private boolean zoomEnabled;
private double oldMouseX, oldMouseY = 0;
private double newMouseX, newMouseY;
public ThirdPersonCamera(Vector3f position, Vector3f rotation, GameObject object, float sensitivity, float distance, float near, float far, boolean clickToMove, boolean clickToZoom, boolean zoomEnabled) {
super(position, rotation);
this.object = object;
this.sensitivity = sensitivity;
this.clickToMove = clickToMove;
this.clickToZoom = clickToZoom;
this.zoomEnabled = zoomEnabled;
this.near = near;
this.far = far;
this.distance = distance;
}
public void create(Input i) throws Exception {
this.i = i;
setRotation(getRotation());
setPosition(getPosition());
}
public void update() throws Exception {
near = 0.1f;
newMouseX = i.getMouseX();
newMouseY = i.getMouseY();
float dx = (float) ((float)newMouseX - oldMouseX);
float dy = (float) ((float)newMouseY - oldMouseY);
if (clickToMove && clickToZoom) {
if (i.isButtonDown(GLFW.GLFW_MOUSE_BUTTON_LEFT)) {
vertAngle -= dy * sensitivity;
horizAngle += dx * sensitivity;
}
if (i.isButtonDown(GLFW.GLFW_MOUSE_BUTTON_RIGHT) && zoomEnabled) {
if (distance > 0) {
distance += dy * sensitivity;
} else {
distance = near;
}
}
} else if (clickToMove) {
if (i.isButtonDown(GLFW.GLFW_MOUSE_BUTTON_LEFT)) {
vertAngle -= dy * sensitivity;
horizAngle += dx * sensitivity;
}
if (zoomEnabled) {
if (distance > 0) {
distance += dy * sensitivity;
} else {
distance = near;
}
}
} else if (clickToZoom) {
vertAngle -= dy * sensitivity;
horizAngle += dx * sensitivity;
if (i.isButtonDown(GLFW.GLFW_MOUSE_BUTTON_RIGHT) && zoomEnabled) {
if (distance > 0) {
distance += dy * sensitivity;
} else {
distance = near;
}
}
} else {
vertAngle -= dy * sensitivity;
horizAngle += dx * sensitivity;
if (zoomEnabled) {
if (distance > 0) {
distance += dy * sensitivity;
} else {
distance = near;
}
}
}
oldMouseX = newMouseX;
oldMouseY = newMouseY;
float horizDistance = (float) (distance * Math.cos(Math.toRadians(vertAngle)));
float vertDistance = (float) (distance * Math.sin(Math.toRadians(vertAngle)));
float xOffset = (float) (horizDistance * Math.sin(Math.toRadians(-horizAngle)));
float zOffset = (float) (horizDistance * Math.cos(Math.toRadians(-horizAngle)));
setPosition(new Vector3f(object.getPosition().getX() + xOffset, object.getPosition().getY() - vertDistance, object.getPosition().getZ() + zOffset));
setRotation(new Vector3f(-vertAngle, horizAngle,0));
i.reset();
}
}
@@ -0,0 +1,9 @@
package org.hl.engine.objects;
import org.hl.engine.math.lalg.Vector3f;
public class TopDownCamera extends FixedCamera {
public TopDownCamera(Vector3f position) {
super(position, new Vector3f(90, 0, 0));
}
}
@@ -0,0 +1,42 @@
package org.hl.engine.objects.yloaders ;
import java.util.ArrayList;
public class YMesh implements java.io.Serializable {
private String type;
private ArrayList<YPoint> vertices;
private ArrayList<Integer> cull;
private String texture;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public ArrayList<YPoint> getVertices() {
return vertices;
}
public void setVertices(ArrayList<YPoint> vertices) {
this.vertices = vertices;
}
public ArrayList<Integer> getCull() {
return cull;
}
public void setCull(ArrayList<Integer> cull) {
this.cull = cull;
}
public String getTexture() {
return texture;
}
public void setTexture(String texture) {
this.texture = texture;
}
}
@@ -0,0 +1,33 @@
package org.hl.engine.objects.yloaders;
import java.util.ArrayList;
public class YPoint implements java.io.Serializable {
private ArrayList<Float> vertex;
private ArrayList<Float> texture;
private ArrayList<Float> color;
public ArrayList<Float> getVertex() {
return vertex;
}
public void setVertex(ArrayList<Float> vertex) {
this.vertex = vertex;
}
public ArrayList<Float> getTexture() {
return texture;
}
public void setTexture(ArrayList<Float> texture) {
this.texture = texture;
}
public ArrayList<Float> getColor() {
return color;
}
public void setColor(ArrayList<Float> color) {
this.color = color;
}
}