diff --git a/src/lsystem/screen/DrawHelper.java b/src/lsystem/screen/DrawHelper.java new file mode 100644 index 0000000..228147c --- /dev/null +++ b/src/lsystem/screen/DrawHelper.java @@ -0,0 +1,69 @@ +package lsystem.screen; + +import com.jogamp.opengl.GL2; +import com.jogamp.opengl.glu.GLU; +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 drawAxes(GL2 gl, GLUT glut){ + gl.glDisable(GL2.GL_LIGHTING); + gl.glDisable(GL2.GL_LIGHT0); + gl.glRasterPos3f(0f, 0f, 0f); + gl.glColor3f(1f, 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, 0f, 1f); + + gl.glColor3f(1f, 1f, 1f); + for(int i = -5; i < 6; i++) { + gl.glVertex3f(-5f, 0f, i); + gl.glVertex3f(5f, 0f, i); + + gl.glVertex3f(i, 0, -5f); + gl.glVertex3f(i, 0, 5f); + } + 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 drawDebugInformation(GL2 gl, GLU glu, GLUT glut, SwingGLCanvas canvas, int window_height, int window_width) { + gl.glRasterPos3f(0f, 0f, 2f); + gl.glColor3f(1f,1f, 1f); + glut.glutBitmapString(GLUT.BITMAP_HELVETICA_18, + "x=" + canvas.camera[0] + ", y=" + canvas.camera[1] + ", z=" + canvas.camera[2]); + gl.glDepthMask(true); + } + + public static void prepareDraw2D(GL2 gl, GLUT glut, SwingGLCanvas canvas) { + gl.glDisable(GL2.GL_LIGHTING); + gl.glDisable(GL2.GL_LIGHT0); + } + + public static void prepareDraw3D(GL2 gl, GLUT glut, SwingGLCanvas canvas) { + gl.glEnable(GL2.GL_LIGHTING); + gl.glEnable(GL2.GL_LIGHT0); + gl.glEnable(GL2.GL_DEPTH_TEST); + } + + private static double distance(double x1, double y1, double z1, double x2, double y2, double z2) { + return Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2) + Math.pow(z1 - z2, 2)); + } +} diff --git a/src/lsystem/screen/JoglEventListener.java b/src/lsystem/screen/JoglEventListener.java deleted file mode 100644 index 2e342ef..0000000 --- a/src/lsystem/screen/JoglEventListener.java +++ /dev/null @@ -1,99 +0,0 @@ -package lsystem.screen; - -import com.jogamp.opengl.GL; -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; - -public class JoglEventListener implements GLEventListener { - - private GLU glu = new GLU(); - private GLUT glut = new GLUT(); - - private float camera [] = {0f, 0f, 5f}; - - private float [] light_0_ambient = {0.01f, 0.01f, 0.01f, 0.01f}; - private float [] light_0_diffuse = {1.0f, 1.0f, 1.0f, 1.0f}; - private float [] light_0_specular = {1.0f,1.0f, 1.0f, 1.0f}; - private float [] light_0_position = {100f, 0f, 10f, 1f}; - - private float [] material_specular = {1.0f, 1.0f, 1.0f, 1.0f}; - - private float angle = 0f; - - - @Override - public void init(GLAutoDrawable glAutoDrawable) { - GL2 gl = glAutoDrawable.getGL().getGL2(); - - gl.glClearColor(0.8f, 0.5f, 0.0f, 1.0f); - - gl.glEnable(GL2.GL_DEPTH_TEST); - gl.glClearDepth(1.0f); - - gl.glShadeModel(GL2.GL_SMOOTH); - gl.glEnable(GL2.GL_LIGHTING); - - gl.glEnable(GL2.GL_LIGHT0); - 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.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION, light_0_position, 0); - - gl.glEnable(GL2.GL_COLOR_MATERIAL); - 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); - - } - - @Override - public void dispose(GLAutoDrawable glAutoDrawable) { - - } - - @Override - public void display(GLAutoDrawable glAutoDrawable) { - GL2 gl = glAutoDrawable.getGL().getGL2(); - gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT); - gl.glLoadIdentity(); - - glu.gluLookAt( - camera[0], camera[1], camera[2], - 0.0f, 0.0f, 0.0f, - 0.0f, 1.0f, 0.0f - ); - - /* gl.glRotatef(angle, 0f, 1f, 0f); - - gl.glColor3f(1.0f, 0.0f, 0.0f); - glut.glutSolidSphere(1.0f, 20, 20); */ - gl.glColor3f(1f, 1f, 1f); - 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, 0f, 1f); - gl.glEnd(); - gl.glRasterPos3f(1.1f, 0.0f, 0.0f); - glut.glutBitmapCharacter(GLUT.BITMAP_HELVETICA_18, 'X'); - angle += 0.1f; - angle %= 360f; - } - - @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, 10.0f); - - gl.glMatrixMode(GL2.GL_MODELVIEW); - } -} diff --git a/src/lsystem/screen/SwingGLCanvas.java b/src/lsystem/screen/SwingGLCanvas.java index 98b5d1a..f2ec172 100644 --- a/src/lsystem/screen/SwingGLCanvas.java +++ b/src/lsystem/screen/SwingGLCanvas.java @@ -3,7 +3,12 @@ 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.JoglMouseListener; +import lsystem.screen.listener.KeyboardListener; import javax.swing.*; import java.awt.*; @@ -12,11 +17,22 @@ import java.awt.event.WindowEvent; 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 GLU glu = new GLU(); + public GLUT glut = new GLUT(); + public SwingGLCanvas() { GLProfile glProfile = GLProfile.getDefault(); GLCapabilities glCapabilities = new GLCapabilities(glProfile); - final GLCanvas glCanvas = new GLCanvas(glCapabilities); - glCanvas.addGLEventListener(new JoglEventListener()); + this.glCanvas = new GLCanvas(glCapabilities); + glCanvas.addGLEventListener(new JoglEventListener(this)); + JoglMouseListener mouse = new JoglMouseListener(this); + glCanvas.addMouseListener(mouse); + glCanvas.addMouseMotionListener(mouse); + glCanvas.addMouseWheelListener(mouse); + glCanvas.addKeyListener(new KeyboardListener(this)); final JFrame jframe = new JFrame("L-System"); final FPSAnimator animator = new FPSAnimator(glCanvas, 60); jframe.addWindowListener(new WindowAdapter() { diff --git a/src/lsystem/screen/listener/JoglEventListener.java b/src/lsystem/screen/listener/JoglEventListener.java new file mode 100644 index 0000000..63a3b3a --- /dev/null +++ b/src/lsystem/screen/listener/JoglEventListener.java @@ -0,0 +1,117 @@ +package lsystem.screen.listener; + +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.screen.DrawHelper; +import lsystem.screen.SwingGLCanvas; + +public class JoglEventListener implements GLEventListener { + + + private final SwingGLCanvas 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 = {100f, 0f, 10f, 1f}; + + private float[] material_specular = {0.8f, 0.8f, 0.8f, 0.8f}; + + private float angle = 0f; + + private final GLU glu; + private final GLUT glut; + private int width; + private int height; + + public JoglEventListener(SwingGLCanvas 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); + + } + + @Override + public void dispose(GLAutoDrawable glAutoDrawable) { + + } + + @Override + public void display(GLAutoDrawable glAutoDrawable) { + GL2 gl = glAutoDrawable.getGL().getGL2(); + gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT); + gl.glLoadIdentity(); + gl.glTranslatef(0, 0, 0); + DrawHelper.placeCamera(gl, canvas); + // 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.glPushMatrix(); + gl.glTranslatef(0f, 0f, -4f); + gl.glRotatef(angle, 0f, 1f, 0f); + gl.glColor3f(1.0f, 0.0f, 1.0f); + glut.glutSolidSphere(2f, 20, 20); + gl.glPopMatrix(); + + gl.glPushMatrix(); + gl.glTranslatef(2f, 0.75f, 1.25f); + gl.glRotatef(angle, 0f, 1f, 0f); + gl.glColor3f(0.5f, 0.0f, 1.0f); + glut.glutSolidSphere(0.75f, 20, 20); + gl.glPopMatrix(); + + angle += 0.1f; + angle %= 360f; + 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); + DrawHelper.drawAxes(gl, glut); + DrawHelper.prepareDraw2D(gl, glut, canvas); + DrawHelper.drawDebugInformation(gl, glu, glut, canvas, glAutoDrawable.getSurfaceHeight(), glAutoDrawable.getSurfaceWidth()); + gl.glFlush(); + } + + @Override + public void reshape(GLAutoDrawable glAutoDrawable, int x, int y, int width, int height) { + this.width = width; + this.height = 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.glMatrixMode(GL2.GL_MODELVIEW); + } +} diff --git a/src/lsystem/screen/listener/JoglMouseListener.java b/src/lsystem/screen/listener/JoglMouseListener.java new file mode 100644 index 0000000..a9c3ced --- /dev/null +++ b/src/lsystem/screen/listener/JoglMouseListener.java @@ -0,0 +1,86 @@ +package lsystem.screen.listener; + +import com.jogamp.opengl.glu.GLU; +import lsystem.screen.Constants; +import lsystem.screen.SwingGLCanvas; + +import java.awt.*; +import java.awt.event.*; + +public class JoglMouseListener implements MouseListener, MouseMotionListener, MouseWheelListener { + + private final GLU glu; + private final SwingGLCanvas canvas; + private int button = 0; + private Point origine; + + public JoglMouseListener(SwingGLCanvas 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()) % 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; + } + 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(); + } + } + + @Override + public void mouseMoved(MouseEvent e) { + } + + @Override + public void mouseWheelMoved(MouseWheelEvent e) { + canvas.camera[0] += e.getWheelRotation() * 0.25 * canvas.camera[3]; + canvas.camera[1] += e.getWheelRotation() * 0.25 * canvas.camera[4]; + + canvas.camera[2] += Math.sin(e.getWheelRotation() + Math.PI / 2) * 0.25; + } +} diff --git a/src/lsystem/screen/listener/KeyboardListener.java b/src/lsystem/screen/listener/KeyboardListener.java new file mode 100644 index 0000000..0d2273a --- /dev/null +++ b/src/lsystem/screen/listener/KeyboardListener.java @@ -0,0 +1,40 @@ +package lsystem.screen.listener; + +import lsystem.screen.SwingGLCanvas; + +import java.awt.event.KeyEvent; +import java.awt.event.KeyListener; + +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': + break; + case 's': + break; + case 'q': + break; + case 'd': + break; + } + } + + @Override + public void keyPressed(KeyEvent e) { + + } + + @Override + public void keyReleased(KeyEvent e) { + + } +}