diff --git a/src/lsystem/screen/AbstractCanvas.java b/src/lsystem/screen/gl3d/AbstractCanvas.java similarity index 97% rename from src/lsystem/screen/AbstractCanvas.java rename to src/lsystem/screen/gl3d/AbstractCanvas.java index 45e0588..e9438ec 100644 --- a/src/lsystem/screen/AbstractCanvas.java +++ b/src/lsystem/screen/gl3d/AbstractCanvas.java @@ -1,4 +1,4 @@ -package lsystem.screen; +package lsystem.screen.gl3d; import com.jogamp.opengl.GLCapabilities; import com.jogamp.opengl.GLProfile; @@ -9,6 +9,7 @@ import com.jogamp.opengl.util.gl2.GLUT; import lsystem.engine.Element; import lsystem.engine.Parser; import lsystem.engine.Rewrite; +import lsystem.screen.Constants; import lsystem.utils.Pair; import javax.swing.*; diff --git a/src/lsystem/screen/AbstractListener.java b/src/lsystem/screen/gl3d/AbstractListener.java similarity index 92% rename from src/lsystem/screen/AbstractListener.java rename to src/lsystem/screen/gl3d/AbstractListener.java index 64a66cd..d665e55 100644 --- a/src/lsystem/screen/AbstractListener.java +++ b/src/lsystem/screen/gl3d/AbstractListener.java @@ -1,4 +1,4 @@ -package lsystem.screen; +package lsystem.screen.gl3d; import com.jogamp.opengl.GL2; import com.jogamp.opengl.GLAutoDrawable; @@ -13,6 +13,7 @@ public abstract class AbstractListener implements GLEventListener { protected final GLU glu; protected final GLUT glut; protected int fps; + boolean firstGen; public AbstractListener(AbstractCanvas swingGLCanvas) { this.canvas = swingGLCanvas; @@ -50,7 +51,7 @@ public abstract class AbstractListener implements GLEventListener { gl.glMatrixMode(GL2.GL_PROJECTION); gl.glLoadIdentity(); - glu.gluPerspective(60.0f, (float) width / height, 0.1f, 1000.0f); + glu.gluPerspective(60.0f, ((float) width) / height, 0.1f, 1000.0f); } public abstract void drawLSystem(GL2 gl, Element element); diff --git a/src/lsystem/screen/gl3d/DrawHelper.java b/src/lsystem/screen/gl3d/DrawHelper.java index 1e963b8..54fd284 100644 --- a/src/lsystem/screen/gl3d/DrawHelper.java +++ b/src/lsystem/screen/gl3d/DrawHelper.java @@ -2,7 +2,8 @@ package lsystem.screen.gl3d; import com.jogamp.opengl.GL2; import com.jogamp.opengl.util.gl2.GLUT; -import lsystem.screen.AbstractCanvas; +import lsystem.engine.Element; +import lsystem.engine.ElementProperties; public class DrawHelper { @@ -43,43 +44,29 @@ public class DrawHelper { glut.glutBitmapCharacter(GLUT.BITMAP_HELVETICA_18, 'Z'); // draw the z axis } - public static void drawRectangularPrism(GL2 gl, float leftBottomX, float leftBottomY, float leftBottomZ, - float rightUpX, float rightUpY, float rightUpZ) { + public static void drawLSystem(AbstractListener listener, GL2 gl, GLUT glut, Element element) { gl.glPushMatrix(); - gl.glBegin(GL2.GL_QUADS); - gl.glVertex3f(leftBottomX, leftBottomY, leftBottomZ); - gl.glVertex3f(rightUpX, leftBottomY, leftBottomZ); - gl.glVertex3f(rightUpX, rightUpY, leftBottomZ); - gl.glVertex3f(leftBottomX, rightUpY, leftBottomZ); + gl.glRotatef(element.rotation[0] * 360, 1f, 0f, 0f); + gl.glRotatef(element.rotation[1] * 360, 0f, 1f, 0f); + gl.glRotated(-Math.sin(element.rotation[0]) * 180 - 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])); - gl.glVertex3f(leftBottomX, leftBottomY, leftBottomZ); - gl.glVertex3f(leftBottomX, leftBottomY, rightUpZ); - gl.glVertex3f(leftBottomX, rightUpY, rightUpZ); - gl.glVertex3f(leftBottomX, rightUpY, leftBottomZ); + if(element.property == ElementProperties.DRAW) { + if(listener.firstGen) { + listener.canvas.camera[1] += 0.10f; + listener.canvas.camera[2] += 0.10f; + } + glut.glutSolidCylinder(0.25f, 1f, 10, 10); + gl.glTranslatef(0f, 0f, 1f); + } - gl.glVertex3f(rightUpX, leftBottomY, leftBottomZ); - gl.glVertex3f(rightUpX, leftBottomY, rightUpZ); - gl.glVertex3f(rightUpX, rightUpY, rightUpZ); - gl.glVertex3f(rightUpX, rightUpY, leftBottomZ); - - gl.glVertex3f(leftBottomX, leftBottomY, rightUpZ); - 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(); + for(Element child : element.children) { + drawLSystem(listener, gl, glut, child); + } gl.glPopMatrix(); } + public static void drawDebugInformation(GL2 gl, GLUT glut, AbstractCanvas canvas) { gl.glRasterPos3f(canvas.camera[0], canvas.camera[1], canvas.camera[2] - 1); gl.glColor3f(1f, 1f, 1f); diff --git a/src/lsystem/screen/gl3d/GLCanvas.java b/src/lsystem/screen/gl3d/GLCanvas.java index cec118f..505b72e 100644 --- a/src/lsystem/screen/gl3d/GLCanvas.java +++ b/src/lsystem/screen/gl3d/GLCanvas.java @@ -1,7 +1,5 @@ package lsystem.screen.gl3d; -import lsystem.screen.AbstractCanvas; - public class GLCanvas extends AbstractCanvas { diff --git a/src/lsystem/screen/gl3d/GLEventListener.java b/src/lsystem/screen/gl3d/GLEventListener.java index bb1eb8b..5d14b7e 100644 --- a/src/lsystem/screen/gl3d/GLEventListener.java +++ b/src/lsystem/screen/gl3d/GLEventListener.java @@ -3,14 +3,11 @@ package lsystem.screen.gl3d; import com.jogamp.opengl.GL2; import com.jogamp.opengl.GLAutoDrawable; import lsystem.engine.Element; -import lsystem.engine.ElementProperties; import lsystem.screen.Constants; -import lsystem.screen.AbstractListener; public class GLEventListener extends AbstractListener { private final float[] light_0_position = {1000f, 1000f, 1000f, 1f}; - private boolean firstGen; public GLEventListener(GLCanvas swingGLCanvas) { @@ -67,26 +64,7 @@ public class GLEventListener extends AbstractListener { } @Override public void drawLSystem(GL2 gl, Element element) { - gl.glPushMatrix(); - gl.glRotatef(element.rotation[0] * 360, 1f, 0f, 0f); - gl.glRotatef(element.rotation[1] * 360, 0f, 1f, 0f); - gl.glRotated(-Math.sin(element.rotation[0]) * 180 - 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])); - - if(element.property == ElementProperties.DRAW) { - if(firstGen) { - canvas.camera[1] += 0.10f; - canvas.camera[2] += 0.10f; - } - glut.glutSolidCylinder(0.25f, 1f, 10, 10); - gl.glTranslatef(0f, 0f, 1f); - } - - - for(Element child : element.children) { - drawLSystem(gl, child); - } - gl.glPopMatrix(); + DrawHelper.drawLSystem(this, gl, glut, element); } @Override diff --git a/src/lsystem/screen/gl3d/GLMouseListener.java b/src/lsystem/screen/gl3d/GLMouseListener.java index 3291e63..7451848 100644 --- a/src/lsystem/screen/gl3d/GLMouseListener.java +++ b/src/lsystem/screen/gl3d/GLMouseListener.java @@ -1,7 +1,6 @@ package lsystem.screen.gl3d; import com.jogamp.opengl.glu.GLU; -import lsystem.screen.AbstractCanvas; import java.awt.*; import java.awt.event.*; diff --git a/src/lsystem/screen/main/Listener.java b/src/lsystem/screen/main/Listener.java index cc0c18c..7a391fe 100644 --- a/src/lsystem/screen/main/Listener.java +++ b/src/lsystem/screen/main/Listener.java @@ -2,7 +2,7 @@ package lsystem.screen.main; import lsystem.Main; import lsystem.engine.Parser; -import lsystem.screen.AbstractCanvas; +import lsystem.screen.gl3d.AbstractCanvas; import lsystem.utils.Pair; import javax.swing.*; diff --git a/src/lsystem/utils/Pair.java b/src/lsystem/utils/Pair.java index 4ed5df6..313b7a8 100644 --- a/src/lsystem/utils/Pair.java +++ b/src/lsystem/utils/Pair.java @@ -38,7 +38,7 @@ public class Pair { return false; } final Pair other = (Pair) obj; - return this.left.equals(other.getLeft()) && this.left.equals(other.getRight()); + return this.left.equals(other.getLeft()) && this.right.equals(other.getRight()); } @Override