1 #include "MasterState.hpp"
3 #include "../creature/Creature.hpp"
4 #include "../graphics/Viewport.hpp"
5 #include "../world/Body.hpp"
6 #include "../world/Planet.hpp"
7 #include "../world/Simulation.hpp"
8 #include "../world/Sun.hpp"
10 #include <glm/gtx/transform.hpp>
16 MasterState::MasterState(Assets &assets, world::Simulation &sim) noexcept
27 MasterState::~MasterState() noexcept {
31 void MasterState::OnResize(int w, int h) {
32 assets.shaders.canvas.Activate();
33 assets.shaders.canvas.Resize(float(w), float(h));
34 assets.shaders.alpha_sprite.Activate();
35 assets.shaders.alpha_sprite.SetVP(glm::mat4(1.0f), glm::ortho(0.0f, float(w), float(h), 0.0f, 1.0e4f, -1.0e4f));
37 cam.Aspect(float(w), float(h));
38 assets.shaders.planet_surface.Activate();
39 assets.shaders.planet_surface.SetVP(cam.View(), cam.Projection());
40 assets.shaders.sun_surface.Activate();
41 assets.shaders.sun_surface.SetVP(cam.View(), cam.Projection());
42 assets.shaders.creature_skin.Activate();
43 assets.shaders.creature_skin.SetVP(cam.View(), cam.Projection());
46 void MasterState::OnUpdate(int dt) {
48 while (remain >= FrameMS()) {
53 void MasterState::Tick() {
58 thirds = (thirds + 1) % 3;
61 int MasterState::FrameMS() const noexcept {
62 return thirds == 0 ? 16 : 17;
66 void MasterState::OnKeyDown(const SDL_KeyboardEvent &e) {
67 if (e.keysym.sym == SDLK_p) {
72 void MasterState::OnRender(graphics::Viewport &viewport) {
74 for (auto sun : sim.Suns()) {
75 // TODO: source sun's light color and strength
76 glm::vec3 pos(cam.View() * cam.Model(*sun)[3]);
77 glm::vec3 col(1.0f, 1.0f, 1.0f);
79 assets.shaders.planet_surface.Activate();
80 assets.shaders.planet_surface.SetLight(num_lights, pos, col, str);
81 assets.shaders.creature_skin.Activate();
82 assets.shaders.creature_skin.SetLight(num_lights, pos, col, str);
84 if (num_lights >= graphics::PlanetSurface::MAX_LIGHTS || num_lights >= graphics::CreatureSkin::MAX_LIGHTS) {
88 for (auto planet : sim.Planets()) {
89 // TODO: indirect light from planets, calculate strength and get color somehow
90 glm::vec3 pos(cam.View() * cam.Model(*planet)[3]);
91 glm::vec3 col(1.0f, 1.0f, 1.0f);
93 assets.shaders.planet_surface.Activate();
94 assets.shaders.planet_surface.SetLight(num_lights, pos, col, str);
95 assets.shaders.creature_skin.Activate();
96 assets.shaders.creature_skin.SetLight(num_lights, pos, col, str);
98 if (num_lights >= graphics::PlanetSurface::MAX_LIGHTS || num_lights >= graphics::CreatureSkin::MAX_LIGHTS) {
102 assets.shaders.planet_surface.Activate();
103 assets.shaders.planet_surface.SetNumLights(num_lights);
104 assets.shaders.creature_skin.Activate();
105 assets.shaders.creature_skin.SetNumLights(num_lights);
107 assets.shaders.planet_surface.Activate();
108 assets.shaders.planet_surface.SetTexture(assets.textures.tiles);
109 for (auto planet : sim.Planets()) {
110 assets.shaders.planet_surface.SetM(cam.Model(*planet));
111 planet->Draw(assets, viewport);
114 assets.shaders.sun_surface.Activate();
115 for (auto sun : sim.Suns()) {
116 double sun_radius = sun->Radius();
117 assets.shaders.sun_surface.SetM(
118 cam.Model(*sun) * glm::scale(glm::vec3(sun_radius, sun_radius, sun_radius)));
119 assets.shaders.sun_surface.SetLight(glm::vec3(1.0f, 1.0f, 1.0f), 1.0e6f);
120 assets.shaders.sun_surface.Draw();
123 assets.shaders.creature_skin.Activate();
124 assets.shaders.creature_skin.SetTexture(assets.textures.skins);
125 // TODO: extend to nearby bodies as well
126 for (auto c : cam.Reference().Creatures()) {
127 assets.shaders.creature_skin.SetM(cam.Model(cam.Reference()) * glm::mat4(c->LocalTransform()));
128 c->Draw(assets, viewport);
131 viewport.ClearDepth();
132 cp.Draw(assets, viewport);