PDA

View Full Version : Window coordinate systeme to OGL coordinate



Tosh
12-08-2005, 06:12 AM
Hi, for the moment i use gluUnproject fonction, that works well in orthographic mode, but in perspective mode it give me uncomprehensive numbers...

Is this fuction work ONLY with orthographic mode ?(not seen this in the red book).

I don't want to get the standard fuction of pointing object, just need to make a func that translate coordinate systems between window and ogl.

I don't use glut or aux lib.

Thanks.

memfr0b
12-08-2005, 10:12 AM
gluUnProject should cope with perspective projection without any problems, assuming you provide it with the correct matrizes. Problems should only appear when one of your matrizes cannot be inverted, which might happen with perspective matrix if you use a zNear value lower or equal to zero.

Tosh
12-09-2005, 03:00 AM
Hi thanks for your answer

Here is my code :

__Point __fastcall COglView::ScreenPointToViewPoint(__Point ptScreen)
{
GLint viewport[4];
GLdouble modelview[16];
GLdouble projection[16];
GLfloat winX, winY, winZ;
GLdouble posX, posY, posZ;

glGetDoublev( GL_MODELVIEW_MATRIX, modelview );
glGetDoublev( GL_PROJECTION_MATRIX, projection );
glGetIntegerv( GL_VIEWPORT, viewport );

winX = (float)ptScreen.GetLeft();
winY = (float)viewport[3] - (float)ptScreen.GetTop();
glReadPixels(ptScreen.GetLeft(), int(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ );

gluUnProject(winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ);

return __Point((float)posX, (float)posY);
}

/////////////////////////////////////////////////////////////////////////////////// posZ return 1.0f, that should be ok

for my initialisation :

void COglView::ResizeGLScene()
{
// Reset The Main Viewport
glViewport(0, 0, (GLint)m_rcView.GetWidth(), (GLint)m_rcView.GetHeight());

// Select The Projection Matrix
glMatrixMode(GL_PROJECTION);

// Reset The Projection Matrix
glLoadIdentity();

// Calculate The Aspect Ratio Of The Window
if (m_pOGL->GetProjectionMode() == OGL_Perspective)
{
gluPerspective(m_fMainFov, (GLfloat)m_rcView.GetWidth() / (GLfloat)m_rcView.GetHeight(), 0.1f, 1000.0f);
}
else if (m_pOGL->GetProjectionMode() == OGL_Orthogonal)
{
glOrtho(0, (GLfloat)m_rcView.GetWidth(), (GLfloat)m_rcView.GetHeight(), 0, 0.001, 1000000.0);
}

// Reselect The Modelview Matrix
glMatrixMode(GL_MODELVIEW);

// Reset The Modelview Matrix
glLoadIdentity();
}

///////////////////////////////////////////////////////////////////////////////////ANd now for the drawing :

void COglView::DrawQuad()
{
__Point ptLeftTop(10.f, 10.f);
__Point ptRightTop(250.f, 10.f);
__Point ptRightBottom(250.f, 250.f);
__Point ptLeftBottom(10.f, 250.f);

__Point ptOglLeftTop = ScreenPointToViewPoint(ptLeftTop);
__Point ptOglRightTop = ScreenPointToViewPoint(ptRightTop);
__Point ptOglRightBottom = ScreenPointToViewPoint(ptRightBottom);
__Point ptOglLeftBottom = ScreenPointToViewPoint(ptLeftBottom);

glColor3f(1.0f, 0.0f, 0.0f);

glBegin(GL_QUADS); // Draw A Quad
glVertex3f(ptOglLeftTop.GetLeft(), ptOglLeftTop.GetTop(), 0.0f);
glVertex3f(ptOglRightTop.GetLeft(), ptOglRightTop.GetTop(), 0.0f); // Top Right
glVertex3f(ptOglRightBottom.GetLeft(), ptOglRightBottom.GetTop(), 0.0f); // Bottom Right
glVertex3f(ptOglLeftBottom.GetLeft(), ptOglLeftBottom.GetTop(), 0.0f); // Bottom Left
glEnd();
}I know using conversion function for each point is stupide, but it is juste for testing...

I you see a mistake in this please tell me

Thanks.

memfr0b
12-09-2005, 04:54 AM
You're placing your near clip plane at z = 0.1 or z = 0.001, but are drawing at z = 0. So unless you have an additional translation somewhere before DrawQuad, your quad will always be in front of the near clip plane, and thus be culled.

Tosh
12-09-2005, 06:39 AM
Thanks, but it was not the probleme, i still set z at 0.0 and it works (strange is'nt it ?)

i juste add this code before drawing my quads :

// Clear The Screen And The Depth Buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// Select The Projection Matrix
glMatrixMode(GL_PROJECTION);

// Reset The Projection Matrix
glLoadIdentity();

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();