1 #include "MasterState.hpp"
3 #include "../world/Body.hpp"
4 #include "../world/Simulation.hpp"
6 #include <glm/gtx/transform.hpp>
12 MasterState::MasterState(Assets &assets, world::Simulation &sim) noexcept
16 , reference(&sim.Root())
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)));
29 MasterState::~MasterState() noexcept {
33 void MasterState::OnResize(int w, int h) {
34 cam.Aspect(float(w), float(h));
37 void MasterState::OnUpdate(int dt) {
39 while (remain >= FrameMS()) {
44 void MasterState::Tick() {
49 thirds = (thirds + 1) % 3;
52 int MasterState::FrameMS() const noexcept {
53 return thirds == 0 ? 16 : 17;
57 void MasterState::OnKeyDown(const SDL_KeyboardEvent &e) {
58 if (e.keysym.sym == SDLK_p) {
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);
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);
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);
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();