From d7c426227de4f6ff03e2b3e8e7d76d093dc34533 Mon Sep 17 00:00:00 2001 From: Antoine Date: Sun, 6 Jun 2021 16:32:58 +0200 Subject: [PATCH] Doc pour Engine + Introduction Tracking Tore --- src/engine/Engine.java | 198 +++++++++++++++------- src/engine/TestEngine.java | 43 +++-- src/engine/{object => camera}/Camera.java | 3 +- src/engine/camera/TrackingDummy.java | 9 + src/engine/camera/TrackingTore.java | 52 ++++++ src/engine/gui/UIDummy.java | 2 +- src/engine/gui/UIElement.java | 2 +- src/engine/gui/UIElementText.java | 2 +- src/engine/object/ObjectGl.java | 8 + 9 files changed, 234 insertions(+), 85 deletions(-) rename src/engine/{object => camera}/Camera.java (95%) create mode 100644 src/engine/camera/TrackingDummy.java create mode 100644 src/engine/camera/TrackingTore.java diff --git a/src/engine/Engine.java b/src/engine/Engine.java index 591ca16..731a4c1 100644 --- a/src/engine/Engine.java +++ b/src/engine/Engine.java @@ -1,11 +1,10 @@ package engine; +import engine.camera.*; import engine.gui.UIDummy; -import engine.gui.UIElementText; import engine.input.*; import engine.math.*; import engine.object.*; -import engine.sound.*; import org.lwjgl.glfw.GLFWFramebufferSizeCallback; import org.lwjgl.glfw.GLFWVidMode; @@ -30,14 +29,21 @@ public class Engine { private final int width; private final int height; - public float viewXPos; - public float viewYPos; - public float viewZPos; - public Vector3f transformationView; - public final Camera camera; + private float viewXPos; + private float viewYPos; + private Vector3f transformationView; + private final Camera camera; + + private TrackingDummy tracking; + + /* + Init Method + */ /** * Create the engine and initial attributes use .init() to start the engine + * Initial projection is -1000;1000 in width and -1000*aspectRatio; 1000*aspectRatio + * Initial Camera position is (0, 0, -1) //TODO vérifiez */ public Engine(int width, int height, Vector3f aspectRatio) { this.running = false; @@ -45,10 +51,12 @@ public class Engine { this.uiElements = new ArrayList<>(); this.width = width; this.height = height; + this.viewXPos = 0.0f; + this.viewYPos = 0.0f; this.camera = new Camera(1000, aspectRatio, this); ObjectGl.view = Matrix4f.translate(new Vector3f(0.0f, 0.0f, 1.0f)); - this.viewXPos = 0.0f; this.transformationView = new Vector3f(); + this.tracking = null; } /** @@ -103,20 +111,10 @@ public class Engine { System.out.println("OpenGL: " + glGetString(GL_VERSION)); } - /** - * + /* + RENDER / UPDATE */ - public void update() { - glfwPollEvents(); - // METS A JOUR LA POSITION DES ELEMENTS D'INTERFACE - for (UIDummy uiElement : this.uiElements){ - uiElement.update(); - } - } - /** - * - */ public void render() { glEnable(GL_SCISSOR_TEST); @@ -134,42 +132,19 @@ public class Engine { } /** - * Add obj to the render queue - * @param obj ObjectGl to render + * */ - public void add_objectGl(ObjectGl obj) { - this.objectsGl.add(obj); - } - - public void add_objectsGl(List objs) { - this.objectsGl.addAll(objs); - } - - public void remove_objectGl(ObjectGl obj) { - this.objectsGl.remove(obj); - } - - public void add_uiElement(UIDummy uiElement) { - uiElement.init(); - this.uiElements.add(uiElement); - } - - public void setUIElementZoomFactor(float scaleFactor){ + public void update() { + glfwPollEvents(); + // METS A JOUR LA POSITION DES ELEMENTS D'INTERFACE for (UIDummy uiElement : this.uiElements){ - uiElement.updateScalingFactor(scaleFactor); + uiElement.update(); } } - public void cameraTrackingObjectGl(ObjectGl obj, float xOffset){ - Vector3f zangiefTracking = new Vector3f((- obj.getXPos() - this.viewXPos) + xOffset,0.0f ,0.0f); - this.translateView(zangiefTracking); - } - - public void translateView(Vector3f vec){ - ObjectGl.view = ObjectGl.view.multiply(Matrix4f.translate(vec)); - viewXPos += vec.x; - this.transformationView = this.transformationView.addXYZ(vec); - } + /* + Window / Viewport Management + */ public static void correctViewport(int width, int height){ int heightViewport, widthViewport, x, y; @@ -188,30 +163,87 @@ public class Engine { glViewport(x, y, widthViewport, heightViewport); } - public boolean isRunning() { - return running; - } - - public void setRunning(boolean b) { - running = b; - } - public boolean shouldClose() { return glfwWindowShouldClose(getWindow()); } - public static long getWindow() { - return window; + /* + ObjectGl Management + */ + + /** + * Add obj to the render queue + * @param obj ObjectGl to render + */ + public void add_objectGl(ObjectGl obj) { + this.objectsGl.add(obj); } - public Camera getCamera(){ - return this.camera; + public void add_objectsGl(List objs) { + this.objectsGl.addAll(objs); } - public void setWindow(long window) { - Engine.window = window; + public void remove_objectGl(ObjectGl obj) { + this.objectsGl.remove(obj); } + /* + UIElement Management + */ + + public void add_uiElement(UIDummy uiElement) { + uiElement.init(); + this.uiElements.add(uiElement); + } + + public void setUIElementZoomFactor(float scaleFactor){ + for (UIDummy uiElement : this.uiElements){ + uiElement.updateScalingFactor(scaleFactor); + } + } + + /* + CAMERA + */ + + public void translateView(Vector3f vec){ + ObjectGl.view = ObjectGl.view.multiply(Matrix4f.translate(vec)); + viewXPos += vec.x; + this.transformationView = this.transformationView.addXYZ(vec); + } + + /** + * Translate la camera pour avoir obj au centre de l'image, comme le cadrage se fait sur le point en haut à gauche + * de l'objet un offset peut parfois être nécessaire + * @param obj l'objectGl que la camera va suivre + * @param xOffset un offSet sur l'axe X + */ + public void cameraTrackingObjectGl(ObjectGl obj, float xOffset){ + Vector3f trackingVector = new Vector3f((- obj.getXPos() - this.viewXPos) + xOffset,0.0f ,0.0f); + this.translateView(trackingVector); + } + + public void setCameraTrackingBetweenTwoObjectGl(ObjectGl obj1, ObjectGl obj2, float deadZone){ + // obj2 est considéré à droite probablement à modifier + this.tracking = new TrackingTore( + deadZone, + obj1, + obj2, + this); + } + + public void cameraTrackingBetweenTwoObjectGl(){ + if (this.tracking == null) System.out.println("Utilisez setCameraTrackingBetweenTwoObjectGl avant cameraTrackingBetweenTwoObjectGl"); + else { + Vector3f trackingVector = this.tracking.getViewVector(); + this.translateView(trackingVector); + } + } + + /* + CALLBACK + */ + /** * Est appelé à chaque modification de la taille de la fenêtre, et modifie la taille de la zone de rendu * pour quelle corresponde à la taille de la fenêtre @@ -223,4 +255,40 @@ public class Engine { } }; + /* + GET/SET + */ + + public boolean getRunning() { + return running; + } + + public void setRunning(boolean b) { + running = b; + } + + public float getViewXPos(){ + return viewXPos; + } + + public float getViewYPos(){ + return viewYPos; + } + + public Vector3f getTransformationView() { + return transformationView; + } + + public Camera getCamera(){ + return this.camera; + } + + public static long getWindow() { + return window; + } + + public void setWindow(long window) { + Engine.window = window; + } + } diff --git a/src/engine/TestEngine.java b/src/engine/TestEngine.java index 49a7a8a..61cbb8c 100644 --- a/src/engine/TestEngine.java +++ b/src/engine/TestEngine.java @@ -1,23 +1,16 @@ package engine; import engine.gui.UIElementText; -import engine.input.Button; -import engine.input.GamepadInput; -import engine.input.InputConst; -import engine.input.KeyboardInput; +import engine.input.*; import engine.math.Vector3f; import engine.object.ObjectGl; import engine.object.Sprite; -import engine.sound.SoundBuffer; -import engine.sound.SoundListener; -import engine.sound.SoundManager; -import engine.sound.SoundSource; +import engine.sound.*; import java.util.ArrayList; import java.util.List; -import static org.lwjgl.glfw.GLFW.GLFW_JOYSTICK_1; -import static org.lwjgl.glfw.GLFW.glfwJoystickPresent; +import static org.lwjgl.glfw.GLFW.*; public class TestEngine { @@ -60,6 +53,14 @@ public class TestEngine { zangief.setShader("shaders/StylishShaders/BasicVert.glsl", "shaders/StylishShaders/FlashFrag.glsl"); zangief.useTime = true; + ObjectGl zangief2 = new Sprite(9.0f, 10f, path, null); + zangief2.setTextureWrap(58, 0, 62, 84, ObjectGl.DEFAULT); + engine.add_objectGl(zangief2); + zangief2.translate(new Vector3f(1000.0f, 200.0f, 0.0f)); + zangief2.flipTextureWrapH(); + + engine.setCameraTrackingBetweenTwoObjectGl(zangief, zangief2, 1000.0f); + //Create background ObjectGl background = new ObjectGl(0f,1f,1f,10f, pathToBG, null); background.setTextureWrap(0,0,621, 224, ObjectGl.DEFAULT); @@ -73,6 +74,9 @@ public class TestEngine { UIElementText uiTextTest = new UIElementText("Boulevard Combattant", 5.0f, 0.0f,1.0f, 25.0f, engine); engine.add_uiElement(uiTextTest); + UIElementText uiTextCoordP1 = new UIElementText("Boulevard Combattant", 7.0f, 0.0f,0.05f, 25.0f, engine); + engine.add_uiElement(uiTextCoordP1); + // 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))); @@ -96,15 +100,19 @@ public class TestEngine { gamepad1.inputRefresh(); List listZoomPlus = new ArrayList<>(); listZoomPlus.add(InputConst.buttonA); + List listZoomMinusKeyboard = new ArrayList<>(); + listZoomMinusKeyboard.add(GLFW_KEY_R); List listZoomMinus = new ArrayList<>(); listZoomMinus.add(InputConst.buttonB); - zoom = new Button("zoom", listZoomPlus, new ArrayList<>(), gamepad1); - dezoom = new Button("dezoom", listZoomMinus, new ArrayList<>(), gamepad1); + List listZoomPlusKeyboard = new ArrayList<>(); + listZoomPlusKeyboard.add(GLFW_KEY_F); + zoom = new Button("zoom", listZoomPlus, listZoomPlusKeyboard, gamepad1); + dezoom = new Button("dezoom", listZoomMinus, listZoomMinusKeyboard, gamepad1); } engine.translateView(new Vector3f(0.0f, -125.0f, 0.0f)); - while (engine.isRunning()) { + while (engine.getRunning()) { lastFrame = System.currentTimeMillis(); // Game logic should fit here @@ -113,13 +121,16 @@ public class TestEngine { // Check si le personnage a sauté if (zoom.isButtonPressed()){ // Le personnage saute - engine.camera.zoom(1.001f); + engine.getCamera().zoom(1.001f); }if(dezoom.isButtonPressed()){ - engine.camera.zoom(0.999f); + engine.getCamera().zoom(0.999f); } } - engine.cameraTrackingObjectGl(zangief, -250.0f); +// engine.cameraTrackingObjectGl(zangief, -250.0f); + engine.cameraTrackingBetweenTwoObjectGl(); + + uiTextCoordP1.setText("X: " + zangief.getXPos() + " Y: " + zangief.getYPos()); KeyboardInput.keyboardInput(zangief, speed); diff --git a/src/engine/object/Camera.java b/src/engine/camera/Camera.java similarity index 95% rename from src/engine/object/Camera.java rename to src/engine/camera/Camera.java index 3ce67b2..bffb0ae 100644 --- a/src/engine/object/Camera.java +++ b/src/engine/camera/Camera.java @@ -1,8 +1,9 @@ -package engine.object; +package engine.camera; import engine.Engine; import engine.math.Matrix4f; import engine.math.Vector3f; +import engine.object.ObjectGl; public class Camera { diff --git a/src/engine/camera/TrackingDummy.java b/src/engine/camera/TrackingDummy.java new file mode 100644 index 0000000..cddfd2f --- /dev/null +++ b/src/engine/camera/TrackingDummy.java @@ -0,0 +1,9 @@ +package engine.camera; + +import engine.math.Vector3f; + +public interface TrackingDummy { + + public Vector3f getViewVector(); + +} diff --git a/src/engine/camera/TrackingTore.java b/src/engine/camera/TrackingTore.java new file mode 100644 index 0000000..585e43f --- /dev/null +++ b/src/engine/camera/TrackingTore.java @@ -0,0 +1,52 @@ +package engine.camera; + +import engine.Engine; +import engine.math.Vector3f; +import engine.object.ObjectGl; + +public class TrackingTore implements TrackingDummy { + + private float rayonExt; + private final float offset; + private final ObjectGl obj1; + private final ObjectGl obj2; + private final Engine engine; + + public TrackingTore(float offset, ObjectGl obj1, ObjectGl obj2, Engine engine){ + this.rayonExt = offset; + this.offset = offset; + this.obj1 = obj1; + this.obj2 = obj2; + this.engine = engine; + } + + public Vector3f getViewVector(){ + Vector3f vec = new Vector3f(0.0f,0.0f,0.0f); + + //which object is to the left + ObjectGl left = obj1.getXPos() >= obj2.getXPos() ? obj2 : obj1; + ObjectGl right = obj1.getXPos() < obj2.getXPos() ? obj2 : obj1; + + //Track his current position and other useful data + float xPos = this.engine.getViewXPos(); + float dimension = this.engine.getCamera().getDimension(); + float distance = Math.abs(left.getXPos() - right.getXPos()) + right.getWidth() * right.getScalingFactor(); + float middle_point = (left.getXPos() + right.getXPos() + right.getWidth() * right.getScalingFactor()) / 2; + + if (left.getXPos() < xPos - rayonExt){ + // Il faut décaler à gauche + dezoom + vec.x = - middle_point - xPos; + this.rayonExt = Math.abs(left.getXPos() - xPos); + //mtn on dezoom il faut calculer le rapport necessaire pour avoir les deux objets dans le champs + this.engine.getCamera().zoom(distance/dimension); + } else if(left.getXPos() > xPos - rayonExt + offset){ +// this.engine.getCamera().zoom(dimension/distance); + // Il faut décaler à droite + zoom + } else if(right.getXPos() > xPos + rayonExt){ + // Il faut décaler à droite + dezoom + } else if(right.getXPos() < xPos + rayonExt - offset){ + // Il faut décaler à gauche + zoom + } + return vec; + } +} diff --git a/src/engine/gui/UIDummy.java b/src/engine/gui/UIDummy.java index eb7186e..8830a75 100644 --- a/src/engine/gui/UIDummy.java +++ b/src/engine/gui/UIDummy.java @@ -1,7 +1,7 @@ package engine.gui; import engine.Engine; -import engine.object.Camera; +import engine.camera.Camera; /** * Classe dont hérite tous les autres élements d'interface diff --git a/src/engine/gui/UIElement.java b/src/engine/gui/UIElement.java index 26f21fb..b7643eb 100644 --- a/src/engine/gui/UIElement.java +++ b/src/engine/gui/UIElement.java @@ -47,6 +47,6 @@ public class UIElement extends UIDummy{ float y = dimension * ar * 2 * this.yPos - dimension * ar; obj.translate(new Vector3f(x, y, this.zPos)); // Camera position - obj.translate(new Vector3f(- engine.transformationView.x, -engine.transformationView.y)); + obj.translate(new Vector3f(- engine.getTransformationView().x, -engine.getTransformationView().y)); } } diff --git a/src/engine/gui/UIElementText.java b/src/engine/gui/UIElementText.java index bb144cc..77ac42b 100644 --- a/src/engine/gui/UIElementText.java +++ b/src/engine/gui/UIElementText.java @@ -62,7 +62,7 @@ public class UIElementText extends UIDummy{ obj.translate(new Vector3f(x, y, this.zPos)); obj.translate(new Vector3f(10.0f * i * this.scalingFactor)); // 10.0f est dependant de la taille de la police à changer si besoin rendre dynamique si plusieurs police // Camera position - obj.translate(new Vector3f(- engine.transformationView.x, - engine.transformationView.y)); + obj.translate(new Vector3f(- engine.getTransformationView().x, - engine.getTransformationView().y)); i++; } } diff --git a/src/engine/object/ObjectGl.java b/src/engine/object/ObjectGl.java index 4824095..f089637 100644 --- a/src/engine/object/ObjectGl.java +++ b/src/engine/object/ObjectGl.java @@ -253,6 +253,14 @@ public class ObjectGl { return zPos; } + public float getWidth(){ + return this.width; + } + + public float getHeight(){ + return this.height; + } + public float getScalingFactor(){ return scalingFactor; }