From 32303c22baa9c2c6f4d0cbcac12e41a30b94b0a3 Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Tue, 16 Mar 2021 17:59:36 +0100 Subject: [PATCH] Improved l-system draw in 3d --- .gitignore | 5 ++++- src/lsystem/engine/Parser.java | 5 +++-- src/lsystem/engine/Rewrite.java | 4 +++- src/lsystem/screen/gl3d/GLEventListener.java | 17 ++++++++++----- .../screen/gl3d/GLKeyboardListener.java | 21 ++++++++++--------- 5 files changed, 33 insertions(+), 19 deletions(-) diff --git a/.gitignore b/.gitignore index 241925e..2ff5f31 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,7 @@ # ide files (eclipse, intellij...) .idea/ out/ -/bin/ +bin/ .project .classpath .bin @@ -9,6 +9,9 @@ out/ .metadata *.iml +# don't commit jre fatal errors +hs_err_pid*.log + # log files rapport/ !rapport/rapport.tex diff --git a/src/lsystem/engine/Parser.java b/src/lsystem/engine/Parser.java index 2992737..28b96ee 100644 --- a/src/lsystem/engine/Parser.java +++ b/src/lsystem/engine/Parser.java @@ -13,6 +13,7 @@ public class Parser { private final List rules; private final int nbIterations; private final char[] validChars = {'=',']','[','.','+','-','X','Y','x','y','z','0','1','2','3','4','5','6','7','8','9',' '}; + private static final float TWENTY_FIVE_DEGREES = 25/360f; public Parser(String axiom, List rules,int nbIterations) { this.axiom = axiom; @@ -150,9 +151,9 @@ public class Parser { private static float getFloat(String number) throws NumberFormatException { float n; if(number.equals("") || number.equals("+")) - n = 0.25f; + n = TWENTY_FIVE_DEGREES; else if(number.equals("-")) { - n = -0.25f; + n = -TWENTY_FIVE_DEGREES; }else{ System.out.println(number); n = Float.parseFloat(number); diff --git a/src/lsystem/engine/Rewrite.java b/src/lsystem/engine/Rewrite.java index 3a2253a..329806f 100644 --- a/src/lsystem/engine/Rewrite.java +++ b/src/lsystem/engine/Rewrite.java @@ -31,7 +31,9 @@ public class Rewrite { rewritten = replaceIDByRuleApplication(toRewrite, rules); System.out.println(i + " / " + recurrences + " : " + rewritten.length()); } - return rewritten.replace("[", "Y["); + rewritten = rewritten.replace("[", "Y["); + System.out.println(rewritten); + return rewritten; } } diff --git a/src/lsystem/screen/gl3d/GLEventListener.java b/src/lsystem/screen/gl3d/GLEventListener.java index d15efe5..de7e310 100644 --- a/src/lsystem/screen/gl3d/GLEventListener.java +++ b/src/lsystem/screen/gl3d/GLEventListener.java @@ -5,6 +5,7 @@ import com.jogamp.opengl.GLAutoDrawable; import com.jogamp.opengl.glu.GLU; import com.jogamp.opengl.util.gl2.GLUT; import lsystem.engine.Element; +import lsystem.engine.ElementProperties; public class GLEventListener implements com.jogamp.opengl.GLEventListener { @@ -77,7 +78,7 @@ public class GLEventListener implements com.jogamp.opengl.GLEventListener { glu.gluLookAt(canvas.camera[0], canvas.camera[1], canvas.camera[2], canvas.camera[0], canvas.camera[1], canvas.camera[2] - 1, 0f, 1f, 0f); gl.glPushMatrix(); gl.glRotatef(90f, -1f, 0f, 0f); - + gl.glColor3f(0f, 1f, 0f); displayLSystem(gl, glut, canvas.getLSystem()); gl.glPopMatrix(); @@ -89,12 +90,18 @@ public class GLEventListener implements com.jogamp.opengl.GLEventListener { private void displayLSystem(GL2 gl, GLUT glut, Element element) { gl.glPushMatrix(); - gl.glRotatef(element.rotation[0] * 360 , 1f, 0f, 0f); + gl.glRotatef(element.rotation[0] * 360, 1f, 0f, 0f); gl.glRotatef(element.rotation[1] * 360, 0f, 1f, 0f); - gl.glRotatef((element.rotation[0] + element.rotation[1]) * 360, 0f, 0f, 1f); + gl.glRotated(-Math.sin(element.rotation[0]) * 180, 0f, 0f, 1f); + gl.glRotated(-Math.sin(element.rotation[1]) * 180, 0f, 0f, 1f); gl.glTranslated(-Math.sin(element.rotation[0]), -Math.sin(element.rotation[1]), -Math.sin(element.rotation[0] + element.rotation[1])); - glut.glutSolidCylinder(0.25f, 1f, 10, 10); - gl.glTranslatef(0f, 0f, 1f); + if(element.property == ElementProperties.DRAW) { + glut.glutSolidCylinder(0.25f, 1f, 10, 10); + gl.glTranslatef(0f, 0f, 1f); + //gl.glTranslated(Math.cos(element.rotation[0] * Math.PI), Math.sin(element.rotation[1] * Math.PI), Math.cos(element.rotation[2] * Math.PI)); + } + + for(Element child : element.children) { displayLSystem(gl, glut, child); } diff --git a/src/lsystem/screen/gl3d/GLKeyboardListener.java b/src/lsystem/screen/gl3d/GLKeyboardListener.java index 4b0c04d..4852213 100644 --- a/src/lsystem/screen/gl3d/GLKeyboardListener.java +++ b/src/lsystem/screen/gl3d/GLKeyboardListener.java @@ -6,6 +6,7 @@ import java.awt.event.KeyListener; public class GLKeyboardListener implements KeyListener { private final GLCanvas canvas; + private final float MULTIPLIER = 0.2f; public GLKeyboardListener(GLCanvas swingGLCanvas) { @@ -17,20 +18,20 @@ public class GLKeyboardListener implements KeyListener { switch (e.getKeyChar()) { case 'z': - canvas.camera[2] -= 0.1f*Math.cos(Math.toRadians(canvas.camera[3])); - canvas.camera[0] += 0.1f*Math.cos(Math.toRadians(90-canvas.camera[3])); + canvas.camera[2] -= MULTIPLIER * Math.cos(Math.toRadians(canvas.camera[3])); + canvas.camera[0] += MULTIPLIER * Math.cos(Math.toRadians(90 - canvas.camera[3])); break; case 's': - canvas.camera[2] += 0.1f*Math.cos(Math.toRadians(canvas.camera[3])); - canvas.camera[0] -= 0.1f*Math.cos(Math.toRadians(90-canvas.camera[3])); + canvas.camera[2] += MULTIPLIER * Math.cos(Math.toRadians(canvas.camera[3])); + canvas.camera[0] -= MULTIPLIER * Math.cos(Math.toRadians(90 - canvas.camera[3])); break; case 'q': - canvas.camera[2] += 0.1f*Math.cos(Math.toRadians(canvas.camera[3]+90)); - canvas.camera[0] += 0.1f*Math.cos(Math.toRadians(180-canvas.camera[3])); + canvas.camera[2] += MULTIPLIER * Math.cos(Math.toRadians(canvas.camera[3] + 90)); + canvas.camera[0] += MULTIPLIER * Math.cos(Math.toRadians(180 - canvas.camera[3])); break; case 'd': - canvas.camera[2] -= 0.1f*Math.cos(Math.toRadians(canvas.camera[3]+90)); - canvas.camera[0] -= 0.1f*Math.cos(Math.toRadians(180-canvas.camera[3])); + canvas.camera[2] -= MULTIPLIER * Math.cos(Math.toRadians(canvas.camera[3] + 90)); + canvas.camera[0] -= MULTIPLIER * Math.cos(Math.toRadians(180 - canvas.camera[3])); break; case 'a': canvas.camera[3] -= 1; @@ -39,10 +40,10 @@ public class GLKeyboardListener implements KeyListener { canvas.camera[3] += 1; break; case 'w': - canvas.camera[1] += 0.1f; + canvas.camera[1] += MULTIPLIER; break; case 'x': - canvas.camera[1] -= 0.1f; + canvas.camera[1] -= MULTIPLIER; break; } }