-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbody.cpp
More file actions
154 lines (133 loc) · 3.36 KB
/
Copy pathbody.cpp
File metadata and controls
154 lines (133 loc) · 3.36 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
#include "body.hpp"
#include <iostream>
int Body::numLights = 0;
int Body::maxLights = -1;
Body::Body()
{
position = vector3(0.0f, 0.0f, 0.0f);
speed = vector3(0.0f, 0.0f, 0.0f);
mass = 1.0f;
radius = 1.0f;
color[0] = 1.0;
color[1] = 1.0;
color[2] = 1.0;
color[3] = 1.0;
isStar = false;
}
Body::Body(vector3 position, vector3 speed, float mass, float radius, std::vector<triangle> hull, float color[4], bool isStar){
this->position = position;
this->speed = speed;
this->mass = mass;
this->radius = radius;
this->hull = hull;
this->color[0] = color[0];
this->color[1] = color[1];
this->color[2] = color[2];
this->color[3] = color[3];
this->isStar = isStar;
if(isStar){
initLight();
}
}
void Body::applyImpulse(vector3 impulse){
speed += (impulse/mass);
//std::cout << "new Speed: "<<speed.x<<","<<speed.y<<","<<speed.z<<"\n";
}
void Body::move(float timestep){
position += speed*timestep;
}
void Body::draw(){
float black[4] = {0.0f, 0.0f, 0.0f, 1.0f};
if(isStar){
float pos[4] = {position.x, position.y, position.z, 1.0f};
glLightfv(GL_LIGHT0+lightid, GL_POSITION, pos);
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, black);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, black);
glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, color);
}
else{
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, black);
glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, black);
}
glPushMatrix();
glTranslatef(position.x, position.y, position.z);
glScalef(radius, radius, radius);
#if USE_MODELS
glBegin(GL_TRIANGLES);
for(std::vector<triangle>::const_iterator i=hull.begin(); i!=hull.end(); i++){
glNormal3fv(i->norm[0].c);
glVertex3fv(i->pos[0].c);
glNormal3fv(i->norm[1].c);
glVertex3fv(i->pos[1].c);
glNormal3fv(i->norm[2].c);
glVertex3fv(i->pos[2].c);
}
glEnd();
#else
glutSolidSphere(1.0, 20, 10);
#endif
glPopMatrix();
}
void Body::initLight(){
if(maxLights == -1){
glGetIntegerv(GL_MAX_LIGHTS, &maxLights);
}
if(numLights<maxLights){
lightid = numLights;
numLights++;
glEnable(GL_LIGHT0+lightid);
float diff[4] = {color[0]*0.8f,color[1]*0.8f,color[2]*0.8f,color[3]*0.8f};
glLightfv(GL_LIGHT0+lightid,GL_DIFFUSE,diff);
float black[4] = {0.0f,0.0f,0.0f,0.0f};
glLightfv(GL_LIGHT0+lightid,GL_SPECULAR,black);
}
else{
isStar = false;
}
}
vector3 Body::getPosition(){
return position;
}
void Body::setPosition(vector3 value){
this->position = value;
}
vector3 Body::getSpeed(){
return speed;
}
void Body::setSpeed(vector3 value){
this->speed = value;
}
float Body::getMass() const
{
return mass;
}
void Body::setMass(float value)
{
mass = value;
}
float Body::getRadius() const
{
return radius;
}
void Body::setRadius(float value)
{
radius = value;
}
std::vector<triangle> Body::getHull() const
{
return hull;
}
void Body::setHull(const std::vector<triangle> &value)
{
hull = value;
}
float* Body::getColor(){
return color;
}
void Body::setColor(float value[4]){
color[0] = value[0];
color[1] = value[1];
color[2] = value[2];
color[3] = value[3];
}