From 1eda03f9650d8d029ebec5d5d54120f9ba422877 Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Sat, 20 Feb 2021 13:41:32 +0100 Subject: [PATCH] optimized import + added keyboard controls and fps log --- src/lsystem/Main.java | 6 --- src/lsystem/engine/Parser.java | 6 +-- src/lsystem/engine/Rewrite.java | 1 - src/lsystem/screen/DrawHelper.java | 52 ++++++++++++++++--- src/lsystem/screen/Jogl.java | 9 ++-- src/lsystem/screen/MainFrame.java | 6 +-- src/lsystem/screen/SwingGLCanvas.java | 17 +++++- src/lsystem/screen/Tab.java | 1 - .../screen/listener/JoglEventListener.java | 32 +++++++++--- .../screen/listener/JoglMouseListener.java | 23 ++++---- .../screen/listener/KeyboardListener.java | 20 ++++++- 11 files changed, 121 insertions(+), 52 deletions(-) diff --git a/src/lsystem/Main.java b/src/lsystem/Main.java index 582586e..50cfe09 100644 --- a/src/lsystem/Main.java +++ b/src/lsystem/Main.java @@ -1,11 +1,5 @@ package lsystem; -import lsystem.engine.Parser; -import lsystem.engine.Rewrite; - -import java.util.ArrayList; -import java.util.List; -import java.util.Scanner; import lsystem.screen.MainFrame; import lsystem.screen.SwingGLCanvas; diff --git a/src/lsystem/engine/Parser.java b/src/lsystem/engine/Parser.java index 13e54e9..3ecb8ab 100644 --- a/src/lsystem/engine/Parser.java +++ b/src/lsystem/engine/Parser.java @@ -1,11 +1,11 @@ package lsystem.engine; -import java.util.ArrayList; -import java.util.List; - import lsystem.Type; import lsystem.utils.Pair; +import java.util.ArrayList; +import java.util.List; + public class Parser { private final String axiom; diff --git a/src/lsystem/engine/Rewrite.java b/src/lsystem/engine/Rewrite.java index 6fc4696..7f77b1a 100644 --- a/src/lsystem/engine/Rewrite.java +++ b/src/lsystem/engine/Rewrite.java @@ -2,7 +2,6 @@ package lsystem.engine; import lsystem.utils.Pair; -import java.util.Arrays; import java.util.List; public class Rewrite { diff --git a/src/lsystem/screen/DrawHelper.java b/src/lsystem/screen/DrawHelper.java index 228147c..f5475fd 100644 --- a/src/lsystem/screen/DrawHelper.java +++ b/src/lsystem/screen/DrawHelper.java @@ -12,7 +12,7 @@ public class DrawHelper { gl.glRotatef(canvas.camera[5], 0f, 0f, 1f); } - public static void drawAxes(GL2 gl, GLUT glut){ + public static void drawAxes(GL2 gl, GLUT glut, SwingGLCanvas canvas){ gl.glDisable(GL2.GL_LIGHTING); gl.glDisable(GL2.GL_LIGHT0); gl.glRasterPos3f(0f, 0f, 0f); @@ -28,12 +28,12 @@ public class DrawHelper { gl.glVertex3f(0f, 0f, 1f); gl.glColor3f(1f, 1f, 1f); - for(int i = -5; i < 6; i++) { - gl.glVertex3f(-5f, 0f, i); - gl.glVertex3f(5f, 0f, i); + for(int i = -50; i < 51; i++) { + gl.glVertex3f(-50f, 0f, i); + gl.glVertex3f(50f, 0f, i); - gl.glVertex3f(i, 0, -5f); - gl.glVertex3f(i, 0, 5f); + gl.glVertex3f(i, 0, -50f); + gl.glVertex3f(i, 0, 50f); } gl.glEnd(); gl.glRasterPos3f(1.1f, 0.0f, 0.0f); @@ -44,8 +44,46 @@ public class DrawHelper { glut.glutBitmapCharacter(GLUT.BITMAP_HELVETICA_18, 'Z'); //draw the z axis } + public static void drawRectangular0Prism(GL2 gl, float leftBottomX, float leftBottomY, float leftBottomZ, float rightUpX, float rightUpY, float rightUpZ) { + gl.glPushMatrix(); + gl.glBegin(GL2.GL_TRIANGLES); + gl.glVertex3f(leftBottomX, leftBottomY, leftBottomZ); + gl.glVertex3f(rightUpX, leftBottomY, leftBottomZ); + gl.glVertex3f(rightUpX, rightUpY, leftBottomZ); + + gl.glVertex3f(leftBottomX, leftBottomY, leftBottomZ); + gl.glVertex3f(leftBottomX, rightUpY, leftBottomZ); + gl.glVertex3f(rightUpX, rightUpY, leftBottomZ); + + gl.glVertex3f(leftBottomX, leftBottomY, leftBottomZ); + gl.glVertex3f(leftBottomX, rightUpY, leftBottomZ); + gl.glVertex3f(leftBottomX, leftBottomY, rightUpZ); + + gl.glVertex3f(leftBottomX, rightUpY, leftBottomZ); + gl.glVertex3f(leftBottomX, rightUpY, rightUpZ); + gl.glVertex3f(leftBottomX, leftBottomY, rightUpZ); + + gl.glVertex3f(rightUpX, leftBottomY, leftBottomZ); + gl.glVertex3f(rightUpX, rightUpY, leftBottomZ); + gl.glVertex3f(rightUpX, leftBottomY, rightUpZ); + + gl.glVertex3f(rightUpX, rightUpY, leftBottomZ); + gl.glVertex3f(rightUpX, rightUpY, rightUpZ); + gl.glVertex3f(rightUpX, leftBottomY, rightUpZ); + + gl.glVertex3f(leftBottomX, leftBottomY, rightUpZ); + gl.glVertex3f(leftBottomX, rightUpY, rightUpZ); + gl.glVertex3f(rightUpX, leftBottomY, rightUpZ); + + gl.glVertex3f(rightUpX, leftBottomY, rightUpZ); + gl.glVertex3f(rightUpX, rightUpY, rightUpZ); + gl.glVertex3f(leftBottomX, rightUpY, rightUpZ); + gl.glEnd(); + gl.glPopMatrix(); + } + public static void drawDebugInformation(GL2 gl, GLU glu, GLUT glut, SwingGLCanvas canvas, int window_height, int window_width) { - gl.glRasterPos3f(0f, 0f, 2f); + 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]); diff --git a/src/lsystem/screen/Jogl.java b/src/lsystem/screen/Jogl.java index 27501f6..6a7608d 100644 --- a/src/lsystem/screen/Jogl.java +++ b/src/lsystem/screen/Jogl.java @@ -1,16 +1,15 @@ package lsystem.screen; -import java.awt.*; -import java.awt.event.*; - import com.jogamp.opengl.GL; import com.jogamp.opengl.GL2; import com.jogamp.opengl.GLAutoDrawable; import com.jogamp.opengl.GLEventListener; import com.jogamp.opengl.awt.GLCanvas; -import com.jogamp.opengl.glu.GLU; import com.jogamp.opengl.util.Animator; -import com.jogamp.opengl.util.gl2.GLUT; + +import java.awt.*; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; /** * Article1.java author: InfoRital diff --git a/src/lsystem/screen/MainFrame.java b/src/lsystem/screen/MainFrame.java index 5a2e3ec..7681cd5 100644 --- a/src/lsystem/screen/MainFrame.java +++ b/src/lsystem/screen/MainFrame.java @@ -3,12 +3,8 @@ package lsystem.screen; import lsystem.screen.listener.Listener; -import java.awt.*; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; - import javax.swing.*; +import java.awt.*; public class MainFrame extends JFrame { diff --git a/src/lsystem/screen/SwingGLCanvas.java b/src/lsystem/screen/SwingGLCanvas.java index f2ec172..90d66a9 100644 --- a/src/lsystem/screen/SwingGLCanvas.java +++ b/src/lsystem/screen/SwingGLCanvas.java @@ -9,19 +9,23 @@ import com.jogamp.opengl.util.gl2.GLUT; import lsystem.screen.listener.JoglEventListener; import lsystem.screen.listener.JoglMouseListener; import lsystem.screen.listener.KeyboardListener; +import lsystem.utils.Pair; import javax.swing.*; import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; +import java.util.LinkedList; +import java.util.Random; public class SwingGLCanvas { public final GLCanvas glCanvas; - public float camera[] = {0f, 1f, 5f, // camera pos x,y, z - 0f, 0f, 0f}; // camera rotation yaw(y-axis), pitch(z-axis), roll(x-axis) + public float[] camera = {0f, 1f, 5f, // camera pos x,y, z + 0f, 0f, 0f}; // camera rotation yaw(x-axis), pitch(y-axis), roll(z-axis) public GLU glu = new GLU(); public GLUT glut = new GLUT(); + public LinkedList> prismPosition = new LinkedList<>(); public SwingGLCanvas() { GLProfile glProfile = GLProfile.getDefault(); @@ -44,6 +48,15 @@ public class SwingGLCanvas { jframe.getContentPane().add(glCanvas, BorderLayout.CENTER); jframe.setSize(Constants.WIDTH, Constants.HEIGHT); + + for(int i = -50; i < 51; ++i) { + for(int j = -50; j < 51; ++j) { + if(new Random().nextFloat() < 0.05) { + prismPosition.add(new Pair(i, j)); + } + } + } + animator.start(); jframe.setVisible(true); } diff --git a/src/lsystem/screen/Tab.java b/src/lsystem/screen/Tab.java index f0ab9f3..b7d4870 100644 --- a/src/lsystem/screen/Tab.java +++ b/src/lsystem/screen/Tab.java @@ -6,7 +6,6 @@ import javax.swing.*; import java.awt.*; import java.util.ArrayList; import java.util.List; -import javax.swing.JTabbedPane; public class Tab extends JPanel{ diff --git a/src/lsystem/screen/listener/JoglEventListener.java b/src/lsystem/screen/listener/JoglEventListener.java index 63a3b3a..29e8b8a 100644 --- a/src/lsystem/screen/listener/JoglEventListener.java +++ b/src/lsystem/screen/listener/JoglEventListener.java @@ -15,7 +15,7 @@ public class JoglEventListener implements GLEventListener { private final float[] light_0_ambient = {0.01f, 0.01f, 0.01f, 0.01f}; private final float[] light_0_diffuse = {1.0f, 1.0f, 1.0f, 1.0f}; private final float[] light_0_specular = {1.0f,1.0f, 1.0f, 1.0f}; - private final float[] light_0_position = {100f, 0f, 10f, 1f}; + private final float[] light_0_position = {1000f, 1000f, 1000f, 1f}; private float[] material_specular = {0.8f, 0.8f, 0.8f, 0.8f}; @@ -25,6 +25,7 @@ public class JoglEventListener implements GLEventListener { private final GLUT glut; private int width; private int height; + private int fps; public JoglEventListener(SwingGLCanvas swingGLCanvas) { this.canvas = swingGLCanvas; @@ -38,7 +39,6 @@ public class JoglEventListener implements GLEventListener { GL2 gl = glAutoDrawable.getGL().getGL2(); gl.glClearColor(0f, 0f, 0f, 1.0f); - gl.glEnable(GL2.GL_DEPTH_TEST); gl.glEnable(GL2.GL_LIGHT0); gl.glEnable(GL2.GL_LIGHTING); @@ -54,6 +54,19 @@ public class JoglEventListener implements GLEventListener { gl.glDepthFunc(GL2.GL_LEQUAL); gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL2.GL_NICEST); + new Thread(() -> { + while (true) { + synchronized (this){ + try { + wait(1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + System.out.println(fps); + fps = 0; + } + } + }).start(); } @@ -72,6 +85,10 @@ public class JoglEventListener implements GLEventListener { // x, y, z, x of where the camera looks at, y of where the camera looks at, z of where the camera looks at glu.gluLookAt(canvas.camera[0], canvas.camera[1], canvas.camera[2], canvas.camera[0], canvas.camera[1], canvas.camera[2] - 1, 0f, 1f, 0f); DrawHelper.prepareDraw3D(gl, glut, canvas); + 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); + gl.glMaterialfv(GL2.GL_FRONT_AND_BACK, GL2.GL_SPECULAR, material_specular, 0); gl.glPushMatrix(); gl.glTranslatef(0f, 0f, -4f); @@ -89,16 +106,15 @@ public class JoglEventListener implements GLEventListener { angle += 0.1f; angle %= 360f; - gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION, light_0_position, 0); + gl.glColor3f(0, 144, 255); + canvas.prismPosition.forEach(pair -> + DrawHelper.drawRectangular0Prism(gl, pair.getLeft(), 0f, pair.getRight(), pair.getLeft() + 1, 1f, pair.getRight() + 1)); - gl.glColorMaterial(GL2.GL_FRONT_AND_BACK, GL2.GL_AMBIENT_AND_DIFFUSE); - gl.glMateriali(GL2.GL_FRONT_AND_BACK, GL2.GL_SHININESS, 90); - gl.glMaterialfv(GL2.GL_FRONT_AND_BACK, GL2.GL_SPECULAR, material_specular, 0); - DrawHelper.drawAxes(gl, glut); - DrawHelper.prepareDraw2D(gl, glut, canvas); + DrawHelper.drawAxes(gl, glut, canvas); DrawHelper.drawDebugInformation(gl, glu, glut, canvas, glAutoDrawable.getSurfaceHeight(), glAutoDrawable.getSurfaceWidth()); gl.glFlush(); + fps++; } @Override diff --git a/src/lsystem/screen/listener/JoglMouseListener.java b/src/lsystem/screen/listener/JoglMouseListener.java index a9c3ced..8838e25 100644 --- a/src/lsystem/screen/listener/JoglMouseListener.java +++ b/src/lsystem/screen/listener/JoglMouseListener.java @@ -1,7 +1,6 @@ package lsystem.screen.listener; import com.jogamp.opengl.glu.GLU; -import lsystem.screen.Constants; import lsystem.screen.SwingGLCanvas; import java.awt.*; @@ -52,24 +51,22 @@ public class JoglMouseListener implements MouseListener, MouseMotionListener, Mo @Override public void mouseDragged(MouseEvent e) { if(origine != null) { - double xDiff = (origine.getX() - e.getPoint().getX()) % 360; - double yDiff = (origine.getY() - e.getPoint().getY()) % 360; - if(button == 2) { - canvas.camera[0] += Math.cos(canvas.camera[4] % 360 - Math.PI / 2) * xDiff * 0.01; - canvas.camera[1] += Math.cos(canvas.camera[3] % 360) * yDiff * 0.01; - canvas.camera[2] += Math.sin(canvas.camera[4] % 360) * xDiff * 0.01; - } + 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; } - if(button == 1) { - // canvas.camera[0] += Math.cos(xDiff / Constants.WIDTH + Math.PI / 2) * 1.2; - // canvas.camera[1] += Math.sin(-yDiff / Constants.WIDTH); - // canvas.camera[2] += Math.sin(yDiff / Constants.HEIGHT) * 1.2; - } origine = e.getPoint(); } + for (int i = 0; i < canvas.camera.length; i++) { + canvas.camera[i] = canvas.camera[i] % 360; + } } @Override diff --git a/src/lsystem/screen/listener/KeyboardListener.java b/src/lsystem/screen/listener/KeyboardListener.java index 0d2273a..dcd8d26 100644 --- a/src/lsystem/screen/listener/KeyboardListener.java +++ b/src/lsystem/screen/listener/KeyboardListener.java @@ -9,25 +9,43 @@ public class KeyboardListener implements KeyListener { private final SwingGLCanvas canvas; + public KeyboardListener(SwingGLCanvas swingGLCanvas) { this.canvas = swingGLCanvas; } @Override public void keyTyped(KeyEvent e) { - System.out.println("typed" + e.getKeyCode()); + 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) {