]> git.localhorst.tv Git - blank.git/blob - src/graphics/viewport.cpp
allow disabling vsync to fail
[blank.git] / src / graphics / viewport.cpp
1 #include "Camera.hpp"
2 #include "Canvas.hpp"
3 #include "SkyBox.hpp"
4 #include "Viewport.hpp"
5
6 #include "../app/error.hpp"
7 #include "../geometry/const.hpp"
8
9 #include <GL/glew.h>
10 #include <glm/gtc/matrix_transform.hpp>
11 #include <glm/gtx/transform.hpp>
12 #include <SDL.h>
13
14
15 namespace blank {
16
17 Camera::Camera() noexcept
18 : fov(PI_0p25)
19 , aspect(1.0f)
20 , near(0.1f)
21 , far(256.0f)
22 , projection(glm::perspective(fov, aspect, near, far))
23 , view(1.0f) {
24
25 }
26
27
28 void Camera::FOV(float f) noexcept {
29         fov = f;
30         UpdateProjection();
31 }
32
33 void Camera::Aspect(float r) noexcept {
34         aspect = r;
35         UpdateProjection();
36 }
37
38 void Camera::Aspect(float w, float h) noexcept {
39         Aspect(w / h);
40 }
41
42 void Camera::Clip(float n, float f) noexcept {
43         near = n;
44         far = f;
45         UpdateProjection();
46 }
47
48 void Camera::View(const glm::mat4 &v) noexcept {
49         view = v;
50 }
51
52 void Camera::UpdateProjection() noexcept {
53         projection = glm::perspective(fov, aspect, near, far);
54 }
55
56
57 Canvas::Canvas() noexcept
58 : offset(0.0f, 0.0f)
59 , size(1.0f, 1.0f)
60 , near(100.0f)
61 , far(-100.0f)
62 , projection(glm::ortho(offset.x, size.x, size.y, offset.y, near, far))
63 , view(1.0f) {
64
65 }
66
67
68 void Canvas::Resize(float w, float h) noexcept {
69         size.x = w;
70         size.y = h;
71         UpdateProjection();
72 }
73
74
75 void Canvas::UpdateProjection() noexcept {
76         projection = glm::ortho(offset.x, size.x, size.y, offset.y, near, far);
77 }
78
79
80 SkyBox::SkyBox(CubeMap &&tex)
81 : texture(std::move(tex))
82 , mesh() {
83         mesh.LoadUnitBox();
84 }
85
86 void SkyBox::Render(Viewport &viewport) noexcept {
87         SkyBoxShader &prog = viewport.SkyBoxProgram();
88         prog.SetTexture(texture);
89         mesh.Draw();
90 }
91
92
93 Viewport::Viewport()
94 : cam()
95 , canv()
96 , cursor(1.0f)
97 , cam_offset(0.0f)
98 , chunk_prog()
99 , entity_prog()
100 , sky_prog()
101 , sprite_prog()
102 , active_prog(NONE) {
103         glClearColor(0.0, 0.0, 0.0, 1.0);
104 }
105
106 void Viewport::VSync(bool b) {
107         if (SDL_GL_SetSwapInterval(b) != 0) {
108                 if (b) {
109                         throw SDLError("SDL_GL_SetSwapInterval(1)");
110                 } else {
111                         // allow failure, because this usually means there's no vsync
112                         // support at all, i.e. "it's off"
113                 }
114         }
115 }
116
117 void Viewport::EnableDepthTest() noexcept {
118         glEnable(GL_DEPTH_TEST);
119         glDepthFunc(GL_LESS);
120 }
121
122 void Viewport::EqualDepthTest() noexcept {
123         glEnable(GL_DEPTH_TEST);
124         glDepthFunc(GL_LEQUAL);
125 }
126
127 void Viewport::DisableDepthTest() noexcept {
128         glDisable(GL_DEPTH_TEST);
129 }
130
131 void Viewport::EnableBackfaceCulling() noexcept {
132         glEnable(GL_CULL_FACE);
133 }
134
135 void Viewport::DisableBackfaceCulling() noexcept {
136         glDisable(GL_CULL_FACE);
137 }
138
139 void Viewport::EnableAlphaBlending() noexcept {
140         glEnable(GL_BLEND);
141         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
142 }
143
144 void Viewport::EnableInvertBlending() noexcept {
145         glEnable(GL_BLEND);
146         glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ZERO);
147 }
148
149 void Viewport::DisableBlending() noexcept {
150         glDisable(GL_BLEND);
151 }
152
153 void Viewport::Resize(int w, int h) noexcept {
154         glViewport(0, 0, w, h);
155         float fw = w;
156         float fh = h;
157         cam.Aspect(fw, fh);
158         canv.Resize(fw, fh);
159
160         ChunkProgram().SetProjection(Perspective());
161         SkyBoxProgram().SetProjection(Perspective());
162         SpriteProgram().SetProjection(Ortho());
163 }
164
165 void Viewport::Clear() noexcept {
166         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
167 }
168
169 void Viewport::ClearDepth() noexcept {
170         glClear(GL_DEPTH_BUFFER_BIT);
171 }
172
173
174 glm::vec2 Viewport::GetPosition(const glm::vec2 &off, Gravity grav) const noexcept {
175         return align(grav, canv.Size(), off + canv.Offset());
176 }
177
178 void Viewport::SetCursor(const glm::vec3 &pos) noexcept {
179         cursor[3].x = pos.x;
180         cursor[3].y = pos.y;
181         cursor[3].z = pos.z;
182 }
183
184 void Viewport::SetCursor(const glm::vec3 &pos, Gravity grav) noexcept {
185         glm::vec2 p(GetPosition(glm::vec2(pos), grav));
186         cursor[3].x = p.x;
187         cursor[3].y = p.y;
188         cursor[3].z = pos.z;
189 }
190
191 void Viewport::MoveCursor(const glm::vec3 &d) noexcept {
192         cursor[3].x += d.x;
193         cursor[3].y += d.y;
194         cursor[3].z += d.z;
195 }
196
197
198 BlockLighting &Viewport::ChunkProgram() noexcept {
199         if (active_prog != CHUNK) {
200                 chunk_prog.Activate();
201                 EnableDepthTest();
202                 EnableBackfaceCulling();
203                 DisableBlending();
204                 active_prog = CHUNK;
205         }
206         return chunk_prog;
207 }
208
209 DirectionalLighting &Viewport::EntityProgram() noexcept {
210         if (active_prog != ENTITY) {
211                 entity_prog.Activate();
212                 EnableDepthTest();
213                 EnableBackfaceCulling();
214                 DisableBlending();
215                 entity_prog.SetVP(cam.View(), cam.Projection());
216                 active_prog = ENTITY;
217         }
218         return entity_prog;
219 }
220
221 DirectionalLighting &Viewport::HUDProgram() noexcept {
222         if (active_prog != HUD) {
223                 entity_prog.Activate();
224                 EnableDepthTest();
225                 EnableBackfaceCulling();
226                 entity_prog.SetVP(canv.View(), canv.Projection());
227                 active_prog = HUD;
228         }
229         return entity_prog;
230 }
231
232 PlainColor &Viewport::WorldColorProgram() noexcept {
233         if (active_prog != COLOR_WORLD) {
234                 color_prog.Activate();
235                 color_prog.SetVP(cam.View(), cam.Projection());
236                 active_prog = COLOR_WORLD;
237         }
238         return color_prog;
239 }
240
241 PlainColor &Viewport::HUDColorProgram() noexcept {
242         if (active_prog != COLOR_HUD) {
243                 color_prog.Activate();
244                 color_prog.SetVP(canv.View(), canv.Projection());
245                 active_prog = COLOR_HUD;
246         }
247         return color_prog;
248 }
249
250 SkyBoxShader &Viewport::SkyBoxProgram() noexcept {
251         if (active_prog != SKY_BOX) {
252                 sky_prog.Activate();
253                 DisableBlending();
254                 DisableBackfaceCulling();
255                 EqualDepthTest();
256                 active_prog = SKY_BOX;
257         }
258         return sky_prog;
259 }
260
261 BlendedSprite &Viewport::SpriteProgram() noexcept {
262         if (active_prog != SPRITE) {
263                 sprite_prog.Activate();
264                 EnableAlphaBlending();
265                 active_prog = SPRITE;
266         }
267         return sprite_prog;
268 }
269
270
271 void Viewport::WorldPosition(const glm::mat4 &t) noexcept {
272         cam.View(glm::translate(glm::inverse(t), glm::vec3(t * glm::vec4(cam_offset, 0.0f))));
273         ChunkProgram().SetView(cam.View());
274         sky_prog.Activate();
275         SkyBoxProgram().SetView(cam.View());
276 }
277
278 }