Camera zoom + package gui

This commit is contained in:
Antoine 2021-06-04 17:29:17 +02:00
parent ab6939466d
commit db073106cd
5 changed files with 74 additions and 20 deletions

View File

@ -1,5 +1,6 @@
package engine; package engine;
import engine.gui.UIElement;
import engine.input.*; import engine.input.*;
import engine.math.*; import engine.math.*;
import engine.object.*; import engine.object.*;
@ -8,7 +9,6 @@ import engine.sound.*;
import org.lwjgl.glfw.GLFWFramebufferSizeCallback; import org.lwjgl.glfw.GLFWFramebufferSizeCallback;
import org.lwjgl.glfw.GLFWVidMode; import org.lwjgl.glfw.GLFWVidMode;
import org.lwjgl.opengl.GL; import org.lwjgl.opengl.GL;
import org.lwjgl.system.CallbackI;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -22,6 +22,7 @@ public class Engine {
private static long window; private static long window;
private final List<ObjectGl> objectsGl; private final List<ObjectGl> objectsGl;
public final List<UIElement> uiElements;
private boolean running; private boolean running;
@ -30,6 +31,7 @@ public class Engine {
private float viewXPos; private float viewXPos;
public Vector3f transformationView; public Vector3f transformationView;
private final Camera camera;
/** /**
* Create the engine and initial attributes use .init() to start the engine * Create the engine and initial attributes use .init() to start the engine
@ -37,9 +39,10 @@ public class Engine {
public Engine(int width, int height, float aspectRatio) { public Engine(int width, int height, float aspectRatio) {
this.running = false; this.running = false;
this.objectsGl = new ArrayList<>(); this.objectsGl = new ArrayList<>();
this.uiElements = new ArrayList<>();
this.width = width; this.width = width;
this.height = height; this.height = height;
ObjectGl.projection = Matrix4f.orthographic(-1000, 1000, -1000 * aspectRatio, 1000 * aspectRatio, 0.1f, 100.0f); this.camera = new Camera(1000, new Vector3f(4.0f, 3.0f), this);
ObjectGl.view = Matrix4f.translate(new Vector3f(0.0f, 0.0f, 1.0f)); ObjectGl.view = Matrix4f.translate(new Vector3f(0.0f, 0.0f, 1.0f));
this.viewXPos = 0.0f; this.viewXPos = 0.0f;
this.transformationView = new Vector3f(); this.transformationView = new Vector3f();
@ -128,11 +131,15 @@ public class Engine {
* @param obj ObjectGl to render * @param obj ObjectGl to render
*/ */
public void add_objectGl(ObjectGl obj) { public void add_objectGl(ObjectGl obj) {
objectsGl.add(obj); this.objectsGl.add(obj);
} }
public void remove_objectGl(ObjectGl obj) { public void remove_objectGl(ObjectGl obj) {
objectsGl.remove(obj); this.objectsGl.remove(obj);
}
public void add_uiElement(UIElement uiElement) {
this.uiElements.add(uiElement);
} }
public void translateView(Vector3f vec){ public void translateView(Vector3f vec){
@ -173,6 +180,10 @@ public class Engine {
return window; return window;
} }
public Camera getCamera(){
return this.camera;
}
public void setWindow(long window) { public void setWindow(long window) {
Engine.window = window; Engine.window = window;
} }
@ -237,9 +248,9 @@ public class Engine {
texTest.translate(new Vector3f(-1000.0f, (float) (-1000.0f * (3.0 / 4.0f) + 100.0f))); texTest.translate(new Vector3f(-1000.0f, (float) (-1000.0f * (3.0 / 4.0f) + 100.0f)));
UIElement fpsCounter = new UIElement(texTest.getObj(), engine); UIElement fpsCounter = new UIElement(texTest.getObj(), engine);
engine.add_uiElement(fpsCounter); // Pour être atteint par les modification du frustum
texTest.linkToUIElement(fpsCounter); texTest.linkToUIElement(fpsCounter);
long timer = System.currentTimeMillis(); long timer = System.currentTimeMillis();
long lastFrame; long lastFrame;
int frame = 0; int frame = 0;
@ -283,6 +294,8 @@ public class Engine {
fpsCounter.update(); fpsCounter.update();
engine.camera.zoom(1.001f);
/* /*
******************** ********************
* essential part v * * essential part v *
@ -296,7 +309,7 @@ public class Engine {
if (System.currentTimeMillis() - timer > 1000) { if (System.currentTimeMillis() - timer > 1000) {
timer += 1000; timer += 1000;
System.out.println("FPS: " + frame); System.out.println("FPS: " + frame);
texTest.setNewText("FPS " + frame); texTest.setNewText("FPS: " + frame);
frame = 0; frame = 0;
} }

View File

@ -1,11 +1,11 @@
package engine.object; package engine.gui;
import engine.Engine; import engine.Engine;
import engine.math.Vector3f; import engine.math.Vector3f;
import engine.object.ObjectGl;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Objects;
public class UIElement { public class UIElement {

View File

@ -0,0 +1,13 @@
package engine.gui;
import engine.Engine;
import engine.gui.UIElement;
import engine.object.ObjectGl;
import java.util.List;
public class UIElementText extends UIElement {
public UIElementText(List<ObjectGl> objs, Engine engine) {
super(objs, engine);
}
}

View File

@ -0,0 +1,27 @@
package engine.object;
import engine.Engine;
import engine.math.Matrix4f;
import engine.math.Vector3f;
public class Camera {
private float dimension;
private final Vector3f aspectRatio;
private final Engine engine;
public Camera(float dimension, Vector3f aspectRatio, Engine engine){
this.dimension = dimension;
this.aspectRatio = aspectRatio;
this.engine = engine;
float ar = aspectRatio.y / aspectRatio.x;
ObjectGl.projection = Matrix4f.orthographic(-dimension, dimension, -dimension * ar, dimension * ar, 0.1f, 100.0f);
}
public void zoom(float zoomFactor){
this.dimension *= zoomFactor;
float ar = aspectRatio.y / aspectRatio.x;
ObjectGl.projection = Matrix4f.orthographic(-dimension, dimension, -dimension * ar, dimension * ar, 0.1f, 100.0f);
}
}

View File

@ -1,6 +1,7 @@
package engine.object; package engine.object;
import engine.Engine; import engine.Engine;
import engine.gui.UIElement;
import engine.math.Vector3f; import engine.math.Vector3f;
import java.util.ArrayList; import java.util.ArrayList;