-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrotating_star.cpp.txt
More file actions
236 lines (208 loc) · 4.32 KB
/
rotating_star.cpp.txt
File metadata and controls
236 lines (208 loc) · 4.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
* ROTATING STARS
*
*
*/
#include<stdio.h>
#include <windows.h>
#include<stdlib.h>
#include<math.h>
//#include<times.h>
#include<GL/glut.h>
#define INNER_RADIUS 0.11
//#define INNER_REDIUS 1.1
//#define OUTER_RADIUS 1.3
#define OUTER_RADIUS 1.0
#define NUM_STAR_POINTS 8
#define NUM_STARS 8
#define ROT_INC 0.8
#define STAR_IDX 8
#define M_PI 22/7
static GLfloat g_rotate=0;
int scene;
static GLfloat g_rotateInc=ROT_INC;
// Step size in x and y directions
// (number of pixels to move each time)
GLfloat xstep = 1.0f;
GLfloat ystep = 1.0f;
GLsizei rsize = 50;
int al=1;
// Keep track of windows changing width and height
GLfloat windowWidth;
GLfloat windowHeight;
int flag=0;
int a=0;
void Draw_Stars(GLfloat inner,GLfloat outer,int numpoints)
{
GLfloat step=M_PI*2.0/(GLfloat)(2*numpoints);
register int i;
GLfloat angle,r;
glBegin(GL_POLYGON);
for(i=0;i<numpoints*2;++i)
{
r=(i%2==0?inner:outer);
angle=i*step;
glVertex2f(r*cos(angle),r*sin(angle));
}
glEnd();
}
void Background1(void)
{
glColor3f(1.0,1.5,1.0);
glBegin(GL_POLYGON);
glVertex2f(-800,-800);
glVertex2f(-800,800);
glVertex2f(800,800);
glVertex2f(800,-800);
glEnd();
glFlush();
}
void Background2(void)
{ if(al!=0)
{
glColor3f(1.0,0.5,1.5);
glBegin(GL_POLYGON);
glVertex2f(-800,-800);
glVertex2f(-800,800);
glVertex2f(800,800);
glVertex2f(800,-800);
glEnd();
glFlush();
glutSwapBuffers();
} }
void Display(void)
{
register int i;
register GLfloat c;
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT);
switch(scene)
{
case 1:
for(i=0;i<NUM_STARS;++i)
{
glPushMatrix();
glRotatef((360.0/NUM_STARS*i),0,0,1);
glTranslatef(OUTER_RADIUS,0,0);
if(a==1)
glRotatef(g_rotate+(3*i),0,1,1);
else
glRotatef(g_rotate-(3*i),1,0,1);
c=1.0/NUM_STARS*(GLfloat)i;
glColor3f(1.0-c,0.0,c);
glCallList(STAR_IDX);
glPopMatrix();
}
break;
}
glFlush();
glutSwapBuffers();
}
void myReshape(int w,int h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(w<=h)
glOrtho(-2.0,2.0,-2.0*(GLfloat)h/(GLfloat)w,2.0*(GLfloat)h/(GLfloat)w,-2.0,2.0);
else
glOrtho(-2.0*(GLfloat)w/(GLfloat)h,2.0*(GLfloat)w/(GLfloat)h,-2.0,2.0,-2.0,2.0);
glMatrixMode(GL_MODELVIEW);
}
void menu(int value){
switch(value){
case 1:Display();
break;
case 2: glutIdleFunc(NULL);
break;
case 3: exit(0);
break;
}
}
void myKey(unsigned char k,int x ,int y)
{
switch(k){
case'm':
{
a=1;
glClear(GL_COLOR_BUFFER_BIT);
//Background2();
glFlush();
break;
}
case's':
{
glClearColor(1.0,1.0,1.0,0);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glFlush();
break;
}
// case 'b':
// TimerFunction(1);
// break;
case'r':
{
a=0;
Background1();
break;
}
case'q':
case'Q': exit(0);
break;
case 'n':
scene=scene+1;
break;
default:
printf("unknown printable command\'%c\'.\n",k);
break;
}
}
void myMouse(int btn,int state,int x,int y)
{
if(btn==GLUT_LEFT_BUTTON&&state==GLUT_DOWN)g_rotateInc=ROT_INC;
if(btn==GLUT_MIDDLE_BUTTON&&state==GLUT_DOWN)g_rotateInc=ROT_INC;
if(btn==GLUT_RIGHT_BUTTON&&state==GLUT_DOWN)g_rotateInc=ROT_INC;
//glutSwapBuffers();
//glutPostRedisplay();
}
void myIdleFunc(void)
{
if(a==0)
{
g_rotate-=g_rotateInc;
glutSwapBuffers();
glutPostRedisplay();
}
else{
g_rotate+=g_rotateInc;
glutSwapBuffers();
glutPostRedisplay();
}
}
int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutInitWindowSize(800,800);
glutInitWindowPosition(0,0);
glutCreateWindow("Star Instance Test");
glutCreateMenu(menu);
glutAddMenuEntry("start",1);
glutAddMenuEntry("stop",2);
glutAddMenuEntry("quit",3);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutReshapeFunc(myReshape);
glutDisplayFunc(Display);
glutIdleFunc(myIdleFunc);
glutKeyboardFunc(myKey);
// glutTimerFunc(33, TimerFunction, 1);
glutMouseFunc(myMouse);
glNewList(STAR_IDX,GL_COMPILE);
Draw_Stars(INNER_RADIUS,OUTER_RADIUS,NUM_STAR_POINTS);
glEndList();
glutMainLoop();
//return 0;
}