Compare commits

..

2 Commits

Author SHA1 Message Date
7e2c4c7135 slkjflsd 2020-07-18 22:27:01 -04:00
5fe794304e testing rotation 2020-05-30 21:51:16 -04:00
12 changed files with 200402 additions and 13 deletions

View File

@ -3,7 +3,7 @@
<output-path>$PROJECT_DIR$/out/artifacts/Helium_Engine_jar</output-path> <output-path>$PROJECT_DIR$/out/artifacts/Helium_Engine_jar</output-path>
<root id="archive" name="Helium-Engine.jar"> <root id="archive" name="Helium-Engine.jar">
<element id="module-output" name="Helium-Engine" /> <element id="module-output" name="Helium-Engine" />
<element id="extracted-dir" path="$USER_HOME$/Downloads/joml-1.9.24.jar" path-in-jar="/" /> <element id="extracted-dir" path="$USER_HOME$/Downloads/snakeyaml-1.9.jar" path-in-jar="/" />
<element id="extracted-dir" path="$USER_HOME$/Downloads/lwjgl-release-3.2.3-custom/lwjgl.jar" path-in-jar="/" /> <element id="extracted-dir" path="$USER_HOME$/Downloads/lwjgl-release-3.2.3-custom/lwjgl.jar" path-in-jar="/" />
<element id="extracted-dir" path="$USER_HOME$/Downloads/lwjgl-release-3.2.3-custom/lwjgl-egl.jar" path-in-jar="/" /> <element id="extracted-dir" path="$USER_HOME$/Downloads/lwjgl-release-3.2.3-custom/lwjgl-egl.jar" path-in-jar="/" />
<element id="extracted-dir" path="$USER_HOME$/Downloads/lwjgl-release-3.2.3-custom/lwjgl-lz4.jar" path-in-jar="/" /> <element id="extracted-dir" path="$USER_HOME$/Downloads/lwjgl-release-3.2.3-custom/lwjgl-lz4.jar" path-in-jar="/" />

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

@ -0,0 +1,59 @@
type: color
vertices:
-
vertex:
- -20
- -0.5000001
- 20
texture:
- 0
- 0
color:
- 0
- 0
- 1
-
vertex:
- -20
- -0.5000001
- -20
texture:
- 0
- 1
color:
- 0
- 0
- 1
-
vertex:
- 20
- -0.5000001
- -20
texture:
- 1
- 1
color:
- 1
- 0
- 1
-
vertex:
- 20
- -0.5000001
- 20
texture:
- 1
- 0
color:
- 1
- 0
- 1
cull:
- 0
- 1
- 3
- 3
- 1
- 2
texture: "resources/textures/thonk.png"

BIN
resources/textures/b.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.0 KiB

195
src/Test.java Normal file
View File

