X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fapp%2Fstates.cpp;h=b1a20db92a50740934dfac6849065940c84e4ab6;hb=2ab70a92ae39cebc6166ef15545ebcbd31a31c38;hp=0298e0ce6b1a7eeb6e3b923beefe0d7eceb3e952;hpb=be413456f57da06e918ae7bf4c4f35e5198ff7ce;p=blobs.git diff --git a/src/app/states.cpp b/src/app/states.cpp index 0298e0c..b1a20db 100644 --- a/src/app/states.cpp +++ b/src/app/states.cpp @@ -1,7 +1,11 @@ #include "MasterState.hpp" +#include "../creature/Creature.hpp" +#include "../graphics/Viewport.hpp" #include "../world/Body.hpp" +#include "../world/Planet.hpp" #include "../world/Simulation.hpp" +#include "../world/Sun.hpp" #include @@ -13,11 +17,11 @@ MasterState::MasterState(Assets &assets, world::Simulation &sim) noexcept : State() , assets(assets) , sim(sim) -, reference(&sim.Root()) -, cam() +, cam(sim.Root()) +, cp(assets) , remain(0) -, thirds(0) { - cam.View(glm::translate(glm::vec3(-3.0f, -2.0f, -10.0f))); +, thirds(0) +, paused(false) { } MasterState::~MasterState() noexcept { @@ -25,7 +29,18 @@ MasterState::~MasterState() noexcept { void MasterState::OnResize(int w, int h) { + assets.shaders.canvas.Activate(); + assets.shaders.canvas.Resize(float(w), float(h)); + assets.shaders.alpha_sprite.Activate(); + assets.shaders.alpha_sprite.SetVP(glm::mat4(1.0f), glm::ortho(0.0f, float(w), float(h), 0.0f, 1.0e4f, -1.0e4f)); + cam.Aspect(float(w), float(h)); + assets.shaders.planet_surface.Activate(); + assets.shaders.planet_surface.SetVP(cam.View(), cam.Projection()); + assets.shaders.sun_surface.Activate(); + assets.shaders.sun_surface.SetVP(cam.View(), cam.Projection()); + assets.shaders.creature_skin.Activate(); + assets.shaders.creature_skin.SetVP(cam.View(), cam.Projection()); } void MasterState::OnUpdate(int dt) { @@ -36,7 +51,9 @@ void MasterState::OnUpdate(int dt) { } void MasterState::Tick() { - sim.Tick(); + if (!paused) { + sim.Tick(); + } remain -= FrameMS(); thirds = (thirds + 1) % 3; } @@ -46,11 +63,85 @@ int MasterState::FrameMS() const noexcept { } +void MasterState::OnKeyDown(const SDL_KeyboardEvent &e) { + if (e.keysym.sym == SDLK_p) { + paused = !paused; + } +} + void MasterState::OnRender(graphics::Viewport &viewport) { + if (cp.Shown()) { + cam.TopDown(cp.GetCreature(), 10.0f); + assets.shaders.planet_surface.Activate(); + assets.shaders.planet_surface.SetV(cam.View()); + assets.shaders.sun_surface.Activate(); + assets.shaders.sun_surface.SetV(cam.View()); + assets.shaders.creature_skin.Activate(); + assets.shaders.creature_skin.SetV(cam.View()); + } + + int num_lights = 0; + for (auto sun : sim.Suns()) { + // TODO: source sun's light color and strength + glm::vec3 pos(cam.View() * cam.Model(*sun)[3]); + glm::vec3 col(1.0f, 1.0f, 1.0f); + float str = 1.0e6f; + assets.shaders.planet_surface.Activate(); + assets.shaders.planet_surface.SetLight(num_lights, pos, col, str); + assets.shaders.creature_skin.Activate(); + assets.shaders.creature_skin.SetLight(num_lights, pos, col, str); + ++num_lights; + if (num_lights >= graphics::PlanetSurface::MAX_LIGHTS || num_lights >= graphics::CreatureSkin::MAX_LIGHTS) { + break; + } + } + for (auto planet : sim.Planets()) { + // TODO: indirect light from planets, calculate strength and get color somehow + glm::vec3 pos(cam.View() * cam.Model(*planet)[3]); + glm::vec3 col(1.0f, 1.0f, 1.0f); + float str = 10.0f; + assets.shaders.planet_surface.Activate(); + assets.shaders.planet_surface.SetLight(num_lights, pos, col, str); + assets.shaders.creature_skin.Activate(); + assets.shaders.creature_skin.SetLight(num_lights, pos, col, str); + ++num_lights; + if (num_lights >= graphics::PlanetSurface::MAX_LIGHTS || num_lights >= graphics::CreatureSkin::MAX_LIGHTS) { + break; + } + } + assets.shaders.planet_surface.Activate(); + assets.shaders.planet_surface.SetNumLights(num_lights); + assets.shaders.creature_skin.Activate(); + assets.shaders.creature_skin.SetNumLights(num_lights); + assets.shaders.planet_surface.Activate(); - assets.shaders.planet_surface.SetMVP(glm::mat4(1.0f), cam.View(), cam.Projection()); assets.shaders.planet_surface.SetTexture(assets.textures.tiles); - reference->Draw(assets, viewport); + for (auto planet : sim.Planets()) { + assets.shaders.planet_surface.SetM(cam.Model(*planet)); + planet->Draw(assets, viewport); + } + + assets.shaders.sun_surface.Activate(); + for (auto sun : sim.Suns()) { + double sun_radius = sun->Radius(); + assets.shaders.sun_surface.SetM( + cam.Model(*sun) * glm::scale(glm::vec3(sun_radius, sun_radius, sun_radius))); + assets.shaders.sun_surface.SetLight(glm::vec3(1.0f, 1.0f, 1.0f), 1.0e6f); + assets.shaders.sun_surface.Draw(); + } + + assets.shaders.creature_skin.Activate(); + assets.shaders.creature_skin.SetTexture(assets.textures.skins); + // TODO: extend to nearby bodies as well + for (auto c : cam.Reference().Creatures()) { + assets.shaders.creature_skin.SetM(cam.Model(cam.Reference()) * glm::mat4(c->LocalTransform())); + assets.shaders.creature_skin.SetBaseColor(c->BaseColor()); + assets.shaders.creature_skin.SetHighlightColor(c->HighlightColor()); + c->Draw(viewport); + } + + viewport.ClearDepth(); + cp.Draw(assets, viewport); } }