質問編集履歴

1

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

2020/06/24 03:59

投稿

ssssssa
ssssssa

スコア4

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