UIElement scale with zoom level of the projection

This commit is contained in:
Antoine 2021-06-04 20:14:10 +02:00
parent db073106cd
commit 12b89079ce
8 changed files with 123 additions and 57 deletions

View File

@ -1,5 +1,6 @@
package engine; package engine;
import engine.gui.UIDummy;
import engine.gui.UIElement; import engine.gui.UIElement;
import engine.input.*; import engine.input.*;
import engine.math.*; import engine.math.*;
@ -22,7 +23,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; public final List<UIDummy> uiElements;
private boolean running; private boolean running;
@ -105,6 +106,10 @@ public class Engine {
*/ */
public void update() { public void update() {
glfwPollEvents(); glfwPollEvents();
// METS A JOUR LA POSITION DES ELEMENTS D'INTERFACE
for (UIDummy uiElement : this.uiElements){
uiElement.update();
}
} }
/** /**
@ -139,9 +144,16 @@ public class Engine {
} }
public void add_uiElement(UIElement uiElement) { public void add_uiElement(UIElement uiElement) {
uiElement.init();
this.uiElements.add(uiElement); this.uiElements.add(uiElement);
} }
public void setUIElementZoomFactor(float scaleFactor){
for (UIDummy uiElement : this.uiElements){
uiElement.updateScalingFactor(scaleFactor);
}
}
public void translateView(Vector3f vec){ public void translateView(Vector3f vec){
ObjectGl.view = ObjectGl.view.multiply(Matrix4f.translate(vec)); ObjectGl.view = ObjectGl.view.multiply(Matrix4f.translate(vec));
viewXPos += vec.x; viewXPos += vec.x;
@ -230,7 +242,7 @@ public class Engine {
String pathToBG = "textures/background_beach.png"; String pathToBG = "textures/background_beach.png";
String pathToText = "textures/dejavu10x10_gs_tc.png"; String pathToText = "textures/dejavu10x10_gs_tc.png";
ObjectGl zangief = new ObjectGl(10.0f, 1f, 1f, 10f, path, null); ObjectGl zangief = new Sprite(10.0f, 10f, path, null);
zangief.setTextureWrap(58, 0, 62, 84, ObjectGl.DEFAULT); zangief.setTextureWrap(58, 0, 62, 84, ObjectGl.DEFAULT);
engine.add_objectGl(zangief); engine.add_objectGl(zangief);
zangief.translate(new Vector3f(-1000.0f, 200.0f, 0.0f)); zangief.translate(new Vector3f(-1000.0f, 200.0f, 0.0f));
@ -244,13 +256,20 @@ public class Engine {
background.translate(new Vector3f(-3011.0f, 1400.0f, 1.0f)); background.translate(new Vector3f(-3011.0f, 1400.0f, 1.0f));
engine.add_objectGl(background); engine.add_objectGl(background);
ObjectGl smiley = new Sprite(15.0f, 500.0f, path2, null);
UIElement uiElement = new UIElement(smiley, 0.0f, 1.0f, engine);
engine.add_uiElement(uiElement);
Text texTest = new Text("ABCDEFGHIJKLMNOPQRSTUVWYZ",20.0f, 10, engine); Text texTest = new Text("ABCDEFGHIJKLMNOPQRSTUVWYZ",20.0f, 10, engine);
texTest.show();
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 // 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,18 +302,17 @@ public class Engine {
// Check si le personnage a sauté // Check si le personnage a sauté
if (jump.isButtonPressed()){ if (jump.isButtonPressed()){
// Le personnage saute // Le personnage saute
System.out.println(" JE SAUTE "); engine.camera.zoom(1.001f);
} }
} }
KeyboardInput.keyboardInput(zangief, speed); KeyboardInput.keyboardInput(zangief, speed);
engine.transformationView = new Vector3f((- zangief.getXPos() - engine.viewXPos) - 250.0f,0.0f,0.0f); Vector3f zangiefTracking = new Vector3f((- zangief.getXPos() - engine.viewXPos) - 250.0f,0.0f,0.0f);
engine.translateView(engine.transformationView); // LE MOUVEMENT DE LA CAMERA POUR CETTE FRAME
engine.translateView(zangiefTracking);
fpsCounter.update(); // LA SOMME TOTAL DES MOUVEMENTS DE CAMERA DEPUIS L'ORIGINE
engine.transformationView = engine.transformationView.addXYZ(zangiefTracking);
engine.camera.zoom(1.001f);
/* /*
******************** ********************
@ -309,7 +327,6 @@ 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);
frame = 0; frame = 0;
} }

View File

@ -0,0 +1,16 @@
package engine.gui;
public class UIDummy {
public void init(){
}
private void getObjInPosition(){
}
public void updateScalingFactor(float scaleFactor){
}
public void update(){
}
}

View File

@ -1,47 +1,64 @@
package engine.gui; package engine.gui;
import engine.Engine; import engine.Engine;
import engine.math.Vector3f; import engine.math.Vector3f;
import engine.object.Camera;
import engine.object.ObjectGl; import engine.object.ObjectGl;
import java.util.ArrayList; public class UIElement extends UIDummy{
import java.util.List;
public class UIElement { private final ObjectGl obj;
private final Engine engine;
private final Camera camera;
private float scalingFactor;
private float xPos;
private float yPos;
private float zPos;
private List<ObjectGl> objs; /**
private Engine engine; * Crée un elements d'interface càd un sprite qui suis les mouvements de la camera, pas besoin d'ajouter l'ObjectGl
private Vector3f transformation; * dans la liste de rendu cette classe s'en occupe lors de l'initialisation
* @param obj l'objet à mettre dans le conteneur
public UIElement(List<ObjectGl> objs, Engine engine){ * @param posX la position relative à la camera souhaité sur l'axe X, 0.0 à l'extreme gauche, 1.0 à l'extreme droite
this.objs = new ArrayList<>(); * @param posY la position relative à la camera souhaité sur l'axe Y, 0.0 à l'extreme bas, 1.0 à l'extreme haut
this.objs.addAll(objs); * @param engine le moteur de rendu depuis lequel UIElement va determiner la camera à tracker
*/
public UIElement(ObjectGl obj, float posX, float posY, Engine engine){
this.obj = obj;
this.engine = engine; this.engine = engine;
this.transformation = new Vector3f(); this.camera = engine.getCamera();
this.scalingFactor = obj.getScalingFactor();
this.xPos = posX;
this.yPos = posY;
this.zPos = obj.getZPos();
this.getObjInPosition();
} }
public void addObj(ObjectGl obj){ public void init(){
obj.translate(this.transformation); this.engine.add_objectGl(obj);
this.objs.add(obj);
} }
public void addObj(List<ObjectGl> objs){ private void getObjInPosition(){
for (ObjectGl obj : objs){ obj.resetTransform();
obj.translate(this.transformation); obj.scale(new Vector3f(this.scalingFactor, this.scalingFactor, 1.0f));
this.objs.add(obj); // Position in the camera space
} float dimension = this.camera.getDimension();
float ar = this.camera.getAspectRatio().y / this.camera.getAspectRatio().x;
float x = dimension * 2 * this.xPos - dimension;
float y = dimension * ar * 2 * this.yPos - dimension * ar;
obj.translate(new Vector3f(x, y, this.zPos));
System.out.println(x);
// Camera position
obj.translate(new Vector3f(- engine.transformationView.x, engine.transformationView.y));
} }
public void removeObj(ObjectGl obj){ public void updateScalingFactor(float scaleFactor){
this.objs.remove(obj); this.scalingFactor *= scaleFactor;
} }
public void update(){ public void update(){
Vector3f translationViewPoint = new Vector3f(-engine.transformationView.x, engine.transformationView.y); this.getObjInPosition();
this.transformation = this.transformation.addXYZ(translationViewPoint);
for (ObjectGl obj : this.objs){
obj.translate(translationViewPoint); // Tous les elmts font le même déplacement que la caméra
}
} }
} }

