Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>org.opengame</groupId>
<artifactId>engine</artifactId>
<version>0.0.1-SNAPSHOT</version>
<version>0.0.2-SNAPSHOT</version>

<properties>
<maven.compiler.source>11</maven.compiler.source>
Expand Down
14 changes: 10 additions & 4 deletions src/main/java/org/opengame/engine/Engine.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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);
}

Expand All @@ -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();

Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -209,4 +211,8 @@ public static int getScreenWidth() {
public static int getScreenHeight() {
return instance.getConfig().getWindowHeight();
}

public static AppConfig getAppConfig() {
return instance.config;
}
}
12 changes: 7 additions & 5 deletions src/main/java/org/opengame/engine/app/AppConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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;
}
}
92 changes: 79 additions & 13 deletions src/main/java/org/opengame/engine/camera/Camera.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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();
}

Expand Down
45 changes: 13 additions & 32 deletions src/main/java/org/opengame/engine/camera/FlyingCamera.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();

Expand All @@ -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();

Expand All @@ -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();
Expand All @@ -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);
}
Expand Down
11 changes: 4 additions & 7 deletions src/main/java/org/opengame/engine/event/EventBus.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -15,13 +12,13 @@
public enum EventBus {
INSTANCE;

private final Map<EventType, List<Consumer<EventData>>> eventListeners;
private final Map<EventType, List<Consumer<Object>>> eventListeners;

EventBus() {
eventListeners = new HashMap<>();
}

public static void subscribeToEvent(EventType eventType, Consumer<EventData> consumer) {
public static void subscribeToEvent(EventType eventType, Consumer<Object> consumer) {
log.info("Subscribed to " + eventType);

if (!INSTANCE.eventListeners.containsKey(eventType)) {
Expand All @@ -31,7 +28,7 @@ public static void subscribeToEvent(EventType eventType, Consumer<EventData> con
INSTANCE.eventListeners.get(eventType).add(consumer);
}

public static void broadcastEvent(EventType eventType, EventData data) {
public static void broadcastEvent(EventType eventType, Object data) {
//log.warning("Event broadcast " + eventType);

if (!INSTANCE.eventListeners.containsKey(eventType)) return;
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/org/opengame/engine/event/EventData.java

This file was deleted.

11 changes: 8 additions & 3 deletions src/main/java/org/opengame/engine/event/EventType.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package org.opengame.engine.event;

import org.opengame.engine.object.SceneObject;

public enum EventType {
KEY_PRESSED(KeyEventData.class),
KEY_RELEASED(KeyEventData.class),
MOUSE_BUTTON_EVENT(KeyEventData.class),
MOUSE_MOVED(MouseEventData.class);
MOUSE_MOVED(MouseEventData.class),

// Scene
OBJECT_ADDED_TO_SCENE(SceneObject .class);

private final Class<? extends EventData> eventDataClass;
private final Class<?> eventDataClass;

EventType(Class<? extends EventData> eventDataClass) {
EventType(Class<?> eventDataClass) {
this.eventDataClass = eventDataClass;
}
}
Loading