[Bprocessor-commit] gl/src/net/sourceforge/bprocessor/gl/view View3D.java,1.2,1.3
Status: Pre-Alpha
Brought to you by:
henryml
From: Nordholt <nor...@us...> - 2005-08-15 13:00:55
|
Update of /cvsroot/bprocessor/gl/src/net/sourceforge/bprocessor/gl/view In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv19284 Modified Files: View3D.java Log Message: Implemented the getObjectAtPoint method to support selection in 3D Index: View3D.java =================================================================== RCS file: /cvsroot/bprocessor/gl/src/net/sourceforge/bprocessor/gl/view/View3D.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** View3D.java 5 Aug 2005 11:02:31 -0000 1.2 --- View3D.java 15 Aug 2005 13:00:46 -0000 1.3 *************** *** 11,15 **** import net.java.games.jogl.GL; import net.java.games.jogl.GLDrawable; ! import net.java.games.jogl.GLU; import org.apache.log4j.Logger; --- 11,26 ---- import net.java.games.jogl.GL; import net.java.games.jogl.GLDrawable; ! //import net.java.games.jogl.GLU; ! import net.java.games.jogl.util.BufferUtils; ! ! ! import net.sourceforge.bprocessor.model.Edge; ! import net.sourceforge.bprocessor.model.EdgeFacade; ! import net.sourceforge.bprocessor.model.Vertex; ! ! import java.util.Iterator; ! import java.util.Set; ! ! import java.nio.IntBuffer; import org.apache.log4j.Logger; *************** *** 30,33 **** --- 41,53 ---- /** The rotation about the Y axis */ private static double rotationY = -30; + + /** The picking state */ + private int picking = 0; + + /** x coordinate */ + private double x; + + /** y coordinate */ + private double y; /** *************** *** 48,57 **** } gl = gld.getGL(); ! GLU glu = gld.getGLU(); ! ! gl.glShadeModel(GL.GL_FLAT); gl.glMatrixMode(GL.GL_PROJECTION); gl.glLoadIdentity(); double aspect = width / height; if (aspect < 1.0) { --- 68,82 ---- } gl = gld.getGL(); ! glu = gld.getGLU(); ! int[] viewport = new int[] {0, 0, (int)width, (int)height}; ! ! gl.glShadeModel(GL.GL_SMOOTH); //hacking linux gl.glMatrixMode(GL.GL_PROJECTION); gl.glLoadIdentity(); + //Set the PickMatrix if we are trying to pick something + if (picking > 0) { + glu.gluPickMatrix(x, viewport[3] - y, 2, 2, viewport); + } double aspect = width / height; if (aspect < 1.0) { *************** *** 157,159 **** --- 182,275 ---- } } + + /** + *Names objects in the scene for selection + */ + protected void nameObjects() { + gl.glClear(GL.GL_COLOR_BUFFER_BIT); + gl.glMatrixMode(GL.GL_MODELVIEW); + gl.glPushMatrix(); + + gl.glInitNames(); + + Set edges = EdgeFacade.getInstance().findAll(); + Iterator it = edges.iterator(); + gl.glColor3dv(STD_LINE_COLOR); + gl.glLineWidth(1.0f); + gl.glColor3dv(STD_LINE_COLOR); + while (it.hasNext()) { + Edge e = (Edge)it.next(); + Vertex to = e.getTo(); + Vertex from = e.getFrom(); + int name = e.getId().intValue(); + if (to != null && from != null) { + gl.glPushName(name); + gl.glBegin(GL.GL_LINES); + gl.glVertex3d(to.getX(), to.getY(), to.getZ()); + gl.glVertex3d(from.getX(), from.getY(), from.getZ()); + gl.glEnd(); + gl.glPopName(); + } + } + gl.glPopMatrix(); + } + + /** + * Gives an id of the object under the point of a mouseclick + * @param x the x coordinate of the point + * @param y the y coordinate of the point + * @return a Long id of the object under the point, null if no object is there + */ + public Long getObjectAtPoint(double x, double y) { + this.x = x; + this.y = y; + int hits; + IntBuffer selectBuffer = BufferUtils.newIntBuffer(1024); + Long id = null; + int[] mode = new int[1]; + int[] viewport = new int[] {0, 0, (int)width, (int)height}; + + gl.glSelectBuffer(1024, selectBuffer); + gl.glRenderMode(GL.GL_SELECT); + + //drawing scene in selection mode + picking = 1; //indicating that I am trying to pick(used in camera) + gl.glMatrixMode(GL.GL_MODELVIEW); + gl.glPushMatrix(); + gl.glLoadIdentity(); + gl.glClear(GL.GL_COLOR_BUFFER_BIT); + gl.glViewport(0, 0, (int)width, (int)height); + camera(drawable); + grid(); + coords(); + gl.glColor3dv(STD_LINE_COLOR); + gl.glLineWidth(1.0f); + drawAll(drawable); + gl.glPopMatrix(); + picking = 0; + //end drawing scene + + //gl.glMatrixMode(GL.GL_PROJECTION); + //gl.glPopMatrix(); + gl.glFlush(); + + //Geting hits and going back to RENDER mode + hits = gl.glRenderMode(GL.GL_RENDER); + + //log.info("x,y = " + x + "," + y + " hits = " + hits + ";SB = " + selectBuffer.get(0)); + + //Processing hits + if (hits >= 1) { + Integer intId = new Integer(selectBuffer.get(3)); + id = new Long(intId.longValue()); + } + + gl.glMatrixMode(GL.GL_MODELVIEW); + + if (id != null) { + return id; + } else { + return null; + } + } } |