Hi,
I've been doing opengl picking these days. Here is a demo from Red Book. When I debug this demo, I didn't get selectBuff in the whole process, Why? package ui; import static com.jogamp.opengl.fixedfunc.GLMatrixFunc.GL_MODELVIEW; import static com.jogamp.opengl.fixedfunc.GLMatrixFunc.GL_PROJECTION; import java.nio.IntBuffer; import org.eclipse.swt.SWT; import org.eclipse.swt.events.MouseEvent; import org.eclipse.swt.events.MouseListener; import org.eclipse.swt.events.PaintEvent; import org.eclipse.swt.events.PaintListener; import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.opengl.GLCanvas; import org.eclipse.swt.opengl.GLData; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.Shell; import com.jogamp.common.nio.Buffers; import com.jogamp.opengl.GL2; import com.jogamp.opengl.GLContext; import com.jogamp.opengl.GLDrawableFactory; import com.jogamp.opengl.GLProfile; import com.jogamp.opengl.glu.GLU; public class RedBookDemo { public static void main(String[] args) { Display display = new Display(); final Shell shell = new Shell(display); shell.setLayout(new FillLayout()); shell.setText("SWT/Cylinder Example"); shell.setSize(640, 480); final Composite comp = new Composite(shell, SWT.NONE); comp.setLayout(new FillLayout()); GLData data = new GLData(); data.doubleBuffer = true; final GLCanvas canvas = new GLCanvas(comp, SWT.NO_BACKGROUND, data); canvas.setCurrent(); GLProfile glprofile = GLProfile.getDefault(); final GLContext context = GLDrawableFactory.getFactory(glprofile).createExternalGLContext(); GL2 gl2 = context.getGL().getGL2(); canvas.addListener(SWT.Resize, new Listener() { @Override public void handleEvent(Event event) { Rectangle rect = canvas.getClientArea(); canvas.setCurrent(); context.makeCurrent(); resizeGLScene(gl2, rect.width, rect.height); context.release(); } }); context.makeCurrent(); initGL(gl2); context.release(); canvas.addPaintListener(new PaintListener() { @Override public void paintControl(PaintEvent e) { canvas.setCurrent(); context.makeCurrent(); drawGLScene(gl2); canvas.swapBuffers(); context.release(); } }); canvas.addMouseListener(new MouseListener() { @Override public void mouseDoubleClick(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseDown(MouseEvent e) { if (e.button == 1) { canvas.setCurrent(); context.makeCurrent(); IntBuffer selectBuff = Buffers.newDirectIntBuffer(512); int hits; IntBuffer viewport = IntBuffer.allocate(4); gl2.glGetIntegerv(GL2.GL_VIEWPORT, viewport); gl2.glSelectBuffer(512, selectBuff); gl2.glRenderMode(GL2.GL_SELECT); gl2.glInitNames(); gl2.glPushName(0); gl2.glMatrixMode(GL2.GL_PROJECTION); gl2.glPushMatrix(); gl2.glLoadIdentity(); GLU glu = new GLU(); glu.gluPickMatrix(e.x, viewport.get(3)-e.y, 5, 5, viewport); glu.gluOrtho2D(0, 3, 0, 3); drawSquares(gl2, GL2.GL_SELECT); gl2.glMatrixMode(GL2.GL_PROJECTION); gl2.glPopMatrix(); hits = gl2.glRenderMode(GL2.GL_RENDER); canvas.swapBuffers(); context.release(); } } @Override public void mouseUp(MouseEvent e) { } }); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } canvas.dispose(); display.dispose(); } static void resizeGLScene(GL2 gl2, int width, int height) { if (height == 0) { height = 1; } gl2.glViewport(0, 0, width, height); GLU glu = new GLU(); gl2.glMatrixMode(GL_PROJECTION); gl2.glLoadIdentity(); glu.gluOrtho2D(0, 3, 0, 3); gl2.glMatrixMode(GL_MODELVIEW); gl2.glLoadIdentity(); } static void initGL(GL2 gl2) { gl2.glShadeModel(GL2.GL_SMOOTH); gl2.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); gl2.glClearDepth(1.0f); gl2.glEnable(GL2.GL_DEPTH_TEST); gl2.glDepthFunc(GL2.GL_LEQUAL); gl2.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL2.GL_NICEST); } static void drawGLScene(GL2 gl2) { gl2.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT); // 清除屏幕和深度缓存 drawSquares(gl2, GL2.GL_RENDER); } static void drawSquares(GL2 gl2, int mode) { int i,j; for(i = 0; i < 3; i++) { if(mode == GL2.GL_SELECT) { gl2.glLoadName(i); } for(j = 0; j < 3; j++) { if(mode == GL2.GL_SELECT) { gl2.glPushName(j); } gl2.glColor3d(i / 3.0, j / 3.0f, 0.0f); gl2.glRectf(i, j, i + 1, j + 1); if(mode == GL2.GL_SELECT) { gl2.glPopName(); } } } } } |
Administrator
|
In reply to this post by Irene Tang
This example of picking works:
http://jogamp.org/jogl-demos/src/demos/misc/Picking.java
Julien Gouesse | Personal blog | Website
|
Free forum by Nabble | Edit this page |