操纵矩阵堆栈
矩阵堆栈的操作
一切变换操作,都通过矩阵来完成的
入栈
glPushMatrix
出栈
glPopMatrix
入栈void glPushMatrix()
功能:
函数:无
返回值:无
备注:
出栈
void glPopMatrix()
功能:
函数:无
返回值:无
备注:
1.左右平移(按左右键):
#include
#include
#include
GLfloat step = 0.0f;//!!!!
void display()
{
glClear( GL_COLOR_BUFFER_BIT );
glPushMatrix();
glTranslated(step, 0.0f, 0.0f);//平移
glColor3f( 0.0, 1.0, 0.0 );
glBegin( GL_TRIANGLES);
glVertex2f( -0.2, -0.2 );
glVertex2f( -0.2, 0.2 );
glVertex2f( 0.2, 0.2 );
glEnd();
glPopMatrix();
glFlush();
}
void SpecialKeys(int key, int x, int y)
{
if(key == GLUT_KEY_LEFT)
step -= 0.1f;
if(key == GLUT_KEY_RIGHT)
step += 0.1f;
glutPostRedisplay();
}
int main( int argc, char ** argv )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_SINGLE | GLUT_RGB );
glutInitWindowPosition( 100, 100 );
glutInitWindowSize( 400, 400 );
glutCreateWindow( "OpenGL Examples!" );
glutSpecialFunc(SpecialKeys);
glutDisplayFunc( display );
glutMainLoop();
return 0;
}
平移
void glTranslatef(GLfloat x, GLfloat y, GLfloat z)
功能:
参数:
返回值:无
2.缩放
void glScalef(GLfloat x, GLfloat y, GLfloat z)
功能:
参数:
返回值:无
备注:无
#include
#include
#include
GLfloat step = 1.0f;//!!!!!
void display()
{
glClear( GL_COLOR_BUFFER_BIT );
glPushMatrix();
glScalef(step, step, step);//缩放
glColor3f( 0.0, 1.0, 0.0 );
glBegin( GL_TRIANGLES);
glVertex2f( -0.2, -0.2 );
glVertex2f( -0.2, 0.2 );
glVertex2f( 0.2, 0.2 );
glEnd();
glPopMatrix();
glFlush();
}
void SpecialKeys(int key, int x, int y)
{
if(key == GLUT_KEY_LEFT)
step -= 0.1f;
if(key == GLUT_KEY_RIGHT)
step += 0.1f;
glutPostRedisplay();
}
int main( int argc, char ** argv )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_SINGLE | GLUT_RGB );
glutInitWindowPosition( 100, 100 );
glutInitWindowSize( 400, 400 );
glutCreateWindow( “OpenGL Examples!” );
glutSpecialFunc(SpecialKeys);
glutDisplayFunc( display );
glutMainLoop();
return 0;
}
3.旋转
#include
#include
#include
GLfloat step = 0.0f;
void display()
{
glClear( GL_COLOR_BUFFER_BIT );
glPushMatrix();
glRotatef(step, 0.0f, 0.0f, 1.0f);
glColor3f( 0.0, 1.0, 0.0 );
glBegin( GL_TRIANGLES);
glVertex2f( -0.2, -0.2 );
glVertex2f( -0.2, 0.2 );
glVertex2f( 0.2, 0.2 );
glEnd();
glPopMatrix();
glFlush();
}
void SpecialKeys(int key, int x, int y)//键盘控制
{
if(key == GLUT_KEY_UP)
step += 30.0f;
if(key == GLUT_KEY_DOWN)
step -= 30.0f;
step = (GLfloat)((int)step % 360);
glutPostRedisplay();
}
int main( int argc, char ** argv )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_SINGLE | GLUT_RGB );
glutInitWindowPosition( 100, 100 );
glutInitWindowSize( 400, 400 );
glutCreateWindow( "OpenGL Examples!" );
glutSpecialFunc(SpecialKeys);
glutDisplayFunc( display );
glutMainLoop();
return 0;
}
void glRotatef(GLfloat angle, GLfloat x, GLfloat y, GLfloat z)
功能:
参数:
备注:
#include
#include
#include
GLfloat step = 0.0f;
void display()
{
glClear( GL_COLOR_BUFFER_BIT );
glColor3f(1.0, 1.0, 0.0);
glPointSize(10);
glBegin( GL_POINTS );
glVertex2f( 0.0, 0.0 );
glEnd();
glPushMatrix();
glTranslatef(0.2f,0.2f,0.0f);
glRotatef(step, 0.0f, 0.0f, 1.0f);
//glTranslatef(0.2f,0.2f,0.0f);
glColor3f( 0.0, 1.0, 0.0 );
glBegin( GL_POLYGON );
glVertex2f( -0.2, -0.2 );
glVertex2f( -0.2, 0.2 );
glVertex2f( 0.2, 0.2 );
glVertex2f( 0.2, -0.2 );
glEnd();
glPopMatrix();
glFlush();
}
void SpecialKeys(int key, int x, int y)
{
if(key == GLUT_KEY_UP)
step += 1.0f;
if(key == GLUT_KEY_DOWN)
step -=1.0f;
glutPostRedisplay();
}
int main( int argc, char ** argv )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_SINGLE | GLUT_RGB );
glutInitWindowPosition( 100, 100 );
glutInitWindowSize( 400, 400 );
glutCreateWindow( “OpenGL Examples!” );
glutSpecialFunc(SpecialKeys);
glutDisplayFunc( display );
glutMainLoop();
return 0;
}
…
#include
#include
#include
#include
GLfloat step = 0.0f;
void display()
{
glClear( GL_COLOR_BUFFER_BIT );
glPushMatrix();
GLfloat rotateYMatrix[]=
{
cos(step),sin(step),0.0f,0.0f,
-sin(step),cos(step),0.0f,0.0f,
0.0f,0.0f,1.0f,0.0f,
0.0f,0.0f,0.0f,1.0f
};
glMultMatrixf(rotateYMatrix);
glColor3f( 0.0, 1.0, 0.0 );
glBegin( GL_TRIANGLES);
glVertex2f( -0.2, -0.2 );
glVertex2f( -0.2, 0.2 );
glVertex2f( 0.2, 0.2 );
glEnd();
glPopMatrix();
glFlush();
}
void SpecialKeys(int key, int x, int y)
{
if(key == GLUT_KEY_UP)
step += 0.2f;
if(key == GLUT_KEY_DOWN)
step -= 0.2f;
glutPostRedisplay();
}
int main( int argc, char ** argv )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_SINGLE | GLUT_RGB );
glutInitWindowPosition( 100, 100 );
glutInitWindowSize( 400, 400 );
glutCreateWindow( "OpenGL Examples!" );
glutSpecialFunc(SpecialKeys);
glutDisplayFunc( display );
glutMainLoop();
return 0;
}
…
平移+旋转
#include
#include
#include
#include
GLfloat step = 0.0f;
void display()
{
glClear( GL_COLOR_BUFFER_BIT );
glColor3f(1.0, 1.0, 0.0);
glPointSize(10);
glBegin( GL_POINTS );
glVertex2f( 0.0, 0.0 );
glEnd();
glPushMatrix();
glTranslatef(step,0.2f,0.0f);
glRotatef((step*100), 0.0f, 0.0f, 1.0f);
//glTranslatef(0.2f,0.2f,0.0f);
glColor3f( 0.0, 1.0, 0.0 );
glBegin( GL_POLYGON );
glVertex2f( -0.2, -0.2 );
glVertex2f( -0.2, 0.2 );
glVertex2f( 0.2, 0.2 );
glVertex2f( 0.2, -0.2 );
glEnd();
glPopMatrix();
glFlush();
}
void SpecialKeys(int key, int x, int y)
{
if(key == GLUT_KEY_UP)
step += 0.1f;
if(key == GLUT_KEY_DOWN)
step -= 0.1f;
glutPostRedisplay();
}
int main( int argc, char ** argv )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_SINGLE | GLUT_RGB );
glutInitWindowPosition( 100, 100 );
glutInitWindowSize( 400, 400 );
glutCreateWindow( "OpenGL Examples!" );
glutSpecialFunc(SpecialKeys);
glutDisplayFunc( display );
glutMainLoop();
return 0;
}
…
先旋转再平移
glRotatef(step, 0.0f, 0.0f, 1.0f);
glTranslatef(0.2f,0.2f,0.0f);
先旋转后平移,相当于旋转中心点为(0,0,0)
4.键盘操控:用ad控制
#include
#include
#include
GLfloat step = 0.0f;
void display()
{
glClear( GL_COLOR_BUFFER_BIT );
glColor3f(1.0, 1.0, 0.0);
glPointSize(10);
glBegin( GL_POINTS );
glVertex2f( 0.0, 0.0 );
glEnd();
glPushMatrix();
glTranslatef(0.2f,0.2f,0.0f);
glRotatef(step, 0.0f, 0.0f, 1.0f);
//glTranslatef(0.2f,0.2f,0.0f);
glColor3f( 0.0, 1.0, 0.0 );
glBegin( GL_POLYGON );
glVertex2f( -0.2, -0.2 );
glVertex2f( -0.2, 0.2 );
glVertex2f( 0.2, 0.2 );
glVertex2f( 0.2, -0.2 );
glEnd();
glPopMatrix();
glFlush();
}
void NormalKeys(unsigned char key, int x, int y)//键盘
{
if(key == 'a')
step += 5.0f;
if(key == 'd')
step -= 5.0f;
step = (GLfloat)((int)step % 360);
glutPostRedisplay();
}
int main( int argc, char ** argv )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_SINGLE | GLUT_RGB );
glutInitWindowPosition( 100, 100 );
glutInitWindowSize( 400, 400 );
glutCreateWindow( "OpenGL Examples!" );
//glutSpecialFunc(SpecialKeys);
glutKeyboardFunc(NormalKeys);//!!!!!!!!!!
glutDisplayFunc( display );
glutMainLoop();
return 0;
}