From: Andy <and...@bt...> - 2000-05-17 01:45:52
|
I'm trying to use picking. I draw three rectangles using drawRects(), and then use selection mode to determine whether the user has clicked on a specific rectangle (this is just a very simple example). Under NT the code works fine, when you click on a square you get a HIT. Under Win98, it registers a hit, but the next glj.gljCheckGL() throws an exception (stack underflow or stack overflow). I've narrowed it down to the glPushMatrix and PopMatrix calls in drawRects when using GL_SELECT mode, but don't have a clue as to why. Its got to be something silly I've overlooked (maybe you can't have two glPushMatrix calls when using perspective mode, or can push a matrix when in one mode and the another glPushMatrix after switching modes), but I don't know. Can anyone tell me what I'm doing wrong. I've include a small bit of the code (showing the relevant sections) below. Many thanks Andy Qua ----------------- void drawRects(int mode) { gl.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); gl.glPushMatrix(); gl.glTranslated( -2.0, 1.0, -5.0 ); if (mode == GL_SELECT) gl.glLoadName (1); gl.glColor3f (1.0f, 1.0f, 0.0f); gl.glBegin (GL_QUADS); gl.glVertex3i (0, 0, 0); gl.glVertex3i (0, 0, 1); gl.glVertex3i (1, 0, 1); gl.glVertex3i (1, 0, 0); gl.glEnd(); if (mode == GL_SELECT) gl.glLoadName (2); gl.glColor3f (0.0f, 1.0f, 1.0f); gl.glBegin (GL_QUADS); gl.glVertex3i (2, 0, 0); gl.glVertex3i (2, 0, 1); gl.glVertex3i (3, 0, 1); gl.glVertex3i (3, 0, 0); gl.glEnd(); if (mode == GL_SELECT) gl.glLoadName (3); gl.glColor3f (1.0f, 0.0f, 1.0f); gl.glBegin (GL_QUADS); gl.glVertex3i (4, 0, 0); gl.glVertex3i (4, 0, 1); gl.glVertex3i (5, 0, 1); gl.glVertex3i (5, 0, 0); gl.glEnd (); gl.glPopMatrix(); } void processSelection( int x, int y ) { int BUFSIZE = 512; int[] selectBuf = new int[BUFSIZE]; int hits; int[] viewport = new int[4]; gl.glGetIntegerv (GL_VIEWPORT, viewport); gl.glSelectBuffer (BUFSIZE, selectBuf); gl.glRenderMode (GL_SELECT); gl.glInitNames(); gl.glPushName(-1); gl.glMatrixMode (GL_PROJECTION); gl.glPushMatrix (); gl.glLoadIdentity (); /* create 5x5 pixel picking region near cursor location */ glu.gluPickMatrix ((double) x, (double) (viewport[3] - y), 5.0, 5.0, viewport); glu.gluPerspective(45.0, (float)640/480, 1.0, 100.0); drawRects (GL_SELECT); gl.glPopMatrix (); gl.glFlush (); hits = gl.glRenderMode (GL_RENDER); gl.glMatrixMode (GL_MODELVIEW); if (hits > 0 ) processHits (hits, selectBuf); } void processHits (int hits, int buffer[]) { System.out.println( "HIT" ); } public void mousePressed(MouseEvent e) { System.out.println("Mouse Pressed"); int xPos = e.getX(); int yPos = e.getY(); if(!glj.gljMakeCurrent(true)) return; processSelection(xPos, yPos); glj.gljSwap(); glj.gljCheckGL(); glj.gljFree(); } |