void T3DPlanet::GetOGLPos(int x, int y, t3dpoint & np, t3dpoint & fp)
{
glGetDoublev( GL_MODELVIEW_MATRIX, modelview );
// glGetDoublev( GL_PROJECTION_MATRIX, projection );
glMatrixMode(GL_PROJECTION); // Change Matrix Mode to Projection
glPushMatrix();
glLoadIdentity(); // Reset View
gluPerspectiveA(90.0f, DISPLAY_WIDTH/DISPLAY_HEIGHT,1000.0f, 2000.0f); // Do the perspective calculations. Last value = max clipping depth
glGetDoublev( GL_PROJECTION_MATRIX, projection );
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
// int i;
// for (i=0; i<16;i++) ShowMessage(FloatToStr(projection[i]));
glGetIntegerv( GL_VIEWPORT, viewport );
double nx,ny,nz; double fx,fy,fz;
float winX = float(x);
float winY = float(viewport[3]) - float(y);
t3dpoint neara;
t3dpoint fara;
gluUnProject(winX, winY, 0, modelview, projection, viewport, &nx, &ny, &nz);
gluUnProject(winX, winY, 1, modelview, projection, viewport, &fx, &fy, &fz);
np.x = nx;
np.y = ny;
np.z = nz;
NEAR_HIT = np;
fp.x = fx;// - nx;
fp.y = fy;// - ny;
fp.z = fz;// - nz;
FAR_HIT = fp;
}
and heres a thing that i use when mouse cursor is moving through window and it uses the proc above
[code]
void __fastcall T3DPlanet::MouseInsertMove(int X, int Y,HWND hwnd)
{
glViewport(0, 0, int( DISPLAY_WIDTH), int(DISPLAY_HEIGHT)); // Set the viewport for the OpenGL window
glMatrixMode(GL_PROJECTION); // Change Matrix Mode to Projection
glLoadIdentity(); // Reset View
gluPerspectiveA(90.0, DISPLAY_WIDTH/DISPLAY_HEIGHT,1000.0f, 1000.0 * 1000.0 * 1000.0 * 1000.0); // Do the perspective calculations. Last value = max clipping depth
glMatrixMode(GL_MODELVIEW);
sx = X;
sy = Y;
//glClear(GL_COLOR_BUFFER_BIT);
//glClear(GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glLoadIdentity();
gluLookAt(EYEPOS.x,EYEPOS.y,EYEPOS.z,EYEPOS.x+EYELOOKATPOINT.x,
EYEPOS.y-EYELOOKATPOINT.y,EYEPOS.z-EYELOOKATPOINT.z,
0,1,0);
if (insert_mode == true) {
TPoint mouse;
GetCursorPos(&mouse); // Gets The Current Cursor Coordinates (Mouse Coordinates)
ScreenToClient(hwnd, &mouse);
t3dpoint np, fp;
GetOGLPos(mouse.x,mouse.y,np,fp);
t3dpoint vLine[2];
np = vectorAB(NEAR_HIT,FAR_HIT);
np = Normalize( np ); //nice one milion kilometers away ;u
np = vector_multiple(np, 1000.0f * 1000.0f * 1000.0f);
vLine[0] = NEAR_HIT;
vLine[1] = vectors_add(NEAR_HIT, np);
//
//
int i;
for (i=0; i<SCENE_SELECTION[0].selectedfaces.Length;i++)
if (LineIntersectFaceFromModel(SCENE_SELECTION[0].selectedfaces[i],PLANET_MODEL,vLine, HIT_SPACE_POSITION ) == true)
return;
}
glPopMatrix();
}
[/code]
Ah please dont ask me why i call perspective projection and loadidentity before getoglpos ( i cant remember!!!)
well i remember that just draw your scene then use getoglpos
but it returns me two POINTS (NOT THE DEPTH POINT or etc) just 2 points that make the line from screen to the outter space ;x
then i must find a collision point between polygon ray intersection
I think you can bypass ray poly intersection by reading the acutal depth value from image (by glReadPixels let assume that will be a var called KKK)
so then gluUnProject(winX, winY, KKK, modelview, projection, viewport, &nx, &ny, &nz); nx ny nz are output pos vals but i don't know if it works
===================
and here's a proove that it works :X but watch after 1 min :/