其他分享
首页 > 其他分享> > 图形变换——变速移动

图形变换——变速移动

作者:互联网

#include <GL/glut.h>
#include <iostream>
using namespace std;
#include <stdlib.h>

GLfloat rtri = 0.0; //金字塔旋转角度
GLfloat speed = 0.0;
GLfloat WYDistance = -5.0;
GLfloat level = -1.0;

void init(void)

{

    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

    glShadeModel(GL_SMOOTH); //设为smooth处理方式,注意看区别

    glEnable(GL_DEPTH_TEST); //激活深度测试
}

void display(void)

{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //清除颜色缓存和深度缓存

    glLoadIdentity();

    glTranslatef(level, 0.0f, WYDistance); //请尝试修改此函数,掌握其用法与参数含义

    glRotatef(rtri, 0.0f, 1.0f, 0.0f); //请尝试修改此函数,掌握其用法与参数含义

    glBegin(GL_TRIANGLES);

    glColor3f(1.0f, 0.0f, 0.0f);

    glVertex3f(0.0f, 1.0f, 0.0f);

    glColor3f(0.0f, 1.0f, 0.0f);

    glVertex3f(-1.0f, -1.0f, 1.0f);

    glColor3f(0.0f, 0.0f, 1.0f);

    glVertex3f(1.0f, -1.0f, 1.0f);

    glColor3f(1.0f, 0.0f, 0.0f);

    glVertex3f(0.0f, 1.0f, 0.0f);

    glColor3f(0.0f, 0.0f, 1.0f);

    glVertex3f(1.0f, -1.0f, 1.0f);

    glColor3f(0.0f, 1.0f, 0.0f);

    glVertex3f(1.0f, -1.0f, -1.0f);

    glColor3f(1.0f, 0.0f, 0.0f);

    glVertex3f(0.0f, 1.0f, 0.0f);

    glColor3f(0.0f, 1.0f, 0.0f);

    glVertex3f(1.0f, -1.0f, -1.0f);

    glColor3f(0.0f, 0.0f, 1.0f);

    glVertex3f(-1.0f, -1.0f, -1.0f);

    glColor3f(1.0f, 0.0f, 0.0f);

    glVertex3f(0.0f, 1.0f, 0.0f);

    glColor3f(0.0f, 0.0f, 1.0f);

    glVertex3f(-1.0f, -1.0f, -1.0f);

    glColor3f(0.0f, 1.0f, 0.0f);

    glVertex3f(-1.0f, -1.0f, 1.0f);

    glEnd();

    rtri = rtri + speed; //加一个角度

    glutSwapBuffers(); //交换双缓存
}

void reshape(int width, int height)

{

    glViewport(0, 0, width, height);

    glMatrixMode(GL_PROJECTION);

    glLoadIdentity();

    gluPerspective(45.0f, (GLfloat)width / (GLfloat)height, 0.1f, 100.0f);

    glMatrixMode(GL_MODELVIEW);

    glLoadIdentity();
}

void keyboard(unsigned char key, int x, int y)

{ //请参考"变换示例参考"一文,考虑添加键盘命令,交互式来控制金字塔的旋转

    switch (key)

    {

    case 'w':

        WYDistance = WYDistance + 1.0;

        break;
    case 's':

        WYDistance = WYDistance - 1.0;

        break;
    case 'a':
        level = level - 0.1;
        break;
    case 'd':
        level = level + 0.1;
        break;

    default:

        break;
    }
}

void mouse(int button, int state, int x, int y) //监听鼠标动作
{
    if (button == GLUT_LEFT_BUTTON && state == GLUT_UP)
    {
        speed = speed + 5.0;
    }
    else if (button == GLUT_RIGHT_BUTTON && state == GLUT_UP)
    {
        speed = speed - 5.0;
    }
}

int main(int argc, char **argv)

{

    cout << "操作手册:  " << endl;
    cout << "            鼠标左键——加快速度;" << endl;
    cout << "            鼠标右键——减慢速度;" << endl;
    cout << "               w    ——变近;" << endl;
    cout << "               s    ——变远;" << endl;
    cout << "               a    ——左移;" << endl;
    cout << "               d    ——右移;" << endl;
    glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); //使用双缓存模式和深度缓存

    glutInitWindowSize(640, 480);

    glutInitWindowPosition(100, 100);

    glutCreateWindow("Transform2");

    init();

    glutDisplayFunc(display);

    glutReshapeFunc(reshape);

    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);

    glutIdleFunc(display); //设置空闲时调用的函数

    glutMainLoop();

    return 0;
}

 

标签:1.0,glVertex3f,变换,0.0,void,int,变速,glColor3f,图形
来源: https://blog.csdn.net/AK47red/article/details/114956584