View File

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

View File

@ -20,8 +20,17 @@ public class Camera {
} }
public void zoom(float zoomFactor){ public void zoom(float zoomFactor){
engine.setUIElementZoomFactor(zoomFactor);
this.dimension *= zoomFactor; this.dimension *= zoomFactor;
float ar = aspectRatio.y / aspectRatio.x; float ar = aspectRatio.y / aspectRatio.x;
ObjectGl.projection = Matrix4f.orthographic(-dimension, dimension, -dimension * ar, dimension * ar, 0.1f, 100.0f); ObjectGl.projection = Matrix4f.orthographic(-dimension, dimension, -dimension * ar, dimension * ar, 0.1f, 100.0f);
} }
public float getDimension(){
return this.dimension;
}
public Vector3f getAspectRatio(){
return this.aspectRatio;
}
} }

View File

@ -98,7 +98,7 @@ public class ObjectGl {
} }
/** /**
* Reset the transform matrix, the model will appear at the 0.0.0 coordinate, his scaleFactor will be set to zero * Reset the transform matrix, the model will appear at the 0.0.0 coordinate, his scaleFactor will be set to one
* Because the model is at position 0 on the z axis he will not show up on screen * Because the model is at position 0 on the z axis he will not show up on screen
*/ */
public void resetTransform(){ public void resetTransform(){
@ -236,7 +236,7 @@ public class ObjectGl {
this.vertexArray = new VertexArray(Primitive.createRectangle(this.zPos, this.width, this.height), Primitive.rectangle_indices, colorBuffer, Primitive.stdTexWrap); this.vertexArray = new VertexArray(Primitive.createRectangle(this.zPos, this.width, this.height), Primitive.rectangle_indices, colorBuffer, Primitive.stdTexWrap);
} }
private void setTextureWrap(float[] texture){ public void setTextureWrap(float[] texture){
this.textureWrap = texture; this.textureWrap = texture;
this.vertexArray.swapTextureBufferObject(texture); this.vertexArray.swapTextureBufferObject(texture);
} }
@ -253,6 +253,10 @@ public class ObjectGl {
return zPos; return zPos;
} }
public float getScalingFactor(){
return scalingFactor;
}
/** /**
* Do shader binding, texture binding and vertexArray drawing * Do shader binding, texture binding and vertexArray drawing
*/ */

