OpenGL几何变换程序
void glMatrixMode(Glenum mode); //指定当前堆栈。Mode的可 取值:GL_MODELVIEW, GL_PROJECTION, GL_TEXTURE void glLoadIdentity(void) ; //变换矩阵初始化 void glTranslatef(GLfloat x, GLfloat y, GLfloat z); void glTranslated(GLdouble x, GLdouble y, GLdouble z); void glRotatef(GLfloat angle, GLfloat x, GLfloat y, GLfloat z); void glRotated(GLdouble angle, GLdouble x, GLdouble y, GLdouble z); void glScalef}(GLfloat x, GLfloat y, GLfloat z); void glScaled}(GLdouble x, GLdouble y, GLdouble z);
OpenGL的通用变换函数 General-Purpose Transformation Commands glMatrixMode() glLoadIdentity() glTranslate*() glScale*() glRotate *() glLoadMatrix*() glMultMatrix*() glPushMatrix() glPopMatrix()
glLoadMatrix*() void glLoadMatrix{fd}(const TYPE *m); 指定某特定的矩阵为当前矩阵 例如: glFloat m[]={m1,m2,…m15,m16}; glLoadMatrix(m)
glMultMatrix*() void glMultMatrix{fd}(const TYPE *m); 用当前矩阵乘以其所指定的矩阵,结果作为当前矩阵。既C=CM。 m为一个单精度或双精度浮点数指针,指向一个按列存储的44矩阵
glPushMatrix() void glPushMatrix(void); Pushes all matrices in the current stack down one level. The current stack is determined by glMatrixMode(). The topmost matrix is copied, so its contents are duplicated in both the top and second-from-the-top matrix. If too many matrices are pushed, an error is generated.
glPopMatrix() void glPopMatrix(void); Pops the top matrix off the stack. What was the second-from-the-top matrix becomes the top matrix. The current stack is determined by glMatrixMode(). The contents of the topmost matrix are destroyed. If the stack contains a single matrix, calling glPopMatrix() generates an error.
3D变换的实例 void DrawTetra() { glBegin(GL_TRIANGLES); glColor3f(1.0, 0.0, 0.0); glVertex3f(1.0, 0.0, 0.0); glVertex3f(0.0, 1.0, 0.0); glVertex3f(0.0, 0.0, 1.0); glColor3f(0.0, 1.0, 0.0); glVertex3f(0.0, 0.0, 0.0); glColor3f(1.0, 0.0, 1.0); glColor3f(0.5, 0.5, 0.5); glEnd(); }
3D变换的实例 void DrawTetra() { glBegin(GL_TRIANGLES); glColor3f(1.0, 0.0, 0.0); glVertex3f(1.0, 0.0, 0.0); glVertex3f(0.0, 1.0, 0.0); glVertex3f(0.0, 0.0, 1.0); glColor3f(0.0, 1.0, 0.0); glVertex3f(0.0, 0.0, 0.0); glColor3f(1.0, 0.0, 1.0); glColor3f(0.5, 0.5, 0.5); glEnd(); }
变换的实例 可执行程序 源程序 void display(void) { glClear (GL_COLOR_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glPolygonMode(GL_FRONT,GL_LINE); DrawTangle(); glEnable(GL_LINE_STIPPLE); glLineStipple(1, 0xF0F0); glTranslatef(90.0, 0.0, 0.0); glLineStipple(1, 0xF00F); glTranslatef(110.0, 90.0, 0.0); glRotatef(30.0,0.0,0.0,1.0); glDisable(GL_LINE_STIPPLE); glFlush(); } void DrawTangle() { glBegin(GL_TRIANGLES); glColor3f (0.0, 0.0, 1.0); glVertex2f(20.0,20.0); glColor3f (0.0, 1.0, 0.0); glVertex2f(80.0,30.0); glColor3f (1.0, 0.0, 0.0); glVertex2f(50.0,70.0); glEnd(); } 可执行程序 源程序