Ortho and picking

Started by
4 comments, last by the_stabilizer 19 years, 11 months ago
Hello,I'm writting an OpenGL GUI,I use ortho mode for the GUI and the prespective for the 3D rendering. I also want to use Selection(for the 3D Objects not the GUI). When I use Selection with 3D objects everything works fine , but when I add a GUI element to the scene the selection is messed up completely , I think it's something wrong with the GUI rendering specificly the switching between Prespective and Ortho modes --------------------------------------------------------------- the GUI rendering code: glDisable(GL_DEPTH_TEST); glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); glOrtho(0 ,Window.Width , Window.Height , 0 ,0,1); glMatrixMode(GL_MODELVIEW); glPushMatrix(); glLoadIdentity(); glBegin(GL_LINE_LOOP); glColor3f(0.3f,0.3f,0.3f); glVertex2f(m_x ,m_y - TEXTBOX_MARGINS ); glVertex2f(m_x + m_fWidth - m_fLetterSize + 1 ,m_y - TEXTBOX_MARGINS ); glVertex2f(m_x + m_fWidth - m_fLetterSize + 1 ,m_y + m_fHeight + TEXTBOX_MARGINS ); glVertex2f(m_x ,m_y + m_fHeight + TEXTBOX_MARGINS ); glEnd(); glMatrixMode(GL_PROJECTION); glPopMatrix(); glMatrixMode( GL_MODELVIEW ); glPopMatrix(); glEnable(GL_DEPTH_TEST); ---------------------------------------------------------------- note that nothing is wrong with the rendering everything is in place. [edited by - The_Stabilizer on May 29, 2004 8:48:29 AM]
Advertisement
Looks ok to me, how about posting the selection code and other 3d rendering parts so we can see what you do.

--------------------------------------------------------
Life would be so much easier if we could just get the source code.
--------------------------------------------------------Life would be so much easier if we could just get the source code.

The 3D code has been complicated , I simplified it to make it easier for me to debug,but I'm still facing the same problem with selection
Here goes
----------------------------------------------------------------
bool IsSelected = false;

bool IsObjectSelected(int name)
{
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);
glInitNames();
glPushName(0);
glLoadIdentity();
gluPickMatrix(MouseStatus.Mouse_X, viewportCoords[3] - MouseStatus.Mouse_Y, 2, 2, viewportCoords);
gluPerspective(45.0f,(float)Window.Width/(float)Window.Height,0.1f,1500.0f);
glMatrixMode(GL_MODELVIEW);
Update(); //Render GL Scene
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];
}
}

if (selectedObject == name)return true;
` return false;
}

return false;
}

//HandleMouse Function is called for every frame After the Scene is rendered
void HandleMouse(MouseStruct * MouseStatus)
{
if(IsObjectSelected(5))IsSelected = true;
else IsSelected = false;
return;
}



bool Update()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glLoadName(5);
glBegin(GL_QUADS);
if(IsSelected)glColor3f(1.0f,1.0f,0.0f);
else glColor3f(1.0f,1.0f,1.0f);
glVertex3f(- 1.0f,-1.0f,-5.0f);
glVertex3f( 1.0f,-1.0f,-5.0f);
glVertex3f( 1.0f, 1.0f,-5.0f);
glVertex3f(- 1.0f, 1.0f,-5.0f);
glEnd();
glEnd();

RenderGUI(); // The line of troubles,When commented selection works ok.
return true;
}
void RenderGUI()
{
glDisable(GL_DEPTH_TEST);

glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();

glOrtho(0 ,Window.Width , Window.Height , 0 ,0,1);

glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();



glBegin(GL_LINE_LOOP);
glColor3f(0.3f,0.3f,0.3f);

glVertex2f(m_x ,m_y - TEXTBOX_MARGINS );
glVertex2f(m_x + m_fWidth - m_fLetterSize + 1 ,m_y - TEXTBOX_MARGINS );
glVertex2f(m_x + m_fWidth - m_fLetterSize + 1 ,m_y + m_fHeight + TEXTBOX_MARGINS );
glVertex2f(m_x ,m_y + m_fHeight + TEXTBOX_MARGINS );
glEnd();

glMatrixMode(GL_PROJECTION);
glPopMatrix();

glMatrixMode( GL_MODELVIEW );
glPopMatrix();

glEnable(GL_DEPTH_TEST);

}
----------------------------------------------------------------




[edited by - The_Stabilizer on May 29, 2004 12:19:56 PM]
Hiya. One thing I notice is, in your Update() function, when setting up the ortho mode, you do:

// You do this first...glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); glOrtho(0 ,Window.Width , Window.Height , 0 ,0,1);glMatrixMode(GL_MODELVIEW); glPushMatrix(); glLoadIdentity();//Then you do this to recover the previous matrices...glMatrixMode(GL_PROJECTION); glPopMatrix();glMatrixMode( GL_MODELVIEW );glPopMatrix();


What you want to do is first recover the modelview matrix since that's what you last popped. Then, recover the projection matrix.

Note: Pushing and popping is a LIFO (last in first out) operation. The last (most recent) item that was pushed is the first to be popped (recovered).
~Urayami

[edited by - urayami on May 29, 2004 3:07:24 PM]
~Urayami
Thank you for your replies , I'm still having the same problem though , I found some way around it,I added a global flag to render the GUI only if I'm in Render mode , and skips the GUI rendering in the Select mode.
Still I would appreciate it if someone found what's wrong with the code.
Thanks again for the replies.

[edited by - The_Stabilizer on June 1, 2004 1:12:39 PM]
Are you sure you tried this for your switching in RenderGUI ()?:

glMatrixMode (GL_PROJECTION) ;
glPushMatrix () ;
glLoadIdentity () ;
glOrtho (...) ;
glMatrixMode (GL_MODELVIEW) ;
glPushMatrix () ;
glLoadIdentity () ;

// Render GUI stuff
// ...
// ...

glMatrixMode (GL_MODELVIEW) ;
glPopMatrix () ;
glMatrixMode (GL_PROJECTION) ;
glPopMatrix () ;

~Urayami

[edited by - urayami on June 1, 2004 3:00:07 PM]
~Urayami

This topic is closed to new replies.

Advertisement