# OpenGL Selection Routine Area Off

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

## Recommended Posts

Hello, I am currently having trouble with my selection routine with the fact that the area for detection is off from where it is actually displayed. I have included the following screen shot with a red rectangle drawn to indicate the valid detection area for that particular quad. The valid detection area for that quad only places itself where it is suppose to be if I initiate a "fullscreen" routine. If it is in windowed mode, even when maximized, the detection area is off by about that much. Here is my selection routine:
//Selection function for Opengl ~Thanks to www.gametutorials.com~
int Select(int x, int y)
{
int objectsFound = 0;
int	viewportCoords[4] = {0};
unsigned int selectBuffer[64] = {0};
glSelectBuffer(64, selectBuffer);
glGetIntegerv(GL_VIEWPORT, viewportCoords);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glRenderMode(GL_SELECT);
gluPickMatrix(x, viewportCoords[3] - y, 2, 2, viewportCoords);
gluPerspective(45.0f,(float)g_rRect.right/(float)g_rRect.bottom,0.1f,150.0f);
glMatrixMode(GL_MODELVIEW);
DrawScene();
objectsFound = glRenderMode(GL_RENDER);
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);

if (objectsFound > 0)
{
unsigned int lowestDepth = selectBuffer[1];
int selectedObject = selectBuffer[3];
for(int i = 1; i < objectsFound; i++)
{
if(selectBuffer[(i * 4) + 1] < lowestDepth)
{
lowestDepth = selectBuffer[(i * 4) + 1];
selectedObject = selectBuffer[(i * 4) + 3];
}
}
return selectedObject;
}
return 0;
}


This is how I am implementing my selection routine for the rendering code:
             const float button_loc[]={0.0, -0.4, -0.8};
glInitNames();
glPushName(2005);
for(int loop=0; loop < 3; loop++)
{
if(Selection.ID != 2005+loop)
{
glColor3f(0.2, 0.2, 1.0);
glBindTexture(GL_TEXTURE_2D, texture[IDB_LBUTTON]);
}
else
{
glColor3f(1.0, 1.0, 1.0);
glBindTexture(GL_TEXTURE_2D, texture[IDB_LBUTTONA]);
}
glPushMatrix();
glTranslatef(-0.94, button_loc[loop], -4.0);
glScalef(1.8, 0.25, 1.0);
xia_PersonalDrawParticle(1.0, 1.0);
glPopMatrix();
glEnd();
}
glPopName();


Is there a way to adjust it so that the detection area and the textured quad would be exactly at the same place? First I thought it could be my scaling but after rendering one with a 1:1 ratio, the result is the same. If I construct a quad utilizing glBegin and glEnd, the detection area and the textured quad are exactly the same. Thank you.

##### Share on other sites
After some arduous code testing, I have isolated the cause of this problem down to these lines of code. The only problem is that I do not know how to correct it - I do not even know where to start.

Implementation of Resizing:
...//~~~~~Bring Window in front and in focus	ShowWindow(hwnd,SW_SHOW);	SetForegroundWindow(hwnd);	SetFocus(hwnd);	ReSizeGLScene(width, height);	if(!option.fullscreen)	{	    ShowWindow(hwnd,SW_MAXIMIZE);        }	return TRUE;}

Resizing:
//Resize scene based on dimensionsvoid ReSizeGLScene(float width, float height){	if (height == 0)	{		height=1;	}	glViewport(0, 0, (int)width, (int)height);	glMatrixMode(GL_PROJECTION);	glLoadIdentity();	gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,150.0f);	glMatrixMode(GL_MODELVIEW);	glLoadIdentity();}

Now, here is what I have learned from stepping through and trying a few things. If I take out this function, "ShowWindow(hwnd,SW_MAXIMIZE);", the resulting window will be 800x600. The detection is till off but now instead of being slightly above the object area, it is now below it. If I maximize the window manually, clicking on the maximize tab, the detection area will revert back to being slightly above the object area again.

If I leave the function, "ShowWindow(hwnd,SW_MAXIMIZE);", in but move the "ReSizeGLScene(width, height);" after "ShowWindow(hwnd,SW_MAXIMIZE);", the result will be of that of the described fullscreen scenerio. The buttons in questions will align about right, but other buttons that are rendered will be off by having their detection area extended a little below their actual object area.

Does anyone have an idea about what is wrong?

Thank you.

##### Share on other sites
All I can think of is that you're not taking the window's title bar height into account when you're calculating offsets for your selection area. This would account for the discrepancy between fullscreen and windowed modes.

1. 1
2. 2
Rutin
21
3. 3
A4L
15
4. 4
5. 5
khawk
14

• 13
• 26
• 10
• 11
• 44
• ### Forum Statistics

• Total Topics
633742
• Total Posts
3013638
×