﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-mhorse</title><link>http://www.cppblog.com/mhorse/</link><description /><language>zh-cn</language><lastBuildDate>Tue, 09 Jun 2026 18:53:16 GMT</lastBuildDate><pubDate>Tue, 09 Jun 2026 18:53:16 GMT</pubDate><ttl>60</ttl><item><title>OpenGL: 3D坐标到屏幕坐标的转换逻辑(转)</title><link>http://www.cppblog.com/mhorse/archive/2008/11/22/67583.html</link><dc:creator>mhorse</dc:creator><author>mhorse</author><pubDate>Sat, 22 Nov 2008 06:30:00 GMT</pubDate><guid>http://www.cppblog.com/mhorse/archive/2008/11/22/67583.html</guid><wfw:comment>http://www.cppblog.com/mhorse/comments/67583.html</wfw:comment><comments>http://www.cppblog.com/mhorse/archive/2008/11/22/67583.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/mhorse/comments/commentRss/67583.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/mhorse/services/trackbacks/67583.html</trackback:ping><description><![CDATA[<font size=2>遇到需要将3D坐标转换到屏幕坐标的问题，在网上很多朋友也在寻找答案，下面是glu中gluProject函数的实现。<br>矩阵按行优先存储<br><br>GLint gluProject(GLdouble objx, GLdouble objy, GLdouble objz, <br>const GLdouble model[16], const GLdouble proj[16], <br>const GLint viewport[4], <br>GLdouble * winx, GLdouble * winy, GLdouble * winz) <br>{ <br>/* matrice de transformation */ <br>GLdouble in[4], out[4]; <br><br>/* initilise la matrice et le vecteur a transformer */ <br>in[0] = objx; <br>in[1] = objy; <br>in[2] = objz; <br>in[3] = 1.0; <br>transform_point(out, model, in); <br>transform_point(in, proj, out); <br><br>/* d&#8217;ou le resultat normalise entre -1 et 1 */ <br>if (in[3] == 0.0) <br>return GL_FALSE; <br><br>in[0] /= in[3]; <br>in[1] /= in[3]; <br>in[2] /= in[3]; <br><br>/* en coordonnees ecran */ <br>*winx = viewport[0] + (1 + in[0]) * viewport[2] / 2; <br>*winy = viewport[1] + (1 + in[1]) * viewport[3] / 2; <br>/* entre 0 et 1 suivant z */ <br>*winz = (1 + in[2]) / 2; <br>return GL_TRUE; <br>} <br><br><br>/* <br>* Transform a point (column vector) by a 4x4 matrix. I.e. out = m * in <br>* Input: m - the 4x4 matrix <br>* in - the 4x1 vector <br>* Output: out - the resulting 4x1 vector. <br>*/ <br>static void <br>transform_point(GLdouble out[4], const GLdouble m[16], const GLdouble in[4]) <br>{ <br>#define M(row,col) m[col*4+row] <br>out[0] = <br>M(0, 0) * in[0] + M(0, 1) * in[1] + M(0, 2) * in[2] + M(0, 3) * in[3]; <br>out[1] = <br>M(1, 0) * in[0] + M(1, 1) * in[1] + M(1, 2) * in[2] + M(1, 3) * in[3]; <br>out[2] = <br>M(2, 0) * in[0] + M(2, 1) * in[1] + M(2, 2) * in[2] + M(2, 3) * in[3]; <br>out[3] = <br>M(3, 0) * in[0] + M(3, 1) * in[1] + M(3, 2) * in[2] + M(3, 3) * in[3]; <br>#undef M <br>} </font><br style="mso-special-character: line-break">
<img src ="http://www.cppblog.com/mhorse/aggbug/67583.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/mhorse/" target="_blank">mhorse</a> 2008-11-22 14:30 <a href="http://www.cppblog.com/mhorse/archive/2008/11/22/67583.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>