I have recived the Viewport[4], projectionMatrix[16],observeMatrix[16],and the modelMatrix[16].I want ti convert the screen point to World point ,so i write the follow function to do this,
///////////////////////////////////////////
void toWorldCoord(Vector3d viewCoord,Vector3d worldCoord)
{
int Viewprot[4];
glGetIntegerv(GL_VIEWPORT,Viewprot);
float fz;
glReadPixels(viewCoord[0],viewCoord[1],1,1,GL_DEPTH_COMPONENT,GL_FLOAT,&fz);
gluUnProject(viewCoord[0],viewCoord[1],fz,
transformMatrix,projectionMatrix,Viewprot,
worldCoord,worldCoord+1,worldCoord+2);
}
////////////////////////////////////////////////
but the funtion can not work well.
the brief call codes as follow:
#include <windows.h> // use proper includes for your system
#include <math.h>
#include <gl/Gl.h>
#include <gl/glut.h>
#include “matrix.h” //the class matrix i write
int screenWidth = 640; // width of screen window in pixels
int screenHeight = 480; // height of screen window in pixels
typedef GLdouble OpenglMatrix[16];
OpenglMatrix projectionMatrix;//the projection matrix
OpenglMatrix modelMatrix; //the model view matrix
OpenglMatrix observeMatrix;// the observer matrix
OpenglMatrix transformMatrix;//the total transform matrix ,equal observeMatrix*modelMatrix
GLdouble dNear=1,dFar=10;//the clip plane distance
//<<<<<<<<<<<<<<<<<<<<<<< myInit >>>>>>>>>>>>>>>>>>>>
void myInit(void)
{
glClearColor(1.0,1.0,1.0,0.0); // background color is white
glColor3f(0.0f, 0.0f, 0.0f); // drawing color is black
}
//<<<<<<<<<<<<<<<<<<<<<<<< myDisplay >>>>>>>>>>>>>>>>>
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT); // clear the screen
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glLoadMatrixd(observeMatrix);//load the observer matrix
glMultMatrixd(modelMatrix);// multiply the model matrix
glGetDoublev(GL_MODELVIEW_MATRIX,transformMatrix);// store the the current the transform matrix ,prepare for the coordinate convertion
glBegin(GL_LINES);
glVertex3d(0,0,0);
glVertex3d(0.0,0.5,0);
glEnd();
glFlush(); // send all output to display
}
// called by mouse event
void myMouseFun(int button,int state ,int x, int y)
{
y= screenHeight - y;
Vector3d viewCoord;
viewCoord[0]=x;
viewCoord[1]=y;
viewCoord[2]=0;
if(button == GLUT_LEFT_BUTTON)
{
}
else if(button == GLUT_RIGHT_BUTTON)
{
Vector3d worldCoord;
//test code here,the test result is wrong ,i dont know the reasons
toWorldCoord(viewCoord,worldCoord);
printf("OK
");
}
}
void myKeyboardFun(unsigned char thekey ,int x, int y)
{
y=screenHeight-y;
switch(thekey) {
case ‘e’:
exit(0);
break;
default:
break;
}
}
void myReshape(int cx,int cy)
{
glMatrixMode(GL_PROJECTION); // set “camera shape”
glLoadIdentity();
screenWidth=cx;
screenHeight=cy;
double aspect = double(screenWidth)/screenHeight;
glOrtho(-1,1,-1,1,0,10);
glGetDoublev(GL_PROJECTION_MATRIX ,projectionMatrix);
glViewport(0,0,cx,cy);
}
//<<<<<<<<<<<<<<<<<<<<<<<< main >>>>>>>>>>>>>>>>>>>>>>
void main(int argc, char** argv)
{
Matrix::IdentityMatrix(projectionMatrix);
Matrix::IdentityMatrix(modelMatrix);
Matrix::IdentityMatrix(observeMatrix);
observeMatrix[14]=-(dNear+dFar)/2;//translate the eye position
glutInit(&argc, argv); // initialize the toolkit
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // set display mode
glutInitWindowSize(screenWidth, screenHeight); // set window size
glutInitWindowPosition(100, 150); // set window position on screen
glutCreateWindow("glut window"); // open the screen window
glutDisplayFunc(myDisplay); // register redraw function
glutReshapeFunc(myReshape);
glutMouseFunc(myMouseFun);
glutKeyboardFunc(myKeyboardFun);
myInit();
glutMainLoop(); // go into a perpetual loop
}
thanks for your help !