1 #include "MasterState.hpp"
3 #include "../world/Body.hpp"
4 #include "../world/Planet.hpp"
5 #include "../world/Simulation.hpp"
6 #include "../world/Sun.hpp"
8 #include <glm/gtx/transform.hpp>
14 MasterState::MasterState(Assets &assets, world::Simulation &sim) noexcept
24 MasterState::~MasterState() noexcept {
28 void MasterState::OnResize(int w, int h) {
29 cam.Aspect(float(w), float(h));
32 void MasterState::OnUpdate(int dt) {
34 while (remain >= FrameMS()) {
39 void MasterState::Tick() {
44 thirds = (thirds + 1) % 3;
47 int MasterState::FrameMS() const noexcept {
48 return thirds == 0 ? 16 : 17;
52 void MasterState::OnKeyDown(const SDL_KeyboardEvent &e) {
53 if (e.keysym.sym == SDLK_p) {
58 void MasterState::OnRender(graphics::Viewport &viewport) {
59 assets.shaders.planet_surface.Activate();
60 assets.shaders.planet_surface.SetTexture(assets.textures.tiles);
63 for (auto sun : sim.Suns()) {
64 // TODO: source sun's light color and strength
65 assets.shaders.planet_surface.SetLight(
67 glm::vec3(cam.View() * cam.Model(*sun)[3]),
68 glm::vec3(1.0f, 1.0f, 1.0f),
71 if (num_lights >= graphics::PlanetSurface::MAX_LIGHTS) {
75 for (auto planet : sim.Planets()) {
76 // TODO: indirect light from planets, calculate strength and get color somehow
77 assets.shaders.planet_surface.SetLight(
79 glm::vec3(cam.View() * cam.Model(*planet)[3]),
80 glm::vec3(1.0f, 1.0f, 1.0f),
83 if (num_lights >= graphics::PlanetSurface::MAX_LIGHTS) {
87 assets.shaders.planet_surface.SetNumLights(num_lights);
89 for (auto planet : sim.Planets()) {
90 assets.shaders.planet_surface.SetMVP(cam.Model(*planet), cam.View(), cam.Projection());
91 planet->Draw(assets, viewport);
94 assets.shaders.sun_surface.Activate();
95 for (auto sun : sim.Suns()) {
96 double sun_radius = sun->Radius();
97 assets.shaders.sun_surface.SetMVP(
98 cam.Model(*sun) * glm::scale(glm::vec3(sun_radius, sun_radius, sun_radius)),
99 cam.View(), cam.Projection());
100 assets.shaders.sun_surface.SetLight(glm::vec3(1.0f, 1.0f, 1.0f), 1.0e6f);
101 assets.shaders.sun_surface.Draw();