基于图形之能动起来的语法基础

操纵矩阵堆栈

  • 矩阵堆栈的操作

    • 入栈(保存当前矩阵)
    • 出栈(恢复上一次保存的矩阵)
  • 一切变换操作,都通过矩阵来完成的

  • 入栈

    • 保存当前变换效果
    • 实现函数: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)

  • 功能:

    • 场景平移
  • 参数:

    • x: X轴方向平移的距离
    • y: Y轴方向平移的距离
    • z: Z轴方向平移的距离
  • 返回值:无

2.缩放

  • void glScalef(GLfloat x, GLfloat y, GLfloat z)

  • 功能:

    • 场景缩放
  • 参数:

    • x: X轴方向的缩放因子
    • y: Y轴方向的缩放因子
    • z: 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)

  • 功能:

    • 场景旋转
  • 参数:

    • angle: 旋转的角度
    • x,y,z: 用于指定旋转轴
  • 备注:

    • 绕原点(0,0,0)到(x,y,z)的直线逆时针旋转angle个角度

    #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;
}

你可能感兴趣的:(基于图形之能动起来的语法基础)