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 void Viewport::SetCursor(const glm::vec3 &pos) {
174 void Viewport::SetCursor(const glm::vec3 &pos, Gravity grav) {
175 glm::vec2 p(align(grav, canv.Size(), glm::vec2(pos) + canv.Offset()));
181 void Viewport::MoveCursor(const glm::vec3 &d) {
188 BlockLighting &Viewport::ChunkProgram() noexcept {
189 if (active_prog != CHUNK) {
190 chunk_prog.Activate();
192 EnableBackfaceCulling();
199 DirectionalLighting &Viewport::EntityProgram() noexcept {
200 if (active_prog != ENTITY) {
201 entity_prog.Activate();
203 EnableBackfaceCulling();
205 entity_prog.SetVP(cam.View(), cam.Projection());
206 active_prog = ENTITY;
211 DirectionalLighting &Viewport::HUDProgram() noexcept {
212 if (active_prog != HUD) {
213 entity_prog.Activate();
215 EnableBackfaceCulling();
216 entity_prog.SetVP(canv.View(), canv.Projection());
222 PlainColor &Viewport::WorldColorProgram() noexcept {
223 if (active_prog != COLOR_WORLD) {
224 color_prog.Activate();
225 color_prog.SetVP(cam.View(), cam.Projection());
226 active_prog = COLOR_WORLD;
231 PlainColor &Viewport::HUDColorProgram() noexcept {
232 if (active_prog != COLOR_HUD) {
233 color_prog.Activate();
234 color_prog.SetVP(canv.View(), canv.Projection());
235 active_prog = COLOR_HUD;
240 SkyBoxShader &Viewport::SkyBoxProgram() noexcept {
241 if (active_prog != SKY_BOX) {
244 DisableBackfaceCulling();
246 active_prog = SKY_BOX;
251 BlendedSprite &Viewport::SpriteProgram() noexcept {
252 if (active_prog != SPRITE) {
253 sprite_prog.Activate();
254 EnableAlphaBlending();
255 active_prog = SPRITE;
261 void Viewport::WorldPosition(const glm::mat4 &t) noexcept {
262 const glm::vec3 offset(0.0f, 0.0f, 0.0f);
263 //const glm::vec3 offset(0.0f, 0.0f, -5.0f);
264 cam.View(glm::translate(glm::inverse(t), glm::vec3(t * glm::vec4(offset, 0.0f))));
265 ChunkProgram().SetView(cam.View());
267 SkyBoxProgram().SetView(cam.View());