Hi, I was thought that gluProject and gluUnproject performed inverse
operations. I thought a test of this would be the following code where
after calling these function <w> would not change. Why does <w> change
after calling gluProject?
w[0] = (GLdouble)x;
w[1] = (GLdouble)y;
glReadPixels(x, y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &z);
w[2] = nearPlane + ((farPlane - nearPlane) * (GLdouble)z);
gluUnProject(w[0], w[1], w[2], mv, pj, vp, &o[0], &o[1], &o[2]);
gluProject(o[1], o[2], o[0], mv, pj, vp, &w[0], &w[1], &w[2]);
where:
nearPlane = 3.2320508075689198
farPlane = 4.9641016151378201
I should note that <z> returned from glReadPixels is very small ~0.00x
Thanks
|
|
0
|
|
|
|
Reply
|
John
|
12/5/2003 6:36:54 AM |
|
John wrote:
> Hi, I was thought that gluProject and gluUnproject performed inverse
> operations. I thought a test of this would be the following code where
> after calling these function <w> would not change. Why does <w> change
> after calling gluProject?
>
> w[0] = (GLdouble)x;
> w[1] = (GLdouble)y;
> glReadPixels(x, y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &z);
> w[2] = nearPlane + ((farPlane - nearPlane) * (GLdouble)z);
Why are you messing with Z? gluUnProject() wants the
value from the depth buffer.
--
<\___/> For email, remove my socks.
/ O O \
\_____/ FTB. Why isn't there mouse-flavored cat food?
|
|
0
|
|
|
|
Reply
|
fungus
|
12/5/2003 10:51:27 AM
|
|
fungus wrote:
> Why are you messing with Z? gluUnProject() wants the
> value from the depth buffer.
Thanks fungus, I didn't know that, but the problem persists:
w[0] = (GLdouble)x;
w[1] = (GLdouble)y;
glReadPixels(x, y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &z);
w[2] = (GLdouble)z;
gluUnProject(w[0], w[1], w[2], mv, pj, vp, &o[0], &o[1], &o[2]);
gluProject(o[1], o[2], o[0], mv, pj, vp, &w[0], &w[1], &w[2]);
|
|
0
|
|
|
|
Reply
|
John
|
12/6/2003 2:49:38 AM
|
|
John wrote:
> but the problem persists:
>
> w[0] = (GLdouble)x;
> w[1] = (GLdouble)y;
> glReadPixels(x, y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &z);
> w[2] = (GLdouble)z;
> gluUnProject(w[0], w[1], w[2], mv, pj, vp, &o[0], &o[1], &o[2]);
> gluProject(o[1], o[2], o[0], mv, pj, vp, &w[0], &w[1], &w[2]);
>
static PIXELFORMATDESCRIPTOR pfd=
{
sizeof(PIXELFORMATDESCRIPTOR),
1, // version number
PFD_DRAW_TO_WINDOW | // support window
PFD_SUPPORT_OPENGL | // support OpenGL
PFD_DOUBLEBUFFER, // support double buffering
PFD_TYPE_RGBA, // RGBA format
(int)dm.dmBitsPerPel, // color depth
0, 0, 0, 0, 0, 0, // color bits ignored
0, // no alpha buffer
0, // shift bit ignored
0, // no accumulation buffer
0, 0, 0, 0, // accumulation bits ignored
24, // 24bit z-buffer (depth buffer)
8, // 8bit stencil buffer
0, // no auxiliary buffer
PFD_MAIN_PLANE, // main drawing layer
0, // reserved
0, 0, 0 // layer masks ignored
};
|
|
0
|
|
|
|
Reply
|
John
|
12/6/2003 3:16:08 AM
|
|
John wrote:
>> gluUnProject(w[0], w[1], w[2], mv, pj, vp, &o[0], &o[1], &o[2]);
>> gluProject(o[1], o[2], o[0], mv, pj, vp, &w[0], &w[1], &w[2]);
>>
Unless you pass weird numbers to gluUnproject() you
should get the same thing.
....unless you don't have a valid rendering context.
If you don't have a rendercontext then OpenGL functions
do nothing.
>
> static PIXELFORMATDESCRIPTOR pfd=
> {
> ...
The pixel format isn't going to amke any difference.
--
<\___/> For email, remove my socks.
/ O O \
\_____/ FTB. Why isn't there mouse-flavored cat food?
|
|
0
|
|
|
|
Reply
|
fungus
|
12/6/2003 5:18:37 AM
|
|
fungus wrote:
> John wrote:
>
>>> gluUnProject(w[0], w[1], w[2], mv, pj, vp, &o[0], &o[1], &o[2]);
>>> gluProject(o[1], o[2], o[0], mv, pj, vp, &w[0], &w[1], &w[2]);
>>>
>
> Unless you pass weird numbers to gluUnproject() you
> should get the same thing.
I assume then, that my perspective transform is no good, unless I'm
missing something else. Full code below, Thanks.
m_camera = (0.0, 0.0, 1.0)
x = 200
y=300
....
RECT rc;
GetClientRect(&rc);
w[0] = (GLdouble)x;
w[1] = (GLdouble)y;
SetPerspective(rc);
SetModelView();
glGetDoublev(GL_MODELVIEW_MATRIX, mv);
glGetDoublev(GL_PROJECTION_MATRIX, pj);
glGetIntegerv(GL_VIEWPORT, vp);
glReadPixels(x, y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &z);
w[2] = (GLdouble)z;
gluUnProject(w[0], w[1], w[2], mv, pj, vp, &o[0], &o[1], &o[2]);
gluProject(o[1], o[2], o[0], mv, pj, vp, &w[0], &w[1], &w[2]);
....
void
COGLView::SetPerspective(RECT& rc)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
double ar = (double)rc.right/(double)rc.bottom; // aspect ratio
double d = 1.7320508075689; // diagonal: sqrt(1+1+1)
if( ar > 1.0 ) // height larger then width
{
width = (d*ar);
height = d;
}
else
{
width = d;
height = (d/ar);
}
fovAngle = 30. * DEGTORAD;
nearPlane = float(height / (2.0f * tan(fovAngle / 2.0f)));
farPlane = nearPlane + d;
width *= m_camera[2] * 0.5;
height *= m_camera[2] * 0.5;
glFrustum(-width, width, -height, height, nearPlane, farPlane);
glMatrixMode(GL_MODELVIEW);
}
void
COGLView::SetModelView()
{
glClear(GL_COLOR_BUFFER_BIT);
glClear(GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslated(camera[0], m_camera[1], -0.5 * GLdouble(nearPlane +
farPlane));
}
|
|
0
|
|
|
|
Reply
|
John
|
12/6/2003 8:42:14 PM
|
|
|
5 Replies
180 Views
(page loaded in 0.886 seconds)
|