Add mouse input to camera.
This commit is contained in:
parent
168257e92c
commit
3da44ff2cd
9 changed files with 97 additions and 26 deletions
|
@ -41,7 +41,7 @@ public class Camera {
|
|||
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.y += y;
|
||||
this.rotation.z += z;
|
||||
|
|
|
@ -10,20 +10,24 @@ public class EngineManager {
|
|||
|
||||
private static int fps;
|
||||
private static float frametime = 1.0f / framerate;
|
||||
public static float currentFrameTime = 0;
|
||||
|
||||
private boolean isRunning;
|
||||
|
||||
private WindowManager window;
|
||||
private WindowManager windowManager;
|
||||
private MouseInput mouseInput;
|
||||
private GLFWErrorCallback errorCallback;
|
||||
private ILogic gameLogic;
|
||||
|
||||
private void init() throws Exception {
|
||||
GLFW.glfwSetErrorCallback(errorCallback = GLFWErrorCallback.createPrint(System.err));
|
||||
window = Launcher.getWindow();
|
||||
windowManager = Launcher.getWindow();
|
||||
gameLogic = Launcher.getGame();
|
||||
mouseInput = new MouseInput();
|
||||
|
||||
window.init();
|
||||
windowManager.init();
|
||||
gameLogic.init();
|
||||
mouseInput.init();
|
||||
}
|
||||
|
||||
public void start() throws Exception {
|
||||
|
@ -58,13 +62,14 @@ public class EngineManager {
|
|||
render = true;
|
||||
unprocessedTime -= frametime;
|
||||
|
||||
if (window.windowShouldClose())
|
||||
if (windowManager.windowShouldClose())
|
||||
stop();
|
||||
|
||||
if (frameCounter >= nanosecond) {
|
||||
setFps(frames);
|
||||
|
||||
window.setTitle(Constants.title + " - FPS: " + getFps());
|
||||
currentFrameTime = 1.0f / fps;
|
||||
windowManager.setTitle(Constants.title + " - FPS: " + getFps());
|
||||
|
||||
frames = 0;
|
||||
frameCounter = 0;
|
||||
|
@ -72,7 +77,7 @@ public class EngineManager {
|
|||
}
|
||||
|
||||
if (render) {
|
||||
update();
|
||||
update(frametime);
|
||||
render();
|
||||
frames++;
|
||||
}
|
||||
|
@ -88,20 +93,21 @@ public class EngineManager {
|
|||
}
|
||||
|
||||
private void input() {
|
||||
mouseInput.input();
|
||||
gameLogic.input();
|
||||
}
|
||||
|
||||
private void render() {
|
||||
gameLogic.render();
|
||||
window.update();
|
||||
windowManager.update();
|
||||
}
|
||||
|
||||
private void update() {
|
||||
gameLogic.update();
|
||||
private void update(float interval) {
|
||||
gameLogic.update(interval, mouseInput);
|
||||
}
|
||||
|
||||
private void cleanup() {
|
||||
window.cleanup();
|
||||
windowManager.cleanup();
|
||||
gameLogic.cleanup();
|
||||
|
||||
errorCallback.free();
|
||||
|
|
|
@ -5,7 +5,7 @@ public interface ILogic {
|
|||
|
||||
void input();
|
||||
|
||||
void update();
|
||||
void update(float interval, MouseInput mouseInput);
|
||||
|
||||
void render();
|
||||
|
||||
|
|
|
@ -6,7 +6,6 @@ import lombok.Getter;
|
|||
import org.lwjgl.Version;
|
||||
|
||||
public class Launcher {
|
||||
|
||||
@Getter private static WindowManager window;
|
||||
@Getter private static TestGame game;
|
||||
|
||||
|
|
56
src/main/java/com/okseby/core/MouseInput.java
Normal file
56
src/main/java/com/okseby/core/MouseInput.java
Normal 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;
|
||||
}
|
||||
}
|
|
@ -1,5 +1,6 @@
|
|||
package com.okseby.core;
|
||||
|
||||
import com.okseby.core.utils.Constants;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.joml.Matrix4f;
|
||||
|
@ -11,10 +12,6 @@ import org.lwjgl.opengl.GL11;
|
|||
import org.lwjgl.system.MemoryUtil;
|
||||
|
||||
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 long window;
|
||||
@Getter private int width, height;
|
||||
|
@ -122,11 +119,11 @@ public class WindowManager {
|
|||
|
||||
public Matrix4f updateProjectionMatrix() {
|
||||
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) {
|
||||
float aspectRatio = (float) width / height;
|
||||
return matrix.setPerspective(fov, aspectRatio, zNear, zFar);
|
||||
return matrix.setPerspective(Constants.fov, aspectRatio, Constants.zNear, Constants.zFar);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,7 +15,7 @@ public class Entity {
|
|||
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.y += y;
|
||||
this.position.z += z;
|
||||
|
@ -27,7 +27,7 @@ public class Entity {
|
|||
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.y += y;
|
||||
this.rotation.z += z;
|
||||
|
|
|
@ -4,13 +4,13 @@ import com.okseby.core.*;
|
|||
import com.okseby.core.entity.Entity;
|
||||
import com.okseby.core.entity.Model;
|
||||
import com.okseby.core.entity.Texture;
|
||||
import com.okseby.core.utils.Constants;
|
||||
import org.joml.Vector2f;
|
||||
import org.joml.Vector3f;
|
||||
import org.lwjgl.glfw.GLFW;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
|
||||
public class TestGame implements ILogic {
|
||||
private static final float cameraMoveSpeed = 0.05f;
|
||||
|
||||
private final RenderManager renderer;
|
||||
private final ObjectLoader loader;
|
||||
private final WindowManager window;
|
||||
|
@ -111,9 +111,15 @@ public class TestGame implements ILogic {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void update() {
|
||||
camera.movePosition(cameraInc.x * cameraMoveSpeed, cameraInc.y * cameraMoveSpeed, cameraInc.z * cameraMoveSpeed);
|
||||
entity.incrementRotation(0.0f, 0.5f, 0.0f);
|
||||
public void update(float interval, MouseInput mouseInput) {
|
||||
camera.movePosition(cameraInc.x * Constants.cameraMoveSpeed, cameraInc.y * Constants.cameraMoveSpeed, cameraInc.z * Constants.cameraMoveSpeed);
|
||||
|
||||
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
|
||||
|
|
|
@ -2,4 +2,11 @@ package com.okseby.core.utils;
|
|||
|
||||
public class Constants {
|
||||
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;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue