-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBody.cpp
More file actions
211 lines (160 loc) · 5.08 KB
/
Body.cpp
File metadata and controls
211 lines (160 loc) · 5.08 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
#include "Body.h"
/** IMPLEMENTATION **/
///////////////////////////////////////////////////////////////////////////////////
void displayAuxiliarData(float radius, double deltaX, double deltaY, double deltaZ)
{
glDisable(GL_LIGHTING);
// Vector speed
glBegin(GL_LINES);
glColor3f(1, 1, 0);
glVertex3f(0, 0, 0);
glVertex3f(100 * deltaX, 100 * deltaY, 100 * deltaZ);
glEnd();
// Axis
glBegin(GL_LINES);
// X
glColor3f(1.0, 0, 0.0);
glVertex3f(2 * radius, 0, 0);
glVertex3f(0, 0, 0);
// Y
glColor3f(0.0, 1.0, 0.0);
glVertex3f(0, 2 * radius, 0);
glVertex3f(0, 0, 0);
// Z
glColor3f(0.0, 0, 1.0);
glVertex3f(0, 0, 2 * radius);
glVertex3f(0, 0, 0);
glEnd();
// Cube
glColor3f(1.0, 0, 1.0);
glutWireCube(radius * 2);
glEnable(GL_LIGHTING);
}
//////////////////////////////////////////////////////////////////
Body::Body(float X = 0, float Y = 0, float Z = 0)
{
// Set default values
fx = fy = fz = 0.0; // 0 force
ax = ay = az = 0.0; // 0 for acceleration
dx = dy = dz = 0.0; // 0 for speed
x = X; // Origin for space
y = Y; // Origin for space
z = Z; // Origin for space
mass = 1.0;
radius = 0.5;
// Allocate a material struct
material = new Material;
if(material != 0)
{
for(int i = 0; i < 4; i++) // Default values
{
material->diffuse [i] = 0.4;
material->specular[i] = 0.6;
}
material->shininess[0] = 20.0;
}
}
//////////////////////////////////////////////////////////////////
inline void Body::printCoord()
{
printf("Coord: (%f, %f, %f).\n", x, y, z);
}
void Body::printForce()
{
printf("Force: (%f, %f, %f).\n", fx, fy, fz);
}
inline void Body::printAccel()
{
printf("Accel: (%f, %f, %f).\n", ax, ay, az);
}
inline void Body::printSpeed()
{
printf("Speed: (%f, %f, %f).\n", dx, dy, dz);
}
void Body::printData()
{
printf("\n-- Body --\n");
printf("Mass: %f.\n", mass);
printf("Radius: %f.\n", radius);
this->printCoord();
this->printSpeed();
this->printAccel();
printf("Specular: (%f, %f, %f, %f).\n", material->specular[0], material->specular[1], material->specular[2], material->specular[3]);
printf("Diffuse: (%f, %f, %f, %f).\n", material->diffuse[0], material->diffuse[1], material->diffuse[2], material->diffuse[3]);
printf("Shininnes: (%f).\n", material->shininess[0]);
}
//////////////////////////////////////////////////////////////////
void Body::setMaterial()
{
glMaterialfv(GL_FRONT, GL_SPECULAR, material->specular);
glMaterialfv(GL_FRONT, GL_SHININESS, material->shininess);
glMaterialfv(GL_FRONT, GL_DIFFUSE, material->diffuse);
}
//////////////////////////////////////////////////////////////////
void Body::display(int detailsOn, int projOn, int mode)
{
glPushMatrix();
glTranslated(x, y, z);
// Display axis and auxiliar data on the screen
if(detailsOn)
{
if(mode == SHOW_ACCEL_VECTOR)
displayAuxiliarData(radius, ax, ay, az);
else if(mode == SHOW_FORCE_VECTOR)
displayAuxiliarData(radius, fx, fy, fz);
else if(mode == SHOW_SPEED_VECTOR)
displayAuxiliarData(radius, dx, dy, dz);
}
if(projOn)
{
glDisable(GL_LIGHTING);
glColor3fv(material->diffuse);
glBegin(GL_LINES);
glVertex3f(radius, -y, radius);
glVertex3f(-radius, -y, -radius);
glVertex3f(radius, -y, -radius);
glVertex3f(-radius, -y, radius);
glEnd();
glEnable(GL_LIGHTING);
}
this->setMaterial();
glutSolidSphere(radius, 24, 24);
glPopMatrix();
}
//////////////////////////////////////////////////////////////////
void Body::updatePosition()
{
// Update acceleration
ax = fx/mass;
ay = fy/mass;
az = fz/mass;
// Update speed
dz += az;
dy += ay;
dx += ax;
// Update coordinates
z += dz;
y += dy;
x += dx;
}
//////////////////////////////////////////////////////////////////
void Body::loadPropertiesFromFile(FILE *document)
{
// Inherent properties
fscanf(document, "%f ", &mass);
fscanf(document, "%f ", &radius);
// Initial coordinates
fscanf(document, "%lf ", &x);
fscanf(document, "%lf ", &y);
fscanf(document, "%lf ", &z);
// Initial speed
fscanf(document, "%lf ", &dx);
fscanf(document, "%lf ", &dy);
fscanf(document, "%lf ", &dz);
// Materials
for(int i = 0; i < 4; i++)
fscanf(document, "%f", &material->specular[i]);
for(int i = 0; i < 4; i++)
fscanf(document, "%f", &material->diffuse[i]);
fscanf(document, "%f", &material->shininess[0]);
}