From 84f55b6ca4b0858a2913c2d6c735ce16ec185e00 Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Tue, 9 Mar 2021 12:23:01 +0100 Subject: [PATCH] 3D interface now launch when rules has been parsed + deleted useless rectangular prism --- src/lsystem/Main.java | 2 -- src/lsystem/Main2D.java | 2 +- src/lsystem/engine/Parser.java | 16 ++++++++-------- src/lsystem/screen/AbstractCanvas.java | 5 ++++- src/lsystem/screen/gl2d/SwingGLCanvas2D.java | 5 +++-- src/lsystem/screen/gl3d/DrawHelper.java | 20 +++++++++++++++----- src/lsystem/screen/gl3d/GLCanvas.java | 5 +++-- src/lsystem/screen/gl3d/GLEventListener.java | 12 +++++------- src/lsystem/screen/main/Listener.java | 13 +++++++++---- 9 files changed, 48 insertions(+), 32 deletions(-) diff --git a/src/lsystem/Main.java b/src/lsystem/Main.java index 562da5c..76dab7a 100644 --- a/src/lsystem/Main.java +++ b/src/lsystem/Main.java @@ -8,8 +8,6 @@ public class Main { public static void main(String[] args) { MainFrame frame = new MainFrame(); frame.setVisible(true); - GLCanvas canvas = new GLCanvas(); - canvas.setVisible(true); } } diff --git a/src/lsystem/Main2D.java b/src/lsystem/Main2D.java index c5ffde3..9044f09 100644 --- a/src/lsystem/Main2D.java +++ b/src/lsystem/Main2D.java @@ -9,7 +9,7 @@ public class Main2D { MainFrame frame = new MainFrame(); frame.setVisible(true); - SwingGLCanvas2D canvas = new SwingGLCanvas2D(); + SwingGLCanvas2D canvas = new SwingGLCanvas2D(null); canvas.setVisible(true); } diff --git a/src/lsystem/engine/Parser.java b/src/lsystem/engine/Parser.java index a21eded..ccfbd16 100644 --- a/src/lsystem/engine/Parser.java +++ b/src/lsystem/engine/Parser.java @@ -128,19 +128,19 @@ public class Parser { appliedRotation[property.getDirection()] = n; } } else { - for(char n : numbers) { - if(c == n) { - number += c; - break; - } - } if(c == '[') { workingElement = lastCreatedElement; bracket.add(lastCreatedElement); - } - if(c == ']') { + } else if(c == ']') { assert workingElement != null && !bracket.isEmpty(); workingElement = bracket.remove(bracket.size() - 1); + } else { + for(char n : numbers) { + if(c == n) { + number += c; + break; + } + } } } } diff --git a/src/lsystem/screen/AbstractCanvas.java b/src/lsystem/screen/AbstractCanvas.java index b370263..da7fd69 100644 --- a/src/lsystem/screen/AbstractCanvas.java +++ b/src/lsystem/screen/AbstractCanvas.java @@ -6,6 +6,7 @@ import com.jogamp.opengl.awt.GLCanvas; import com.jogamp.opengl.glu.GLU; import com.jogamp.opengl.util.FPSAnimator; import com.jogamp.opengl.util.gl2.GLUT; +import lsystem.engine.Element; import javax.swing.*; import java.awt.*; @@ -14,6 +15,7 @@ import java.awt.event.WindowEvent; public abstract class AbstractCanvas { + private final Element lsystem; public JFrame frame; protected FPSAnimator animator; public final GLCanvas glCanvas; @@ -23,7 +25,8 @@ public abstract class AbstractCanvas { 0f, 0f, 0f}; // camera rotation yaw(x-axis), pitch(y-axis), roll(z-axis) - protected AbstractCanvas() { + protected AbstractCanvas(Element parsed) { + this.lsystem = parsed; GLProfile glProfile = GLProfile.getDefault(); GLCapabilities glCapabilities = new GLCapabilities(glProfile); this.glCanvas = new GLCanvas(glCapabilities); diff --git a/src/lsystem/screen/gl2d/SwingGLCanvas2D.java b/src/lsystem/screen/gl2d/SwingGLCanvas2D.java index 7a2e068..931be1b 100644 --- a/src/lsystem/screen/gl2d/SwingGLCanvas2D.java +++ b/src/lsystem/screen/gl2d/SwingGLCanvas2D.java @@ -1,12 +1,13 @@ package lsystem.screen.gl2d; +import lsystem.engine.Element; import lsystem.screen.AbstractCanvas; public class SwingGLCanvas2D extends AbstractCanvas { - public SwingGLCanvas2D() { - super(); + public SwingGLCanvas2D(Element parsed) { + super(parsed); } @Override diff --git a/src/lsystem/screen/gl3d/DrawHelper.java b/src/lsystem/screen/gl3d/DrawHelper.java index 77a268e..b1db104 100644 --- a/src/lsystem/screen/gl3d/DrawHelper.java +++ b/src/lsystem/screen/gl3d/DrawHelper.java @@ -32,12 +32,12 @@ public class DrawHelper { gl.glVertex3f(0f, 0f, 1f); gl.glColor3f(255f, 255f, 255f); - for (int i = -25; i < 26; i++) { - gl.glVertex3f(-25f, 0f, i); - gl.glVertex3f(25f, 0f, i); + for (int i = -20; i < 21; i++) { + gl.glVertex3f(-20f, 0f, i); + gl.glVertex3f(20f, 0f, i); - gl.glVertex3f(i, 0, -25f); - gl.glVertex3f(i, 0, 25f); + gl.glVertex3f(i, 0, -20f); + gl.glVertex3f(i, 0, 20f); } gl.glEnd(); gl.glRasterPos3f(1.1f, 0.0f, 0.0f); @@ -71,6 +71,16 @@ public class DrawHelper { gl.glVertex3f(rightUpX, leftBottomY, rightUpZ); gl.glVertex3f(rightUpX, rightUpY, rightUpZ); gl.glVertex3f(leftBottomX, rightUpY, rightUpZ); + + gl.glVertex3f(leftBottomX, leftBottomY, leftBottomZ); + gl.glVertex3f(rightUpX, leftBottomY, leftBottomZ); + gl.glVertex3f(rightUpX, leftBottomY, rightUpZ); + gl.glVertex3f(leftBottomX, leftBottomY, rightUpZ); + + gl.glVertex3f(leftBottomX, rightUpY, leftBottomZ); + gl.glVertex3f(rightUpX, rightUpY, leftBottomZ); + gl.glVertex3f(rightUpX, rightUpY, rightUpZ); + gl.glVertex3f(leftBottomX, rightUpY, rightUpZ); gl.glEnd(); gl.glPopMatrix(); } diff --git a/src/lsystem/screen/gl3d/GLCanvas.java b/src/lsystem/screen/gl3d/GLCanvas.java index 97b5ccc..d1a2c0f 100644 --- a/src/lsystem/screen/gl3d/GLCanvas.java +++ b/src/lsystem/screen/gl3d/GLCanvas.java @@ -1,12 +1,13 @@ package lsystem.screen.gl3d; +import lsystem.engine.Element; import lsystem.screen.AbstractCanvas; public class GLCanvas extends AbstractCanvas { - public GLCanvas() { - super(); + public GLCanvas(Element parsed) { + super(parsed); } @Override diff --git a/src/lsystem/screen/gl3d/GLEventListener.java b/src/lsystem/screen/gl3d/GLEventListener.java index 0f7dd54..e5e0112 100644 --- a/src/lsystem/screen/gl3d/GLEventListener.java +++ b/src/lsystem/screen/gl3d/GLEventListener.java @@ -104,15 +104,13 @@ public class GLEventListener implements com.jogamp.opengl.GLEventListener { glut.glutSolidSphere(0.75f, 20, 20); gl.glPopMatrix(); - angle = (angle + 0.1f) % 360; - - gl.glColor3f(0f, 0.5f, 1f); gl.glPushMatrix(); - for(Pair pair : prismPosition) { - DrawHelper.drawRectangularPrism(gl, pair.getLeft(), 0f, pair.getRight(), pair.getLeft() + 1, 1f, pair.getRight() + 1); - } + gl.glColor3f(1f, 1f, 1f); + DrawHelper.drawRectangularPrism(gl, -3f, 0f, -2f, -2.5f, 1.5f, -1.5f); + glut.glutSolidCylinder(0.25f, 1.5f, 15, 2); gl.glPopMatrix(); - gl.glTranslatef(0f, 0f, 0f); + + angle = (angle + 0.1f) % 360; DrawHelper.drawAxes(gl, glut); DrawHelper.drawDebugInformation(gl, glut, canvas); diff --git a/src/lsystem/screen/main/Listener.java b/src/lsystem/screen/main/Listener.java index b016e17..3fa6219 100644 --- a/src/lsystem/screen/main/Listener.java +++ b/src/lsystem/screen/main/Listener.java @@ -3,6 +3,7 @@ package lsystem.screen.main; import lsystem.engine.Element; import lsystem.engine.Parser; import lsystem.engine.Rewrite; +import lsystem.screen.gl3d.GLCanvas; import javax.swing.*; import java.awt.event.ActionEvent; @@ -52,10 +53,14 @@ public class Listener implements ActionListener, KeyListener { JOptionPane.showMessageDialog(null, "Vos règles ou votre axiome ne sont pas correctement écrites, veuillez recommencer"); new Listener(null, index, "Clear",tab); } else { - Rewrite rewriter = new Rewrite(axiom, parser.parseRules(), tab.getNbIterations()); - final String word = rewriter.rewrite(); - System.out.println(word); - final Element parsed = Parser.parse(word); + new Thread(() -> { + Rewrite rewriter = new Rewrite(axiom, parser.parseRules(), tab.getNbIterations()); + final String word = rewriter.rewrite(); + System.out.println(word); + final Element parsed = Parser.parse(word); + GLCanvas canvas = new GLCanvas(parsed); + canvas.setVisible(true); + }).start(); } break;