]> git.localhorst.tv Git - blobs.git/blob - src/app/states.cpp
more orbits and stuff
[blobs.git] / src / app / states.cpp
1 #include "MasterState.hpp"
2
3 #include "../world/Body.hpp"
4 #include "../world/Simulation.hpp"
5
6 #include <glm/gtx/transform.hpp>
7
8
9 namespace blobs {
10 namespace app {
11
12 MasterState::MasterState(Assets &assets, world::Simulation &sim) noexcept
13 : State()
14 , assets(assets)
15 , sim(sim)
16 , reference(&sim.Root())
17 , cam()
18 , remain(0)
19 , thirds(0)
20 , paused(false) {
21         // sunset view: standing in the center of surface 0 (+Z), looking west (-X)
22         //cam.View(glm::lookAt(glm::vec3(0.0f, 0.0f, 5.6f), glm::vec3(-1.0f, 0.0f, 5.6f), glm::vec3(0.0f, 0.0f, 1.0f)));
23         // sunrise view: standing in the center of surface 0 (+Z), looking east (+X)
24         cam.View(glm::lookAt(glm::vec3(0.0f, 0.0f, 5.6f), glm::vec3(1.0f, 0.0f, 5.6f), glm::vec3(0.0f, 0.0f, 1.0f)));
25         // far out, looking at planet
26         //cam.View(glm::lookAt(glm::vec3(10.0f, 10.0f, 50.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f)));
27 }
28
29 MasterState::~MasterState() noexcept {
30 }
31
32
33 void MasterState::OnResize(int w, int h) {
34         cam.Aspect(float(w), float(h));
35 }
36
37 void MasterState::OnUpdate(int dt) {
38         remain += dt;
39         while (remain >= FrameMS()) {
40                 Tick();
41         }
42 }
43
44 void MasterState::Tick() {
45         if (!paused) {
46                 sim.Tick();
47         }
48         remain -= FrameMS();
49         thirds = (thirds + 1) % 3;
50 }
51
52 int MasterState::FrameMS() const noexcept {
53         return thirds == 0 ? 16 : 17;
54 }
55
56
57 void MasterState::OnKeyDown(const SDL_KeyboardEvent &e) {
58         if (e.keysym.sym == SDLK_p) {
59                 paused = !paused;
60         }
61 }
62
63 void MasterState::OnRender(graphics::Viewport &viewport) {
64         glm::dmat4 ppos = reference->InverseTransform() * reference->ToParent();
65         assets.shaders.planet_surface.Activate();
66         assets.shaders.planet_surface.SetTexture(assets.textures.tiles);
67
68         assets.shaders.planet_surface.SetMVP(glm::mat4(1.0f), cam.View(), cam.Projection());
69         assets.shaders.planet_surface.SetLight(glm::vec3(cam.View() * ppos[3]), glm::vec3(1.0f, 1.0f, 1.0f), 2.0e4f);
70         reference->Draw(assets, viewport);
71
72         world::Body *child = reference->Children()[0];
73         assets.shaders.planet_surface.SetMVP(reference->InverseTransform() * child->FromParent() * child->LocalTransform(), cam.View(), cam.Projection());
74         child->Draw(assets, viewport);
75
76         assets.shaders.sun_surface.Activate();
77         assets.shaders.sun_surface.SetMVP(ppos * reference->Parent().LocalTransform(), cam.View(), cam.Projection());
78         assets.shaders.sun_surface.SetLight(glm::vec3(1.0f, 1.0f, 1.0f), 2.0e4f);
79         assets.shaders.sun_surface.Draw();
80 }
81
82 }
83 }