From 9a156f8d118ae0b3424f0b8533ae396d7db0dbf1 Mon Sep 17 00:00:00 2001 From: Antonin Boyon Date: Tue, 2 Mar 2021 18:31:57 +0100 Subject: [PATCH] 2D engine --- src/lsystem/Main2D.java | 17 ++ src/lsystem/screen/Constants.java | 2 +- src/lsystem/screen/DrawHelper.java | 206 +++++++++++++----- src/lsystem/screen/Jogl.java | 28 +-- src/lsystem/screen/SwingGLCanvas2D.java | 53 +++++ .../screen/listener/JoglEventListener2D.java | 113 ++++++++++ .../screen/listener/JoglMouseListener2D.java | 90 ++++++++ .../screen/listener/KeyboardListener2D.java | 59 +++++ 8 files changed, 496 insertions(+), 72 deletions(-) create mode 100644 src/lsystem/Main2D.java create mode 100644 src/lsystem/screen/SwingGLCanvas2D.java create mode 100644 src/lsystem/screen/listener/JoglEventListener2D.java create mode 100644 src/lsystem/screen/listener/JoglMouseListener2D.java create mode 100644 src/lsystem/screen/listener/KeyboardListener2D.java diff --git a/src/lsystem/Main2D.java b/src/lsystem/Main2D.java new file mode 100644 index 0000000..5ff480a --- /dev/null +++ b/src/lsystem/Main2D.java @@ -0,0 +1,17 @@ +package lsystem; + +import lsystem.screen.MainFrame; +import lsystem.screen.SwingGLCanvas; +import lsystem.screen.SwingGLCanvas2D; + +public class Main2D { + + public static void main(String[] args) { + + MainFrame frame = new MainFrame(); + frame.setVisible(true); + SwingGLCanvas2D canvas = new SwingGLCanvas2D(); + + } + +} diff --git a/src/lsystem/screen/Constants.java b/src/lsystem/screen/Constants.java index 8c3dd04..f3882b7 100644 --- a/src/lsystem/screen/Constants.java +++ b/src/lsystem/screen/Constants.java @@ -1,4 +1,4 @@ -package lsystem.screen; + package lsystem.screen; public class Constants { diff --git a/src/lsystem/screen/DrawHelper.java b/src/lsystem/screen/DrawHelper.java index e3f8c6d..fbfac5b 100644 --- a/src/lsystem/screen/DrawHelper.java +++ b/src/lsystem/screen/DrawHelper.java @@ -5,72 +5,162 @@ import com.jogamp.opengl.util.gl2.GLUT; public class DrawHelper { - public static void placeCamera(GL2 gl, SwingGLCanvas 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, SwingGLCanvas 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) { + 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 drawAxes(GL2 gl, GLUT glut){ - gl.glRasterPos3f(0f, 0f, 0f); - gl.glColor3f(255f, 0f, 0f); - gl.glBegin(GL2.GL_LINES); - gl.glVertex3f(0f, 0f, 0f); - gl.glVertex3f(1f, 0f, 0f); + public static void drawAxes(GL2 gl, GLUT glut) { + gl.glRasterPos3f(0f, 0f, 0f); + gl.glColor3f(255f, 0f, 0f); + gl.glBegin(GL2.GL_LINES); + gl.glVertex3f(0f, 0f, 0f); + gl.glVertex3f(1f, 0f, 0f); - gl.glVertex3f(0f, 0f, 0f); - gl.glVertex3f(0f, 1f, 0f); + gl.glVertex3f(0f, 0f, 0f); + gl.glVertex3f(0f, 1f, 0f); - gl.glVertex3f(0f, 0f, 0f); - gl.glVertex3f(0f, 0f, 1f); + gl.glVertex3f(0f, 0f, 0f); + gl.glVertex3f(0f, 0f, 1f); - gl.glColor3f(255f, 255f, 255f); - for(int i = -25; i < 26; i++) { - gl.glVertex3f(-25f, 0f, i); - gl.glVertex3f(25f, 0f, i); + gl.glColor3f(255f, 255f, 255f); + for (int i = -25; i < 26; i++) { + gl.glVertex3f(-25f, 0f, i); + gl.glVertex3f(25f, 0f, i); - gl.glVertex3f(i, 0, -25f); - gl.glVertex3f(i, 0, 25f); - } - gl.glEnd(); - gl.glRasterPos3f(1.1f, 0.0f, 0.0f); - glut.glutBitmapCharacter(GLUT.BITMAP_HELVETICA_18, 'X'); - gl.glRasterPos3f(0.0f, 1.1f, 0.0f); - glut.glutBitmapCharacter(GLUT.BITMAP_HELVETICA_18, 'Y'); //draw the y axis - gl.glRasterPos3f(0.0f, 0.0f, 1.1f); - glut.glutBitmapCharacter(GLUT.BITMAP_HELVETICA_18, 'Z'); //draw the z axis - } + gl.glVertex3f(i, 0, -25f); + gl.glVertex3f(i, 0, 25f); + } + gl.glEnd(); + gl.glRasterPos3f(1.1f, 0.0f, 0.0f); + glut.glutBitmapCharacter(GLUT.BITMAP_HELVETICA_18, 'X'); + gl.glRasterPos3f(0.0f, 1.1f, 0.0f); + glut.glutBitmapCharacter(GLUT.BITMAP_HELVETICA_18, 'Y'); // draw the y axis + gl.glRasterPos3f(0.0f, 0.0f, 1.1f); + 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) { - 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); + public static void drawRectangularPrism(GL2 gl, float leftBottomX, float leftBottomY, float leftBottomZ, + float rightUpX, float rightUpY, float rightUpZ) { + 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.glVertex3f(leftBottomX, leftBottomY, leftBottomZ); - gl.glVertex3f(leftBottomX, leftBottomY, rightUpZ); - gl.glVertex3f(leftBottomX, rightUpY, rightUpZ); - gl.glVertex3f(leftBottomX, rightUpY, leftBottomZ); + gl.glVertex3f(leftBottomX, leftBottomY, leftBottomZ); + gl.glVertex3f(leftBottomX, leftBottomY, rightUpZ); + gl.glVertex3f(leftBottomX, rightUpY, rightUpZ); + gl.glVertex3f(leftBottomX, rightUpY, leftBottomZ); - gl.glVertex3f(rightUpX, leftBottomY, leftBottomZ); - gl.glVertex3f(rightUpX, leftBottomY, rightUpZ); - gl.glVertex3f(rightUpX, rightUpY, rightUpZ); - gl.glVertex3f(rightUpX, rightUpY, leftBottomZ); + 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.glEnd(); - gl.glPopMatrix(); - } + gl.glVertex3f(leftBottomX, 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, GLUT glut, SwingGLCanvas 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) { + 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 drawStick(GL2 gl, float echelle, float x, float y, int angle) { + angle = angle - ((angle / 360) * 360); + switch (angle) { + case -315: + angle = 45; + ; + case -270: + angle = 90; + ; + case -225: + angle = 135; + ; + case -180: + angle = 180; + ; + case -135: + angle = 225; + ; + case -90: + angle = 270; + ; + case -45: + angle = 315; + ; + } + + // Direction + int newX=0, newY=0; + switch (angle) { + case 0: + newX = 1; + newY = 1; + break; + case 45: + newX = 1; + newY = 0; + break; + case 90: + newX = 1; + newY = -1; + break; + case 135: + newX = 0; + newY = -1; + break; + case 180: + newX = -1; + newY = -1; + break; + case 225: + newX = -1; + newY = 0; + break; + case 270: + newX = -1; + newY = 1; + break; + case 315: + newX = 0; + newY = 1; + break; + } + gl.glBegin(GL2.GL_LINES); + gl.glVertex2f(x, y); + gl.glVertex2f(x + (echelle * newX), y + (echelle * newY)); + gl.glEnd(); + /* System.out.println("X : " +(x + (echelle * newX))); + System.out.println("Y : " +(y + (echelle * newY))); + System.out.println("angle : " +angle); + System.out.println("newX : " +newX); + System.out.println("newY : " +newY);*/ + } - public static void drawDebugInformation(GL2 gl, GLUT glut, SwingGLCanvas 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]); - } } diff --git a/src/lsystem/screen/Jogl.java b/src/lsystem/screen/Jogl.java index 6a7608d..ee9da97 100644 --- a/src/lsystem/screen/Jogl.java +++ b/src/lsystem/screen/Jogl.java @@ -73,19 +73,21 @@ public class Jogl implements GLEventListener { // display() sera appelée en boucle tout au long de l'application par la classe Animator. // C'est dans cette fonction qu'on fera tout ce qui doit être affiché public void display(GLAutoDrawable drawable) { - final GL2 gl = drawable.getGL().getGL2(); - - gl.glBegin(GL2.GL_LINES); - // 2eme argument : angle - gl.glVertex2f(-0.25f, -0.25f); - gl.glVertex2f(0.5f, 0.15f); - gl.glEnd(); - - gl.glBegin(GL2.GL_LINES); - gl.glVertex2f(0.5f, 0.15f); - gl.glVertex2f(0.8f, 0.8f); - gl.glEnd(); - + GL2 gl = drawable.getGL().getGL2(); + + float xDefault = -1.0f, yDefault = -1.0f; + + /*gl.glBegin(GL2.GL_LINES); + gl.glVertex2f(xDefault, yDefault); + gl.glVertex2f(xDefault + (0.1f * 1), yDefault + (0.1f * 1)); + gl.glEnd(); + + gl.glBegin(GL2.GL_LINES); + gl.glVertex2f(xDefault + (0.1f * 1), -1.1f); + gl.glVertex2f(xDefault + (0.1f * 1) + (0.2f*1), yDefault + (0.1f * 1) + (0.2f*-1)); + gl.glEnd();*/ + DrawHelper.drawStick(gl, 0.1f, xDefault, yDefault, 0); + DrawHelper.drawStick(gl, 0.2f, -0.9f, -0.9f, 90); } /** diff --git a/src/lsystem/screen/SwingGLCanvas2D.java b/src/lsystem/screen/SwingGLCanvas2D.java new file mode 100644 index 0000000..0c23d46 --- /dev/null +++ b/src/lsystem/screen/SwingGLCanvas2D.java @@ -0,0 +1,53 @@ +package lsystem.screen; + +import com.jogamp.opengl.GLCapabilities; +import com.jogamp.opengl.GLProfile; +import com.jogamp.opengl.awt.GLCanvas; +import com.jogamp.opengl.glu.GLU; +import com.jogamp.opengl.util.FPSAnimator; +import com.jogamp.opengl.util.gl2.GLUT; +import lsystem.screen.listener.JoglEventListener; +import lsystem.screen.listener.JoglEventListener2D; +import lsystem.screen.listener.JoglMouseListener; +import lsystem.screen.listener.JoglMouseListener2D; +import lsystem.screen.listener.KeyboardListener; +import lsystem.screen.listener.KeyboardListener2D; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; +public class SwingGLCanvas2D { + + public final GLCanvas glCanvas; + 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 SwingGLCanvas2D() { + GLProfile glProfile = GLProfile.getDefault(); + GLCapabilities glCapabilities = new GLCapabilities(glProfile); + this.glCanvas = new GLCanvas(glCapabilities); + glCanvas.addGLEventListener(new JoglEventListener2D(this)); + JoglMouseListener2D mouse = new JoglMouseListener2D(this); + glCanvas.addMouseListener(mouse); + glCanvas.addMouseMotionListener(mouse); + glCanvas.addMouseWheelListener(mouse); + glCanvas.addKeyListener(new KeyboardListener2D(this)); + final JFrame jframe = new JFrame("L-System"); + final FPSAnimator animator = new FPSAnimator(glCanvas, 60); + jframe.addWindowListener(new WindowAdapter() { + @Override + public void windowClosing(WindowEvent e) { + jframe.dispose(); + } + }); + + jframe.getContentPane().add(glCanvas, BorderLayout.CENTER); + jframe.setSize(Constants.WIDTH, Constants.HEIGHT); + + animator.start(); + jframe.setVisible(true); + } +} diff --git a/src/lsystem/screen/listener/JoglEventListener2D.java b/src/lsystem/screen/listener/JoglEventListener2D.java new file mode 100644 index 0000000..d6127e3 --- /dev/null +++ b/src/lsystem/screen/listener/JoglEventListener2D.java @@ -0,0 +1,113 @@ +package lsystem.screen.listener; + +import com.jogamp.opengl.*; +import com.jogamp.opengl.glu.GLU; +import com.jogamp.opengl.util.gl2.GLUT; +import lsystem.screen.DrawHelper; +import lsystem.screen.SwingGLCanvas; +import lsystem.screen.SwingGLCanvas2D; +import lsystem.utils.Pair; + +import java.util.LinkedList; +import java.util.Random; + +public class JoglEventListener2D implements GLEventListener { + + private final SwingGLCanvas2D canvas; + 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 = {1000f, 1000f, 1000f, 1f}; + + private final float[] material_specular = {0.8f, 0.8f, 0.8f, 0.8f}; + + private float angle = 0f; + private final LinkedList> prismPosition = new LinkedList<>(); + + private final GLU glu; + private final GLUT glut; + private int fps; + + public JoglEventListener2D(SwingGLCanvas2D 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); + gl.glEnable(GL2.GL_DEPTH_TEST); + gl.glEnable(GL2.GL_LIGHT0); + gl.glEnable(GL2.GL_LIGHTING); + gl.glEnable(GL2.GL_NORMALIZE); + gl.glEnable(GL2.GL_COLOR_MATERIAL); + + gl.glClearDepth(1.0f); + gl.glShadeModel(GL2.GL_SMOOTH); + + gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_AMBIENT, light_0_ambient, 0); + gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_DIFFUSE, light_0_diffuse, 0); + gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_SPECULAR, light_0_specular, 0); + + gl.glDepthFunc(GL2.GL_LEQUAL); + gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL2.GL_NICEST); + 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)); + } + } + } + System.out.println(prismPosition.size() * 8); + new Thread(() -> { + while (true) { + synchronized (this){ + try { + wait(1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + System.out.println(fps); + fps = 0; + } + } + }).start(); + } + + @Override + public void dispose(GLAutoDrawable glAutoDrawable) { + + } + + @Override + public void display (GLAutoDrawable glAutoDrawable) { + GL2 gl = glAutoDrawable.getGL().getGL2(); + + float xDefault = -1.0f, yDefault = -1.0f; + + + DrawHelper.drawStick(gl, 0.1f, xDefault, yDefault, 0); + DrawHelper.drawStick(gl, 0.2f, 1.1f, 1.1f, 90); + } + + @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); + + 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.glMatrixMode(GL2.GL_MODELVIEW); + } +} diff --git a/src/lsystem/screen/listener/JoglMouseListener2D.java b/src/lsystem/screen/listener/JoglMouseListener2D.java new file mode 100644 index 0000000..a1111b0 --- /dev/null +++ b/src/lsystem/screen/listener/JoglMouseListener2D.java @@ -0,0 +1,90 @@ +package lsystem.screen.listener; + +import com.jogamp.opengl.glu.GLU; +import lsystem.screen.SwingGLCanvas; +import lsystem.screen.SwingGLCanvas2D; + +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/listener/KeyboardListener2D.java b/src/lsystem/screen/listener/KeyboardListener2D.java new file mode 100644 index 0000000..7a42ca2 --- /dev/null +++ b/src/lsystem/screen/listener/KeyboardListener2D.java @@ -0,0 +1,59 @@ +package lsystem.screen.listener; + +import lsystem.screen.SwingGLCanvas; +import lsystem.screen.SwingGLCanvas2D; + +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) { + + } +}