4 #include "Viewport.hpp"
6 #include "../app/init.hpp"
7 #include "../geometry/const.hpp"
10 #include <glm/gtc/matrix_transform.hpp>
11 #include <glm/gtx/transform.hpp>
17 Camera::Camera() noexcept
22 , projection(glm::perspective(fov, aspect, near, far))
28 void Camera::FOV(float f) noexcept {
33 void Camera::Aspect(float r) noexcept {
38 void Camera::Aspect(float w, float h) noexcept {
42 void Camera::Clip(float n, float f) noexcept {
48 void Camera::View(const glm::mat4 &v) noexcept {
52 void Camera::UpdateProjection() noexcept {
53 projection = glm::perspective(fov, aspect, near, far);
57 Canvas::Canvas() noexcept
62 , projection(glm::ortho(offset.x, size.x, size.y, offset.y, near, far))
68 void Canvas::Resize(float w, float h) noexcept {
75 void Canvas::UpdateProjection() noexcept {
76 projection = glm::ortho(offset.x, size.x, size.y, offset.y, near, far);
80 SkyBox::SkyBox(CubeMap &&tex)
81 : texture(std::move(tex))
86 void SkyBox::Render(Viewport &viewport) noexcept {
87 SkyBoxShader &prog = viewport.SkyBoxProgram();
88 prog.SetTexture(texture);
102 , active_prog(NONE) {
103 glClearColor(0.0, 0.0, 0.0, 1.0);
106 void Viewport::VSync(bool b) noexcept {
107 if (SDL_GL_SetSwapInterval(b) != 0) {
108 throw SDLError("SDL_GL_SetSwapInterval");
112 void Viewport::EnableDepthTest() noexcept {
113 glEnable(GL_DEPTH_TEST);
114 glDepthFunc(GL_LESS);
117 void Viewport::EqualDepthTest() noexcept {
118 glEnable(GL_DEPTH_TEST);
119 glDepthFunc(GL_LEQUAL);
122 void Viewport::DisableDepthTest() noexcept {
123 glDisable(GL_DEPTH_TEST);
126 void Viewport::EnableBackfaceCulling() noexcept {
127 glEnable(GL_CULL_FACE);
130 void Viewport::DisableBackfaceCulling() noexcept {
131 glDisable(GL_CULL_FACE);
134 void Viewport::EnableAlphaBlending() noexcept {
136 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
139 void Viewport::EnableInvertBlending() noexcept {
141 glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ZERO);
144 void Viewport::DisableBlending() noexcept {
148 void Viewport::Resize(int w, int h) noexcept {
149 glViewport(0, 0, w, h);
155 ChunkProgram().SetProjection(Perspective());
156 SkyBoxProgram().SetProjection(Perspective());
157 SpriteProgram().SetProjection(Ortho());
160 void Viewport::Clear() noexcept {
161 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
164 void Viewport::ClearDepth() noexcept {
165 glClear(GL_DEPTH_BUFFER_BIT);
169 glm::vec2 Viewport::GetPosition(const glm::vec2 &off, Gravity grav) const noexcept {
170 return align(grav, canv.Size(), off + canv.Offset());
173 void Viewport::SetCursor(const glm::vec3 &pos) noexcept {
179 void Viewport::SetCursor(const glm::vec3 &pos, Gravity grav) noexcept {
180 glm::vec2 p(GetPosition(glm::vec2(pos), grav));
186 void Viewport::MoveCursor(const glm::vec3 &d) noexcept {
193 BlockLighting &Viewport::ChunkProgram() noexcept {
194 if (active_prog != CHUNK) {
195 chunk_prog.Activate();
197 EnableBackfaceCulling();
204 DirectionalLighting &Viewport::EntityProgram() noexcept {
205 if (active_prog != ENTITY) {
206 entity_prog.Activate();
208 EnableBackfaceCulling();
210 entity_prog.SetVP(cam.View(), cam.Projection());
211 active_prog = ENTITY;
216 DirectionalLighting &Viewport::HUDProgram() noexcept {
217 if (active_prog != HUD) {
218 entity_prog.Activate();
220 EnableBackfaceCulling();
221 entity_prog.SetVP(canv.View(), canv.Projection());
227 PlainColor &Viewport::WorldColorProgram() noexcept {
228 if (active_prog != COLOR_WORLD) {
229 color_prog.Activate();
230 color_prog.SetVP(cam.View(), cam.Projection());
231 active_prog = COLOR_WORLD;
236 PlainColor &Viewport::HUDColorProgram() noexcept {
237 if (active_prog != COLOR_HUD) {
238 color_prog.Activate();
239 color_prog.SetVP(canv.View(), canv.Projection());
240 active_prog = COLOR_HUD;
245 SkyBoxShader &Viewport::SkyBoxProgram() noexcept {
246 if (active_prog != SKY_BOX) {
249 DisableBackfaceCulling();
251 active_prog = SKY_BOX;
256 BlendedSprite &Viewport::SpriteProgram() noexcept {
257 if (active_prog != SPRITE) {
258 sprite_prog.Activate();
259 EnableAlphaBlending();
260 active_prog = SPRITE;
266 void Viewport::WorldPosition(const glm::mat4 &t) noexcept {
267 cam.View(glm::translate(glm::inverse(t), glm::vec3(t * glm::vec4(cam_offset, 0.0f))));
268 ChunkProgram().SetView(cam.View());
270 SkyBoxProgram().SetView(cam.View());