@ -0,0 +1,195 @@
import org.hl.engine.graphics.*;
import org.hl.engine.io.Display;
import org.hl.engine.io.Input;
import org.hl.engine.math.lalg.Vector3f;
import org.hl.engine.math.lalg.Vector2f;
import org.hl.engine.objects.FirstPersonCamera;
import org.hl.engine.objects.GameObject;
import org.hl.engine.objects.ThirdPersonCamera;
import org.hl.engine.utils.FileUtils;
import org.lwjgl.glfw.GLFW;
import java.io.IOException;
public class Test implements Game {
// Defining original parts of the game
public final static int WIDTH = 1280, HEIGHT = 760;
public final String windowName = "Game!";
public Display display;
public Input i;
public Renderer renderer;
public Shader shader;
public boolean lockToggle = false;
public GameObject cubeObject = new GameObject(new Mesh(new Vertex[] {
//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, 1.0f)),
new Vertex(new Vector3f( 0.5f, -0.5f, -0.5f), new Vector2f(1.0f, 1.0f)),
new Vertex(new Vector3f( 0.5f, 0.5f, -0.5f), new Vector2f(1.0f, 0.0f)),
//Front 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, 1.0f)),
new Vertex(new Vector3f( 0.5f, -0.5f, 0.5f), new Vector2f(1.0f, 1.0f)),
new Vertex(new Vector3f( 0.5f, 0.5f, 0.5f), new Vector2f(1.0f, 0.0f)),
//Right 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, 1.0f)),
new Vertex(new Vector3f( 0.5f, -0.5f, 0.5f), new Vector2f(1.0f, 1.0f)),
new Vertex(new Vector3f( 0.5f, 0.5f, 0.5f), new Vector2f(1.0f, 0.0f)),
//Left 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, 1.0f)),
new Vertex(new Vector3f(-0.5f, -0.5f, 0.5f), new Vector2f(1.0f, 1.0f)),
new Vertex(new Vector3f(-0.5f, 0.5f, 0.5f), new Vector2f(1.0f, 0.0f)),
//Top 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, 1.0f)),
new Vertex(new Vector3f( 0.5f, 0.5f, -0.5f), new Vector2f(1.0f, 1.0f)),
new Vertex(new Vector3f( 0.5f, 0.5f, 0.5f), new Vector2f(1.0f, 0.0f)),
//Bottom 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, 1.0f)),
new Vertex(new Vector3f( 0.5f, -0.5f, -0.5f), new Vector2f(1.0f, 1.0f)),
new Vertex(new Vector3f( 0.5f, -0.5f, 0.5f), new Vector2f(1.0f, 0.0f)),
}, new int[] {
//Back face
0, 1, 3,
3, 1, 2,
//Front face
4, 5, 7,
7, 5, 6,
//Right face
8, 9, 11,
11, 9, 10,
//Left face
12, 13, 15,
15, 13, 14,
//Top face
16, 17, 19,
19, 17, 18,
//Bottom face
20, 21, 23,
23, 21, 22
}, new Material(new Texture("resources/textures/b.png")), "texture"), 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("resources/models/dragon.obj", "resources/textures/b.png", 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, 12f, true, true, true);
// public FirstPersonCamera camera = new FirstPersonCamera(new Vector3f(0, 0, 5), new Vector3f(0, 0, 0), 0.1f, 0.15f);
public Test() throws Exception {
}
public void run() throws Exception {
setup();
while (!(display.shouldClose())) {
loop();
}
close();
}
public void loop() throws Exception {
//First updating
int frames = display.update();
display.setWindowName(display.getWindowName().substring(0, 4) + " (Frames : " + frames + ")");
if (display.isLocked()) {
camera.standardKeybindUpdate();
}
if (i.buttonPress(GLFW.GLFW_MOUSE_BUTTON_LEFT)) {
if (!lockToggle) {
lockToggle = true;
display.mouseState(true);
}
} else if (i.isKeyDown(GLFW.GLFW_KEY_ESCAPE)) {
lockToggle = false;
display.mouseState(lockToggle);
}
cubeObject.setRotation(Vector3f.add(cubeObject.getRotation(), new Vector3f(1, 1, 1)));
i.reset();
// Now Render!
// rendering the cube and plane
renderer.renderObject3D(cubeObject, camera);
renderer.renderObject3D(planeObject, camera);
renderer.renderObject3D(dragonObject, camera);
//swap buffers so the new one will appear
display.reset();
}
public void setup() throws Exception {
//First, set up the display
display = new Display(WIDTH, HEIGHT, windowName);
display.create();
// Open the shaders
shader = new Shader(0);
// Set up the renderer
renderer = new Renderer(display, shader);
// Changing the background color
display.setBackgroundColor(0.53f, .81f, 0.92f);
// Creating / displaying the cube and plane
planeObject.create();
cubeObject.create();
dragonObject.create();
// Creating the shader
shader.create();
// Creating the input
i = new Input(display);
// Creating the camera
camera.create(i);
}
public void close() {
// Removing everything
display.destroy();
shader.destroy();
cubeObject.destroy();
planeObject.destroy();
dragonObject.destroy();
}
public static void main(String[] args) throws Exception {
//Running
new Test().run();
}
}

View File

@ -13,13 +13,20 @@ public class Renderer {
private Shader shader; private Shader shader;
private Display display; private Display display;
private Matrix4f projectionMatrix;
private static final float fov = 70;
private static final float near = 0.1f;
private static final float far = 1000f;
private Matrix4f orthoProjection;
public Renderer(Display display, Shader shader) { public Renderer(Display display, Shader shader) {
this.shader = shader; this.shader = shader;
this.display = display; this.display = display;
this.projectionMatrix = Matrix4f.perspective(fov, this.display.getAspectRatio(), near, far);
} }
public void renderMesh(GameObject object, Camera camera) { public void renderObject3D(GameObject object, Camera camera) {
// Renders the mesh by drawing it using triangles (least complicated) // Renders the mesh by drawing it using triangles (least complicated)
GL30.glBindVertexArray(object.getMesh().getVertexArrayObject()); GL30.glBindVertexArray(object.getMesh().getVertexArrayObject());
@ -35,7 +42,7 @@ public class Renderer {
shader.bind(); shader.bind();
shader.setUniform("type", object.getMesh().isType()); shader.setUniform("type", object.getMesh().isType());
shader.setUniform("projection", display.getProjectionMatrix()); shader.setUniform("projection", this.projectionMatrix);
shader.setUniform("view", Matrix4f.view(camera.getPosition(), camera.getRotation())); shader.setUniform("view", Matrix4f.view(camera.getPosition(), camera.getRotation()));
shader.setUniform("model", Matrix4f.transform(object.getPosition(), object.getRotation(), object.getScale())); shader.setUniform("model", Matrix4f.transform(object.getPosition(), object.getRotation(), object.getScale()));
@ -48,4 +55,66 @@ public class Renderer {
GL30.glBindVertexArray(0); GL30.glBindVertexArray(0);
} }
public void renderScene3D(Scene scene, Camera camera) {
for (GameObject object: scene.getObjects()) {
GL30.glBindVertexArray(object.getMesh().getVertexArrayObject());
GL30.glEnableVertexAttribArray(0);
GL30.glEnableVertexAttribArray(1);
GL30.glEnableVertexAttribArray(2);
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, object.getMesh().getIndicesBufferObject());
GL13.glActiveTexture(GL13.GL_TEXTURE0);
GL13.glBindTexture(GL11.GL_TEXTURE_2D, object.getMesh().getMaterial().getTexture().getId());
shader.bind();
shader.setUniform("type", object.getMesh().isType());
shader.setUniform("projection", this.projectionMatrix);
shader.setUniform("view", Matrix4f.view(camera.getPosition(), camera.getRotation()));
shader.setUniform("model", Matrix4f.transform(object.getPosition(), object.getRotation(), object.getScale()));
GL11.glDrawElements(GL11.GL_TRIANGLES, object.getMesh().getIndices().length, GL11.GL_UNSIGNED_INT, 0);
shader.unbind();
GL30.glDisableVertexAttribArray(0);
GL30.glDisableVertexAttribArray(1);
GL30.glDisableVertexAttribArray(2);
GL30.glBindVertexArray(0);
}
}
public void renderObject2D(GameObject object, Camera camera, float top, float bottom, float left, float right) {
orthoProjection = Matrix4f.orthoProjection(right, left, bottom, top, near, far);
// Renders the mesh by drawing it using triangles (least complicated)
GL30.glBindVertexArray(object.getMesh().getVertexArrayObject());
GL30.glEnableVertexAttribArray(0);
GL30.glEnableVertexAttribArray(1);
GL30.glEnableVertexAttribArray(2);
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, object.getMesh().getIndicesBufferObject());
GL13.glActiveTexture(GL13.GL_TEXTURE0);
GL13.glBindTexture(GL11.GL_TEXTURE_2D, object.getMesh().getMaterial().getTexture().getId());
shader.bind();
shader.setUniform("type", object.getMesh().isType());
shader.setUniform("projection", this.orthoProjection);
shader.setUniform("view", Matrix4f.view(camera.getPosition(), camera.getRotation()));
shader.setUniform("model", Matrix4f.transform(object.getPosition(), object.getRotation(), object.getScale()));
GL11.glDrawElements(GL11.GL_TRIANGLES, object.getMesh().getIndices().length, GL11.GL_UNSIGNED_INT, 0);
shader.unbind();
GL30.glDisableVertexAttribArray(0);
GL30.glDisableVertexAttribArray(1);
GL30.glDisableVertexAttribArray(2);
GL30.glBindVertexArray(0);
}
} }

