From 80ab5fb99b8692cab7999d222a73ca9bc7aaaf13 Mon Sep 17 00:00:00 2001 From: Antoine Date: Wed, 2 Jun 2021 16:46:29 +0200 Subject: [PATCH] =?UTF-8?q?Impl=C3=A9mentation=20de=20la=20conservation=20?= =?UTF-8?q?de=20l'aspect=20ratio=20quand=20on=20modifie=20la=20taille=20de?= =?UTF-8?q?=20la=20fen=C3=AAtre?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/engine/Engine.java | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/engine/Engine.java b/src/engine/Engine.java index 0ce61be..865c3fa 100644 --- a/src/engine/Engine.java +++ b/src/engine/Engine.java @@ -27,6 +27,7 @@ public class Engine { private final int width; private final int height; + private float aspectRatio; /** * Create the engine and initial attributes use .init() to start the engine @@ -36,6 +37,7 @@ public class Engine { this.objectsGl = new ArrayList<>(); this.width = width; this.height = height; + this.aspectRatio = aspectRatio; ObjectGl.projection = Matrix4f.orthographic(-1000, 1000, -1000 * aspectRatio, 1000 * aspectRatio, 0.1f, 100.0f); ObjectGl.view = Matrix4f.translate(new Vector3f(0.0f, 0.0f, 1.0f)); } @@ -83,6 +85,7 @@ public class Engine { glEnable(GL_DEPTH_TEST); // Z-Buffer plus z est grand plus l'objet est proche de la camera limite à 100.0f au dela l'objet disparait + glScissor(0,0,this.width,this.height); // La zone de dessin du background glEnable(GL_BLEND); // Transparence glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); @@ -102,6 +105,7 @@ public class Engine { */ public void render() { + glEnable(GL_SCISSOR_TEST); glClearColor(0.2f, 0.3f, 0.3f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); @@ -155,7 +159,20 @@ public class Engine { private static final GLFWFramebufferSizeCallback resizeWindow = new GLFWFramebufferSizeCallback() { @Override public void invoke(long window, int width, int height) { - glViewport(0, 0, width, height); + int heightViewport, widthViewport, x, y; + if (width >= height){ + heightViewport = height; + widthViewport = (int) (height * 4.0f/3.0f); + y = 0; + x = (width - widthViewport)/2; + } else { + widthViewport = width; + heightViewport = (int) (width * 3.0f/4.0f); + y = (height - heightViewport)/2; + x = 0; + } + glScissor(x, y, widthViewport, heightViewport); + glViewport(x, y, widthViewport, heightViewport); } }; @@ -180,7 +197,7 @@ public class Engine { /* Engine Init */ - Engine engine = new Engine(1280, 720, 9.0f / 16.0f); + Engine engine = new Engine(800, 600, 3.0f / 4.0f); int speed = 10; //vitesse déplacement Object engine.init();