diff --git a/pom.xml b/pom.xml
index 4235934..02069ba 100644
--- a/pom.xml
+++ b/pom.xml
@@ -6,7 +6,7 @@
org.opengame
engine
- 0.0.1-SNAPSHOT
+ 0.0.2-SNAPSHOT
11
diff --git a/src/main/java/org/opengame/engine/Engine.java b/src/main/java/org/opengame/engine/Engine.java
index c355455..02678ff 100644
--- a/src/main/java/org/opengame/engine/Engine.java
+++ b/src/main/java/org/opengame/engine/Engine.java
@@ -8,6 +8,7 @@
import org.joml.Vector2f;
import org.lwjgl.PointerBuffer;
import org.lwjgl.bgfx.BGFXInit;
+import org.lwjgl.bgfx.BGFXResolution;
import org.lwjgl.glfw.GLFWErrorCallback;
import org.lwjgl.glfw.GLFWNativeCocoa;
import org.lwjgl.glfw.GLFWNativeWin32;
@@ -42,7 +43,7 @@ public class Engine {
private Scene currentScene;
private float tickTime = 1000;
- private int msPerUpdate = 1000 / 20;
+ private int msPerUpdate;
/**
* Init Vulkan context and window
@@ -52,6 +53,7 @@ public void Init(AppConfig config) {
log.info("Working directory: " + config.getWorkingDirectory());
instance = this;
this.config = config;
+ msPerUpdate = 1000 / config.getUps();
initWindow(config);
}
@@ -66,7 +68,7 @@ private void initWindow(AppConfig config) {
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
windowHandle = glfwCreateWindow(config.getWindowWidth(), config.getWindowHeight(),
- config.getAppName(), NULL, NULL);
+ config.getAppName(), config.isFullScreenMode() ? glfwGetPrimaryMonitor() : NULL, NULL);
setupInputCallbacks();
@@ -107,7 +109,7 @@ private void initWindow(AppConfig config) {
log.info("bgfx renderer: " + bgfx_get_renderer_name(bgfx_get_renderer_type()));
bgfx_set_debug(BGFX_DEBUG_TEXT);
- bgfx_set_view_clear(0, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH, 0x303030ff, 1.0f, 0);
+ bgfx_set_view_clear(0, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH, 0x0, 1.0f, 0);
}
private void logAvailableRenderers() {
@@ -171,7 +173,7 @@ public void startLoop() {
var frameMs = (float) toMs * frameTime;
if (currentScene != null) {
if (tickTime >= msPerUpdate) {
- currentScene.update();
+ currentScene.update((float) time, msPerUpdate);
tickTime = 0;
}
tickTime += frameMs;
@@ -209,4 +211,8 @@ public static int getScreenWidth() {
public static int getScreenHeight() {
return instance.getConfig().getWindowHeight();
}
+
+ public static AppConfig getAppConfig() {
+ return instance.config;
+ }
}
diff --git a/src/main/java/org/opengame/engine/app/AppConfig.java b/src/main/java/org/opengame/engine/app/AppConfig.java
index 4c96aa4..2468d20 100644
--- a/src/main/java/org/opengame/engine/app/AppConfig.java
+++ b/src/main/java/org/opengame/engine/app/AppConfig.java
@@ -26,6 +26,11 @@ public class AppConfig {
private int windowWidth = 800;
private int windowHeight = 600;
+ private boolean fullScreenMode = false;
+
+ // private int fpsLimit = -1;
+ private int ups = 40;
+
private String workingDirectory;
public String getWorkingDirectory() {
@@ -36,10 +41,7 @@ public String getWorkingDirectory() {
return workingDirectory;
}
- public void setWorkingDirectory(String relativePath) throws URISyntaxException, UnsupportedEncodingException {
- var jarPath = new File(AppConfig.class.getProtectionDomain().getCodeSource().getLocation()
- .toURI()).getPath();
- var jarUtfPath = URLDecoder.decode(jarPath, StandardCharsets.UTF_8);
- workingDirectory = jarUtfPath.substring(0, jarUtfPath.lastIndexOf("/") + 1) + relativePath + "/";
+ public void setWorkingDirectory(String fullPath) {
+ workingDirectory = fullPath;
}
}
diff --git a/src/main/java/org/opengame/engine/camera/Camera.java b/src/main/java/org/opengame/engine/camera/Camera.java
index 158da77..6558a99 100644
--- a/src/main/java/org/opengame/engine/camera/Camera.java
+++ b/src/main/java/org/opengame/engine/camera/Camera.java
@@ -1,11 +1,15 @@
package org.opengame.engine.camera;
+import lombok.Getter;
+import lombok.Setter;
+import lombok.extern.java.Log;
import org.joml.Matrix4f;
import org.joml.Matrix4x3f;
import org.joml.Vector3f;
+import org.joml.Vector4f;
import org.lwjgl.system.MemoryUtil;
import org.opengame.engine.Engine;
-import org.opengame.engine.object.SceneObject;
+import org.opengame.engine.object.MaterialObject;
import org.opengame.engine.render.CameraUtils;
import java.nio.FloatBuffer;
@@ -15,55 +19,117 @@
/**
* Base camera class
*/
-public class Camera extends SceneObject {
+@Log
+public class Camera extends MaterialObject {
private final Matrix4x3f view = new Matrix4x3f();
private final FloatBuffer viewBuffer;
private final Matrix4f projection = new Matrix4f();
private final FloatBuffer projectionBuffer;
- protected Vector3f position;
- protected Vector3f rotation;
protected Vector3f direction;
protected Vector3f right;
protected Vector3f left;
protected Vector3f up;
+ private float nearPlane = 0.1f;
+ private float farPlane = 100f;
+
+ @Setter
+ @Getter
+ private boolean debugMode = false;
+
public Camera() {
viewBuffer = MemoryUtil.memAllocFloat(16);
projectionBuffer = MemoryUtil.memAllocFloat(16);
- position = new Vector3f();
- rotation = new Vector3f();
direction = new Vector3f(0, 0, -1);
right = new Vector3f(1, 0, 0);
left = new Vector3f(1, 0, 0);
up = new Vector3f(0, 1, 0);
- CameraUtils.perspective(35, Engine.getScreenWidth(), Engine.getScreenHeight(),
- 0.1f, 100.0f, projection);
+ setPerspective(35, Engine.getScreenWidth(), Engine.getScreenHeight(),
+ nearPlane, nearPlane);
+ }
+
+ public void setPerspective(float fov, int screenWidth, int screenHeight, float nearPlane, float farPlane) {
+ CameraUtils.perspective(fov, screenWidth, screenHeight, nearPlane, farPlane, projection);
+ setViewProjection();
}
public void setViewProjection() {
view.identity();
- var eye = new Vector3f(position);
+ var eye = new Vector3f(getPosition());
eye.add(direction);
- CameraUtils.lookAt(position, eye, view);
+ CameraUtils.lookAt(getPosition(), eye, view);
bgfx_set_view_transform(0, view.get4x4(viewBuffer), projection.get(projectionBuffer));
+
+ if (debugMode) {
+ log.info("Current pos: " + getPosition());
+ log.info("Current rotation: " + getRotation());
+ }
+ }
+
+ @Override
+ public void setPosition(Vector3f position) {
+ super.setPosition(position);
+
+ setViewProjection();
+ }
+
+ @Override
+ public void setRotation(Vector3f rotation) {
+ super.setRotation(rotation);
+
+ rotateLeftRight(rotation.x);
+ rotateUpDown(rotation.y);
+
+ setViewProjection();
+ }
+
+
+ protected void rotateUpDown(float delta) {
+ var normalizedDirection = direction.normalize();
+ var directionNoY = new Vector3f(normalizedDirection.x, 0, normalizedDirection.z).normalize();
+
+ var currentAngleDegrees = Math.toDegrees(Math.acos(directionNoY.dot(direction)));
+ if (normalizedDirection.y < 0.0f) {
+ currentAngleDegrees = -currentAngleDegrees;
+ }
+
+ var newAngleDegrees = currentAngleDegrees + delta;
+
+ if (newAngleDegrees < -85.0f || newAngleDegrees > 85.0f) return;
+
+ var rotationAxis = new Vector3f(normalizedDirection).cross(up).normalize();
+ var rotationMatrix = new Matrix4f().rotate((float) Math.toRadians(delta), rotationAxis);
+
+ var rotatedDirection = new Vector4f(normalizedDirection, 0).mul(rotationMatrix).normalize();
+ direction = new Vector3f(rotatedDirection.x, rotatedDirection.y, rotatedDirection.z);
+ }
+
+ protected void rotateLeftRight(float delta) {
+ var normalizedDirection = direction.normalize();
+ var rotationMatrix = new Matrix4f().rotate((float) Math.toRadians(delta), up);
+ var rotatedDirection = new Vector4f(normalizedDirection, 0).mul(rotationMatrix);
+ direction = new Vector3f(rotatedDirection.x, rotatedDirection.y, rotatedDirection.z);
+
+ var rotatedRight = new Vector4f(right.normalize(), 0).mul(rotationMatrix);
+ right = new Vector3f(rotatedRight.x, rotatedRight.y, rotatedRight.z);
}
public void moveForward(float offset) {
- position.add(direction.x * offset, direction.y * offset, direction.z * offset);
+ getPosition().add(direction.x * offset, direction.y * offset, direction.z * offset);
setViewProjection();
}
public void moveRight(float offset) {
- position.add(right.x * offset, right.y * offset, right.z * offset);
+ getPosition().add(right.x * offset, right.y * offset, right.z * offset);
setViewProjection();
}
public void moveUp(float offset) {
- position.add(up.x * offset, up.y * offset, up.z * offset);
+ getPosition().add(up.x * offset, up.y * offset, up.z * offset);
setViewProjection();
}
diff --git a/src/main/java/org/opengame/engine/camera/FlyingCamera.java b/src/main/java/org/opengame/engine/camera/FlyingCamera.java
index f7eef64..4a820b4 100644
--- a/src/main/java/org/opengame/engine/camera/FlyingCamera.java
+++ b/src/main/java/org/opengame/engine/camera/FlyingCamera.java
@@ -32,6 +32,8 @@ public class FlyingCamera extends Camera {
private float currentForwardSpeed = 0;
private float currentUpSpeed = 0;
+ private float boostMultiplier = 10f;
+
private float startTime;
private float mouseSensitivity = 0.15f;
@@ -66,10 +68,14 @@ private void processMouseMovedEvent(MouseEventData eventData) {
var deltaY = lastMouseY - eventData.getYPos();
if (deltaX != 0) {
- rotateLeftRight((float) deltaX * mouseSensitivity);
+ var angle = (float) deltaX * mouseSensitivity;
+ rotateLeftRight(angle);
+ getRotation().add(angle, 0, 0);
}
if (deltaY != 0) {
+ var angle = (float) deltaY * mouseSensitivity;
rotateUpDown((float) deltaY * mouseSensitivity);
+ getRotation().add(0, angle, 0);
}
setViewProjection();
@@ -79,36 +85,6 @@ private void processMouseMovedEvent(MouseEventData eventData) {
//Engine.setCursorPos(windowCenterPos);
}
- private void rotateUpDown(float delta) {
- var normalizedDirection = direction.normalize();
- var directionNoY = new Vector3f(normalizedDirection.x, 0, normalizedDirection.z).normalize();
-
- var currentAngleDegrees = Math.toDegrees(Math.acos(directionNoY.dot(direction)));
- if (normalizedDirection.y < 0.0f) {
- currentAngleDegrees = -currentAngleDegrees;
- }
-
- var newAngleDegrees = currentAngleDegrees + delta;
-
- if (newAngleDegrees < -85.0f || newAngleDegrees > 85.0f) return;
-
- var rotationAxis = new Vector3f(normalizedDirection).cross(up).normalize();
- var rotationMatrix = new Matrix4f().rotate((float) Math.toRadians(delta), rotationAxis);
-
- var rotatedDirection = new Vector4f(normalizedDirection, 0).mul(rotationMatrix).normalize();
- direction = new Vector3f(rotatedDirection.x, rotatedDirection.y, rotatedDirection.z);
- }
-
- private void rotateLeftRight(float delta) {
- var normalizedDirection = direction.normalize();
- var rotationMatrix = new Matrix4f().rotate((float) Math.toRadians(delta), up);
- var rotatedDirection = new Vector4f(normalizedDirection, 0).mul(rotationMatrix);
- direction = new Vector3f(rotatedDirection.x, rotatedDirection.y, rotatedDirection.z);
-
- var rotatedRight = new Vector4f(right.normalize(), 0).mul(rotationMatrix);
- right = new Vector3f(rotatedRight.x, rotatedRight.y, rotatedRight.z);
- }
-
private void processKeyPressedEvent(KeyEventData eventData) {
var key = eventData.getKeyCode();
@@ -129,6 +105,11 @@ private void processKeyPressedEvent(KeyEventData eventData) {
if (key == GLFW_KEY_SPACE) {
currentUpSpeed += flySpeed * isPressedMultiplier;
}
+ if (key == GLFW_KEY_LEFT_SHIFT) {
+ currentUpSpeed = eventData.isPressed() ? currentUpSpeed * boostMultiplier : currentUpSpeed / boostMultiplier;
+ currentStrafeSpeed = eventData.isPressed() ? currentStrafeSpeed * boostMultiplier : currentStrafeSpeed / boostMultiplier;
+ currentForwardSpeed = eventData.isPressed() ? currentForwardSpeed * boostMultiplier : currentForwardSpeed / boostMultiplier;
+ }
if (key == GLFW_MOUSE_BUTTON_RIGHT) {
isChangingDirection = eventData.isPressed();
@@ -145,7 +126,7 @@ private void processMousePressedEvent(KeyEventData eventData) {
}
@Override
- public void update() {
+ public void update(float time, float tickTime) {
if (currentStrafeSpeed != 0) {
moveRight(currentStrafeSpeed);
}
diff --git a/src/main/java/org/opengame/engine/event/EventBus.java b/src/main/java/org/opengame/engine/event/EventBus.java
index 9d2f622..9433f72 100644
--- a/src/main/java/org/opengame/engine/event/EventBus.java
+++ b/src/main/java/org/opengame/engine/event/EventBus.java
@@ -2,10 +2,7 @@
import lombok.extern.java.Log;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
import java.util.function.Consumer;
/**
@@ -15,13 +12,13 @@
public enum EventBus {
INSTANCE;
- private final Map>> eventListeners;
+ private final Map>> eventListeners;
EventBus() {
eventListeners = new HashMap<>();
}
- public static void subscribeToEvent(EventType eventType, Consumer consumer) {
+ public static void subscribeToEvent(EventType eventType, Consumer