Add camera movement.

This commit is contained in:
Sebastian Cabrera 2023-05-31 19:11:43 -04:00
parent 32ed0a2f6c
commit 168257e92c
9 changed files with 161 additions and 48 deletions

BIN
res/textures/grassblock.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

View file

@ -0,0 +1,49 @@
package com.okseby.core;
import lombok.Getter;
import org.joml.Vector3f;
public class Camera {
@Getter private Vector3f position, rotation;
public Camera() {
position = new Vector3f(0, 0, 0);
rotation = new Vector3f(0, 0, 0);
}
public Camera(Vector3f position, Vector3f rotation) {
this.position = position;
this.rotation = rotation;
}
public void movePosition(float x, float y, float z) {
if (z != 0) {
position.x += (float) Math.sin(Math.toRadians(rotation.y)) * -1.0f * z;
position.z += (float) Math.cos(Math.toRadians(rotation.y)) * z;
}
if (x != 0) {
position.x += (float) Math.sin(Math.toRadians(rotation.y - 90)) * -1.0f * x;
position.z += (float) Math.cos(Math.toRadians(rotation.y - 90)) * x;
}
position.y += y;
}
public void setPosition(float x, float y, float z) {
this.position.x = x;
this.position.y = y;
this.position.z = z;
}
public void setRotation(float x, float y, float z) {
this.rotation.x = x;
this.rotation.y = y;
this.rotation.z = z;
}
public void incrementRotation(float x, float y, float z) {
this.rotation.x += x;
this.rotation.y += y;
this.rotation.z += z;
}
}

View file

