Include Cameramovement

This commit is contained in:
Sebastian 2023-08-11 17:32:26 +02:00
parent 0383c43675
commit c7c1bbe0af
6 changed files with 125 additions and 7 deletions

View File

@ -1,5 +1,6 @@
package core.engine; package core.engine;
import core.engine.entity.Camera;
import core.engine.entity.Entity; import core.engine.entity.Entity;
import core.engine.model.RawModel; import core.engine.model.RawModel;
import core.engine.model.TexturedModel; import core.engine.model.TexturedModel;
@ -7,6 +8,7 @@ import core.engine.shader.StaticShader;
import core.engine.textures.ModelTexture; import core.engine.textures.ModelTexture;
import org.lwjgl.Version; import org.lwjgl.Version;
import org.lwjgl.glfw.GLFWErrorCallback; import org.lwjgl.glfw.GLFWErrorCallback;
import org.lwjgl.glfw.GLFWKeyCallback;
import org.lwjgl.glfw.GLFWVidMode; import org.lwjgl.glfw.GLFWVidMode;
import org.lwjgl.opengl.GL; import org.lwjgl.opengl.GL;
import org.lwjgl.system.MemoryStack; import org.lwjgl.system.MemoryStack;
@ -25,8 +27,10 @@ import static org.lwjgl.system.MemoryUtil.NULL;
public class Engine { public class Engine {
private long window; private long window;
private GLFWKeyCallback glfwKeyCallback;
private final Loader loader = new Loader(); private final Loader loader = new Loader();
private final Renderer renderer = new Renderer(); public static int WINDOW_WIDTH = 1280;
public static int WINDOW_HEIGHT = 720;
public Engine() { public Engine() {
@ -126,17 +130,42 @@ public class Engine {
1,0 // V3 1,0 // V3
}; };
Camera camera = new Camera();
glfwSetKeyCallback(window, glfwKeyCallback = new GLFWKeyCallback() {
@Override
public void invoke(long window, int key, int scancode, int action, int mods) {
if(key == GLFW_KEY_A) {
//move camera to left
camera.moveLeft();
}
if(key == GLFW_KEY_D) {
//move camera to right
camera.moveRight();
}
if(key == GLFW_KEY_W) {
camera.moveForward();
}
if(key == GLFW_KEY_S) {
camera.moveBackward();
}
}
});
Renderer renderer = new Renderer(shader);
RawModel model = loader.loadToVAO(vertices,textureCoords, indices); RawModel model = loader.loadToVAO(vertices,textureCoords, indices);
ModelTexture modelTexture = new ModelTexture(loader.loadTexture("test_texture")); ModelTexture modelTexture = new ModelTexture(loader.loadTexture("test_texture"));
TexturedModel texturedModel = new TexturedModel(model, modelTexture); TexturedModel texturedModel = new TexturedModel(model, modelTexture);
Entity entity = new Entity(texturedModel, new Vector3f(-1,0,0), 0,0,0,1); Entity entity = new Entity(texturedModel, new Vector3f(-1,0,-1), 0,0,0,1);
// Run the rendering loop until the user has attempted to close // Run the rendering loop until the user has attempted to close
// the window or has pressed the ESCAPE key. // the window or has pressed the ESCAPE key.
while ( !glfwWindowShouldClose(window) ) { while ( !glfwWindowShouldClose(window) ) {
entity.increasePosition(0.002f, 0,-0.0002f);
entity.increaseRotation(0,1,0);
renderer.prepare(); renderer.prepare();
shader.start(); shader.start();
shader.loadViewMatrix(camera);
renderer.render(entity, shader); renderer.render(entity, shader);
shader.stop(); shader.stop();
glfwSwapBuffers(window); // swap the color buffers glfwSwapBuffers(window); // swap the color buffers

View File

@ -14,8 +14,20 @@ import utils.vectors.Matrix4f;
import static org.lwjgl.opengl.GL11C.*; import static org.lwjgl.opengl.GL11C.*;
public class Renderer { public class Renderer {
private static final float FOV = 70;
private static final float NEAR_PLEANE = 0.1f;
private static final float FAR_PLANE = 1000;
private Matrix4f projectionMatrix;
public Renderer(StaticShader shader) {
createProjectionMatrix();
shader.start();
shader.loadProjectionMatrix(projectionMatrix);
shader.stop();
}
public void prepare() { public void prepare() {
GL11.glEnable(GL11.GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the framebuffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the framebuffer
GL11.glClearColor(1,0,0,1); GL11.glClearColor(1,0,0,1);
} }
@ -36,4 +48,20 @@ public class Renderer {
GL20.glDisableVertexAttribArray(1); GL20.glDisableVertexAttribArray(1);
GL30.glBindVertexArray(0); GL30.glBindVertexArray(0);
} }
private void createProjectionMatrix() {
float aspectRatio = (float) Engine.WINDOW_WIDTH / (float) Engine.WINDOW_HEIGHT;
float y_scale = (float) ((1f / Math.tan(Math.toRadians(FOV / 2f))) * aspectRatio);
float x_scale = y_scale / aspectRatio;
float frustum_length = FAR_PLANE - NEAR_PLEANE;
projectionMatrix = new Matrix4f();
projectionMatrix.m00 = x_scale;
projectionMatrix.m11 = y_scale;
projectionMatrix.m22 = -((FAR_PLANE + NEAR_PLEANE) / frustum_length);
projectionMatrix.m23 = -1;
projectionMatrix.m32 = -((2 * NEAR_PLEANE * FAR_PLANE) / frustum_length);
projectionMatrix.m33 = 0;
}
} }

View File

@ -0,0 +1,43 @@
package core.engine.entity;
import utils.vectors.Vector3f;
public class Camera {
private Vector3f position = new Vector3f(0,0,0);
private float pitch;
private float yaw;
private float roll;
public void moveLeft() {
position.x += 0.02f;
}
public void moveForward() {
position.z -= 0.02f;
}
public void moveBackward() {
position.z += 0.02f;
}
public void moveRight() {
position.x -= 0.02f;
}
public Vector3f getPosition() {
return position;
}
public float getPitch() {
return pitch;
}
public float getYaw() {
return yaw;
}
public float getRoll() {
return roll;
}
}

View File

@ -1,5 +1,7 @@
package core.engine.shader; package core.engine.shader;
import core.engine.entity.Camera;
import utils.MatrixGraphicUtils;
import utils.vectors.Matrix4f; import utils.vectors.Matrix4f;
public class StaticShader extends ShaderProgram{ public class StaticShader extends ShaderProgram{
@ -8,6 +10,8 @@ public class StaticShader extends ShaderProgram{
private static final String FRAGMENT_FILE = "src/main/java/core/engine/shader/fragmentShader.glsl"; private static final String FRAGMENT_FILE = "src/main/java/core/engine/shader/fragmentShader.glsl";
private int location_transformationMatrix; private int location_transformationMatrix;
private int location_projectionMatrix;
private int location_viewMatrix;
public StaticShader() { public StaticShader() {
super(VERTEX_FILE, FRAGMENT_FILE); super(VERTEX_FILE, FRAGMENT_FILE);
} }
@ -15,6 +19,8 @@ public class StaticShader extends ShaderProgram{
@Override @Override
protected void getAllUniformLocations() { protected void getAllUniformLocations() {
this.location_transformationMatrix = super.getUniformLocation("transformationMatrix"); this.location_transformationMatrix = super.getUniformLocation("transformationMatrix");
this.location_projectionMatrix = super.getUniformLocation("projectionMatrix");
this.location_viewMatrix = super.getUniformLocation("viewMatrix");
} }
@Override @Override
@ -26,4 +32,13 @@ public class StaticShader extends ShaderProgram{
public void loadTransformationMatrix(Matrix4f matrix) { public void loadTransformationMatrix(Matrix4f matrix) {
super.loadMatrix(location_transformationMatrix, matrix); super.loadMatrix(location_transformationMatrix, matrix);
} }
public void loadProjectionMatrix(Matrix4f matrix) {
super.loadMatrix(location_projectionMatrix, matrix);
}
public void loadViewMatrix(Camera camera) {
Matrix4f viewMatrix = MatrixGraphicUtils.createViewMatrix(camera);
super.loadMatrix(location_viewMatrix, viewMatrix);
}
} }

View File

@ -4,10 +4,12 @@ in vec3 position;
in vec2 textureCoords; in vec2 textureCoords;
uniform mat4 transformationMatrix; uniform mat4 transformationMatrix;
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
out vec2 pass_textureCoords; out vec2 pass_textureCoords;
void main(void) { void main(void) {
gl_Position = transformationMatrix * vec4(position, 1.0); gl_Position = projectionMatrix * viewMatrix * transformationMatrix * vec4(position, 1.0);
pass_textureCoords = textureCoords; pass_textureCoords = textureCoords;
} }

View File

@ -1,5 +1,6 @@
package utils; package utils;
import core.engine.entity.Camera;
import utils.vectors.Matrix4f; import utils.vectors.Matrix4f;
import utils.vectors.Vector3f; import utils.vectors.Vector3f;
@ -17,7 +18,7 @@ public class MatrixGraphicUtils {
return matrix; return matrix;
} }
/*public static Matrix4f createViewMatrix(Camera camera) { public static Matrix4f createViewMatrix(Camera camera) {
Matrix4f viewMatrix = new Matrix4f(); Matrix4f viewMatrix = new Matrix4f();
viewMatrix.setIdentity(); viewMatrix.setIdentity();
Matrix4f.rotate((float) Math.toRadians(camera.getPitch()), new Vector3f(1, 0, 0), viewMatrix, Matrix4f.rotate((float) Math.toRadians(camera.getPitch()), new Vector3f(1, 0, 0), viewMatrix,
@ -27,5 +28,5 @@ public class MatrixGraphicUtils {
Vector3f negativeCameraPos = new Vector3f(-cameraPos.x,-cameraPos.y,-cameraPos.z); Vector3f negativeCameraPos = new Vector3f(-cameraPos.x,-cameraPos.y,-cameraPos.z);
Matrix4f.translate(negativeCameraPos, viewMatrix, viewMatrix); Matrix4f.translate(negativeCameraPos, viewMatrix, viewMatrix);
return viewMatrix; return viewMatrix;
}*/ }
} }