3 #include "Viewport.hpp"
5 #include "../app/error.hpp"
6 #include "../geometry/const.hpp"
9 #include <glm/gtc/matrix_transform.hpp>
10 #include <glm/gtx/transform.hpp>
17 Camera::Camera() noexcept
18 : fov(geometry::PI_0p25)
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);
89 glClearColor(0.0, 0.0, 0.0, 1.0);
92 void Viewport::VSync(bool b) {
93 if (SDL_GL_SetSwapInterval(b) != 0) {
95 throw app::SDLError("SDL_GL_SetSwapInterval(1)");
97 // allow failure, because this usually means there's no vsync
98 // support at all, i.e. "it's off"
103 void Viewport::EnableDepthTest() noexcept {
104 glEnable(GL_DEPTH_TEST);
105 glDepthFunc(GL_LESS);
108 void Viewport::EqualDepthTest() noexcept {
109 glEnable(GL_DEPTH_TEST);
110 glDepthFunc(GL_LEQUAL);
113 void Viewport::DisableDepthTest() noexcept {
114 glDisable(GL_DEPTH_TEST);
117 void Viewport::EnableBackfaceCulling() noexcept {
118 glEnable(GL_CULL_FACE);
121 void Viewport::DisableBackfaceCulling() noexcept {
122 glDisable(GL_CULL_FACE);
125 void Viewport::EnableAlphaBlending() noexcept {
127 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
130 void Viewport::EnableInvertBlending() noexcept {
132 glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ZERO);
135 void Viewport::DisableBlending() noexcept {
139 void Viewport::Resize(int w, int h) noexcept {
140 glViewport(0, 0, w, h);
146 SkyBoxProgram().SetProjection(Perspective());
147 SpriteProgram().SetProjection(Ortho());
150 void Viewport::Clear() noexcept {
151 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
154 void Viewport::ClearDepth() noexcept {
155 glClear(GL_DEPTH_BUFFER_BIT);
159 glm::vec2 Viewport::GetPosition(const glm::vec2 &off, Gravity grav) const noexcept {
160 return align(grav, canv.Size(), off + canv.Offset());
163 void Viewport::SetCursor(const glm::vec3 &pos) noexcept {
169 void Viewport::SetCursor(const glm::vec3 &pos, Gravity grav) noexcept {
170 glm::vec2 p(GetPosition(glm::vec2(pos), grav));
176 void Viewport::MoveCursor(const glm::vec3 &d) noexcept {
183 PlainColor &Viewport::HUDColorProgram() noexcept {
184 if (active_prog != COLOR_HUD) {
185 color_prog.Activate();
186 color_prog.SetVP(canv.View(), canv.Projection());
187 active_prog = COLOR_HUD;
192 SkyBoxShader &Viewport::SkyBoxProgram() noexcept {
193 if (active_prog != SKY_BOX) {
196 DisableBackfaceCulling();
198 active_prog = SKY_BOX;
203 BlendedSprite &Viewport::SpriteProgram() noexcept {
204 if (active_prog != SPRITE) {
205 sprite_prog.Activate();
206 EnableAlphaBlending();
207 active_prog = SPRITE;
213 void Viewport::WorldPosition(const glm::mat4 &t) noexcept {
214 cam.View(glm::translate(glm::inverse(t), glm::vec3(t * glm::vec4(cam_offset, 0.0f))));