Jump to content
  • Advertisement
Sign in to follow this  

OpenGL Basic Picking with Viewports

This topic is 4102 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Problem: I can get my program to work (picking) with single viewport (full screeen or 1/2 width), but not with any viewport that changes the height of the viewport. Goal: The goal of my program was to draw 4 rectangles in different viewports and then draw a grid in the one that was selected. For picking, the viewports are defined as follows (in the window area) 2 3 0 1 Where viewport 0 is the lower left hand corner (glViewPort(0,0,w/2,h/2)). The drawing all worked great and even moving the grid around worked via keyboard. When I added the picking I had problems. So I went back to just drawing and trying to select viewport 0. The selection viewport 0 didn't work, but I got a hit when I clicked in viewport 2. So, I tried different viewports and as long as I didn't mess with the height, the selection worked properly. If I messed with the height, I had to select viewport above the desired viewport to get a hit. Here is the drawFunction and come of the viewports I tried. void drawFunction( GLenum nMode ) { GLsizei nWidth, nHeight; static GLfloat blueColor[] = { 0.0, 0.0, 1.0 }; static GLfloat greenColor[] = { 0.0, 1.0, 0.0 }; static GLfloat redColor[] = { 1.0, 0.0, 0.0 }; nWidth = glutGet(GLUT_WINDOW_WIDTH); nHeight = glutGet(GLUT_WINDOW_HEIGHT); // ViewPort 0 // Original Code (doesn't work) Have to click on upper left hand corner of wnd. glViewport( 0, 0, nWidth / 2.0f, nHeight / 2.0f ); // Selection Works Properly //glViewport( 0, 0, nWidth , nHeight ); // Selection Works Properly //glViewport( 0, 0, nWidth / 2.0, nHeight ); // Have to click on the upper half of window. //glViewport( 0, 0, nWidth , nHeight / 2.0f ); glColor3fv( blueColor ); if ( nMode == GL_SELECT ) glPushName(0); glRectf( -2.0, -2.0, 2.0, 2.0 ); if ( nMode == GL_SELECT ) glPopName(); if ( grid_pos == 0 ) { glColor3f( 1.0, 1.0, 1.0 ); drawSquareGrid(); } ... (commented out draws of other viewports) } My Mouse Input Function is pretty standard: GLvoid mouseInput( int nButton, int nState, int nX, int nY ) { fprintf( stdout, "mouseInput\n" ); #define BUFSIZE 9 int pickWidth = 2; int pickHeight =2; GLuint selectBuf[BUFSIZE];//create the selection buffer GLint hits; GLint viewport[4];//create a viewport //check for a left mouse click if (nButton == GLUT_LEFT_BUTTON && nState == GLUT_DOWN) { glGetIntegerv (GL_VIEWPORT, viewport);//set the viewport glSelectBuffer (BUFSIZE, selectBuf);//set the select buffer (void) glRenderMode (GL_SELECT);//put OpenGL in select mode glInitNames();//init the name stack glPushName(0);//push a fake id on the stack to prevent load error glPopName(); // get the zero off the stack. glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); //setup a pick matrix gluPickMatrix ((GLdouble) nX, (GLdouble) (viewport[3] - nY), (GLdouble) pickWidth, (GLdouble) pickHeight, viewport); SetPerspective(); drawFunction (GL_SELECT);// Draw to the pick matrix instead of our normal one glMatrixMode (GL_PROJECTION); glPopMatrix (); glFlush (); hits = glRenderMode (GL_RENDER);//count the hits processHits (hits, selectBuf);//check for object selection glutPostRedisplay(); } } and ProcessHits: GLvoid processHits(GLint hits, GLuint buffer[]) { int y = 0; //unselect all if(hits > 0)//Make sure there is at least one hit { y = buffer[3]; grid_pos = y; } } and Finally, set perpective if it matters: void SetPerspective(void) { glOrtho( -2.0, 2.0, -2.0, 2.0, -1.0, 1.0 ); }

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!