質問編集履歴

1

ソースコードの追加 文章訂正

2020/06/24 03:57

投稿

ssssssa
ssssssa

スコア4

test CHANGED
File without changes
test CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
 
4
4
 
5
-
5
+ ```
6
6
 
7
7
  #include "GL/glut.h"
8
8
 
@@ -10,7 +10,11 @@
10
10
 
11
11
  #include <stdlib.h>
12
12
 
13
+
14
+
13
- #include <stdio.h>
15
+ #define MAX_DROPS 50000
16
+
17
+ #define GRAVITY -0.0005
14
18
 
15
19
 
16
20
 
@@ -20,7 +24,7 @@
20
24
 
21
25
 
22
26
 
23
- #define M_PI 3.1415926535897932384626433832795
27
+ #define M_PI (3.1415926535897932384626433832795)
24
28
 
25
29
 
26
30
 
@@ -38,93 +42,43 @@
38
42
 
39
43
 
40
44
 
41
- #define MAX_POINTS 5000
45
+ typedef struct {
42
-
46
+
43
- int numPoints;
47
+ int alive;
44
-
48
+
45
- GLfloat curx, cury;
49
+ GLfloat xpos, ypos;
46
-
47
- GLfloat x[MAX_POINTS], y[MAX_POINTS];
50
+
48
-
49
- GLfloat xacc[MAX_POINTS], yacc[MAX_POINTS];
50
-
51
- GLfloat red, green, blue;
51
+ GLfloat xdir, ydir;
52
-
52
+
53
- int step; int length;
53
+ GLfloat mass;
54
-
55
-
56
-
54
+
57
- void initialize()
55
+ } Particle;
58
-
59
- { int j; double temp, temp2;
56
+
60
-
61
-
62
-
57
+
58
+
63
- numPoints = drand48()*(MAX_POINTS-1);
59
+ Particle water[MAX_DROPS];
64
-
65
- curx = -0.5 + drand48();
60
+
66
-
67
- cury = 0.0 + drand48();
68
-
69
-
70
-
71
- red = 0.5 + 0.5*drand48();
72
-
73
- green = 0.5 + 0.5*drand48();
74
-
75
- blue = 0.5 + 0.5*drand48();
76
-
77
- glPointSize(1.5);
61
+ int NumDrops;
78
-
79
- step = 0;
62
+
80
-
81
- length = 700 + 300*drand48();
63
+
82
-
83
-
84
-
85
-
86
-
87
- /* initialize the blast */
64
+
88
-
89
- for (j=0 ; j<numPoints ; j++ ) {
90
-
91
- x[j] = curx;
92
-
93
- y[j] = cury;
94
-
95
- temp = drand48();
96
-
97
- temp2 = drand48()*2.0*M_PI;
98
-
99
- xacc[j] = (cos(temp2) * temp)/length;
100
-
101
- yacc[j] = (sin(temp2) * temp)/length;
102
-
103
- }
104
-
105
-
106
-
107
- }
108
-
109
-
110
-
111
- void draw_blast(void)
65
+ void draw_waterfall(void)
66
+
112
-
67
+ {
68
+
113
- { int i;
69
+ int i;
114
-
70
+
115
- double glow = (length - step) / (double)length;
71
+ glClear(GL_COLOR_BUFFER_BIT);
116
-
72
+
117
- glColor3f(red*glow, green*glow, blue*glow);
73
+ glColor3f(1.0, 1.5, 1.0);
118
74
 
119
75
  glBegin(GL_POINTS);
120
76
 
121
- for (i=0;i<numPoints;i++) {
77
+ for ( i=0 ; i<NumDrops ; i++ )
122
-
78
+
123
- x[i] += xacc[i];
79
+ if (water[i].alive) {
124
-
125
- y[i] += yacc[i];
80
+
126
-
127
- glVertex2f(x[i], y[i]);
81
+ glVertex2f(water[i].xpos, water[i].ypos);
128
82
 
129
83
  }
130
84
 
@@ -138,27 +92,143 @@
138
92
 
139
93
 
140
94
 
95
+ void time_step(void)
96
+
97
+ {
98
+
99
+ int i;
100
+
101
+ for ( i=0 ; i<NumDrops ; i++ ) {
102
+
103
+ if (water[i].alive) {
104
+
105
+ // set up an object to hit
106
+
107
+ if (water[i].ypos + GRAVITY*water[i].mass < -5.75) {
108
+
109
+ // bounce it off of the "floor"
110
+
111
+ water[i].ydir = -water[i].ydir;
112
+
113
+ } else {
114
+
115
+ // let gravity do its thing
116
+
117
+ water[i].ydir += GRAVITY * water[i].mass;
118
+
119
+ }
120
+
121
+ water[i].xpos += water[i].xdir;
122
+
123
+ water[i].ypos += water[i].ydir;
124
+
125
+ if (water[i].ypos < -1.0 || water[i].xpos > 1.0)
126
+
127
+ water[i].alive = 0;
128
+
129
+ }
130
+
131
+ }
132
+
133
+ }
134
+
135
+
136
+
137
+ void drop_generator(void)
138
+
139
+ {
140
+
141
+ int i,newdrops = drand48()*2;
142
+
143
+
144
+
145
+ if (NumDrops + newdrops > MAX_DROPS)
146
+
147
+ newdrops = MAX_DROPS - NumDrops;
148
+
149
+
150
+
151
+ for ( i=NumDrops ; i<NumDrops+newdrops ; i++ ) {
152
+
153
+ water[i].alive = 1;
154
+
155
+ water[i].xpos = -0.8 + 0.01*drand48();
156
+
157
+ water[i].ypos = 0.8 + 0.01*drand48();
158
+
159
+ water[i].xdir = 0.0075 + 0.0025*drand48();
160
+
161
+ water[i].ydir = 0.0;
162
+
163
+ water[i].mass = 0.5 + 0.5*drand48();
164
+
165
+ }
166
+
167
+ NumDrops += newdrops;
168
+
169
+ }
170
+
171
+
172
+
141
173
  void display(void)
142
174
 
143
- {
175
+ {
144
-
145
- int i;
176
+
146
-
177
+
178
+
147
- glClear(GL_COLOR_BUFFER_BIT);
179
+ drop_generator();
148
-
149
- if (step < 1.9*length) {
180
+
150
-
151
- for (i=0; i<numPoints; i++)
152
-
153
- yacc[i] -= 1.2 / length; // gravity
154
-
155
- draw_blast();
181
+ draw_waterfall();
182
+
156
-
183
+ time_step();
184
+
185
+
186
+
157
- }
187
+ }
188
+
189
+
190
+
158
-
191
+ void keyboard(unsigned char key, int x, int y)
192
+
193
+ {
194
+
159
- step ++;
195
+ switch (key) {
196
+
160
-
197
+ case 27: exit(0); break;
198
+
199
+ }
200
+
201
+ }
202
+
203
+
204
+
205
+ void reshape (int w, int h)
206
+
207
+ {
208
+
161
- if (step > length) initialize();
209
+ glViewport(0, 0, (GLsizei) w, (GLsizei) h);
210
+
211
+ glMatrixMode(GL_PROJECTION);
212
+
213
+ glLoadIdentity();
214
+
215
+ if (w <= h)
216
+
217
+ glOrtho(-1.0, 1.0,
218
+
219
+ -1.0*(GLfloat)h/(GLfloat)w, 1.0*(GLfloat)h/(GLfloat)w,
220
+
221
+ -1.0, 1.0);
222
+
223
+ else
224
+
225
+ glOrtho(-1.0*(GLfloat)w/(GLfloat)h, 1.0*(GLfloat)w/(GLfloat)h,
226
+
227
+ -1.0, 1.0,
228
+
229
+ -1.0, 1.0);
230
+
231
+ glMatrixMode(GL_MODELVIEW);
162
232
 
163
233
  }
164
234
 
@@ -174,52 +244,6 @@
174
244
 
175
245
 
176
246
 
177
- void keyboard(unsigned char key, int x, int y)
178
-
179
- {
180
-
181
- switch (key) {
182
-
183
- case 27: exit(0); break;
184
-
185
- }
186
-
187
- }
188
-
189
-
190
-
191
- void reshape (int w, int h)
192
-
193
- {
194
-
195
- glViewport(0, 0, (GLsizei) w, (GLsizei) h);
196
-
197
- glMatrixMode(GL_PROJECTION);
198
-
199
- glLoadIdentity();
200
-
201
- if (w <= h)
202
-
203
- glOrtho(-1.0, 1.0,
204
-
205
- -1.0*(GLfloat)h/(GLfloat)w, 1.0*(GLfloat)h/(GLfloat)w,
206
-
207
- -1.0, 1.0);
208
-
209
- else
210
-
211
- glOrtho(-1.0*(GLfloat)w/(GLfloat)h, 1.0*(GLfloat)w/(GLfloat)h,
212
-
213
- -1.0, 1.0,
214
-
215
- -1.0, 1.0);
216
-
217
- glMatrixMode(GL_MODELVIEW);
218
-
219
- }
220
-
221
-
222
-
223
247
  int main(int argc, char** argv)
224
248
 
225
249
  {
@@ -228,17 +252,17 @@
228
252
 
229
253
  glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
230
254
 
231
- glutInitWindowSize (800, 800);
255
+ glutInitWindowSize (700, 700);
232
256
 
233
257
  glutInitWindowPosition(0, 0);
234
258
 
235
- glutCreateWindow ("Fireworks");
259
+ glutCreateWindow ("Waterfall");
236
260
 
237
261
 
238
262
 
239
263
  glClearColor (0.0, 0.0, 0.0, 0.0);
240
264
 
241
- initialize();
265
+ glPointSize(5.0);
242
266
 
243
267
 
244
268
 
@@ -254,6 +278,10 @@
254
278
 
255
279
 
256
280
 
257
- return 0;
281
+ return 1;
258
-
282
+
259
- }
283
+ }
284
+
285
+
286
+
287
+ ```