Improved l-system draw in 3d

This commit is contained in:
Quentin Legot 2021-03-16 17:59:36 +01:00
parent 6242618413
commit 32303c22ba
5 changed files with 33 additions and 19 deletions

5
.gitignore vendored
View File

@ -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

View File

@ -13,6 +13,7 @@ public class Parser {
private final List<String> 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<String> 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);

View File

@ -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;
}
}

View File

@ -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);
}

View File

@ -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;
}
}