Move AbstractCanvas and AbstractListener to engine.gl3d + move drawLSystem to DrawHelper

This commit is contained in:
Quentin Legot 2021-04-12 22:13:24 +02:00
parent 0402d49622
commit d5acf85338
7 changed files with 25 additions and 61 deletions

View File

@ -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.*;

View File

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

View File

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

View File

@ -1,7 +1,5 @@
package lsystem.screen.gl3d;
import lsystem.screen.AbstractCanvas;
public class GLCanvas extends AbstractCanvas {

View File

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

View File

@ -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.*;

View File

@ -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.*;