Implémentation de la conservation de l'aspect ratio quand on modifie la taille de la fenêtre

This commit is contained in:
Antoine 2021-06-02 16:46:29 +02:00
parent fbf65f3a6f
commit 80ab5fb99b

View File

@ -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();