diff --git a/client/src/main/java/core/engine/Engine.java b/client/src/main/java/core/engine/Engine.java index 864bf1d..9896df6 100644 --- a/client/src/main/java/core/engine/Engine.java +++ b/client/src/main/java/core/engine/Engine.java @@ -1,5 +1,6 @@ package core.engine; +import core.engine.entity.Camera; import core.engine.entity.Entity; import core.engine.model.RawModel; import core.engine.model.TexturedModel; @@ -7,6 +8,7 @@ import core.engine.shader.StaticShader; import core.engine.textures.ModelTexture; import org.lwjgl.Version; import org.lwjgl.glfw.GLFWErrorCallback; +import org.lwjgl.glfw.GLFWKeyCallback; import org.lwjgl.glfw.GLFWVidMode; import org.lwjgl.opengl.GL; import org.lwjgl.system.MemoryStack; @@ -25,8 +27,10 @@ import static org.lwjgl.system.MemoryUtil.NULL; public class Engine { private long window; + private GLFWKeyCallback glfwKeyCallback; 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() { @@ -126,17 +130,42 @@ public class Engine { 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); ModelTexture modelTexture = new ModelTexture(loader.loadTexture("test_texture")); 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 // the window or has pressed the ESCAPE key. while ( !glfwWindowShouldClose(window) ) { - entity.increasePosition(0.002f, 0,-0.0002f); - entity.increaseRotation(0,1,0); renderer.prepare(); shader.start(); + shader.loadViewMatrix(camera); renderer.render(entity, shader); shader.stop(); glfwSwapBuffers(window); // swap the color buffers diff --git a/client/src/main/java/core/engine/Renderer.java b/client/src/main/java/core/engine/Renderer.java index 9c0ff49..e6d4346 100644 --- a/client/src/main/java/core/engine/Renderer.java +++ b/client/src/main/java/core/engine/Renderer.java @@ -14,8 +14,20 @@ import utils.vectors.Matrix4f; import static org.lwjgl.opengl.GL11C.*; 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() { + GL11.glEnable(GL11.GL_DEPTH_TEST); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the framebuffer GL11.glClearColor(1,0,0,1); } @@ -36,4 +48,20 @@ public class Renderer { GL20.glDisableVertexAttribArray(1); 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; + + } } diff --git a/client/src/main/java/core/engine/entity/Camera.java b/client/src/main/java/core/engine/entity/Camera.java new file mode 100644 index 0000000..718c99a --- /dev/null +++ b/client/src/main/java/core/engine/entity/Camera.java @@ -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; + } +} diff --git a/client/src/main/java/core/engine/shader/StaticShader.java b/client/src/main/java/core/engine/shader/StaticShader.java index 1df4d11..1c55e71 100644 --- a/client/src/main/java/core/engine/shader/StaticShader.java +++ b/client/src/main/java/core/engine/shader/StaticShader.java @@ -1,5 +1,7 @@ package core.engine.shader; +import core.engine.entity.Camera; +import utils.MatrixGraphicUtils; import utils.vectors.Matrix4f; 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 int location_transformationMatrix; + private int location_projectionMatrix; + private int location_viewMatrix; public StaticShader() { super(VERTEX_FILE, FRAGMENT_FILE); } @@ -15,6 +19,8 @@ public class StaticShader extends ShaderProgram{ @Override protected void getAllUniformLocations() { this.location_transformationMatrix = super.getUniformLocation("transformationMatrix"); + this.location_projectionMatrix = super.getUniformLocation("projectionMatrix"); + this.location_viewMatrix = super.getUniformLocation("viewMatrix"); } @Override @@ -26,4 +32,13 @@ public class StaticShader extends ShaderProgram{ public void loadTransformationMatrix(Matrix4f 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); + } } diff --git a/client/src/main/java/core/engine/shader/vertexShader.glsl b/client/src/main/java/core/engine/shader/vertexShader.glsl index 2b0baca..a9c5484 100644 --- a/client/src/main/java/core/engine/shader/vertexShader.glsl +++ b/client/src/main/java/core/engine/shader/vertexShader.glsl @@ -4,10 +4,12 @@ in vec3 position; in vec2 textureCoords; uniform mat4 transformationMatrix; +uniform mat4 projectionMatrix; +uniform mat4 viewMatrix; out vec2 pass_textureCoords; void main(void) { - gl_Position = transformationMatrix * vec4(position, 1.0); + gl_Position = projectionMatrix * viewMatrix * transformationMatrix * vec4(position, 1.0); pass_textureCoords = textureCoords; } \ No newline at end of file diff --git a/client/src/main/java/utils/MatrixGraphicUtils.java b/client/src/main/java/utils/MatrixGraphicUtils.java index 64ce6ea..ff54713 100644 --- a/client/src/main/java/utils/MatrixGraphicUtils.java +++ b/client/src/main/java/utils/MatrixGraphicUtils.java @@ -1,5 +1,6 @@ package utils; +import core.engine.entity.Camera; import utils.vectors.Matrix4f; import utils.vectors.Vector3f; @@ -17,7 +18,7 @@ public class MatrixGraphicUtils { return matrix; } - /*public static Matrix4f createViewMatrix(Camera camera) { + public static Matrix4f createViewMatrix(Camera camera) { Matrix4f viewMatrix = new Matrix4f(); viewMatrix.setIdentity(); 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); Matrix4f.translate(negativeCameraPos, viewMatrix, viewMatrix); return viewMatrix; - }*/ + } }