第三弹 不同方向旋转的三角形和矩形

 1 /*************************************************************************
 2     > File Name: frame.cpp
 3     > Author: 
 4     > Mail: 
 5     > Created Time: 2015年11月21日 星期六 15时28分01秒
 6  ************************************************************************/
 7 
 8 #include<iostream>
 9 #include<GL/glut.h>
10 using namespace std;
11 static GLfloat spin=0.0;
12 void init(void)
13 {
14     glClearColor(0.0,0.0,0.0,0.0);
15     glShadeModel(GL_SMOOTH);
16 }
17 
18 void display(void)
19 {
20     glClear(GL_COLOR_BUFFER_BIT);
21     glPushMatrix();
22     glColor3f(1.0,1.0,1.0);
23 
24     glRotatef(spin,0.0f,0.0f,1.0f);
25     glBegin(GL_TRIANGLES);
26         glColor3f(1.0f,0.0f,0.0f);
27         glVertex3f(0.0f,0.0f,0.0f);
28         glColor3f(0.0f,1.0f,0.0f);
29         glVertex3f(30.0f,0.0f,0.0f);
30         glColor3f(0.0f,0.0f,1.0f);
31         glVertex3f(15.0f,50.0f,0.0f);
32     glEnd();
33 
34     glLoadIdentity();
35     glRotatef(spin,1.0f,0.0f,0.0f);
36     glBegin(GL_QUADS);
37         glVertex3f(0.0f,0.0f,0.0f);
38         glVertex3f(-30.0f,0.0f,0.0f);
39         glVertex3f(-30.0f,30.0f,0.0f);
40         glVertex3f(0.0f,30.0f,0.0f);
41     glEnd();
42     glPopMatrix();
43     glutSwapBuffers();
44 }
45 
46 void spinDisplay(void)
47 {
48     spin=spin+2.0;
49     if(spin>360)
50         spin=spin-360.0;
51     glutPostRedisplay();
52 }
53 
54 void reshape(int w,int h)
55 {
56     glViewport(0,0,(GLsizei)w,(GLsizei)h);
57     glMatrixMode(GL_PROJECTION);
58     glLoadIdentity();
59     glOrtho(-50.0,50.0,-50.0,50.0,-1.0,1.0);
60     glMatrixMode(GL_MODELVIEW);
61     glLoadIdentity();
62 }
63 
64 void mouse(int button,int state,int x,int y)
65 {
66     switch(button)
67     {
68         case GLUT_LEFT_BUTTON:
69             if(state == GLUT_DOWN)
70                 glutIdleFunc(spinDisplay);
71             break;
72         case GLUT_MIDDLE_BUTTON:
73             if(state == GLUT_DOWN)
74                 glutIdleFunc(NULL);
75             break;
76         default:
77             break;
78     }
79 }
80 
81 int main(int argc,char ** argv)
82 {
83     glutInit(&argc,argv);
84     glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
85     glutInitWindowSize(400,400);
86     glutInitWindowPosition(100,100);
87     glutCreateWindow("OpenGL test");
88     init();
89 
90     glutDisplayFunc(display);
91     glutReshapeFunc(reshape);
92     glutMouseFunc(mouse);
93 
94     glutMainLoop();
95     return 0;
96 }

 

效果如图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值