1 #include "MasterState.hpp"
3 #include "../world/Body.hpp"
4 #include "../world/Creature.hpp"
5 #include "../world/Planet.hpp"
6 #include "../world/Simulation.hpp"
7 #include "../world/Sun.hpp"
9 #include <glm/gtx/transform.hpp>
15 MasterState::MasterState(Assets &assets, world::Simulation &sim) noexcept
25 MasterState::~MasterState() noexcept {
29 void MasterState::OnResize(int w, int h) {
30 cam.Aspect(float(w), float(h));
33 void MasterState::OnUpdate(int dt) {
35 while (remain >= FrameMS()) {
40 void MasterState::Tick() {
45 thirds = (thirds + 1) % 3;
48 int MasterState::FrameMS() const noexcept {
49 return thirds == 0 ? 16 : 17;
53 void MasterState::OnKeyDown(const SDL_KeyboardEvent &e) {
54 if (e.keysym.sym == SDLK_p) {
59 void MasterState::OnRender(graphics::Viewport &viewport) {
62 for (auto sun : sim.Suns()) {
63 // TODO: source sun's light color and strength
64 glm::vec3 pos(cam.View() * cam.Model(*sun)[3]);
65 glm::vec3 col(1.0f, 1.0f, 1.0f);
67 assets.shaders.planet_surface.Activate();
68 assets.shaders.planet_surface.SetLight(num_lights, pos, col, str);
69 assets.shaders.creature_skin.Activate();
70 assets.shaders.creature_skin.SetLight(num_lights, pos, col, str);
72 if (num_lights >= graphics::PlanetSurface::MAX_LIGHTS || num_lights >= graphics::CreatureSkin::MAX_LIGHTS) {
76 for (auto planet : sim.Planets()) {
77 // TODO: indirect light from planets, calculate strength and get color somehow
78 glm::vec3 pos(cam.View() * cam.Model(*planet)[3]);
79 glm::vec3 col(1.0f, 1.0f, 1.0f);
81 assets.shaders.planet_surface.Activate();
82 assets.shaders.planet_surface.SetLight(num_lights, pos, col, str);
83 assets.shaders.creature_skin.Activate();
84 assets.shaders.creature_skin.SetLight(num_lights, pos, col, str);
86 if (num_lights >= graphics::PlanetSurface::MAX_LIGHTS || num_lights >= graphics::CreatureSkin::MAX_LIGHTS) {
90 assets.shaders.planet_surface.Activate();
91 assets.shaders.planet_surface.SetNumLights(num_lights);
92 assets.shaders.creature_skin.Activate();
93 assets.shaders.creature_skin.SetNumLights(num_lights);
95 assets.shaders.planet_surface.Activate();
96 assets.shaders.planet_surface.SetTexture(assets.textures.tiles);
97 for (auto planet : sim.Planets()) {
98 assets.shaders.planet_surface.SetMVP(cam.Model(*planet), cam.View(), cam.Projection());
99 planet->Draw(assets, viewport);
102 assets.shaders.sun_surface.Activate();
103 for (auto sun : sim.Suns()) {
104 double sun_radius = sun->Radius();
105 assets.shaders.sun_surface.SetMVP(
106 cam.Model(*sun) * glm::scale(glm::vec3(sun_radius, sun_radius, sun_radius)),
107 cam.View(), cam.Projection());
108 assets.shaders.sun_surface.SetLight(glm::vec3(1.0f, 1.0f, 1.0f), 1.0e6f);
109 assets.shaders.sun_surface.Draw();
112 assets.shaders.creature_skin.Activate();
113 assets.shaders.creature_skin.SetTexture(assets.textures.skins);
114 // TODO: extend to nearby bodies as well
115 for (auto c : cam.Reference().Creatures()) {
116 assets.shaders.creature_skin.SetMVP(cam.Model(c->GetBody()) * glm::mat4(c->LocalTransform()), cam.View(), cam.Projection());
117 c->Draw(assets, viewport);