From e9935403b61ad2fb6981dd9cd83b53bd94efe294 Mon Sep 17 00:00:00 2001 From: Antoine Date: Wed, 19 May 2021 17:59:05 +0200 Subject: [PATCH] Orthographic camera, viewpoint, correct aspect ratio --- shaders/ObjectGl/vert.glsl | 4 +++- shaders/ObjectGlColor/vert.glsl | 4 +++- shaders/ObjectGlTex/vert.glsl | 4 +++- shaders/ObjectGlTexColor/vert.glsl | 4 +++- src/engine/Engine.java | 36 +++++++++++++++++++++--------- src/engine/object/ObjectGl.java | 7 ++++++ src/engine/object/ObjectGlTex.java | 2 ++ 7 files changed, 47 insertions(+), 14 deletions(-) diff --git a/shaders/ObjectGl/vert.glsl b/shaders/ObjectGl/vert.glsl index f6c1771..58be87a 100644 --- a/shaders/ObjectGl/vert.glsl +++ b/shaders/ObjectGl/vert.glsl @@ -2,9 +2,11 @@ layout (location = 0) in vec3 aPos; +uniform mat4 projection; +uniform mat4 view; uniform mat4 transform; void main() { - gl_Position = transform * vec4(aPos, 1.0); + gl_Position = projection * view * transform * vec4(aPos, 1.0); } \ No newline at end of file diff --git a/shaders/ObjectGlColor/vert.glsl b/shaders/ObjectGlColor/vert.glsl index 801c25b..2a867f6 100644 --- a/shaders/ObjectGlColor/vert.glsl +++ b/shaders/ObjectGlColor/vert.glsl @@ -5,10 +5,12 @@ layout (location = 1) in vec3 aColor; out vec3 color; +uniform mat4 projection; +uniform mat4 view; uniform mat4 transform; void main() { - gl_Position = transform * vec4(aPos, 1.0); + gl_Position = projection * view * transform * vec4(aPos, 1.0); color = aColor; } \ No newline at end of file diff --git a/shaders/ObjectGlTex/vert.glsl b/shaders/ObjectGlTex/vert.glsl index 138602e..b90059c 100644 --- a/shaders/ObjectGlTex/vert.glsl +++ b/shaders/ObjectGlTex/vert.glsl @@ -5,10 +5,12 @@ layout (location = 2) in vec2 aTexCoord; out vec2 texCoord; +uniform mat4 projection; +uniform mat4 view; uniform mat4 transform; void main() { - gl_Position = transform * vec4(aPos, 1.0); + gl_Position = projection * view * transform * vec4(aPos, 1.0); texCoord = aTexCoord; } \ No newline at end of file diff --git a/shaders/ObjectGlTexColor/vert.glsl b/shaders/ObjectGlTexColor/vert.glsl index fa6695f..a591694 100644 --- a/shaders/ObjectGlTexColor/vert.glsl +++ b/shaders/ObjectGlTexColor/vert.glsl @@ -7,11 +7,13 @@ layout (location = 2) in vec2 aTexCoord; out vec2 texCoord; out vec4 color; +uniform mat4 projection; +uniform mat4 view; uniform mat4 transform; void main() { - gl_Position = transform * vec4(aPos, 1.0); + gl_Position = projection * view * transform * vec4(aPos, 1.0); color = vec4(aColor, 1.0f); texCoord = aTexCoord; } \ No newline at end of file diff --git a/src/engine/Engine.java b/src/engine/Engine.java index 4a07df7..5b8226b 100644 --- a/src/engine/Engine.java +++ b/src/engine/Engine.java @@ -1,11 +1,7 @@ package engine; -import engine.math.Primitive; -import engine.math.Vector3f; -import engine.object.ObjectGl; -import engine.object.ObjectGlColor; -import engine.object.ObjectGlTex; -import engine.object.ObjectGlTexColor; +import engine.math.*; +import engine.object.*; import org.lwjgl.glfw.GLFWFramebufferSizeCallback; import org.lwjgl.glfw.GLFWVidMode; import org.lwjgl.opengl.GL; @@ -31,6 +27,8 @@ public class Engine { public Engine() { this.running = false; this.objectsGl = new ArrayList<>(); + ObjectGl.projection = Matrix4f.orthographic(-10.0f, 10.0f, -10.0f * 9.0f / 16.0f, 10.0f * 9.0f / 16.0f, 0.1f, 100.0f); + ObjectGl.view = Matrix4f.translate(new Vector3f(0.0f,0.0f,1.0f)); } /** @@ -70,7 +68,7 @@ public class Engine { glfwSetFramebufferSizeCallback(window, resizeWindow); - glEnable(GL_DEPTH_TEST); // Z-Buffer + glEnable(GL_DEPTH_TEST); // Z-Buffer plus z est petit plus l'objet est proche de la camera limite à 0.1f au dela l'objet disparait glEnable(GL_BLEND); // Transparence glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); @@ -141,16 +139,34 @@ public class Engine { // Add objects to render List path = new ArrayList<>(); path.add("textures/awesomeface.png"); - ObjectGlTexColor cube = new ObjectGlTexColor(-0.5f,0.5f,0.0f,1.0f,1.0f, path, Primitive.stdTexWrap, new float[] {1.0f,1.0f,1.0f}); - engine.add_objectGl(cube); + + ObjectGl smiley = new ObjectGlTex(-0.5f,0.5f,0.0f,1.0f,1.0f, path, Primitive.stdTexWrap); + engine.add_objectGl(smiley); + + ObjectGl smiley2 = new ObjectGlTex(-0.5f,0.5f,0.0f,1.0f,1.0f, path, Primitive.stdTexWrap); + engine.add_objectGl(smiley2); + smiley2.translate(new Vector3f(0.5f,0.0f,10.0f)); + + long timer = System.currentTimeMillis(); + int frame = 0; while(engine.isRunning()){ - // Game logic should fit here + smiley.rotateY(1.0f); + smiley2.rotateY(0.8f); //essential part v engine.update(); engine.render(); + + frame++; + + if (System.currentTimeMillis() - timer > 1000) { + timer += 1000; + System.out.println("FPS: " + frame); + frame = 0; + } + if(engine.shouldClose()) engine.setRunning(false); } } diff --git a/src/engine/object/ObjectGl.java b/src/engine/object/ObjectGl.java index 7729df9..9455a09 100644 --- a/src/engine/object/ObjectGl.java +++ b/src/engine/object/ObjectGl.java @@ -15,6 +15,9 @@ public class ObjectGl { protected Shader shader; protected Matrix4f transform; + public static Matrix4f projection; + public static Matrix4f view; + public ObjectGl(){ } @@ -51,7 +54,11 @@ public class ObjectGl { public void render(){ this.shader.enable(); + + this.shader.setUniformMat4f("projection", projection); + this.shader.setUniformMat4f("view", view); this.shader.setUniformMat4f("transform", this.transform); + this.vertexArray.render(); this.shader.disable(); } diff --git a/src/engine/object/ObjectGlTex.java b/src/engine/object/ObjectGlTex.java index 1695410..f431c1d 100644 --- a/src/engine/object/ObjectGlTex.java +++ b/src/engine/object/ObjectGlTex.java @@ -35,6 +35,8 @@ public class ObjectGlTex extends ObjectGl{ public void render() { this.shader.enable(); + this.shader.setUniformMat4f("projection", projection); + this.shader.setUniformMat4f("view", view); this.shader.setUniformMat4f("transform", this.transform); for (Texture t : textures){