zoukankan      html  css  js  c++  java
  • [opengl] 画一个可移动的自行车 二维几何变换(平移、旋转、缩放)

    #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();
    
    }
    
  • 相关阅读:
    python模拟shell
    10.LIKE 操作符
    9.TOP 子句--mysql limit
    8.INSERT INTO 语句 UPDATE 语句
    7.ORDER BY 子句
    6.AND & OR 运算符
    5.WHERE 子句
    4.SELECT DISTINCT 语句
    3.SELECT 语句
    2.sql分类
  • 原文地址:https://www.cnblogs.com/ruoh3kou/p/9962578.html
Copyright © 2011-2022 走看看