]> git.localhorst.tv Git - blobs.git/blob - src/world/world.cpp
concerning orbits
[blobs.git] / src / world / world.cpp
1 #include "Body.hpp"
2 #include "Planet.hpp"
3 #include "Simulation.hpp"
4 #include "Sun.hpp"
5 #include "Tile.hpp"
6
7 #include "../const.hpp"
8 #include "../app/Assets.hpp"
9 #include "../graphics/Viewport.hpp"
10
11 #include <algorithm>
12 #include <cmath>
13 #include <glm/gtx/transform.hpp>
14
15 using blobs::G;
16 using blobs::PI_2p0;
17
18 using std::sin;
19 using std::cos;
20 using std::pow;
21 using std::sqrt;
22
23
24 namespace blobs {
25 namespace world {
26
27 Body::Body()
28 : sim(nullptr)
29 , parent(nullptr)
30 , children()
31 , mass(1.0)
32 , radius(1.0)
33 , sma(1.0)
34 , ecc(0.0)
35 , inc(0.0)
36 , asc(0.0)
37 , arg(0.0)
38 , mna(0.0) {
39 }
40
41 Body::~Body() {
42 }
43
44 void Body::SetSimulation(Simulation &s) noexcept {
45         sim = &s;
46         for (auto child : children) {
47                 child->SetSimulation(s);
48         }
49 }
50
51 void Body::SetParent(Body &p) {
52         if (HasParent()) {
53                 UnsetParent();
54         }
55         parent = &p;
56         parent->AddChild(*this);
57 }
58
59 void Body::UnsetParent() {
60         if (!HasParent()) return;
61         parent->RemoveChild(*this);
62         parent = nullptr;
63 }
64
65 void Body::AddChild(Body &c) {
66         children.push_back(&c);
67         c.SetSimulation(*sim);
68 }
69
70 void Body::RemoveChild(Body &c) {
71         auto entry = std::find(children.begin(), children.end(), &c);
72         if (entry != children.end()) {
73                 children.erase(entry);
74         }
75 }
76
77 double Body::Mass() const noexcept {
78         return mass;
79 }
80
81 void Body::Mass(double m) noexcept {
82         mass = m;
83 }
84
85 double Body::Radius() const noexcept {
86         return radius;
87 }
88
89 void Body::Radius(double r) noexcept {
90         radius = r;
91 }
92
93 double Body::SemiMajorAxis() const noexcept {
94         return sma;
95 }
96
97 void Body::SemiMajorAxis(double s) noexcept {
98         sma = s;
99 }
100
101 double Body::Eccentricity() const noexcept {
102         return ecc;
103 }
104
105 void Body::Eccentricity(double e) noexcept {
106         ecc = e;
107 }
108
109 double Body::Inclination() const noexcept {
110         return inc;
111 }
112
113 void Body::Inclination(double i) noexcept {
114         inc = i;
115 }
116
117 double Body::LongitudeAscending() const noexcept {
118         return asc;
119 }
120
121 void Body::LongitudeAscending(double l) noexcept {
122         asc = l;
123 }
124
125 double Body::ArgumentPeriapsis() const noexcept {
126         return arg;
127 }
128
129 void Body::ArgumentPeriapsis(double a) noexcept {
130         arg = a;
131 }
132
133 double Body::MeanAnomaly() const noexcept {
134         return mna;
135 }
136
137 void Body::MeanAnomaly(double m) noexcept {
138         mna = m;
139 }
140
141 double Body::GravitationalParameter() const noexcept {
142         return G * Mass();
143 }
144
145 double Body::OrbitalPeriod() const noexcept {
146         if (parent) {
147                 return PI_2p0 * sqrt((sma * sma * sma) / (G * (parent->Mass() + Mass())));
148         } else {
149                 return 0.0;
150         }
151 }
152
153 glm::mat4 Body::ToParent() const noexcept {
154         if (!parent) {
155                 return glm::mat4(1.0f);
156         }
157
158         double T = OrbitalPeriod();
159
160         double M = mna + PI_2p0 * (GetSimulation().Time() / T); // + time
161
162         double E = M; // eccentric anomaly, solve M = E - e sin E
163         while (true) {
164                 double dE = (E - ecc * sin(E) - M) / (1 - ecc * cos(E));
165                 E -= dE;
166                 if (abs(dE) < 1.0e-6) break;
167         }
168
169         // coordinates in orbital plane
170         double P = sma * (cos(E) - ecc);
171         double Q = sma * sin(E) * sqrt(1 - (ecc * ecc));
172
173         // tile by argument of periapsis, …
174         double x = cos(arg) * P - sin(arg) * Q;
175         double y = sin(arg) * P + cos(arg) * Q;
176         // …inclination, …
177         double z = sin(inc) * x;
178                x = cos(inc) * x;
179         // …and longitude of ascending node
180         glm::vec3 pos(
181                 cos(asc) * x - sin(asc) * y,
182                 sin(asc) * x + cos(asc) * y,
183                 z);
184
185         // TODO: calculate complete matrix
186         return glm::translate(-pos);
187 }
188
189 glm::mat4 Body::FromParent() const noexcept {
190         if (!parent) {
191                 return glm::mat4(1.0f);
192         }
193         // TODO: calculate real position
194         return glm::translate(glm::vec3(-sma, 0.0f, 0.0f));
195 }
196
197
198 Planet::Planet(int sidelength)
199 : Body()
200 , sidelength(sidelength)
201 , tiles(new Tile[TilesTotal()])
202 , vao() {
203 }
204
205 Planet::~Planet() {
206 }
207
208 void Planet::BuildVAOs() {
209         vao.Bind();
210         vao.BindAttributes();
211         vao.EnableAttribute(0);
212         vao.EnableAttribute(1);
213         vao.AttributePointer<glm::vec3>(0, false, offsetof(Attributes, position));
214         vao.AttributePointer<glm::vec3>(1, false, offsetof(Attributes, tex_coord));
215         vao.ReserveAttributes(TilesTotal() * 4, GL_STATIC_DRAW);
216         {
217                 auto attrib = vao.MapAttributes(GL_WRITE_ONLY);
218                 float offset = sidelength * 0.5f;
219
220                 for (int index = 0, surface = 0; surface < 6; ++surface) {
221                         for (int y = 0; y < sidelength; ++y) {
222                                 for (int x = 0; x < sidelength; ++x, ++index) {
223                                         float tex = TileAt(surface, x, y).type;
224                                         attrib[4 * index + 0].position[(surface + 0) % 3] = x + 0 - offset;
225                                         attrib[4 * index + 0].position[(surface + 1) % 3] = y + 0 - offset;
226                                         attrib[4 * index + 0].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
227                                         attrib[4 * index + 0].tex_coord[0] = 0.0f;
228                                         attrib[4 * index + 0].tex_coord[1] = 0.0f;
229                                         attrib[4 * index + 0].tex_coord[2] = tex;
230
231                                         attrib[4 * index + 1].position[(surface + 0) % 3] = x + 0 - offset;
232                                         attrib[4 * index + 1].position[(surface + 1) % 3] = y + 1 - offset;
233                                         attrib[4 * index + 1].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
234                                         attrib[4 * index + 1].tex_coord[0] = 0.0f;
235                                         attrib[4 * index + 1].tex_coord[1] = 1.0f;
236                                         attrib[4 * index + 1].tex_coord[2] = tex;
237
238                                         attrib[4 * index + 2].position[(surface + 0) % 3] = x + 1 - offset;
239                                         attrib[4 * index + 2].position[(surface + 1) % 3] = y + 0 - offset;
240                                         attrib[4 * index + 2].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
241                                         attrib[4 * index + 2].tex_coord[0] = 1.0f;
242                                         attrib[4 * index + 2].tex_coord[1] = 0.0f;
243                                         attrib[4 * index + 2].tex_coord[2] = tex;
244
245                                         attrib[4 * index + 3].position[(surface + 0) % 3] = x + 1 - offset;
246                                         attrib[4 * index + 3].position[(surface + 1) % 3] = y + 1 - offset;
247                                         attrib[4 * index + 3].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
248                                         attrib[4 * index + 3].tex_coord[0] = 1.0f;
249                                         attrib[4 * index + 3].tex_coord[1] = 1.0f;
250                                         attrib[4 * index + 3].tex_coord[2] = tex;
251                                 }
252                         }
253                 }
254         }
255         vao.BindElements();
256         vao.ReserveElements(TilesTotal() * 6, GL_STATIC_DRAW);
257         {
258                 auto element = vao.MapElements(GL_WRITE_ONLY);
259                 int index = 0;
260                 for (int surface = 0; surface < 3; ++surface) {
261                         for (int y = 0; y < sidelength; ++y) {
262                                 for (int x = 0; x < sidelength; ++x, ++index) {
263                                         element[6 * index + 0] = 4 * index + 0;
264                                         element[6 * index + 1] = 4 * index + 2;
265                                         element[6 * index + 2] = 4 * index + 1;
266                                         element[6 * index + 3] = 4 * index + 1;
267                                         element[6 * index + 4] = 4 * index + 2;
268                                         element[6 * index + 5] = 4 * index + 3;
269                                 }
270                         }
271                 }
272                 for (int surface = 3; surface < 6; ++surface) {
273                         for (int y = 0; y < sidelength; ++y) {
274                                 for (int x = 0; x < sidelength; ++x, ++index) {
275                                         element[6 * index + 0] = 4 * index + 0;
276                                         element[6 * index + 1] = 4 * index + 1;
277                                         element[6 * index + 2] = 4 * index + 2;
278                                         element[6 * index + 3] = 4 * index + 2;
279                                         element[6 * index + 4] = 4 * index + 1;
280                                         element[6 * index + 5] = 4 * index + 3;
281                                 }
282                         }
283                 }
284         }
285         vao.Unbind();
286 }
287
288 void Planet::Draw(app::Assets &assets, graphics::Viewport &viewport) {
289         vao.Bind();
290         const glm::mat4 &MV = assets.shaders.planet_surface.MV();
291         assets.shaders.planet_surface.SetNormal(glm::vec3(MV * glm::vec4(0.0f, 0.0f, 1.0f, 0.0f)));
292         vao.DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 0);
293         assets.shaders.planet_surface.SetNormal(glm::vec3(MV * glm::vec4(1.0f, 0.0f, 0.0f, 0.0f)));
294         vao.DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 1);
295         assets.shaders.planet_surface.SetNormal(glm::vec3(MV * glm::vec4(0.0f, 1.0f, 0.0f, 0.0f)));
296         vao.DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 2);
297         assets.shaders.planet_surface.SetNormal(glm::vec3(MV * glm::vec4(0.0f, 0.0f, -1.0f, 0.0f)));
298         vao.DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 3);
299         assets.shaders.planet_surface.SetNormal(glm::vec3(MV * glm::vec4(-1.0f, 0.0f, 0.0f, 0.0f)));
300         vao.DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 4);
301         assets.shaders.planet_surface.SetNormal(glm::vec3(MV * glm::vec4(0.0f, -1.0f, 0.0f, 0.0f)));
302         vao.DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 5);
303 }
304
305
306 void GenerateTest(Planet &p) {
307         for (int surface = 0; surface <= 5; ++surface) {
308                 for (int y = 0; y < p.SideLength(); ++y) {
309                         for (int x = 0; x < p.SideLength(); ++x) {
310                                 p.TileAt(surface, x, y).type = (x == p.SideLength()/2) + (y == p.SideLength()/2);
311                         }
312                 }
313         }
314         p.BuildVAOs();
315 }
316
317
318 Sun::Sun()
319 : Body() {
320 }
321
322 Sun::~Sun() {
323 }
324
325 }
326 }