3.9 逆变换和模拟变换

来源:互联网 发布:单片机晶振不起振 编辑:程序博客网 时间:2024/06/07 01:54

逆变换,就是OpenGL变换的逆过程。OpenGL是将三维坐标,转换成屏幕坐标。而逆变换,就是将屏幕坐标,转换为三维坐标。在逆变换过程中,同样需要正变换中的那些参数,这些参数有三个: 模型矩阵, 投影矩阵,视口。

逆变换,使用的函数叫gluUnProject,这个是相对于标准深度值的。还有一个函数,叫gluUnProject4,是相对于非标准深度值的。所谓标准深度值,是指深度值范围为[0, 1]。深度值为0的点,位于视景体的近侧裁剪平面上,深度值为1的点,位于视景体的远侧裁剪平面上。

我们知道,屏幕坐标,只有2个值,而物体坐标,通常有三个值,那么这第三个值,OpenGL怎么变换过来,它总不能凭空造一个吧。是的,所以,即使是从屏幕坐标,转换成三维坐标,也还是需要提供第三个值,这个值就是深度值。

gluUnProject

下面就是逆变换函数gluUnProject的原型。

int gluUnProject(GLdouble winx, GLdouble winy, GLdouble winz, const GLdouble modelMatrix[16], const GLdouble projMatrix[16], const GLint viewport[4], GLdouble *objx, GLdouble *objy, GLdouble *objz);

看参数,输入为(winx, winy, winz),即为屏幕坐标和深度值。输出为objx, objy, objz,即物体坐标。然后三大参数,模型矩阵,投影矩阵,视口。

/* *  unproject.c *  When the left mouse button is pressed, this program  *  reads the mouse position and determines two 3D points  *  from which it was transformed.  Very little is displayed. */#include <GL/glut.h>#include <stdlib.h>#include <stdio.h>void display(void){   glClear(GL_COLOR_BUFFER_BIT);   glFlush();}/* Change these values for a different transformation  */void reshape(int w, int h){   glViewport (0, 0, (GLsizei) w, (GLsizei) h);   glMatrixMode(GL_PROJECTION);   glLoadIdentity();   gluPerspective (45.0, (GLfloat) w/(GLfloat) h, 1.0, 100.0);   glMatrixMode(GL_MODELVIEW);   glLoadIdentity();}void mouse(int button, int state, int x, int y) {   GLint viewport[4];   GLdouble mvmatrix[16], projmatrix[16];   GLint realy;  /*  OpenGL y coordinate position  */   GLdouble wx, wy, wz;  /*  returned world x, y, z coords  */   switch (button) {      case GLUT_LEFT_BUTTON:         if (state == GLUT_DOWN) {            glGetIntegerv (GL_VIEWPORT, viewport);            glGetDoublev (GL_MODELVIEW_MATRIX, mvmatrix);            glGetDoublev (GL_PROJECTION_MATRIX, projmatrix);/*  note viewport[3] is height of window in pixels  */            realy = viewport[3] - (GLint) y - 1;            printf ("Coordinates at cursor are (%4d, %4d)\n", x, realy);            gluUnProject ((GLdouble) x, (GLdouble) realy, 0.0,                mvmatrix, projmatrix, viewport, &wx, &wy, &wz);             printf ("World coords at z=0.0 are (%f, %f, %f)\n",                wx, wy, wz);            gluUnProject ((GLdouble) x, (GLdouble) realy, 1.0,                mvmatrix, projmatrix, viewport, &wx, &wy, &wz);             printf ("World coords at z=1.0 are (%f, %f, %f)\n",                wx, wy, wz);         }         break;      case GLUT_RIGHT_BUTTON:         if (state == GLUT_DOWN)            exit(0);         break;      default:         break;   }}/* ARGSUSED1 */void keyboard(unsigned char key, int x, int y){   switch (key) {      case 27:         exit(0);         break;   }}/*  *  Open window, register input callback functions */int main(int argc, char** argv){   glutInit(&argc, argv);   glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);   glutInitWindowSize (500, 500);    glutInitWindowPosition (100, 100);   glutCreateWindow (argv[0]);   glutDisplayFunc(display);    glutReshapeFunc(reshape);    glutKeyboardFunc (keyboard);   glutMouseFunc(mouse);   glutMainLoop();   return 0;}

上面代码的运行结果是,鼠标点击窗口,获得窗口坐标,然后将窗口坐标,转换成物体坐标,输出到控制台窗口。

输入是通过鼠标获得,深度值,设置的为0。输出,显示到控制到窗口。
至于三大参数,模型视图矩阵,投影矩阵,视口,都是通过从OpenGL中获取得到。

gluUnProject4

当深度值为非标准深度值时,即超出【0, 1】范围的深度值,使用gluUnProject4来进行逆变换。下面是gluUnProject4的原型。

int gluUnProject4(GLdouble winx, GLdouble winy, GLdouble winz, GLdouble clipw, const GLdouble modelMatrix[16], const GLdouble projMatrix[16], const GLint viewport[4],GLclampd zNear, GLclampd zFar, GLdouble *objx, GLdouble *objy, GLdouble *objz, GLdouble *objw);

这个函数的参数含义,和gluUnProject的含义一样,只是单个点对应的参数为4个, (winx, winy, winz, clipw)表示输入点,即屏幕上的点。输出点为(*objx, *objy, *objz, *objw)。

模拟变换

所谓模拟变换,就是指模拟OpenGL,将一个物体坐标点,从三维坐标,变换到屏幕坐标。

int gluProject(GLdouble objx, GLdouble objy, GLdouble objz, const GLdouble modelMatrix[16], const GLdouble projMatrix[16], const GLint viewport[4], GLdouble *winx, GLdouble *winy, GLdouble *winz);

我觉得这一小节的内容,以后用来深挖OpenGL的原理,是有用的,省去了自己写矩阵乘法运行的代码。

0 0
原创粉丝点击