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 {
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);
101 , active_prog(NONE) {
102 glClearColor(0.0, 0.0, 0.0, 1.0);
105 void Viewport::VSync(bool b) noexcept {
106 if (SDL_GL_SetSwapInterval(b) != 0) {
107 throw SDLError("SDL_GL_SetSwapInterval");
111 void Viewport::EnableDepthTest() noexcept {
112 glEnable(GL_DEPTH_TEST);
113 glDepthFunc(GL_LESS);
116 void Viewport::EqualDepthTest() noexcept {
117 glEnable(GL_DEPTH_TEST);
118 glDepthFunc(GL_LEQUAL);
121 void Viewport::DisableDepthTest() noexcept {
122 glDisable(GL_DEPTH_TEST);
125 void Viewport::EnableBackfaceCulling() noexcept {
126 glEnable(GL_CULL_FACE);
129 void Viewport::DisableBackfaceCulling() noexcept {
130 glDisable(GL_CULL_FACE);
133 void Viewport::EnableAlphaBlending() noexcept {
135 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
138 void Viewport::EnableInvertBlending() noexcept {
140 glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ZERO);
143 void Viewport::DisableBlending() noexcept {
147 void Viewport::Resize(int w, int h) noexcept {
148 glViewport(0, 0, w, h);
154 ChunkProgram().SetProjection(Perspective());
155 SkyBoxProgram().SetProjection(Perspective());
156 SpriteProgram().SetProjection(Ortho());
159 void Viewport::Clear() noexcept {
160 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
163 void Viewport::ClearDepth() noexcept {
164 glClear(GL_DEPTH_BUFFER_BIT);
168 glm::vec2 Viewport::GetPosition(const glm::vec2 &off, Gravity grav) const noexcept {
169 return align(grav, canv.Size(), off + canv.Offset());
172 void Viewport::SetCursor(const glm::vec3 &pos) noexcept {
178 void Viewport::SetCursor(const glm::vec3 &pos, Gravity grav) noexcept {
179 glm::vec2 p(GetPosition(glm::vec2(pos), grav));
185 void Viewport::MoveCursor(const glm::vec3 &d) noexcept {
192 BlockLighting &Viewport::ChunkProgram() noexcept {
193 if (active_prog != CHUNK) {
194 chunk_prog.Activate();
196 EnableBackfaceCulling();
203 DirectionalLighting &Viewport::EntityProgram() noexcept {
204 if (active_prog != ENTITY) {
205 entity_prog.Activate();
207 EnableBackfaceCulling();
209 entity_prog.SetVP(cam.View(), cam.Projection());
210 active_prog = ENTITY;
215 DirectionalLighting &Viewport::HUDProgram() noexcept {
216 if (active_prog != HUD) {
217 entity_prog.Activate();
219 EnableBackfaceCulling();
220 entity_prog.SetVP(canv.View(), canv.Projection());
226 PlainColor &Viewport::WorldColorProgram() noexcept {
227 if (active_prog != COLOR_WORLD) {
228 color_prog.Activate();
229 color_prog.SetVP(cam.View(), cam.Projection());
230 active_prog = COLOR_WORLD;
235 PlainColor &Viewport::HUDColorProgram() noexcept {
236 if (active_prog != COLOR_HUD) {
237 color_prog.Activate();
238 color_prog.SetVP(canv.View(), canv.Projection());
239 active_prog = COLOR_HUD;
244 SkyBoxShader &Viewport::SkyBoxProgram() noexcept {
245 if (active_prog != SKY_BOX) {
248 DisableBackfaceCulling();
250 active_prog = SKY_BOX;
255 BlendedSprite &Viewport::SpriteProgram() noexcept {
256 if (active_prog != SPRITE) {
257 sprite_prog.Activate();
258 EnableAlphaBlending();
259 active_prog = SPRITE;
265 void Viewport::WorldPosition(const glm::mat4 &t) noexcept {
266 const glm::vec3 offset(0.0f, 0.0f, 0.0f);
267 //const glm::vec3 offset(0.0f, 0.0f, -5.0f);
268 cam.View(glm::translate(glm::inverse(t), glm::vec3(t * glm::vec4(offset, 0.0f))));
269 ChunkProgram().SetView(cam.View());
271 SkyBoxProgram().SetView(cam.View());