4 #include "Viewport.hpp"
6 #include "../app/init.hpp"
7 #include "../model/geometry.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 {
49 void Camera::UpdateProjection() noexcept {
50 projection = glm::perspective(fov, aspect, near, far);
54 Canvas::Canvas() noexcept
59 , projection(glm::ortho(offset.x, size.x, size.y, offset.y, near, far))
65 void Canvas::Resize(float w, float h) noexcept {
72 void Canvas::UpdateProjection() noexcept {
73 projection = glm::ortho(offset.x, size.x, size.y, offset.y, near, far);
77 SkyBox::SkyBox(CubeMap &&tex)
78 : texture(std::move(tex))
83 void SkyBox::Render(Viewport &viewport) noexcept {
84 SkyBoxShader &prog = viewport.SkyBoxProgram();
85 prog.SetTexture(texture);
99 glClearColor(0.0, 0.0, 0.0, 1.0);
102 void Viewport::VSync(bool b) noexcept {
103 if (SDL_GL_SetSwapInterval(b) != 0) {
104 throw SDLError("SDL_GL_SetSwapInterval");
108 void Viewport::EnableDepthTest() noexcept {
109 glEnable(GL_DEPTH_TEST);
110 glDepthFunc(GL_LESS);
113 void Viewport::EqualDepthTest() noexcept {
114 glEnable(GL_DEPTH_TEST);
115 glDepthFunc(GL_LEQUAL);
118 void Viewport::DisableDepthTest() noexcept {
119 glDisable(GL_DEPTH_TEST);
122 void Viewport::EnableBackfaceCulling() noexcept {
123 glEnable(GL_CULL_FACE);
126 void Viewport::DisableBackfaceCulling() noexcept {
127 glDisable(GL_CULL_FACE);
130 void Viewport::EnableAlphaBlending() noexcept {
132 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
135 void Viewport::EnableInvertBlending() noexcept {
137 glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ZERO);
140 void Viewport::DisableBlending() noexcept {
144 void Viewport::Resize(int w, int h) noexcept {
145 glViewport(0, 0, w, h);
151 ChunkProgram().SetProjection(Perspective());
152 SkyBoxProgram().SetProjection(Perspective());
153 SpriteProgram().SetProjection(Ortho());
156 void Viewport::Clear() noexcept {
157 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
160 void Viewport::ClearDepth() noexcept {
161 glClear(GL_DEPTH_BUFFER_BIT);
165 void Viewport::SetCursor(const glm::vec3 &pos) {
171 void Viewport::SetCursor(const glm::vec3 &pos, Gravity grav) {
172 glm::vec2 p(align(grav, canv.Size(), glm::vec2(pos) + canv.Offset()));
178 void Viewport::MoveCursor(const glm::vec3 &d) {
185 BlockLighting &Viewport::ChunkProgram() noexcept {
186 if (active_prog != CHUNK) {
187 chunk_prog.Activate();
189 EnableBackfaceCulling();
196 DirectionalLighting &Viewport::EntityProgram() noexcept {
197 if (active_prog != ENTITY) {
198 entity_prog.Activate();
200 EnableBackfaceCulling();
202 entity_prog.SetVP(cam.View(), cam.Projection());
203 active_prog = ENTITY;
208 DirectionalLighting &Viewport::HUDProgram() noexcept {
209 if (active_prog != HUD) {
210 entity_prog.Activate();
212 EnableBackfaceCulling();
213 entity_prog.SetVP(canv.View(), canv.Projection());
219 PlainColor &Viewport::WorldOutlineProgram() noexcept {
220 if (active_prog != OUTLINE_WORLD) {
221 outline_prog.Activate();
222 outline_prog.SetVP(cam.View(), cam.Projection());
223 active_prog = OUTLINE_WORLD;
228 PlainColor &Viewport::HUDOutlineProgram() noexcept {
229 if (active_prog != OUTLINE_HUD) {
230 outline_prog.Activate();
231 outline_prog.SetVP(canv.View(), canv.Projection());
232 active_prog = OUTLINE_HUD;
237 SkyBoxShader &Viewport::SkyBoxProgram() noexcept {
238 if (active_prog != SKY_BOX) {
241 DisableBackfaceCulling();
243 active_prog = SKY_BOX;
248 BlendedSprite &Viewport::SpriteProgram() noexcept {
249 if (active_prog != SPRITE) {
250 sprite_prog.Activate();
251 EnableAlphaBlending();
252 active_prog = SPRITE;
258 void Viewport::WorldPosition(const glm::mat4 &t) noexcept {
259 cam.View(glm::inverse(t));
260 ChunkProgram().SetView(cam.View());
262 SkyBoxProgram().SetView(cam.View());