Author: mznewfacer (Wolf Geek) Time: August 1, November 30, 2011
You are welcome to reprint it. Please indicate the source!
I have to write a thesis for the past two days. Well, I have to cut down arduino first. It seems that I can only do one thing at the same time and seriously doubt my IQ! By the way, let's write some basic summary. It is used after preparation. It may be boring, but it must be emphasized that these are the mathematical foundations of graphics and images!
Let's first look at several transformation formulas:
So far, all the transformation sets on the two-dimensional plane have been described, as shown in the specific form.
For a three-dimensional image, the corresponding transformation matrix is changed to a fourth-order matrix.
This is the initial coordinate system. The Level 4 homogeneous matrix is a level 4 unit array.
Draw [0.5; 0.5; 0.5] On the coordinate system, and then perform projection on the Oxy plane. The result is shown in:
The transformation matrix is
Then, the coordinates are rotated 45 degrees around the Z axis and 1.5 scales are translated in the Y axis direction. The transformation matrix is
Finally, rotate 45 degrees successively around the X axis, Y axis, and Z axis, as shown in the following figure:
The transformation matrix can be pushed as follows:
As for why the homogeneous coordinate system is introduced, no additional explanation is provided here. For details, see here.
Finally, use opengl to draw a similar coordinate system transformation,
# Include <GL/glut. h> float w, h, tip = 0, turn = 0; float ORG [3] = {0, 0}; float XP [3] = {1, 0, 0 }, XN [3] = {-, 0}, YP [3] = {, 0}, YN [3] = {0 }, ZP [3] = {0, 0}, ZN [3] = {0,-1}, VP [3] = {0, 0,-5 }; void reshape (int nw, int nh) {w = nw; h = nh;} void Turn (int key, int x, int y) {switch (key) {case glu_key_right: turn + = 5; break; case glu_key_left: turn-= 5; break; case glu_key_up: tip-= 5; break; case glu_key_down: tip + = 5; break ;}} void Draw_Axes (void) {glPushMatrix (); glTranslatef (-1.5,-1.5,-5); glRotatef (tip, 0); glRotatef (turn, 0 ); glScalef (1, 1, 1); glLineWidth (2.0); glBegin (GL_LINES); glColor3f (1, 0); // X axis is red. glVertex3fv (ORG); glVertex3fv (XP); glColor3f (0, 1); // Y axis is green. glVertex3fv (ORG); glVertex3fv (YP); glColor3f (0, 0, 1); // z axis is blue. glVertex3fv (ORG); glVertex3fv (ZP); glEnd (); glTranslatef (1, 1, 1); glBegin (GL_LINES); glColor3f (1, 0, 0 ); // X axis is red. glVertex3fv (ORG); glVertex3fv (XN); glColor3f (0, 1); // Y axis is green. glVertex3fv (ORG); glVertex3fv (YN); glColor3f (0, 0, 1); // z axis is blue. glVertex3fv (ORG); glVertex3fv (ZN); glEnd (); glTranslatef (-1, 0, 0); glRotatef (-90, 0, 0); glBegin (GL_LINES ); glColor3f (0, 0, 1); // X axis is blue. glVertex3fv (ORG); glVertex3fv (XN); glColor3f (0, 1); // Y axis is green. glVertex3fv (ORG); glVertex3fv (YN); glColor3f (1, 0); // z axis is redglVertex3fv (ORG); glVertex3fv (ZN); glEnd (); glPointSize (4.0f); glRotatef (-45, 0); glBegin (GL_POINTS); glColor3f (, 0); // X axis is red. glVertex3f (-1.0f,-1.0f, 0.0f); glEnd (); glRotatef (-90, 0); glBegin (GL_POINTS); glColor3f (, 0 ); glVertex3f (1.0f,-1.0f, 0.0f); glEnd (); glLoadIdentity (); glBegin (GL_POINTS); glColor3f (1,0, 0); glVertex3fv (VP); glEnd (); glPopMatrix ();} void display (void) {glViewport (0, 0, w, h); glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); Draw_Axes (); fig ();} void main (void) {maid (600,400 ); glutIdleFunc (display); glureshapefunc (reshape); gluspecialfunc (Turn); glClearColor (0.1, 0.2, 0.1, 1.0); glable (gradient); glMatrixMode (GL_PROJECTION); gluPerspective (50.0, 1.5, 1.0, 10.0); glMatrixMode (GL_MODELVIEW); glumainloop ();}
As for the specific coordinate system transformation of opengl, such as the conversion from the world coordinate system to the observed coordinate system, how to use the GL_PROJECTION projection matrix to project three dimensions to two dimensions, choose glFrustum () or glOrtho () For projection (), and the specific principle of each transformation, Song
Ho Ahn's blog has already been quite detailed, and there are two examples in it. The theoretical part and the example part.