]> git.localhorst.tv Git - blobs.git/blob - src/app/states.cpp
track creature with camera
[blobs.git] / src / app / states.cpp
1 #include "MasterState.hpp"
2
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"
9
10 #include <glm/gtx/transform.hpp>
11
12
13 namespace blobs {
14 namespace app {
15
16 MasterState::MasterState(Assets &assets, world::Simulation &sim) noexcept
17 : State()
18 , assets(assets)
19 , sim(sim)
20 , cam(sim.Root())
21 , cp(assets)
22 , remain(0)
23 , thirds(0)
24 , paused(false) {
25 }
26
27 MasterState::~MasterState() noexcept {
28 }
29
30
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));
36
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());
44 }
45
46 void MasterState::OnUpdate(int dt) {
47         remain += dt;
48         while (remain >= FrameMS()) {
49                 Tick();
50         }
51 }
52
53 void MasterState::Tick() {
54         if (!paused) {
55                 sim.Tick();
56         }
57         remain -= FrameMS();
58         thirds = (thirds + 1) % 3;
59 }
60
61 int MasterState::FrameMS() const noexcept {
62         return thirds == 0 ? 16 : 17;
63 }
64
65
66 void MasterState::OnKeyDown(const SDL_KeyboardEvent &e) {
67         if (e.keysym.sym == SDLK_p) {
68                 paused = !paused;
69         }
70 }
71
72 void MasterState::OnRender(graphics::Viewport &viewport) {
73         if (cp.Shown()) {
74                 cam.TopDown(cp.GetCreature(), 10.0f);
75                 assets.shaders.planet_surface.Activate();
76                 assets.shaders.planet_surface.SetV(cam.View());
77                 assets.shaders.sun_surface.Activate();
78                 assets.shaders.sun_surface.SetV(cam.View());
79                 assets.shaders.creature_skin.Activate();
80                 assets.shaders.creature_skin.SetV(cam.View());
81         }
82
83         int num_lights = 0;
84         for (auto sun : sim.Suns()) {
85                 // TODO: source sun's light color and strength
86                 glm::vec3 pos(cam.View() * cam.Model(*sun)[3]);
87                 glm::vec3 col(1.0f, 1.0f, 1.0f);
88                 float str = 1.0e6f;
89                 assets.shaders.planet_surface.Activate();
90                 assets.shaders.planet_surface.SetLight(num_lights, pos, col, str);
91                 assets.shaders.creature_skin.Activate();
92                 assets.shaders.creature_skin.SetLight(num_lights, pos, col, str);
93                 ++num_lights;
94                 if (num_lights >= graphics::PlanetSurface::MAX_LIGHTS || num_lights >= graphics::CreatureSkin::MAX_LIGHTS) {
95                         break;
96                 }
97         }
98         for (auto planet : sim.Planets()) {
99                 // TODO: indirect light from planets, calculate strength and get color somehow
100                 glm::vec3 pos(cam.View() * cam.Model(*planet)[3]);
101                 glm::vec3 col(1.0f, 1.0f, 1.0f);
102                 float str = 10.0f;
103                 assets.shaders.planet_surface.Activate();
104                 assets.shaders.planet_surface.SetLight(num_lights, pos, col, str);
105                 assets.shaders.creature_skin.Activate();
106                 assets.shaders.creature_skin.SetLight(num_lights, pos, col, str);
107                 ++num_lights;
108                 if (num_lights >= graphics::PlanetSurface::MAX_LIGHTS || num_lights >= graphics::CreatureSkin::MAX_LIGHTS) {
109                         break;
110                 }
111         }
112         assets.shaders.planet_surface.Activate();
113         assets.shaders.planet_surface.SetNumLights(num_lights);
114         assets.shaders.creature_skin.Activate();
115         assets.shaders.creature_skin.SetNumLights(num_lights);
116
117         assets.shaders.planet_surface.Activate();
118         assets.shaders.planet_surface.SetTexture(assets.textures.tiles);
119         for (auto planet : sim.Planets()) {
120                 assets.shaders.planet_surface.SetM(cam.Model(*planet));
121                 planet->Draw(assets, viewport);
122         }
123
124         assets.shaders.sun_surface.Activate();
125         for (auto sun : sim.Suns()) {
126                 double sun_radius = sun->Radius();
127                 assets.shaders.sun_surface.SetM(
128                         cam.Model(*sun) * glm::scale(glm::vec3(sun_radius, sun_radius, sun_radius)));
129                 assets.shaders.sun_surface.SetLight(glm::vec3(1.0f, 1.0f, 1.0f), 1.0e6f);
130                 assets.shaders.sun_surface.Draw();
131         }
132
133         assets.shaders.creature_skin.Activate();
134         assets.shaders.creature_skin.SetTexture(assets.textures.skins);
135         // TODO: extend to nearby bodies as well
136         for (auto c : cam.Reference().Creatures()) {
137                 assets.shaders.creature_skin.SetM(cam.Model(cam.Reference()) * glm::mat4(c->LocalTransform()));
138                 c->Draw(viewport);
139         }
140
141         viewport.ClearDepth();
142         cp.Draw(assets, viewport);
143 }
144
145 }
146 }