码迷,mamicode.com
首页 > 移动开发 > 详细

[opengl] 画一个可移动的自行车 二维几何变换(平移、旋转、缩放)

时间:2018-11-15 13:40:13      阅读:324      评论:0      收藏:0      [点我收藏+]

标签:view   ike   load   isp   awb   变换   arc   img   区域   

技术分享图片

#include <cmath>
#include "glut.h"
#include "iostream"
using namespace std;

void init(void)
{
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    glClear(GL_COLOR_BUFFER_BIT);
    cout << "init.." << endl;
    glLineWidth(1.0f);
    glColor3f(1.0, 0.0, 0.0);
    glMatrixMode(GL_PROJECTION);//设置投影矩阵
    glLoadIdentity();
    gluOrtho2D(0.0, 600.0, 0.0, 600.0);//二维视景区域 左下角为原点

    //glClear(GL_COLOR_BUFFER_BIT);
    //glMatrixMode(GL_MODELVIEW);
    //glLoadIdentity();

}

int dir = 0;
int angle = 0;
// 绘制车轮
void DrawWheel(double x, double y, double r)
{
    int sec = 10;
    for (int i = 0; i <= sec; i++)
    {
        double delta = 3.1415926*2.0 / sec;
        glBegin(GL_LINE_LOOP);
        glVertex2f(x, y);
        glVertex2f(x + r * cos(delta*i), y + r * sin(delta*i));
        glVertex2f(x + r * cos(delta*(i + 1)), y + r * sin(delta*(i + 1)));
        glEnd();
    }

}

//绘制自行车
void DrawBike() {
    glClear(GL_COLOR_BUFFER_BIT);//清除窗口显示内容
    glColor3f(1.0f, 0.0f, 0);

    glPushMatrix();
    glTranslatef(100+dir, 124, 0);
    // 横车杆
    glBegin(GL_LINES);
    glVertex2f(0,0);
    glVertex2f(100,0);
    // 竖车杆
    glVertex2f(70, 0);
    glVertex2f(70, 30);
    // 车把
    glVertex2f(60, 30);
    glVertex2f(80, 30);
    glEnd();
    glPopMatrix();


    // 前车轮
    glPushMatrix();
    glTranslatef(100+dir, 100, 0);
    glRotatef(angle, 0, 0, 1);
    DrawWheel(0,0,25);
    glPopMatrix();

    // 后车轮
    glPushMatrix();
    glTranslatef(200+dir, 100, 0);
    glRotatef(angle, 0, 0, 1);
    DrawWheel(0, 0, 25);;
    glPopMatrix();

    
    glBegin(GL_LINES);
    glVertex2f(0, 75);
    glVertex2f(600, 75);
    glEnd();
    glutSwapBuffers();
}


void keyboard(unsigned char key, int x, int y)
{
    if (key == ‘a‘)// 向左平移
    {
        cout << "左移" << endl;
        dir -= 10;
        angle += 10;
        glutPostRedisplay();//重绘窗口
    }
    if (key == ‘d‘)// 向右平移
    {
        cout << "右移" << endl;
        dir += 10;
        angle -= 10;
        glutPostRedisplay();//重绘窗口
    }
}

void main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowPosition(300, 100);
    glutInitWindowSize(600, 600);
    glutCreateWindow("lab5");


    glutDisplayFunc(DrawBike);
    init();

    glutKeyboardFunc(keyboard);
    glutMainLoop();

}

[opengl] 画一个可移动的自行车 二维几何变换(平移、旋转、缩放)

标签:view   ike   load   isp   awb   变换   arc   img   区域   

原文地址:https://www.cnblogs.com/ruoh3kou/p/9962578.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!