forked from raun/OpenGL-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prog.cpp
163 lines (159 loc) · 3.11 KB
/
prog.cpp
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
#include <iostream>
#include <math.h>
#include <vector>
#include <GL/glut.h>
#define pi 3.14
#define UPDATE_TIME 25
// Class : Ball
class Ball{
private:
float x,y;
float radius;
float direction;
float red,green,blue;
public:
Ball();
Ball(float,float,float,float);
void setLocation(float,float);
void setRadius(float);
void setDirection(float);
void setColor(float,float,float);
float getX();
float getY();
//float getZ();
float getR();
float getDirection();
float getRed();
float getGreen();
float getBlue();
};
Ball::Ball(){
x=y=0;
radius=0.2;
direction=0;
red=0.0;
green=1.0;
blue=0.0;
}
Ball::Ball(float a,float b,float c,float d){
x=a;
y=b;
//z=c;
radius=c;
direction=d;
red=0.0;
green=1.0;
blue=0.0;
}
void Ball::setLocation(float a,float b){
x=a;
y=b;
}
void Ball::setRadius(float a){
radius=a;
}
void Ball::setDirection(float a){
direction=a;
}
void Ball::setColor(float r,float g,float b){
red=r;
green=g;
blue=b;
}
float Ball::getX(){
return x;
}
float Ball::getY(){
return y;
}
/*float Ball::getZ(){
return z;
}*/
float Ball::getR(){
return radius;
}
float Ball::getDirection(){
return direction;
}
float Ball::getRed(){
return red;
}
float Ball::getGreen(){
return green;
}
float Ball::getBlue(){
return blue;
}
/*
Class Ball Ends
*/
std::vector<Ball> v;
Ball b;
void drawBalls(){
for(int i=0,num=v.size();i<num;++i){
glColor3f(v[i].getRed(),v[i].getGreen(),v[i].getBlue());
if(i==0)
glTranslatef(v[i].getX(), v[i].getY(),0.0);
else
glTranslatef(v[i].getX()-v[i-1].getX(), v[i].getY()-v[i-1].getY(),0.0);
glutSolidSphere(v[i].getR(),100, 100);
}
}
void update(int value){
for(int i=0,n=v.size();i<n;++i){
if((v[i].getX()+0.01*cos(v[i].getDirection())>0.9)||(v[i].getY()+0.01*sin(v[i].getDirection())>0.9)||(v[i].getX()+0.01*cos(v[i].getDirection())<-0.9)||(v[i].getY()+0.01*sin(v[i].getDirection())<-0.9)){
// Generating 2 new Balls with half demenions
v[i].setRadius(v[i].getR()/2);
b=v[i];
v[i].setDirection(v[i].getDirection()+pi);
b.setDirection(v[i].getDirection()+pi+1.5);
v.push_back(b);
}
v[i].setLocation(v[i].getX()+0.01*cos(v[i].getDirection()),v[i].getY()+0.01*sin(v[i].getDirection()));
}
glutPostRedisplay();
glutTimerFunc(UPDATE_TIME, update, 0);
}
void initBall(){
Ball b;
//red
b.setLocation(0.0,0.0);
b.setColor(1.0,0.0,0.0);
b.setDirection(1.4);
v.push_back(b);
//green
b.setLocation(0.4,0.4);
b.setColor(0.0,1.0,0.0);
b.setDirection(2.6);
v.push_back(b);
//blue
b.setDirection(1.3);
b.setLocation(-0.4,0.4);
b.setColor(0.0,0.0,1.0);
v.push_back(b);
//yellow
b.setDirection(2.2);
b.setLocation(0.1,0.1);
b.setColor(1.0,1.0,0.0);
v.push_back(b);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
drawBalls();
glutSwapBuffers();
}
int main(int argc,char **argv){
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGBA);
glutInitWindowSize(800,600);
glutInitWindowPosition(0,0);
glutCreateWindow("Ball");
initBall();
glutDisplayFunc(display);
glutTimerFunc(UPDATE_TIME, update, 0);
glutMainLoop();
return 0;
}