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);
84 glClearColor(0.0, 0.0, 0.0, 1.0);
87 void Viewport::VSync(bool b) noexcept {
88 if (SDL_GL_SetSwapInterval(b) != 0) {
89 throw SDLError("SDL_GL_SetSwapInterval");
93 void Viewport::EnableDepthTest() noexcept {
94 glEnable(GL_DEPTH_TEST);
98 void Viewport::DisableDepthTest() noexcept {
99 glDisable(GL_DEPTH_TEST);
102 void Viewport::EnableBackfaceCulling() noexcept {
103 glEnable(GL_CULL_FACE);
106 void Viewport::DisableBackfaceCulling() noexcept {
107 glDisable(GL_CULL_FACE);
110 void Viewport::EnableAlphaBlending() noexcept {
112 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
115 void Viewport::EnableInvertBlending() noexcept {
117 glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ZERO);
120 void Viewport::DisableBlending() noexcept {
124 void Viewport::Resize(int w, int h) noexcept {
125 glViewport(0, 0, w, h);
131 chunk_prog.SetProjection(Perspective());
132 if (active_prog == HUD) {
133 entity_prog.SetProjection(Ortho());
135 entity_prog.SetProjection(Perspective());
137 sprite_prog.SetProjection(Ortho());
140 void Viewport::Clear() noexcept {
141 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
144 void Viewport::ClearDepth() noexcept {
145 glClear(GL_DEPTH_BUFFER_BIT);
149 void Viewport::SetCursor(const glm::vec3 &pos) {
155 void Viewport::SetCursor(const glm::vec3 &pos, Gravity grav) {
156 glm::vec2 p(align(grav, canv.Size(), glm::vec2(pos) + canv.Offset()));
162 void Viewport::MoveCursor(const glm::vec3 &d) {
169 BlockLighting &Viewport::ChunkProgram() noexcept {
170 if (active_prog != CHUNK) {
171 chunk_prog.Activate();
173 EnableBackfaceCulling();
180 DirectionalLighting &Viewport::EntityProgram() noexcept {
181 if (active_prog != ENTITY) {
182 entity_prog.Activate();
184 EnableBackfaceCulling();
186 entity_prog.SetVP(cam.View(), cam.Projection());
187 active_prog = ENTITY;
192 DirectionalLighting &Viewport::HUDProgram() noexcept {
193 if (active_prog != HUD) {
194 entity_prog.Activate();
196 EnableBackfaceCulling();
197 entity_prog.SetVP(canv.View(), canv.Projection());
203 PlainColor &Viewport::WorldOutlineProgram() noexcept {
204 if (active_prog != OUTLINE_WORLD) {
205 outline_prog.Activate();
206 outline_prog.SetVP(cam.View(), cam.Projection());
207 active_prog = OUTLINE_WORLD;
212 PlainColor &Viewport::HUDOutlineProgram() noexcept {
213 if (active_prog != OUTLINE_HUD) {
214 outline_prog.Activate();
215 outline_prog.SetVP(canv.View(), canv.Projection());
216 active_prog = OUTLINE_HUD;
221 BlendedSprite &Viewport::SpriteProgram() noexcept {
222 if (active_prog != SPRITE) {
223 sprite_prog.Activate();
224 EnableAlphaBlending();
225 active_prog = SPRITE;
231 void Viewport::WorldPosition(const glm::mat4 &t) noexcept {
232 cam.View(glm::inverse(t));
233 chunk_prog.SetView(cam.View());