本站首页    管理页面    写新日志    退出


«July 2025»
12345
6789101112
13141516171819
20212223242526
2728293031


公告

☆★☆★☆★☆★☆★☆
生活的点点记录,以及一些体会...........

喜欢是淡淡的爱,爱是深深的喜欢.

时间会见证一切.......................

欢迎大家指出错误,共同进步..........

期待中..............................

☆★☆★☆★☆★☆★☆


我的分类(专题)

日志更新

最新评论

留言板

链接

世纪音频

 

 


Blog信息
blog名称:
日志总数:162
评论数量:312
留言数量:0
访问次数:939797
建立时间:2005年5月17日




[work]opengl中从3d到2d的坐标变换(转)
原创空间

oceanblue 发表于 2008/11/26 21:14:25

首先说一下opengl中的矩阵表示, 一般在c/c++中定义的矩阵和opengl中的矩阵分别是: /***************************************************************************** We define a 4x4 matrix array, OpenGL linear matrix format: referenced as Row,Column as: | 0,0 0,1 0,2 0,3 | |a0 a4 a8 a12| | | | | | 1,0 1,1 1,2 1,3 | |a1 a5 a9 a13| | | | | | 2,0 2,1 2,2 2,3 | |a2 a6 a10 a14| | | | | | 3,0 3,1 3,2 3,3 | |a3 a7 a11 a15| */ 两者行和列正好相反,因此,对于一个4×4的矩阵GLdouble m[16], 那么在操作这个矩阵时必须小心。 下面在说opengl中从物体坐标变换到窗口坐标的算法(实际上就是mesa中gluProject的实现): GLint GLAPIENTRY gluProject(GLdouble objx, GLdouble objy, GLdouble objz, const GLdouble model[16], const GLdouble proj[16], const GLint viewport[4], GLdouble * winx, GLdouble * winy, GLdouble * winz) { /* matrice de transformation */ GLdouble in[4], out[4]; /* initilise la matrice et le vecteur a transformer */ in[0] = objx; in[1] = objy; in[2] = objz; in[3] = 1.0; transform_point(out, model, in); transform_point(in, proj, out); /* d’ou le resultat normalise entre -1 et 1 */ if (in[3] == 0.0) return GL_FALSE; in[0] /= in[3]; in[1] /= in[3]; in[2] /= in[3]; /* en coordonnees ecran */ *winx = viewport[0] + (1 + in[0]) * viewport[2] / 2; *winy = viewport[1] + (1 + in[1]) * viewport[3] / 2; /* entre 0 et 1 suivant z */ *winz = (1 + in[2]) / 2; return GL_TRUE; } /* * Transform a point (column vector) by a 4x4 matrix. I.e. out = m * in * Input: m - the 4x4 matrix * in - the 4x1 vector * Output: out - the resulting 4x1 vector. */ static void transform_point(GLdouble out[4], const GLdouble m[16], const GLdouble in[4]) { #define M(row,col) m[col*4+row] out[0] = M(0, 0) * in[0] + M(0, 1) * in[1] + M(0, 2) * in[2] + M(0, 3) * in[3]; out[1] = M(1, 0) * in[0] + M(1, 1) * in[1] + M(1, 2) * in[2] + M(1, 3) * in[3]; out[2] = M(2, 0) * in[0] + M(2, 1) * in[1] + M(2, 2) * in[2] + M(2, 3) * in[3]; out[3] = M(3, 0) * in[0] + M(3, 1) * in[1] + M(3, 2) * in[2] + M(3, 3) * in[3]; #undef M }


阅读全文(2413) | 回复(0) | 编辑 | 精华
 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.031 second(s), page refreshed 144770623 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号