@ -5,11 +5,11 @@ import org.lwjgl.glfw.GLFW;
import org.lwjgl.glfw.GLFWErrorCallback;
public class EngineManager {
public static final long NANOSECOND = 1000000000;
public static final float FRAMERATE = 1000;
public static final long nanosecond = 1000000000;
public static final float framerate = 1000;
private static int fps;
private static float frametime = 1.0f / FRAMERATE;
private static float frametime = 1.0f / framerate;
private boolean isRunning;
@ -49,7 +49,7 @@ public class EngineManager {
long passedTime = startTime - lastTime;
lastTime = startTime;
unprocessedTime += passedTime / (double) NANOSECOND;
unprocessedTime += passedTime / (double) nanosecond;
frameCounter += passedTime;
input();
@ -61,7 +61,7 @@ public class EngineManager {
if (window.windowShouldClose())
stop();
if (frameCounter >= NANOSECOND) {
if (frameCounter >= nanosecond) {
setFps(frames);
window.setTitle(Constants.title + " - FPS: " + getFps());

View file

@ -13,7 +13,7 @@ public class Launcher {
public static void main(String[] args) {
System.out.println("LWJGL Version: " + Version.getVersion());
window = new WindowManager(Constants.title, 1600, 900, false);
window = new WindowManager(Constants.title, 1600, 900, true);
game = new TestGame();
EngineManager engine = new EngineManager();

View file

@ -27,15 +27,19 @@ public class RenderManager {
shader.createUniform("textureSampler");
shader.createUniform("transformationMatrix");
shader.createUniform("projectionMatrix");
shader.createUniform("viewMatrix");
}
public void render(Entity entity) {
public void render(Entity entity, Camera camera) {
clear();
shader.bind();
shader.setUniform("textureSampler", 0);
shader.setUniform("transformationMatrix", Transformation.createTransformationMatrix(entity));
shader.setUniform("projectionMatrix", window.updateProjectionMatrix());
shader.setUniform("viewMatrix", Transformation.getViewMatrix(camera));
GL30.glBindVertexArray(entity.getModel().getId());
GL20.glEnableVertexAttribArray(0);

View file

@ -11,9 +11,9 @@ 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 Z_NEAR = 0.01f;
public static final float Z_FAR = 1000f;
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;
@ -90,8 +90,9 @@ public class WindowManager {
GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
GL11.glEnable(GL11.GL_DEPTH_TEST);
GL11.glEnable(GL11.GL_STENCIL_TEST);
/* Culling disabled as it stops faces from rendering that should be
GL11.glEnable(GL11.GL_CULL_FACE);
GL11.glCullFace(GL11.GL_BACK);
GL11.glCullFace(GL11.GL_BACK); */
}
public void update() {
@ -121,11 +122,11 @@ public class WindowManager {
public Matrix4f updateProjectionMatrix() {
float aspectRatio = (float) width / height;
return projectionMatrix.setPerspective(FOV, aspectRatio, Z_NEAR, Z_FAR);
return projectionMatrix.setPerspective(fov, aspectRatio, zNear, zFar);
}
public Matrix4f updateProjectionMatrix(Matrix4f matrix) {
float aspectRatio = (float) width / height;
return matrix.setPerspective(FOV, aspectRatio, Z_NEAR, Z_FAR);
return matrix.setPerspective(fov, aspectRatio, zNear, zFar);
}
}

View file

@ -9,19 +9,23 @@ import org.lwjgl.glfw.GLFW;
import org.lwjgl.opengl.GL11;
public class TestGame implements ILogic {
private int direction = 0;
private float color = 0.0f;
private static final float cameraMoveSpeed = 0.05f;
private final RenderManager renderer;
private final ObjectLoader loader;
private final WindowManager window;
private Entity entity;
private Camera camera;
Vector3f cameraInc;
public TestGame() {
renderer = new RenderManager();
window = Launcher.getWindow();
loader = new ObjectLoader();
camera = new Camera();
cameraInc = new Vector3f(0, 0, 0);
}
@Override
@ -29,51 +33,87 @@ public class TestGame implements ILogic {
renderer.init();
float[] vertices = {
-0.5f, 0.5f, 0f,
-0.5f, -0.5f, 0f,
0.5f, -0.5f, 0f,
0.5f, 0.5f, 0f,
};
int[] indices = {
0, 1, 3,
3, 1, 2
-0.5f, 0.5f, 0.5f,
-0.5f, -0.5f, 0.5f,
0.5f, -0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
-0.5f, -0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
-0.5f, 0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
-0.5f, 0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
0.5f, -0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
-0.5f, -0.5f, 0.5f,
-0.5f, -0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
-0.5f, -0.5f, 0.5f,
0.5f, -0.5f, 0.5f,
};
float[] textureCoordinates = {
0, 0,
0, 1,
1, 1,
1, 0
0.0f, 0.0f,
0.0f, 0.5f,
0.5f, 0.5f,
0.5f, 0.0f,
0.0f, 0.0f,
0.5f, 0.0f,
0.0f, 0.5f,
0.5f, 0.5f,
0.0f, 0.5f,
0.5f, 0.5f,
0.0f, 1.0f,
0.5f, 1.0f,
0.0f, 0.0f,
0.0f, 0.5f,
0.5f, 0.0f,
0.5f, 0.5f,
0.5f, 0.0f,
1.0f, 0.0f,
0.5f, 0.5f,
1.0f, 0.5f,
};
int[] indices = {
0, 1, 3, 3, 1, 2,
8, 10, 11, 9, 8, 11,
12, 13, 7, 5, 12, 7,
14, 15, 6, 4, 14, 6,
16, 18, 19, 17, 16, 19,
4, 6, 7, 5, 4, 7,
};
Model model = loader.loadModel(vertices, textureCoordinates, indices);
model.setTexture(new Texture(loader.loadTexture("res/textures/grassblock.png")));
model.setTexture(new Texture(loader.loadTexture("res/textures/grassblock.jpg")));
entity = new Entity(model, new Vector3f(1, 0, 0), new Vector3f(0, 0, 0), 1);
entity = new Entity(model, new Vector3f(0, 0, -5), new Vector3f(0, 0, 0), 1);
}
@Override
public void input() {
if (window.isKeyPressed(GLFW.GLFW_KEY_UP))
direction = 1;
else if (window.isKeyPressed(GLFW.GLFW_KEY_DOWN))
direction = -1;
else
direction = 0;
cameraInc.set(0, 0, 0);
if (window.isKeyPressed(GLFW.GLFW_KEY_W))
cameraInc.z = -1;
if (window.isKeyPressed(GLFW.GLFW_KEY_A))
cameraInc.x = 1;
if (window.isKeyPressed(GLFW.GLFW_KEY_S))
cameraInc.z = 1;
if (window.isKeyPressed(GLFW.GLFW_KEY_D))
cameraInc.x = -1;
if (window.isKeyPressed(GLFW.GLFW_KEY_Z))
cameraInc.y = 1;
if (window.isKeyPressed(GLFW.GLFW_KEY_X))
cameraInc.y = -1;
}
@Override
public void update() {
color += direction * 0.01f;
if (color > 1)
color = 1.0f;
else if (color <= 0)
color = 0.0f;
if (entity.getPosition().x < -1.5f)
entity.getPosition().x = 1.5f;
entity.getPosition().x -= 0.01f;
camera.movePosition(cameraInc.x * cameraMoveSpeed, cameraInc.y * cameraMoveSpeed, cameraInc.z * cameraMoveSpeed);
entity.incrementRotation(0.0f, 0.5f, 0.0f);
}
@Override
@ -83,8 +123,8 @@ public class TestGame implements ILogic {
window.setResizeable(true);
}
window.setClearColor(color, color, color, 0.0f);
renderer.render(entity);
window.setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
renderer.render(entity, camera);
}
@Override

View file

@ -1,7 +1,9 @@
package com.okseby.core.utils;
import com.okseby.core.Camera;
import com.okseby.core.entity.Entity;
import org.joml.Matrix4f;
import org.joml.Vector3f;
public class Transformation {
public static Matrix4f createTransformationMatrix(Entity entity) {
@ -15,4 +17,19 @@ public class Transformation {
return matrix;
}
public static Matrix4f getViewMatrix(Camera camera) {
Vector3f position = camera.getPosition();
Vector3f rotation = camera.getRotation();
Matrix4f matrix = new Matrix4f();
matrix.identity();
matrix.rotate((float) Math.toRadians(rotation.x), new Vector3f(1, 0, 0)).
rotate((float) Math.toRadians(rotation.y), new Vector3f(0, 1, 0)).
rotate((float) Math.toRadians(rotation.z), new Vector3f(0, 0, 1));
matrix.translate(-position.x, -position.y, -position.z);
return matrix;
}
}

View file

@ -6,8 +6,10 @@ in vec2 textureCoordinates;
out vec2 fragmentTextureCoordinates;
uniform mat4 transformationMatrix;
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
void main() {
gl_Position = transformationMatrix * vec4(position, 1.0);
gl_Position = projectionMatrix * viewMatrix * transformationMatrix * vec4(position, 1.0);
fragmentTextureCoordinates = textureCoordinates;
}