Add mouse input to camera.

This commit is contained in:
Sebastian Cabrera 2023-05-31 19:54:03 -04:00
parent 168257e92c
commit 3da44ff2cd
9 changed files with 97 additions and 26 deletions

View file

@ -41,7 +41,7 @@ public class Camera {
this.rotation.z = z; this.rotation.z = z;
} }
public void incrementRotation(float x, float y, float z) { public void moveRotation(float x, float y, float z) {
this.rotation.x += x; this.rotation.x += x;
this.rotation.y += y; this.rotation.y += y;
this.rotation.z += z; this.rotation.z += z;

View file

@ -10,20 +10,24 @@ public class EngineManager {
private static int fps; private static int fps;
private static float frametime = 1.0f / framerate; private static float frametime = 1.0f / framerate;
public static float currentFrameTime = 0;
private boolean isRunning; private boolean isRunning;
private WindowManager window; private WindowManager windowManager;
private MouseInput mouseInput;
private GLFWErrorCallback errorCallback; private GLFWErrorCallback errorCallback;
private ILogic gameLogic; private ILogic gameLogic;
private void init() throws Exception { private void init() throws Exception {
GLFW.glfwSetErrorCallback(errorCallback = GLFWErrorCallback.createPrint(System.err)); GLFW.glfwSetErrorCallback(errorCallback = GLFWErrorCallback.createPrint(System.err));
window = Launcher.getWindow(); windowManager = Launcher.getWindow();
gameLogic = Launcher.getGame(); gameLogic = Launcher.getGame();
mouseInput = new MouseInput();
window.init(); windowManager.init();
gameLogic.init(); gameLogic.init();
mouseInput.init();
} }
public void start() throws Exception { public void start() throws Exception {
@ -58,13 +62,14 @@ public class EngineManager {
render = true; render = true;
unprocessedTime -= frametime; unprocessedTime -= frametime;
if (window.windowShouldClose()) if (windowManager.windowShouldClose())
stop(); stop();
if (frameCounter >= nanosecond) { if (frameCounter >= nanosecond) {
setFps(frames); setFps(frames);
window.setTitle(Constants.title + " - FPS: " + getFps()); currentFrameTime = 1.0f / fps;
windowManager.setTitle(Constants.title + " - FPS: " + getFps());
frames = 0; frames = 0;
frameCounter = 0; frameCounter = 0;
@ -72,7 +77,7 @@ public class EngineManager {
} }
if (render) { if (render) {
update(); update(frametime);
render(); render();
frames++; frames++;
} }
@ -88,20 +93,21 @@ public class EngineManager {
} }
private void input() { private void input() {
mouseInput.input();
gameLogic.input(); gameLogic.input();
} }
private void render() { private void render() {
gameLogic.render(); gameLogic.render();
window.update(); windowManager.update();
} }
private void update() { private void update(float interval) {
gameLogic.update(); gameLogic.update(interval, mouseInput);
} }
private void cleanup() { private void cleanup() {
window.cleanup(); windowManager.cleanup();
gameLogic.cleanup(); gameLogic.cleanup();
errorCallback.free(); errorCallback.free();

View file

@ -5,7 +5,7 @@ public interface ILogic {
void input(); void input();
void update(); void update(float interval, MouseInput mouseInput);
void render(); void render();

View file

@ -6,7 +6,6 @@ import lombok.Getter;
import org.lwjgl.Version; import org.lwjgl.Version;
public class Launcher { public class Launcher {
@Getter private static WindowManager window; @Getter private static WindowManager window;
@Getter private static TestGame game; @Getter private static TestGame game;

View file

@ -0,0 +1,56 @@
package com.okseby.core;
import lombok.Getter;
import org.joml.Vector2d;
import org.joml.Vector2f;
import org.lwjgl.glfw.GLFW;
public class MouseInput {
private final Vector2d previousPosition, currentPosition;
@Getter private final Vector2f displayVector;
@Getter private boolean inWindow = false, leftButtonPressed = false, rightButtonPressed = false;
public MouseInput() {
previousPosition = new Vector2d(-1, -1);
currentPosition = new Vector2d(0, 0);
displayVector = new Vector2f();
}
public void init() {
GLFW.glfwSetCursorPosCallback(Launcher.getWindow().getWindow(), (window, xpos, ypos) -> {
currentPosition.x = xpos;
currentPosition.y = ypos;
});
GLFW.glfwSetCursorEnterCallback(Launcher.getWindow().getWindow(), (window, entered) -> {
inWindow = entered;
});
GLFW.glfwSetMouseButtonCallback(Launcher.getWindow().getWindow(), (window, buton, action, mods) -> {
leftButtonPressed = buton == GLFW.GLFW_MOUSE_BUTTON_LEFT && action == GLFW.GLFW_PRESS;
rightButtonPressed = buton == GLFW.GLFW_MOUSE_BUTTON_RIGHT && action == GLFW.GLFW_PRESS;
});
}
public void input() {
displayVector.x = 0;
displayVector.y = 0;
if (previousPosition.x > 0 && previousPosition.y > 0 && inWindow) {
double x = currentPosition.x - previousPosition.x;
double y = currentPosition.y - previousPosition.y;
boolean rotateX = x != 0;
boolean rotateY = y != 0;
if (rotateX)
displayVector.y = (float) x;
if (rotateY)
displayVector.x = (float) y;
}
previousPosition.x = currentPosition.x;
previousPosition.y = currentPosition.y;
}
}

View file

@ -1,5 +1,6 @@
package com.okseby.core; package com.okseby.core;
import com.okseby.core.utils.Constants;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import org.joml.Matrix4f; import org.joml.Matrix4f;
@ -11,10 +12,6 @@ import org.lwjgl.opengl.GL11;
import org.lwjgl.system.MemoryUtil; import org.lwjgl.system.MemoryUtil;
public class WindowManager { public class WindowManager {
public static final float fov = (float) Math.toRadians(60);
public static final float zNear = 0.01f;
public static final float zFar = 1000f;
@Getter private String title; @Getter private String title;
@Getter private long window; @Getter private long window;
@Getter private int width, height; @Getter private int width, height;
@ -122,11 +119,11 @@ public class WindowManager {
public Matrix4f updateProjectionMatrix() { public Matrix4f updateProjectionMatrix() {
float aspectRatio = (float) width / height; float aspectRatio = (float) width / height;
return projectionMatrix.setPerspective(fov, aspectRatio, zNear, zFar); return projectionMatrix.setPerspective(Constants.fov, aspectRatio, Constants.zNear, Constants.zFar);
} }
public Matrix4f updateProjectionMatrix(Matrix4f matrix) { public Matrix4f updateProjectionMatrix(Matrix4f matrix) {
float aspectRatio = (float) width / height; float aspectRatio = (float) width / height;
return matrix.setPerspective(fov, aspectRatio, zNear, zFar); return matrix.setPerspective(Constants.fov, aspectRatio, Constants.zNear, Constants.zFar);
} }
} }

View file

@ -15,7 +15,7 @@ public class Entity {
this.scale = scale; this.scale = scale;
} }
public void incrementPosition(float x, float y, float z) { public void movePosition(float x, float y, float z) {
this.position.x += x; this.position.x += x;
this.position.y += y; this.position.y += y;
this.position.z += z; this.position.z += z;
@ -27,7 +27,7 @@ public class Entity {
this.position.z = z; this.position.z = z;
} }
public void incrementRotation(float x, float y, float z) { public void moveRotation(float x, float y, float z) {
this.rotation.x += x; this.rotation.x += x;
this.rotation.y += y; this.rotation.y += y;
this.rotation.z += z; this.rotation.z += z;

View file

@ -4,13 +4,13 @@ import com.okseby.core.*;
import com.okseby.core.entity.Entity; import com.okseby.core.entity.Entity;
import com.okseby.core.entity.Model; import com.okseby.core.entity.Model;
import com.okseby.core.entity.Texture; import com.okseby.core.entity.Texture;
import com.okseby.core.utils.Constants;
import org.joml.Vector2f;
import org.joml.Vector3f; import org.joml.Vector3f;
import org.lwjgl.glfw.GLFW; import org.lwjgl.glfw.GLFW;
import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.GL11;
public class TestGame implements ILogic { public class TestGame implements ILogic {
private static final float cameraMoveSpeed = 0.05f;
private final RenderManager renderer; private final RenderManager renderer;
private final ObjectLoader loader; private final ObjectLoader loader;
private final WindowManager window; private final WindowManager window;
@ -111,9 +111,15 @@ public class TestGame implements ILogic {
} }
@Override @Override
public void update() { public void update(float interval, MouseInput mouseInput) {
camera.movePosition(cameraInc.x * cameraMoveSpeed, cameraInc.y * cameraMoveSpeed, cameraInc.z * cameraMoveSpeed); camera.movePosition(cameraInc.x * Constants.cameraMoveSpeed, cameraInc.y * Constants.cameraMoveSpeed, cameraInc.z * Constants.cameraMoveSpeed);
entity.incrementRotation(0.0f, 0.5f, 0.0f);
if (mouseInput.isRightButtonPressed()) {
Vector2f rotationVector = mouseInput.getDisplayVector();
camera.moveRotation(rotationVector.x * Constants.mouseSensitivity, rotationVector.y * Constants.mouseSensitivity, 0);
}
entity.moveRotation(0.0f, 0.5f, 0.0f);
} }
@Override @Override

View file

@ -2,4 +2,11 @@ package com.okseby.core.utils;
public class Constants { public class Constants {
public static final String title = "Warabi Engine"; public static final String title = "Warabi Engine";
public static final float fov = (float) Math.toRadians(60);
public static final float zNear = 0.01f;
public static final float zFar = 1000f;
public static final float mouseSensitivity = 0.2f;
public static final float cameraMoveSpeed = 0.05f;
} }