View File

@ -0,0 +1,11 @@
package engine.object;
import engine.math.Vector3f;
public class Sprite extends ObjectGl {
public Sprite(float z, float size, String tex, Vector3f color) {
super(z, 1f, 1f, size, tex, color);
}
}

View File

@ -7,13 +7,15 @@ import engine.math.Vector3f;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
/**
* Un texte construit selon la même logique qu'une sprite pour faire un element d'interface utilisez UIElementText
*/
public class Text { public class Text {
private List<ObjectGl> charList; private List<ObjectGl> charList;
private final float size; private final float size;
private final Engine engine; private final Engine engine;
private final float zPos; private final float zPos;
private UIElement linkedUIElement;
private Vector3f transformation; private Vector3f transformation;
@ -24,6 +26,9 @@ public class Text {
this.engine = engine; this.engine = engine;
this.transformation = new Vector3f(); this.transformation = new Vector3f();
this.textToArrayObjectGl(text); this.textToArrayObjectGl(text);
}
public void show(){
this.addCharListInEngine(); this.addCharListInEngine();
} }
@ -44,14 +49,10 @@ public class Text {
private void addCharInEngine(int i, ObjectGl obj){ private void addCharInEngine(int i, ObjectGl obj){
obj.translate(new Vector3f(i * 10.0f * this.size, 0.0f, 0.0f)); obj.translate(new Vector3f(i * 10.0f * this.size, 0.0f, 0.0f));
obj.translate(transformation); obj.translate(transformation);
if (linkedUIElement != null){
linkedUIElement.addObj(obj);
}
this.engine.add_objectGl(obj); this.engine.add_objectGl(obj);
} }
public void linkToUIElement(UIElement ui){ public void linkToUIElement(UIElement ui){
this.linkedUIElement = ui;
} }
public void setNewText(String text){ public void setNewText(String text){
@ -81,7 +82,6 @@ public class Text {
public void remove(){ public void remove(){
for (ObjectGl obj : this.charList){ for (ObjectGl obj : this.charList){
this.engine.remove_objectGl(obj); this.engine.remove_objectGl(obj);
if (linkedUIElement != null) linkedUIElement.removeObj(obj);
} }
} }
@ -94,7 +94,6 @@ public class Text {
for (ObjectGl objectGl : this.charList){ for (ObjectGl objectGl : this.charList){
if (j >= i) { if (j >= i) {
this.engine.remove_objectGl(objectGl); this.engine.remove_objectGl(objectGl);
if (linkedUIElement != null) linkedUIElement.removeObj(objectGl);
} }
j++; j++;
} }