teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

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

2020/06/24 03:57

投稿

ssssssa
ssssssa

スコア4

title CHANGED
File without changes
body CHANGED
@@ -1,15 +1,17 @@
1
1
  このファイルを実行すると、速度が速すぎます。速度を遅くするにはどうしたらいいでしょうか。
2
2
 
3
-
3
+ ```
4
4
  #include "GL/glut.h"
5
5
  #include <math.h>
6
6
  #include <stdlib.h>
7
- #include <stdio.h>
8
7
 
8
+ #define MAX_DROPS 50000
9
+ #define GRAVITY -0.0005
10
+
9
11
  #ifdef WIN32
10
12
  //to correct ASCI deviations in Microsoft VC++ 6.0
11
13
 
12
- #define M_PI 3.1415926535897932384626433832795
14
+ #define M_PI (3.1415926535897932384626433832795)
13
15
 
14
16
  double drand48()
15
17
  { return (rand()%10000)/10000.0; }
@@ -18,74 +20,79 @@
18
20
  #endif
19
21
 
20
22
 
21
- #define MAX_POINTS 5000
23
+ typedef struct {
22
- int numPoints;
24
+ int alive;
23
- GLfloat curx, cury;
25
+ GLfloat xpos, ypos;
24
- GLfloat x[MAX_POINTS], y[MAX_POINTS];
25
- GLfloat xacc[MAX_POINTS], yacc[MAX_POINTS];
26
- GLfloat red, green, blue;
26
+ GLfloat xdir, ydir;
27
+ GLfloat mass;
27
- int step; int length;
28
+ } Particle;
28
29
 
29
- void initialize()
30
- { int j; double temp, temp2;
30
+ Particle water[MAX_DROPS];
31
+ int NumDrops;
31
32
 
32
- numPoints = drand48()*(MAX_POINTS-1);
33
- curx = -0.5 + drand48();
34
- cury = 0.0 + drand48();
35
-
36
- red = 0.5 + 0.5*drand48();
37
- green = 0.5 + 0.5*drand48();
38
- blue = 0.5 + 0.5*drand48();
39
- glPointSize(1.5);
40
- step = 0;
41
- length = 700 + 300*drand48();
42
-
43
-
44
- /* initialize the blast */
45
- for (j=0 ; j<numPoints ; j++ ) {
46
- x[j] = curx;
47
- y[j] = cury;
48
- temp = drand48();
49
- temp2 = drand48()*2.0*M_PI;
50
- xacc[j] = (cos(temp2) * temp)/length;
51
- yacc[j] = (sin(temp2) * temp)/length;
52
- }
53
-
54
- }
55
-
56
- void draw_blast(void)
33
+ void draw_waterfall(void)
34
+ {
57
- { int i;
35
+ int i;
58
- double glow = (length - step) / (double)length;
36
+ glClear(GL_COLOR_BUFFER_BIT);
59
- glColor3f(red*glow, green*glow, blue*glow);
37
+ glColor3f(1.0, 1.5, 1.0);
60
38
  glBegin(GL_POINTS);
61
- for (i=0;i<numPoints;i++) {
39
+ for ( i=0 ; i<NumDrops ; i++ )
62
- x[i] += xacc[i];
40
+ if (water[i].alive) {
63
- y[i] += yacc[i];
64
- glVertex2f(x[i], y[i]);
41
+ glVertex2f(water[i].xpos, water[i].ypos);
65
42
  }
66
43
  glEnd();
67
44
  glFlush();
68
45
  glutSwapBuffers();
69
46
  }
70
47
 
71
- void display(void)
48
+ void time_step(void)
72
- {
49
+ {
73
50
  int i;
74
- glClear(GL_COLOR_BUFFER_BIT);
75
- if (step < 1.9*length) {
76
- for (i=0; i<numPoints; i++)
51
+ for ( i=0 ; i<NumDrops ; i++ ) {
52
+ if (water[i].alive) {
77
- yacc[i] -= 1.2 / length; // gravity
53
+ // set up an object to hit
54
+ if (water[i].ypos + GRAVITY*water[i].mass < -5.75) {
55
+ // bounce it off of the "floor"
56
+ water[i].ydir = -water[i].ydir;
78
- draw_blast();
57
+ } else {
58
+ // let gravity do its thing
59
+ water[i].ydir += GRAVITY * water[i].mass;
79
60
  }
80
- step ++;
61
+ water[i].xpos += water[i].xdir;
62
+ water[i].ypos += water[i].ydir;
81
- if (step > length) initialize();
63
+ if (water[i].ypos < -1.0 || water[i].xpos > 1.0)
64
+ water[i].alive = 0;
82
65
  }
66
+ }
67
+ }
83
68
 
84
- void idle(void)
69
+ void drop_generator(void)
85
70
  {
71
+ int i,newdrops = drand48()*2;
72
+
73
+ if (NumDrops + newdrops > MAX_DROPS)
74
+ newdrops = MAX_DROPS - NumDrops;
75
+
76
+ for ( i=NumDrops ; i<NumDrops+newdrops ; i++ ) {
77
+ water[i].alive = 1;
78
+ water[i].xpos = -0.8 + 0.01*drand48();
79
+ water[i].ypos = 0.8 + 0.01*drand48();
80
+ water[i].xdir = 0.0075 + 0.0025*drand48();
86
- glutPostRedisplay();
81
+ water[i].ydir = 0.0;
82
+ water[i].mass = 0.5 + 0.5*drand48();
87
83
  }
84
+ NumDrops += newdrops;
85
+ }
88
86
 
87
+ void display(void)
88
+ {
89
+
90
+ drop_generator();
91
+ draw_waterfall();
92
+ time_step();
93
+
94
+ }
95
+
89
96
  void keyboard(unsigned char key, int x, int y)
90
97
  {
91
98
  switch (key) {
@@ -109,16 +116,21 @@
109
116
  glMatrixMode(GL_MODELVIEW);
110
117
  }
111
118
 
119
+ void idle(void)
120
+ {
121
+ glutPostRedisplay();
122
+ }
123
+
112
124
  int main(int argc, char** argv)
113
125
  {
114
126
  glutInit(&argc, argv);
115
127
  glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
116
- glutInitWindowSize (800, 800);
128
+ glutInitWindowSize (700, 700);
117
129
  glutInitWindowPosition(0, 0);
118
- glutCreateWindow ("Fireworks");
130
+ glutCreateWindow ("Waterfall");
119
131
 
120
132
  glClearColor (0.0, 0.0, 0.0, 0.0);
121
- initialize();
133
+ glPointSize(5.0);
122
134
 
123
135
  glutDisplayFunc(display);
124
136
  glutReshapeFunc(reshape);
@@ -126,5 +138,7 @@
126
138
  glutKeyboardFunc(keyboard);
127
139
  glutMainLoop();
128
140
 
129
- return 0;
141
+ return 1;
130
- }
142
+ }
143
+
144
+ ```