Jump to content
  • Advertisement
Sign in to follow this  

OpenGL Picking problem after ortho switch

This topic is 4159 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

Hi all, I have a problem with picking. When I'm running my perspective program picking is no problem. But I want to also display a textured tile in 2D (like a GUI) in the top left corner. So I try switch to ortho inside a list which, when I call it in my main rendering loop, switches to ortho, renders the squad and switches back to persective view again. But after the rendering of the 2D tile I cannot pick any object anymore, including the 2D tile. Can someone please help me how I can pick my objects again? Thanks in advance.. 2D tile list:
m_lstBorgMenuRadarScreen = glGenLists(1);
    glNewList(m_lstBorgMenuRadarScreen, GL_COMPILE);

        // Switch to an orthographic-view (non-perspiective) matrix.
	glOrtho(0.0, 25.0, 25.0, 0.0, -1.0, 1.0);

        glBindTexture(GL_TEXTURE_2D, m_unTextures[7]);

        glColor3f(1.0f, 1.0f, 1.0f);
            glTexCoord2f(0.0f, 1.0f); glVertex2f((GLfloat)dX1, (GLfloat)dY1);
            glTexCoord2f(1.0f, 1.0f); glVertex2f((GLfloat)dX2, (GLfloat)dY2);
            glTexCoord2f(1.0f, 0.0f); glVertex2f((GLfloat)dX3, (GLfloat)dY3);
            glTexCoord2f(0.0f, 0.0f); glVertex2f((GLfloat)dX4, (GLfloat)dY4);


        // Switch to an perspective-view matrix.
	gluPerspective(45.0f, m_dViewAspect, 0.1f, 500.0f);


My picking method:
#define BUFSIZE 512
GLuint	selectBuf[BUFSIZE];
static LRESULT OnLButtonDown(HWND hWnd, UINT nFlags, POINT point)
	//Selection Buffer
	GLuint selectBuf[BUFSIZE];
	//Number of Objects that we clicked on
	int hits;

	//create a viewport just for the selection
	int viewport[4]; //0 - x, y - 1, length - 2, width - 3

	//	//set the viewport to the size and location of the screen
	//	glGetIntegerv( GL_VIEWPORT, viewport );
	//set the viewport to the size and location of the screen
	glGetIntegerv( GL_VIEWPORT, viewport );
	//Tell OpenGL to use our array for selection
	glSelectBuffer(BUFSIZE, selectBuf);

	//put openGL in selection mode;
	glRenderMode( GL_SELECT );

	//initialize the name stack
	//push at least one entry on to the stack

	//Now we're restricting drawing to just under the cursor.  We need the projection matrix
	//for this.  Push it to the stack then reset the new matrix with load identity
	glMatrixMode( GL_PROJECTION );

	//Now restrict drawing using gluPickMatrix().  The first parameter is our current 
	//mouse position on the x-axis, the second is the current mouse y axis.  Then the width
	//and height of the picking region.  Finally the current viewport indicates the current boundries.
	//mouse_x and y are the center of the picking region.
	gluPickMatrix((GLdouble) point.x, (GLdouble)(viewport[3]-point.y), 1.0, 1.0, viewport );

	//gluPerspective will restrict the drawing to the area requestd by gluPickmatrix()
	//gluPerspective( 45.0f, (viewport[2] - viewport[0]) / (viewport[3] - viewport[ 1 ]), 0.1f, 100.0f );
	gluPerspective( 45.0f, m_gldAspect, 0.1f, 500.0f );

	//set the modelview to render our targets properly
	glMatrixMode( GL_MODELVIEW );
	// "render" the targets to our modelview
	glMatrixMode( GL_PROJECTION );
	glMatrixMode( GL_MODELVIEW );

	//find out how many objects we hit
	hits = glRenderMode( GL_RENDER );

	//as long as we have more than 0, we select the first object drawn to the area.
	if( hits != 0 )
		int	choose = selectBuf[3];									// Make Our Selection The First Object
		int depth = selectBuf[1];									// Store How Far Away It Is 

		for (int loop = 1; loop < hits; loop++)						// Loop Through All The Detected Hits
			// If This Object Is Closer To Us Than The One We Have Selected
			if (selectBuf[loop*4+1] < GLuint(depth))
				choose = selectBuf[loop*4+3];						// Select The Closer Object
				depth = selectBuf[loop*4+1];						// Store How Far Away It Is


	return TRUE;

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!