3 #include "Viewport.hpp"
5 #include "../app/init.hpp"
6 #include "../model/geometry.hpp"
9 #include <glm/gtc/matrix_transform.hpp>
10 #include <glm/gtx/transform.hpp>
16 Camera::Camera() noexcept
21 , projection(glm::perspective(fov, aspect, near, far))
27 void Camera::FOV(float f) noexcept {
32 void Camera::Aspect(float r) noexcept {
37 void Camera::Aspect(float w, float h) noexcept {
41 void Camera::Clip(float n, float f) noexcept {
48 void Camera::UpdateProjection() noexcept {
49 projection = glm::perspective(fov, aspect, near, far);
53 Canvas::Canvas() noexcept
58 , projection(glm::ortho(offset.x, size.x, size.y, offset.y, near, far))
64 void Canvas::Resize(float w, float h) noexcept {
71 void Canvas::UpdateProjection() noexcept {
72 projection = glm::ortho(offset.x, size.x, size.y, offset.y, near, far);
85 glClearColor(0.0, 0.0, 0.0, 1.0);
88 void Viewport::VSync(bool b) noexcept {
89 if (SDL_GL_SetSwapInterval(b) != 0) {
90 throw SDLError("SDL_GL_SetSwapInterval");
94 void Viewport::EnableDepthTest() noexcept {
95 glEnable(GL_DEPTH_TEST);
99 void Viewport::EqualDepthTest() noexcept {
100 glEnable(GL_DEPTH_TEST);
101 glDepthFunc(GL_LEQUAL);
104 void Viewport::DisableDepthTest() noexcept {
105 glDisable(GL_DEPTH_TEST);
108 void Viewport::EnableBackfaceCulling() noexcept {
109 glEnable(GL_CULL_FACE);
112 void Viewport::DisableBackfaceCulling() noexcept {
113 glDisable(GL_CULL_FACE);
116 void Viewport::EnableAlphaBlending() noexcept {
118 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
121 void Viewport::EnableInvertBlending() noexcept {
123 glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ZERO);
126 void Viewport::DisableBlending() noexcept {
130 void Viewport::Resize(int w, int h) noexcept {
131 glViewport(0, 0, w, h);
137 chunk_prog.SetProjection(Perspective());
138 if (active_prog == HUD) {
139 entity_prog.SetProjection(Ortho());
141 entity_prog.SetProjection(Perspective());
143 sprite_prog.SetProjection(Ortho());
146 void Viewport::Clear() noexcept {
147 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
150 void Viewport::ClearDepth() noexcept {
151 glClear(GL_DEPTH_BUFFER_BIT);
155 void Viewport::SetCursor(const glm::vec3 &pos) {
161 void Viewport::SetCursor(const glm::vec3 &pos, Gravity grav) {
162 glm::vec2 p(align(grav, canv.Size(), glm::vec2(pos) + canv.Offset()));
168 void Viewport::MoveCursor(const glm::vec3 &d) {
175 BlockLighting &Viewport::ChunkProgram() noexcept {
176 if (active_prog != CHUNK) {
177 chunk_prog.Activate();
179 EnableBackfaceCulling();
186 DirectionalLighting &Viewport::EntityProgram() noexcept {
187 if (active_prog != ENTITY) {
188 entity_prog.Activate();
190 EnableBackfaceCulling();
192 entity_prog.SetVP(cam.View(), cam.Projection());
193 active_prog = ENTITY;
198 DirectionalLighting &Viewport::HUDProgram() noexcept {
199 if (active_prog != HUD) {
200 entity_prog.Activate();
202 EnableBackfaceCulling();
203 entity_prog.SetVP(canv.View(), canv.Projection());
209 PlainColor &Viewport::WorldOutlineProgram() noexcept {
210 if (active_prog != OUTLINE_WORLD) {
211 outline_prog.Activate();
212 outline_prog.SetVP(cam.View(), cam.Projection());
213 active_prog = OUTLINE_WORLD;
218 PlainColor &Viewport::HUDOutlineProgram() noexcept {
219 if (active_prog != OUTLINE_HUD) {
220 outline_prog.Activate();
221 outline_prog.SetVP(canv.View(), canv.Projection());
222 active_prog = OUTLINE_HUD;
227 SkyBoxShader &Viewport::SkyBoxProgram() noexcept {
228 if (active_prog != SKY_BOX) {
232 active_prog = SKY_BOX;
237 BlendedSprite &Viewport::SpriteProgram() noexcept {
238 if (active_prog != SPRITE) {
239 sprite_prog.Activate();
240 EnableAlphaBlending();
241 active_prog = SPRITE;
247 void Viewport::WorldPosition(const glm::mat4 &t) noexcept {
248 cam.View(glm::inverse(t));
249 chunk_prog.SetView(cam.View());