3 #include "Simulation.hpp"
7 #include "../const.hpp"
8 #include "../app/Assets.hpp"
9 #include "../graphics/Viewport.hpp"
13 #include <glm/gtx/transform.hpp>
44 void Body::SetSimulation(Simulation &s) noexcept {
46 for (auto child : children) {
47 child->SetSimulation(s);
51 void Body::SetParent(Body &p) {
56 parent->AddChild(*this);
59 void Body::UnsetParent() {
60 if (!HasParent()) return;
61 parent->RemoveChild(*this);
65 void Body::AddChild(Body &c) {
66 children.push_back(&c);
67 c.SetSimulation(*sim);
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);
77 double Body::Mass() const noexcept {
81 void Body::Mass(double m) noexcept {
85 double Body::Radius() const noexcept {
89 void Body::Radius(double r) noexcept {
93 double Body::SemiMajorAxis() const noexcept {
97 void Body::SemiMajorAxis(double s) noexcept {
101 double Body::Eccentricity() const noexcept {
105 void Body::Eccentricity(double e) noexcept {
109 double Body::Inclination() const noexcept {
113 void Body::Inclination(double i) noexcept {
117 double Body::LongitudeAscending() const noexcept {
121 void Body::LongitudeAscending(double l) noexcept {
125 double Body::ArgumentPeriapsis() const noexcept {
129 void Body::ArgumentPeriapsis(double a) noexcept {
133 double Body::MeanAnomaly() const noexcept {
137 void Body::MeanAnomaly(double m) noexcept {
141 double Body::GravitationalParameter() const noexcept {
145 double Body::OrbitalPeriod() const noexcept {
147 return PI_2p0 * sqrt((sma * sma * sma) / (G * (parent->Mass() + Mass())));
153 glm::mat4 Body::ToParent() const noexcept {
155 return glm::mat4(1.0f);
158 double T = OrbitalPeriod();
160 double M = mna + PI_2p0 * (GetSimulation().Time() / T); // + time
162 double E = M; // eccentric anomaly, solve M = E - e sin E
164 double dE = (E - ecc * sin(E) - M) / (1 - ecc * cos(E));
166 if (abs(dE) < 1.0e-6) break;
169 // coordinates in orbital plane
170 double P = sma * (cos(E) - ecc);
171 double Q = sma * sin(E) * sqrt(1 - (ecc * ecc));
173 // tile by argument of periapsis, …
174 double x = cos(arg) * P - sin(arg) * Q;
175 double y = sin(arg) * P + cos(arg) * Q;
177 double z = sin(inc) * x;
179 // …and longitude of ascending node
181 cos(asc) * x - sin(asc) * y,
182 sin(asc) * x + cos(asc) * y,
185 // TODO: calculate complete matrix
186 return glm::translate(-pos);
189 glm::mat4 Body::FromParent() const noexcept {
191 return glm::mat4(1.0f);
193 // TODO: calculate real position
194 return glm::translate(glm::vec3(-sma, 0.0f, 0.0f));
198 Planet::Planet(int sidelength)
200 , sidelength(sidelength)
201 , tiles(new Tile[TilesTotal()])
208 void Planet::BuildVAOs() {
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);
217 auto attrib = vao.MapAttributes(GL_WRITE_ONLY);
218 float offset = sidelength * 0.5f;
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;
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;
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;
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;
256 vao.ReserveElements(TilesTotal() * 6, GL_STATIC_DRAW);
258 auto element = vao.MapElements(GL_WRITE_ONLY);
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;
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;
288 void Planet::Draw(app::Assets &assets, graphics::Viewport &viewport) {
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);
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);