package de.wb3player.render.jogl; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.nio.FloatBuffer; import javax.media.opengl.GL; import javax.media.opengl.GL2; import javax.media.opengl.GL2ES1; import javax.media.opengl.GLAutoDrawable; import javax.media.opengl.GLCapabilities; import javax.media.opengl.GLEventListener; import javax.media.opengl.GLProfile; import javax.media.opengl.awt.GLCanvas; import javax.media.opengl.fixedfunc.GLLightingFunc; import javax.media.opengl.glu.GLU; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPopupMenu; import javax.vecmath.Vector3d; import com.jogamp.opengl.util.FPSAnimator; import com.jogamp.opengl.util.GLBuffers; import com.jogamp.opengl.util.gl2.GLUT; import de.wb3player.controller.WB3Controller; import de.wb3player.fileaccess.output.drawstyle.DrawableMethods; import de.wb3player.fileaccess.output.drawstyle.jogl.AbstractJoglDrawableMethods; import de.wb3player.model.ModelManager; import de.wb3player.model.exception.DrawableMethodsException; import de.wb3player.render.menu.JoglMenuBar; import de.wb3player.render.menu.ObjectTreeFrame; public class JoglWindow extends JFrame implements GLEventListener { /** * */ private static final long serialVersionUID = 4207632556046308249L; private static final int FPS = 60; // Animator's target frames per second private int width; private int height; private JoglMenuBar menuBar; long lastTime = 0; int delta = 0; private GL2 gl; private GLU glu; private GLUT glut; private final FloatBuffer mouseZ = GLBuffers.newDirectFloatBuffer(1); private Vector3d pickPosition = null; public JoglWindow(int posX, int posY, int width, int height) { super(); this.width = width; this.height = height; this.setTitle("WB3 Player"); // Get the default OpenGL profile that best reflect your running platform. GLProfile glProfile = GLProfile.getDefault(); // Specifies a set of OpenGL capabilities, based on your profile. GLCapabilities glCapabilities = new GLCapabilities(glProfile); glCapabilities.setDoubleBuffered(true); glCapabilities.setNumSamples(4); glCapabilities.setSampleBuffers(true); // Allocate a GLDrawable, based on your OpenGL capabilities. final GLCanvas canvas = new GLCanvas(glCapabilities); final FPSAnimator fpsAnimator = new FPSAnimator(canvas, FPS); this.addMouseListener(WB3Controller.getInstance()); this.addMouseMotionListener(WB3Controller.getInstance()); this.addMouseWheelListener(WB3Controller.getInstance()); this.addKeyListener(WB3Controller.getInstance()); canvas.addGLEventListener(this); canvas.addMouseListener(WB3Controller.getInstance()); canvas.addMouseMotionListener(WB3Controller.getInstance()); canvas.addMouseWheelListener(WB3Controller.getInstance()); canvas.addKeyListener(WB3Controller.getInstance()); this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); //make menus appear above GLCanvas JPopupMenu.setDefaultLightWeightPopupEnabled(false); //add the menu bar this.menuBar = new JoglMenuBar(this); this.setJMenuBar(this.menuBar); this.add(canvas); this.setBounds(posX, posY, width, height); this.addWindowListener(new WindowAdapter() { @Override public void windowClosing(final WindowEvent e) { new Thread(new Runnable() { public void run() { fpsAnimator.stop(); System.exit(0); } }).start(); } }); // this.setLocationRelativeTo(null); this.setVisible(true); fpsAnimator.start(); } @Override public void display(GLAutoDrawable drawable) { gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); gl.glLoadIdentity(); try { ModelManager.getInstance().draw(); } catch (DrawableMethodsException e) { e.printStackTrace(); } gl.glPushMatrix(); gl.glTranslated(0.0, 0.0, 20.0); gl.glRotated(90.0, 1.0, 0.0, 0.0); glut.glutSolidTeapot(4.0); gl.glPopMatrix(); setCamera(gl, glu); ModelManager.getInstance().getAdvancedCameraDirector().update(); pick(WB3Controller.getInstance().getCurrentX(), WB3Controller.getInstance().getCurrentY()); } @Override public void dispose(GLAutoDrawable drawable) { // TODO Auto-generated method stub } @Override public void init(GLAutoDrawable drawable) { gl = drawable.getGL().getGL2(); glu = new GLU(); glut = new GLUT(); // Enable smooth shading, which blends colors nicely, and smoothes out lighting. gl.glShadeModel(GLLightingFunc.GL_SMOOTH); // Set background color in RGBA. Alpha: 0 (transparent) 1 (opaque) gl.glClearColor(1.0f, 1.0f, 1.0f, 0.0f); // Setup the depth buffer and enable the depth testing gl.glClearDepth(1.0f); // clear z-buffer to the farthest gl.glEnable(GL.GL_DEPTH_TEST); // enables depth testing gl.glDepthFunc(GL.GL_LEQUAL); // the type of depth test to do // Do the best perspective correction gl.glHint(GL2ES1.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST); // gl.glEnable(GL.GL_LINE_SMOOTH); //enables line smoothing // gl.glHint(GL.GL_LINE_SMOOTH_HINT, GL.GL_DONT_CARE); for (DrawableMethods drawableMethods : ModelManager.getInstance().getDrawableMethods()) { if (drawableMethods instanceof AbstractJoglDrawableMethods) { ((AbstractJoglDrawableMethods)drawableMethods).setGL(gl, glu); } } this.setLight(gl); } @Override public void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, int height) { this.width = width; this.height = height; if (height <= 0) height = 1; setCamera(gl, glu); gl.glMatrixMode(GL2.GL_MODELVIEW); gl.glLoadIdentity(); } private void setCamera(GL2 gl, GLU glu) { // Change to projection matrix. gl.glMatrixMode(GL2.GL_PROJECTION); gl.glLoadIdentity(); // Perspective. gl.glViewport(0, 0, width, height); // Reset The Current Viewport And Perspective Transformation glu.gluPerspective(45.0f, (float)width/height, 0.1f, 100000.0f); // Calculate The Aspect Ratio Of The Window glu.gluLookAt( ModelManager.getInstance().getCamera().getEyePosX(), ModelManager.getInstance().getCamera().getEyePosY(), ModelManager.getInstance().getCamera().getEyePosZ(), ModelManager.getInstance().getCamera().getLookAtX(), ModelManager.getInstance().getCamera().getLookAtY(), ModelManager.getInstance().getCamera().getLookAtZ(), ModelManager.getInstance().getCamera().getUpVectorX(), ModelManager.getInstance().getCamera().getUpVectorY(), ModelManager.getInstance().getCamera().getUpVectorZ()); // Change back to model view matrix. gl.glMatrixMode(GL2.GL_MODELVIEW); gl.glLoadIdentity(); } private void setLight(GL2 gl) { // Set up lighting float[] lightAmbient = {0.9f, 0.9f, 0.9f, 0.9f}; float[] lightDiffuse = {0.9f, 0.9f, 0.9f, 0.9f}; float[] lightSpecular = {0.7f, 0.7f, 0.7f, 1.0f}; float[] lightPosition = {100.0f, -100.0f, 200.0f, 1.0f}; gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_AMBIENT, lightAmbient, 0); gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_DIFFUSE, lightDiffuse, 0); gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_SPECULAR, lightSpecular, 0); gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION, lightPosition, 0); gl.glEnable(GL2.GL_LIGHTING); gl.glEnable(GL2.GL_LIGHT0); gl.glEnable(GL.GL_DEPTH_TEST); gl.glEnable(GL2.GL_SMOOTH); //gl.glEnable(GL.GL_POLYGON_OFFSET_FILL); } public void showMessageDialog(String text) { JOptionPane.showMessageDialog(this,text); } public void showObjectTree() { ObjectTreeFrame objectTreeFrame = new ObjectTreeFrame(); objectTreeFrame.setVisible(true); } private void pick(int mouseX, int mouseY) { final int[] vp = new int[4]; final double[] mv = new double[16]; final double[] p = new double[16]; final double[] result = new double[3]; gl.glGetIntegerv(GL.GL_VIEWPORT, vp, 0); gl.glGetDoublev(GL2.GL_MODELVIEW_MATRIX, mv, 0); gl.glGetDoublev(GL2.GL_PROJECTION_MATRIX, p, 0); gl.glReadPixels(mouseX, mouseY, 1, 1, GL2.GL_DEPTH_COMPONENT, GL.GL_FLOAT, mouseZ); glu.gluUnProject(mouseX, mouseY, mouseZ.get(0), mv, 0, p, 0, vp, 0, result, 0); Vector3d resultPosition = new Vector3d(result[0],result[1],result[2]); this.pickPosition = new Vector3d(resultPosition.x, resultPosition.y, resultPosition.z); } public Vector3d pick() { return this.pickPosition; } }