View File

@ -0,0 +1,40 @@
package org.hl.engine.graphics;
import org.hl.engine.objects.GameObject;
import java.util.ArrayList;
import java.util.Arrays;
public class Scene {
private ArrayList<GameObject> objects;
public Scene() {
objects = new ArrayList();
}
public ArrayList<GameObject> getObjects() {
return objects;
}
public void setObjects(ArrayList<GameObject> objects) {
this.objects = objects;
}
public void setObjects(GameObject[] objects) {
this.objects.addAll(Arrays.asList(objects));
}
public void addObject(GameObject object) {
this.objects.add(object);
}
public void removeObject(int index) {
this.objects.remove(index);
}
public GameObject getObject(int index) {
return this.objects.get(index);
}
}

View File

@ -17,8 +17,10 @@ public class Shader {
private int vertexID, fragmentID, programID; private int vertexID, fragmentID, programID;
public static final String VERTEXSHADER = "/resources/shaders/mainVertex.glsl"; public static final String VERTEX_SHADER_NO_LIGHT = "/resources/shaders/mainVertex.glsl";
public static final String FRAGSHADER = "/resources/shaders/mainFragment.glsl"; public static final String FRAG_SHADER_NO_LIGHT = "/resources/shaders/mainFragment.glsl";
public static final int NO_LIGHT = 0;
public Shader(String vertexPath, String fragmentPath) { public Shader(String vertexPath, String fragmentPath) {
vertexFile = FileUtils.loadAsString(vertexPath); vertexFile = FileUtils.loadAsString(vertexPath);
@ -26,6 +28,13 @@ public class Shader {
} }
public Shader(int type) {
if (type == 0) {
vertexFile = FileUtils.loadAsString(VERTEX_SHADER_NO_LIGHT);
fragmentFile = FileUtils.loadAsString(FRAG_SHADER_NO_LIGHT);
}
}
public void create() { public void create() {
// Creates the program // Creates the program

View File

@ -30,7 +30,6 @@ public class Display {
private int savedPosY; private int savedPosY;
private int savedWidth; private int savedWidth;
private int savedHeight; private int savedHeight;
private Matrix4f projection;
private boolean isLocked; private boolean isLocked;
private double[] cursorX = new double[1]; private double[] cursorX = new double[1];
private double[] cursorY = new double[1]; private double[] cursorY = new double[1];
@ -40,11 +39,14 @@ public class Display {
// Constructor to create the display // Constructor to create the display
public Display (int width, int height, String windowName, float fov, float near, float far) { public Display (int width, int height, String windowName) {
this.width = width; this.width = width;
this.height = height; this.height = height;
this.windowName = windowName; this.windowName = windowName;
projection = Matrix4f.projection(fov, (float)this.width / (float) this.height, near, far); }
public float getAspectRatio() {
return (float)this.width / (float)this.height;
} }
// Change the window name // Change the window name
@ -74,10 +76,6 @@ public class Display {
return isFullscreen; return isFullscreen;
} }
public Matrix4f getProjectionMatrix() {
return projection;
}
// Makes the screen fullscreen or not based on the argument // Makes the screen fullscreen or not based on the argument
public void setFullscreen(boolean fullscreen) { public void setFullscreen(boolean fullscreen) {
isFullscreen = fullscreen; isFullscreen = fullscreen;
@ -124,6 +122,7 @@ public class Display {
public boolean isResized() { public boolean isResized() {
return isResized; return isResized;
} }

View File

@ -108,7 +108,7 @@ public class Matrix4f {
return result; return result;
} }
public static Matrix4f projection( float fov, float aspectRatio, float near, float far) { public static Matrix4f perspective( float fov, float aspectRatio, float near, float far) {
Matrix4f result = Matrix4f.identity(); Matrix4f result = Matrix4f.identity();
float tan = (float)Math.tan(Math.toRadians(fov / 2)); float tan = (float)Math.tan(Math.toRadians(fov / 2));
@ -124,6 +124,17 @@ public class Matrix4f {
return result; return result;
} }
public static Matrix4f orthoProjection(float right, float left, float bottom, float top, float near, float far) {
Matrix4f result = Matrix4f.identity();
result.set(0, 0, 2/(right - left));
result.set(1, 1, 2/(top - bottom));
result.set(2, 2, -2/(far - near));
result.set(0, 3, -(right + left) / (right - left));
result.set(1, 3, -(top + bottom) / (top - bottom));
result.set(2, 3, -(far + near) / (far - near));
return result;
}
public static Matrix4f view(Vector3f position, Vector3f rotation) { public static Matrix4f view(Vector3f position, Vector3f rotation) {
Vector3f negative = new Vector3f(-position.getX(), -position.getY(), -position.getZ()); Vector3f negative = new Vector3f(-position.getX(), -position.getY(), -position.getZ());