OpenGL Math - Projecting Screen space to World space coords -
time little bit of math end of day..
i need project 4 points of window size:
<0,0> <1024,768>
into world space coordinates form quadrilateral shape later used terrain culling - without gluunproject
for test only, use mouse coordinates - , try project them onto world coords
resolved
here's how exactly, step step.
0) obtain mouse coordinates within client area
1) projection matrix , view matrix if no model matrix required.
2) multiply projection * view
3) inverse results of multiplication
4) construct vector4 consisting of
x = mouseposition.x
within range of window x - transform values between -1 , 1
y = mouseposition.y
within range of window y - transform values between -1 , 1 - remember invert mouseposition.y if needed
z = the depth value
( can obtained glreadpixel) - can manually go -1 1 ( znear, zfar )
w = 1.0
5) multiply vector inversed matrix created before
6) divide result vector it's w component after matrix multiplication ( perspective division )
point mousepos; getcursorpos(&mousepos); screentoclient( this->getwindowhwnd(), &mousepos ); cmatrix4x4 matprojection = m_pcamera->getviewmatrix() * m_pcamera->getprojectionmatrix() ; cmatrix4x4 matinverse = matprojection.inverse(); float in[4]; float winz = 1.0; in[0]=(2.0f*((float)(mousepos.x-0)/(this->getresolution().x-0)))-1.0f, in[1]=1.0f-(2.0f*((float)(mousepos.y-0)/(this->getresolution().y-0))); in[2]=2.0* winz -1.0; in[3]=1.0; cvector4 vin = cvector4(in[0],in[1],in[2],in[3]); pos = vin * matinverse; pos.w = 1.0 / pos.w; pos.x *= pos.w; pos.y *= pos.w; pos.z *= pos.w; sprintf(strtitle,"%f %f %f / %f,%f,%f ",m_pcamera->m_vposition.x,m_pcamera->m_vposition.y,m_pcamera->m_vposition.z,pos.x,pos.y,pos.z); setwindowtext(this->getwindowhwnd(),strtitle);
Comments
Post a Comment