From 84db074e0b7086f33d030ccea4653694a08452db Mon Sep 17 00:00:00 2001 From: Antonin Boyon Date: Tue, 16 Mar 2021 18:16:55 +0100 Subject: [PATCH] 2D engine --- src/lsystem/Main2D.java | 22 +++- src/lsystem/engine/Element.java | 4 + .../screen/gl2d/JoglEventListener2D.java | 40 +++++- src/lsystem/screen/gl2d/Point.java | 114 ++++++++++++++++++ src/lsystem/screen/gl3d/DrawHelper.java | 70 ++--------- 5 files changed, 182 insertions(+), 68 deletions(-) create mode 100644 src/lsystem/screen/gl2d/Point.java diff --git a/src/lsystem/Main2D.java b/src/lsystem/Main2D.java index c5ffde3..abcd7e8 100644 --- a/src/lsystem/Main2D.java +++ b/src/lsystem/Main2D.java @@ -1,9 +1,22 @@ package lsystem; +import java.util.ArrayList; + import lsystem.screen.gl2d.SwingGLCanvas2D; import lsystem.screen.main.MainFrame; +import lsystem.utils.Pair; public class Main2D { + + /*public static MainFrame mainFrame; + public static SwingGLCanvas2D joglFrame; + public static void main(String[] args) { + new Thread(() -> { + mainFrame = new MainFrame(); + mainFrame.setVisible(true); + }).start(); + new Thread(() -> joglFrame = new SwingGLCanvas2D()).start(); + } */ public static void main(String[] args) { @@ -11,7 +24,14 @@ public class Main2D { frame.setVisible(true); SwingGLCanvas2D canvas = new SwingGLCanvas2D(); canvas.setVisible(true); - + new Thread(() ->{ + SwingGLCanvas2D joglFrame = new SwingGLCanvas2D(); + String axiom="X"; + ArrayList> rules = new ArrayList<>(); + rules.add(new Pair<>("X=Y+[[X]-X]-Y[-YX]+X", "Y=YY")); + joglFrame.setLSystem(axiom,rules,5); + joglFrame.setVisible(true); + }).start(); } } diff --git a/src/lsystem/engine/Element.java b/src/lsystem/engine/Element.java index 1aed8ab..8d5d544 100644 --- a/src/lsystem/engine/Element.java +++ b/src/lsystem/engine/Element.java @@ -18,5 +18,9 @@ public class Element { this.parent = parent; this.rotation = rotation; } + + public int getRotation2D () { + return (int) rotation[0]; + } } diff --git a/src/lsystem/screen/gl2d/JoglEventListener2D.java b/src/lsystem/screen/gl2d/JoglEventListener2D.java index bfb494d..dfec901 100644 --- a/src/lsystem/screen/gl2d/JoglEventListener2D.java +++ b/src/lsystem/screen/gl2d/JoglEventListener2D.java @@ -1,11 +1,21 @@ package lsystem.screen.gl2d; +import java.awt.Point; +import java.awt.geom.Point2D; +import java.util.ArrayList; + import com.jogamp.opengl.GL2; import com.jogamp.opengl.GLAutoDrawable; import com.jogamp.opengl.GLEventListener; import com.jogamp.opengl.glu.GLU; import com.jogamp.opengl.util.gl2.GLUT; + +import javafx.scene.control.Tab; +import lsystem.engine.Element; +import lsystem.engine.ElementProperties; import lsystem.screen.gl3d.DrawHelper; +import lsystem.screen.gl3d.GLCanvas; +import lsystem.utils.Pair; public class JoglEventListener2D implements GLEventListener { @@ -43,15 +53,37 @@ public class JoglEventListener2D implements GLEventListener { @Override public void dispose(GLAutoDrawable glAutoDrawable) { } + + public void drawAll (GL2 gl, Element actual, lsystem.screen.gl2d.Point origin) { + if (actual.property == ElementProperties.DRAW) { + System.out.println("DESSIN"); + lsystem.screen.gl2d.Point newOrigin = new lsystem.screen.gl2d.Point (origin, actual.getRotation2D()); + DrawHelper.drawStick(gl, origin, newOrigin); + } + System.out.println(actual.children.isEmpty()); + for (Element children : actual.children) { + System.out.println("CHILD"); + drawAll(gl, children, new lsystem.screen.gl2d.Point (origin, actual.getRotation2D())); + } + } @Override public void display (GLAutoDrawable glAutoDrawable) { - GL2 gl = glAutoDrawable.getGL().getGL2(); + /* Element str = new Element(ElementProperties.DRAW, null, new int[]{0, 0, 0}); - float xDefault = -1.0f, yDefault = -1.0f; + Element child1 = new Element(ElementProperties.DRAW, str, new int[]{0, 0, 0}); + Element child2 = new Element(ElementProperties.DRAW, str, new int[]{45, 0, 0}); + str.children.add(child1); + str.children.add(child2); + Element child11 = new Element(ElementProperties.DRAW, child1, new int[]{225, 0, 0}); + Element child12 = new Element(ElementProperties.DRAW, child1, new int[]{270, 0, 0}); + child1.children.add(child11); + child1.children.add(child12);*/ + + GL2 gl = glAutoDrawable.getGL().getGL2(); + + drawAll (gl, canvas.getLSystem(), new lsystem.screen.gl2d.Point(-1.0f, -1.0f)); - DrawHelper.drawStick(gl, 0.1f, xDefault, yDefault, 0); - DrawHelper.drawStick(gl, 0.2f, 1.1f, 1.1f, 90); } @Override diff --git a/src/lsystem/screen/gl2d/Point.java b/src/lsystem/screen/gl2d/Point.java new file mode 100644 index 0000000..d1877f2 --- /dev/null +++ b/src/lsystem/screen/gl2d/Point.java @@ -0,0 +1,114 @@ +package lsystem.screen.gl2d; + +public class Point { + private float x; + private float y; + + public Point () { + + } + + public Point (float x, float y) { + this.x = x; + this.y = y; + } + + public Point (Point old, int angle) { + angle = compactAngle(angle); + //Point a = new Point(); + //x = getNewOrigin(old, angle, 0.1f); + getNewOrigin(old, angle, 0.1f); + } + + public float getX() { + return x; + } + + public void setX(float x) { + this.x = x; + } + + public float getY() { + return y; + } + + public void setY(float y) { + this.y = y; + } + + public void getNewOrigin (Point old, int angle, float echelle) { + + int newX=0, newY=0; + switch (angle) { + case 0: + newX = 1; + newY = 1; + break; + case 45: + newX = 1; + newY = 0; + break; + case 90: + newX = 1; + newY = -1; + break; + case 135: + newX = 0; + newY = -1; + break; + case 180: + newX = -1; + newY = -1; + break; + case 225: + newX = -1; + newY = 0; + break; + case 270: + newX = -1; + newY = 1; + break; + case 315: + newX = 0; + newY = 1; + break; + } + + this.x = old.getX() + echelle * newX; + this.y+= old.getY() + echelle * newY; + //return old; + } + + public String toString () { + return "("+this.x+";"+this.y+")"; + } + + public static int compactAngle (int angle) { + angle = angle - ((angle / 360) * 360); + switch (angle) { + case -315: + angle = 45; + break; + case -270: + angle = 90; + break; + case -225: + angle = 135; + break; + case -180: + angle = 180; + break; + case -135: + angle = 225; + break; + case -90: + angle = 270; + break; + case -45: + angle = 315; + break; + } + return angle; + } + +} diff --git a/src/lsystem/screen/gl3d/DrawHelper.java b/src/lsystem/screen/gl3d/DrawHelper.java index 0ff56bc..65d2231 100644 --- a/src/lsystem/screen/gl3d/DrawHelper.java +++ b/src/lsystem/screen/gl3d/DrawHelper.java @@ -2,6 +2,8 @@ package lsystem.screen.gl3d; import com.jogamp.opengl.GL2; import com.jogamp.opengl.util.gl2.GLUT; + +import lsystem.screen.gl2d.Point; import lsystem.screen.gl2d.SwingGLCanvas2D; public class DrawHelper { @@ -101,72 +103,14 @@ public class DrawHelper { + canvas.camera[3] + " pitch = " + canvas.camera[4] + " roll = " + canvas.camera[5]); } - public static void drawStick(GL2 gl, float echelle, float x, float y, int angle) { - angle = angle - ((angle / 360) * 360); - switch (angle) { - case -315: - angle = 45; - ; - case -270: - angle = 90; - ; - case -225: - angle = 135; - ; - case -180: - angle = 180; - ; - case -135: - angle = 225; - ; - case -90: - angle = 270; - ; - case -45: - angle = 315; - ; - } + public static void drawStick(GL2 gl, Point origin, Point newOrigin) { - // Direction - int newX=0, newY=0; - switch (angle) { - case 0: - newX = 1; - newY = 1; - break; - case 45: - newX = 1; - newY = 0; - break; - case 90: - newX = 1; - newY = -1; - break; - case 135: - newX = 0; - newY = -1; - break; - case 180: - newX = -1; - newY = -1; - break; - case 225: - newX = -1; - newY = 0; - break; - case 270: - newX = -1; - newY = 1; - break; - case 315: - newX = 0; - newY = 1; - break; - } gl.glBegin(GL2.GL_LINES); - gl.glVertex2f(x, y); - gl.glVertex2f(x + (echelle * newX), y + (echelle * newY)); + gl.glVertex2f(origin.getX(), origin.getY()); + gl.glVertex2f(newOrigin.getX(), newOrigin.getY()); gl.glEnd(); } + + }