Using OpenGL to achieve three-dimensional projection of point-to-screen space

Source: Internet
Author: User

There are two ways of using the Gluproject function:

Gldouble modelview[16]; Gldouble projection[16]; Gldouble out[3], in[3]; Assign value to in ... Glpushmatrix (); Glgetdoublev (Gl_modelview_matrix, Modelview); Glgetdoublev (Gl_projection_matrix, PROJECTION); Gluproject (in[0],in[1],in[2],modelview,projection,viewport,& (out[0]),& (out[1]),& (out[2]));

Gluproject Source:

/* Transform a point (column vector) by a 4x4 matrix. Then, out = M * in input:m-----The 4×4 matrix, in----the 4x1 vector output:out----The resulting 4x1 vector */stat IC 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}//Gluproject source code (description see OpenGL API documentation) Glint Gluproject (gldouble objx, gldouble objy, gldouble objz, const Gldouble modelmatrix[16], const gldouble PROJMATRIX[16], const glint VIEWPORT[4], gldouble *winx, gldouble *winy, GLdoubl E *winz) {//Matrice transformation gldouble in[4], out[4];//initialize matrice and column vector as a transformer in[0] = OBJX; In[1] = objY IN[2] = OBJZ; IN[3] = 1.0; Transform_point (out, Modelmatrix, in); Multiplied by the Model View Matrix Transform_point (in, Projmatrix, out); The fourth item multiplied by the projection matrix//homogeneous vector cannot be 0 if (in[3] = = 0.0) return gl_false; Vector homogeneous normalization in[0]/= in[3]; IN[1]/= in[3]; IN[2]/= in[3]; The function of the viewport vector *winx = viewport[0] + (1 + in[0]) * viewport[2]/2; *winy = viewport[1] + (1 + in[1]) * viewport[3]/2; *winz = (1 + in[2])/2; return gl_true; }

The second method is to implement the Gluproject, according to the implementation of the source code, it is important to note that the OpenGL matrix is the main sequence of the column storage.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.