package demos.misc; //================================================================================= // Picking 0.2 (Thomas Bladh) //================================================================================= // A simple picking example using java/jogl. This is far from a complete solution // but it should give you an idea of how to include picking in your assigment // solutions. // // Notes: * Based on example 13-3 (p 542) in the "OpenGL Programming Guide" // * This version should handle overlapping objects correctly. //--------------------------------------------------------------------------------- import com.jogamp.gluegen.runtime.Buffers; import java.awt.*; import java.awt.event.*; import java.awt.Canvas.*; import java.nio.*; import javax.media.opengl.*; import javax.media.opengl.awt.GLCanvas; import javax.media.opengl.glu.*; import com.jogamp.opengl.util.Animator; public class Picking { public static void main(String[] args) { new Picking(); } Picking() { Frame frame = new Frame("Picking Example"); GLCapabilities capabilities = new GLCapabilities(null); GLDrawableFactory factory = GLDrawableFactory.getFactory(capabilities.getGLProfile()); GLCanvas drawable = new GLCanvas(capabilities); final Renderer renderer = new Renderer(); drawable.addGLEventListener(renderer); drawable.addMouseListener(renderer); drawable.addMouseMotionListener(renderer); frame.add(drawable); frame.setSize(400, 400); final Animator animator = new Animator(drawable); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { animator.stop(); System.exit(0); } }); frame.setVisible(true); animator.start(); } static class Renderer implements GLEventListener, MouseListener, MouseMotionListener { static final int NOTHING = 0, UPDATE = 1, SELECT = 2; int cmd = UPDATE; int mouse_x, mouse_y; private GLU glu = new GLU(); private GLAutoDrawable gldrawable; public void init(GLAutoDrawable drawable) { GL2 gl = drawable.getGL().getGL2(); this.gldrawable = drawable; gl.glEnable(GL2.GL_CULL_FACE); gl.glEnable(GL2.GL_DEPTH_TEST); gl.glEnable(GL2.GL_NORMALIZE); gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); } public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { GL2 gl = drawable.getGL().getGL2(); float h = (float) height / (float) width; gl.glViewport(0, 0, width, height); gl.glMatrixMode(GL2.GL_PROJECTION); gl.glLoadIdentity(); glu.gluOrtho2D(0.0f,1.0f,0.0f,1.0f); } public void dispose(GLAutoDrawable drawable) { } public void display(GLAutoDrawable drawable) { GL2 gl = drawable.getGL().getGL2(); switch(cmd) { case UPDATE: drawScene(gl); break; case SELECT: int buffsize = 512; double x = (double) mouse_x, y = (double) mouse_y; int[] viewPort = new int[4]; IntBuffer selectBuffer = Buffers.newDirectIntBuffer(buffsize); int hits = 0; gl.glGetIntegerv(GL2.GL_VIEWPORT, viewPort, 0); gl.glSelectBuffer(buffsize, selectBuffer); gl.glRenderMode(GL2.GL_SELECT); gl.glInitNames(); gl.glMatrixMode(GL2.GL_PROJECTION); gl.glPushMatrix(); gl.glLoadIdentity(); glu.gluPickMatrix(x, (double) viewPort[3] - y, 5.0d, 5.0d, viewPort, 0); glu.gluOrtho2D(0.0d, 1.0d, 0.0d, 1.0d); drawScene(gl); gl.glMatrixMode(GL2.GL_PROJECTION); gl.glPopMatrix(); gl.glFlush(); hits = gl.glRenderMode(GL2.GL_RENDER); processHits(hits, selectBuffer); cmd = UPDATE; break; } } public void processHits(int hits, IntBuffer buffer) { System.out.println("---------------------------------"); System.out.println(" HITS: " + hits); int offset = 0; int names; float z1, z2; for (int i=0;i