From 19346774da45c0ab565de2e8dcb77af84aec9221 Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Tue, 30 Mar 2021 16:30:20 +0200 Subject: [PATCH] Refactored GLEventListener.java and JoglEventListener2D.java + deleted useless 2d classes --- src/lsystem/screen/AbstractListener.java | 57 ++++++++++++ .../screen/gl2d/JoglEventListener2D.java | 55 ++---------- .../screen/gl2d/JoglMouseListener2D.java | 87 ------------------- .../screen/gl2d/KeyboardListener2D.java | 56 ------------ src/lsystem/screen/gl2d/SwingGLCanvas2D.java | 6 -- src/lsystem/screen/gl3d/DrawHelper.java | 26 ++---- src/lsystem/screen/gl3d/GLEventListener.java | 46 +++------- src/lsystem/screen/gl3d/GLMouseListener.java | 13 +-- 8 files changed, 84 insertions(+), 262 deletions(-) create mode 100644 src/lsystem/screen/AbstractListener.java delete mode 100644 src/lsystem/screen/gl2d/JoglMouseListener2D.java delete mode 100644 src/lsystem/screen/gl2d/KeyboardListener2D.java diff --git a/src/lsystem/screen/AbstractListener.java b/src/lsystem/screen/AbstractListener.java new file mode 100644 index 0000000..64a66cd --- /dev/null +++ b/src/lsystem/screen/AbstractListener.java @@ -0,0 +1,57 @@ +package lsystem.screen; + +import com.jogamp.opengl.GL2; +import com.jogamp.opengl.GLAutoDrawable; +import com.jogamp.opengl.GLEventListener; +import com.jogamp.opengl.glu.GLU; +import com.jogamp.opengl.util.gl2.GLUT; +import lsystem.engine.Element; + +public abstract class AbstractListener implements GLEventListener { + + protected final AbstractCanvas canvas; + protected final GLU glu; + protected final GLUT glut; + protected int fps; + + public AbstractListener(AbstractCanvas swingGLCanvas) { + this.canvas = swingGLCanvas; + this.glu = canvas.glu; + this.glut = canvas.glut; + } + + @Override + public void init(GLAutoDrawable glAutoDrawable) { + GL2 gl = glAutoDrawable.getGL().getGL2(); + + gl.glClearColor(0f, 0f, 0f, 1.0f); + + new Thread(() -> { + while (canvas.frame.isVisible()) { + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + System.out.println(fps); + fps = 0; + } + }).start(); + } + + @Override + public void dispose(GLAutoDrawable glAutoDrawable) { + } + + @Override + public void reshape(GLAutoDrawable glAutoDrawable, int x, int y, int width, int height) { + GL2 gl = glAutoDrawable.getGL().getGL2(); + gl.glViewport(x, y, width, height); + + gl.glMatrixMode(GL2.GL_PROJECTION); + gl.glLoadIdentity(); + 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/gl2d/JoglEventListener2D.java b/src/lsystem/screen/gl2d/JoglEventListener2D.java index a42750b..c88facf 100644 --- a/src/lsystem/screen/gl2d/JoglEventListener2D.java +++ b/src/lsystem/screen/gl2d/JoglEventListener2D.java @@ -2,51 +2,24 @@ package lsystem.screen.gl2d; import com.jogamp.opengl.GL2; import com.jogamp.opengl.GLAutoDrawable; -import com.jogamp.opengl.GLEventListener; -import com.jogamp.opengl.glu.GLU; -import com.jogamp.opengl.util.gl2.GLUT; import lsystem.engine.Element; import lsystem.engine.ElementProperties; +import lsystem.screen.AbstractListener; import lsystem.screen.gl3d.DrawHelper; -public class JoglEventListener2D implements GLEventListener { - private final SwingGLCanvas2D canvas; - - private final GLU glu; - private final GLUT glut; - private int fps; +public class JoglEventListener2D extends AbstractListener { public JoglEventListener2D(SwingGLCanvas2D swingGLCanvas) { - this.canvas = swingGLCanvas; - this.glu = canvas.glu; - this.glut = canvas.glut; + super(swingGLCanvas); } @Override - public void init(GLAutoDrawable glAutoDrawable) { - GL2 gl = glAutoDrawable.getGL().getGL2(); - - gl.glClearColor(0f, 0f, 0f, 1.0f); - - new Thread(() -> { - while (canvas.frame.isVisible()) { - try { - Thread.sleep(1000); - } catch (InterruptedException e) { - e.printStackTrace(); - } - System.out.println(fps); - fps = 0; - } - }).start(); + public void drawLSystem(GL2 gl, Element element) { + drawAll(gl, element, new lsystem.screen.gl2d.Point2(-1.0f, -1.0f)); } - @Override - public void dispose(GLAutoDrawable glAutoDrawable) { - } - - public void drawAll(GL2 gl, Element actual, lsystem.screen.gl2d.Point2 origin) { + private void drawAll(GL2 gl, Element actual, lsystem.screen.gl2d.Point2 origin) { if (actual.property == ElementProperties.DRAW) { System.out.println("DESSIN"); lsystem.screen.gl2d.Point2 newOrigin = new lsystem.screen.gl2d.Point2(origin, actual.getRotation2D()); @@ -61,25 +34,11 @@ public class JoglEventListener2D implements GLEventListener { @Override public void display(GLAutoDrawable glAutoDrawable) { - /* - * Element str = new Element(ElementProperties.DRAW, null, new int[]{0, 0, 0}); - * - * Element child1 = new Element(ElementProperties.DRAW, str, new int[]{0, 0, - * 0}); Element child2 = new Element(ElementProperties.DRAW, str, new int[]{45, - * 0, 0}); str.children.add(child1); str.children.add(child2); Element child11 = - * new Element(ElementProperties.DRAW, child1, new int[]{225, 0, 0}); Element - * child12 = new Element(ElementProperties.DRAW, child1, new int[]{270, 0, 0}); - * child1.children.add(child11); child1.children.add(child12); - */ GL2 gl = glAutoDrawable.getGL().getGL2(); + drawLSystem(gl, canvas.getLSystem()); drawAll(gl, canvas.getLSystem(), new lsystem.screen.gl2d.Point2(-1.0f, -1.0f)); } - - @Override - public void reshape(GLAutoDrawable glAutoDrawable, int x, int y, int width, int height) { - - } } diff --git a/src/lsystem/screen/gl2d/JoglMouseListener2D.java b/src/lsystem/screen/gl2d/JoglMouseListener2D.java deleted file mode 100644 index 0fdc4de..0000000 --- a/src/lsystem/screen/gl2d/JoglMouseListener2D.java +++ /dev/null @@ -1,87 +0,0 @@ -package lsystem.screen.gl2d; - -import com.jogamp.opengl.glu.GLU; - -import java.awt.*; -import java.awt.event.*; - -public class JoglMouseListener2D implements MouseListener, MouseMotionListener, MouseWheelListener { - - private final GLU glu; - private final SwingGLCanvas2D canvas; - private int button = 0; - private Point origine; - - public JoglMouseListener2D(SwingGLCanvas2D canvas) { - this.canvas = canvas; - this.glu = canvas.glu; - } - - @Override - public void mouseClicked(MouseEvent e) { - } - - @Override - public void mousePressed(MouseEvent e) { - if(button == 0) { - button = e.getButton(); - origine = e.getPoint(); - } else { - button = 0; - origine = null; - } - } - - @Override - public void mouseReleased(MouseEvent e) { - button = 0; - } - - @Override - public void mouseEntered(MouseEvent e) { - } - - @Override - public void mouseExited(MouseEvent e) { - System.out.println("exited " + canvas.camera[0] + ", " + canvas.camera[1]); - } - - @Override - public void mouseDragged(MouseEvent e) { - if(origine != null) { - double xDiff = origine.getX() - e.getPoint().getX(); - double yDiff = origine.getY() - e.getPoint().getY(); - /* if(button == 2) { - canvas.camera[0] += Math.cos(canvas.camera[3]) * xDiff * 0.01; - canvas.camera[1] += Math.cos(canvas.camera[4]) * yDiff * 0.01; - canvas.camera[2] += Math.sin(canvas.camera[3]) * xDiff * 0.01; - } */ - if(button == 3) { - canvas.camera[3] += xDiff * 0.1; - canvas.camera[4] += yDiff * 0.1; - } - origine = e.getPoint(); - } - for (int i = 0; i < canvas.camera.length; i++) { - canvas.camera[i] = canvas.camera[i] % 360; - } - } - - @Override - public void mouseMoved(MouseEvent e) { - } - - @Override - public void mouseWheelMoved(MouseWheelEvent e) { - if( 45 > Math.abs(canvas.camera[3]) && 45>Math.abs(canvas.camera[4])) - canvas.camera[0] += e.getWheelRotation() * Math.tan(Math.toRadians(-canvas.camera[3])) * 0.25; - else - canvas.camera[0] += e.getWheelRotation() * Math.tan(Math.toRadians(canvas.camera[3])) * 0.25; - canvas.camera[1] += e.getWheelRotation() * Math.tan(Math.toRadians(canvas.camera[4])) * 0.25; - if( 45 < Math.abs(canvas.camera[3]) || 45 < Math.abs(canvas.camera[4])) - canvas.camera[2] += -e.getWheelRotation()*0.25; - else - canvas.camera[2] += e.getWheelRotation()*0.25; - - } -} diff --git a/src/lsystem/screen/gl2d/KeyboardListener2D.java b/src/lsystem/screen/gl2d/KeyboardListener2D.java deleted file mode 100644 index 7989eb9..0000000 --- a/src/lsystem/screen/gl2d/KeyboardListener2D.java +++ /dev/null @@ -1,56 +0,0 @@ -package lsystem.screen.gl2d; - -import java.awt.event.KeyEvent; -import java.awt.event.KeyListener; - -public class KeyboardListener2D implements KeyListener { - - private final SwingGLCanvas2D canvas; - - - public KeyboardListener2D(SwingGLCanvas2D swingGLCanvas) { - this.canvas = swingGLCanvas; - } - - @Override - public void keyTyped(KeyEvent e) { - - switch (e.getKeyChar()) { - case 'z': - canvas.camera[2] -= 0.1f; - break; - case 's': - canvas.camera[2] += 0.1f; - break; - case 'q': - canvas.camera[0] -= 0.1f; - break; - case 'd': - canvas.camera[0] += 0.1f; - break; - case 'a': - canvas.camera[3] -= 1; - break; - case 'e': - canvas.camera[3] += 1; - break; - case 'w': - canvas.camera[1] += 0.1f; - break; - case 'x': - canvas.camera[1] -= 0.1f; - break; - } - } - - - @Override - public void keyPressed(KeyEvent e) { - - } - - @Override - public void keyReleased(KeyEvent e) { - - } -} diff --git a/src/lsystem/screen/gl2d/SwingGLCanvas2D.java b/src/lsystem/screen/gl2d/SwingGLCanvas2D.java index faa981e..9c93afc 100644 --- a/src/lsystem/screen/gl2d/SwingGLCanvas2D.java +++ b/src/lsystem/screen/gl2d/SwingGLCanvas2D.java @@ -7,12 +7,6 @@ public class SwingGLCanvas2D extends AbstractCanvas { @Override protected void addEventsListeners() { - glCanvas.addGLEventListener(new JoglEventListener2D(this)); - JoglMouseListener2D mouse = new JoglMouseListener2D(this); - glCanvas.addMouseListener(mouse); - glCanvas.addMouseMotionListener(mouse); - glCanvas.addMouseWheelListener(mouse); - glCanvas.addKeyListener(new KeyboardListener2D(this)); } } diff --git a/src/lsystem/screen/gl3d/DrawHelper.java b/src/lsystem/screen/gl3d/DrawHelper.java index 840525b..fa22d94 100644 --- a/src/lsystem/screen/gl3d/DrawHelper.java +++ b/src/lsystem/screen/gl3d/DrawHelper.java @@ -2,18 +2,12 @@ package lsystem.screen.gl3d; import com.jogamp.opengl.GL2; import com.jogamp.opengl.util.gl2.GLUT; +import lsystem.screen.AbstractCanvas; import lsystem.screen.gl2d.Point2; -import lsystem.screen.gl2d.SwingGLCanvas2D; public class DrawHelper { - public static void placeCamera(GL2 gl, GLCanvas canvas) { - gl.glRotatef(canvas.camera[4], 1f, 0f, 0f); - gl.glRotatef(canvas.camera[3], 0f, 1f, 0f); - gl.glRotatef(canvas.camera[5], 0f, 0f, 1f); - } - - public static void placeCamera(GL2 gl, SwingGLCanvas2D canvas) { + public static void placeCamera(GL2 gl, AbstractCanvas canvas) { gl.glRotatef(canvas.camera[4], 1f, 0f, 0f); gl.glRotatef(canvas.camera[3], 0f, 1f, 0f); gl.glRotatef(canvas.camera[5], 0f, 0f, 1f); @@ -86,15 +80,7 @@ public class DrawHelper { gl.glPopMatrix(); } - public static void drawDebugInformation(GL2 gl, GLUT glut, GLCanvas canvas) { - gl.glRasterPos3f(canvas.camera[0], canvas.camera[1], canvas.camera[2] - 1); - gl.glColor3f(1f, 1f, 1f); - glut.glutBitmapString(GLUT.BITMAP_HELVETICA_18, - "x=" + canvas.camera[0] + ", y=" + canvas.camera[1] + ", z=" + canvas.camera[2] + "\n yaw = " - + canvas.camera[3] + " pitch = " + canvas.camera[4] + " roll = " + canvas.camera[5]); - } - - public static void drawDebugInformation(GL2 gl, GLUT glut, SwingGLCanvas2D canvas) { + 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); glut.glutBitmapString(GLUT.BITMAP_HELVETICA_18, @@ -103,13 +89,13 @@ public class DrawHelper { } public static void drawStick(GL2 gl, Point2 origin, Point2 newOrigin) { - + gl.glBegin(GL2.GL_LINES); gl.glVertex2f(origin.getX(), origin.getY()); gl.glVertex2f(newOrigin.getX(), newOrigin.getY()); gl.glEnd(); } - - + + } diff --git a/src/lsystem/screen/gl3d/GLEventListener.java b/src/lsystem/screen/gl3d/GLEventListener.java index f336efc..3ae258b 100644 --- a/src/lsystem/screen/gl3d/GLEventListener.java +++ b/src/lsystem/screen/gl3d/GLEventListener.java @@ -2,24 +2,18 @@ package lsystem.screen.gl3d; import com.jogamp.opengl.GL2; 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; import lsystem.screen.Constants; +import lsystem.screen.AbstractListener; -public class GLEventListener implements com.jogamp.opengl.GLEventListener { +public class GLEventListener extends AbstractListener { - private final GLCanvas canvas; private final float[] light_0_position = {1000f, 1000f, 1000f, 1f}; - private final GLU glu; - private final GLUT glut; - private int fps; + public GLEventListener(GLCanvas swingGLCanvas) { - this.canvas = swingGLCanvas; - this.glu = canvas.glu; - this.glut = canvas.glut; + super(swingGLCanvas); } @@ -43,21 +37,7 @@ public class GLEventListener implements com.jogamp.opengl.GLEventListener { gl.glDepthFunc(GL2.GL_LEQUAL); gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL2.GL_NICEST); - new Thread(() -> { - while (canvas.frame.isVisible()) { - try { - Thread.sleep(1000); - } catch (InterruptedException e) { - e.printStackTrace(); - } - System.out.println(fps); - fps = 0; - } - }).start(); - } - - @Override - public void dispose(GLAutoDrawable glAutoDrawable) { + super.init(glAutoDrawable); } @Override @@ -74,7 +54,7 @@ public class GLEventListener implements com.jogamp.opengl.GLEventListener { gl.glPushMatrix(); gl.glRotatef(90f, -1f, 0f, 0f); gl.glColor3f(0f, 1f, 0f); - displayLSystem(gl, glut, canvas.getLSystem()); + drawLSystem(gl, canvas.getLSystem()); gl.glPopMatrix(); DrawHelper.drawAxes(gl, glut); @@ -82,8 +62,8 @@ public class GLEventListener implements com.jogamp.opengl.GLEventListener { gl.glFlush(); fps++; } - - private void displayLSystem(GL2 gl, GLUT glut, Element element) { + @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); @@ -93,25 +73,19 @@ public class GLEventListener implements com.jogamp.opengl.GLEventListener { 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); + drawLSystem(gl, child); } gl.glPopMatrix(); } @Override public void reshape(GLAutoDrawable glAutoDrawable, int x, int y, int width, int height) { + super.reshape(glAutoDrawable, x, y, width, height); GL2 gl = glAutoDrawable.getGL().getGL2(); - gl.glViewport(x, y, width, height); - - gl.glMatrixMode(GL2.GL_PROJECTION); - gl.glLoadIdentity(); - glu.gluPerspective(60.0f, (float) width / height, 0.1f, 1000.0f); - gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION, light_0_position, 0); gl.glColorMaterial(GL2.GL_FRONT_AND_BACK, GL2.GL_AMBIENT_AND_DIFFUSE); gl.glMateriali(GL2.GL_FRONT_AND_BACK, GL2.GL_SHININESS, 90); diff --git a/src/lsystem/screen/gl3d/GLMouseListener.java b/src/lsystem/screen/gl3d/GLMouseListener.java index 964f708..3291e63 100644 --- a/src/lsystem/screen/gl3d/GLMouseListener.java +++ b/src/lsystem/screen/gl3d/GLMouseListener.java @@ -1,14 +1,16 @@ package lsystem.screen.gl3d; import com.jogamp.opengl.glu.GLU; +import lsystem.screen.AbstractCanvas; import java.awt.*; import java.awt.event.*; public class GLMouseListener implements MouseListener, MouseMotionListener, MouseWheelListener { - private final GLU glu; - private final GLCanvas canvas; + protected final GLU glu; + protected final GLCanvas canvas; + private int button = 0; private Point origine; private final float MULTIPLIER = 0.25f; @@ -45,7 +47,6 @@ public class GLMouseListener implements MouseListener, MouseMotionListener, Mous @Override public void mouseExited(MouseEvent e) { - System.out.println("exited " + canvas.camera[0] + ", " + canvas.camera[1]); } @Override @@ -53,11 +54,6 @@ public class GLMouseListener implements MouseListener, MouseMotionListener, Mous if(origine != null) { double xDiff = origine.getX() - e.getPoint().getX(); double yDiff = origine.getY() - e.getPoint().getY(); - /* if(button == 1) { - canvas.camera[0] += Math.cos(canvas.camera[3]) * xDiff * 0.01; - canvas.camera[1] += Math.cos(canvas.camera[4]) * yDiff * 0.01; - canvas.camera[2] += Math.sin(canvas.camera[3]) * xDiff * 0.01; - } */ if(button == 3) { canvas.camera[3] += xDiff * 0.1; canvas.camera[4] += yDiff * 0.1; @@ -75,6 +71,5 @@ public class GLMouseListener implements MouseListener, MouseMotionListener, Mous canvas.camera[2] += Math.cos(Math.toRadians(canvas.camera[4]))*MULTIPLIER* e.getWheelRotation()*Math.cos(Math.toRadians(Math.abs(360-canvas.camera[3]))); canvas.camera[0] -= Math.cos(Math.toRadians(canvas.camera[4]))*MULTIPLIER* e.getWheelRotation()*Math.cos(Math.toRadians(Math.abs(450-canvas.camera[3]))); canvas.camera[1] += (1-Math.cos(Math.toRadians(canvas.camera[4])))*MULTIPLIER* e.getWheelRotation()*Math.cos(Math.toRadians(Math.abs(360-canvas.camera[4]))); - } }