]> git.localhorst.tv Git - blank.git/blob - src/graphics/viewport.cpp
f9558d036865de4e3b388e5e22a96b1d1a8536d8
[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/init.hpp"
7 #include "../model/geometry.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 , chunk_prog()
98 , entity_prog()
99 , sky_prog()
100 , sprite_prog()
101 , active_prog(NONE) {
102         glClearColor(0.0, 0.0, 0.0, 1.0);
103 }
104
105 void Viewport::VSync(bool b) noexcept {
106         if (SDL_GL_SetSwapInterval(b) != 0) {
107                 throw SDLError("SDL_GL_SetSwapInterval");
108         }
109 }
110
111 void Viewport::EnableDepthTest() noexcept {
112         glEnable(GL_DEPTH_TEST);
113         glDepthFunc(GL_LESS);
114 }
115
116 void Viewport::EqualDepthTest() noexcept {
117         glEnable(GL_DEPTH_TEST);
118         glDepthFunc(GL_LEQUAL);
119 }
120
121 void Viewport::DisableDepthTest() noexcept {
122         glDisable(GL_DEPTH_TEST);
123 }
124
125 void Viewport::EnableBackfaceCulling() noexcept {
126         glEnable(GL_CULL_FACE);
127 }
128
129 void Viewport::DisableBackfaceCulling() noexcept {
130         glDisable(GL_CULL_FACE);
131 }
132
133 void Viewport::EnableAlphaBlending() noexcept {
134         glEnable(GL_BLEND);
135         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
136 }
137
138 void Viewport::EnableInvertBlending() noexcept {
139         glEnable(GL_BLEND);
140         glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ZERO);
141 }
142
143 void Viewport::DisableBlending() noexcept {
144         glDisable(GL_BLEND);
145 }
146
147 void Viewport::Resize(int w, int h) noexcept {
148         glViewport(0, 0, w, h);
149         float fw = w;
150         float fh = h;
151         cam.Aspect(fw, fh);
152         canv.Resize(fw, fh);
153
154         ChunkProgram().SetProjection(Perspective());
155         SkyBoxProgram().SetProjection(Perspective());
156         SpriteProgram().SetProjection(Ortho());
157 }
158
159 void Viewport::Clear() noexcept {
160         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
161 }
162
163 void Viewport::ClearDepth() noexcept {
164         glClear(GL_DEPTH_BUFFER_BIT);
165 }
166
167
168 void Viewport::SetCursor(const glm::vec3 &pos) {
169         cursor[3].x = pos.x;
170         cursor[3].y = pos.y;
171         cursor[3].z = pos.z;
172 }
173
174 void Viewport::SetCursor(const glm::vec3 &pos, Gravity grav) {
175         glm::vec2 p(align(grav, canv.Size(), glm::vec2(pos) + canv.Offset()));
176         cursor[3].x = p.x;
177         cursor[3].y = p.y;
178         cursor[3].z = pos.z;
179 }
180
181 void Viewport::MoveCursor(const glm::vec3 &d) {
182         cursor[3].x += d.x;
183         cursor[3].y += d.y;
184         cursor[3].z += d.z;
185 }
186
187
188 BlockLighting &Viewport::ChunkProgram() noexcept {
189         if (active_prog != CHUNK) {
190                 chunk_prog.Activate();
191                 EnableDepthTest();
192                 EnableBackfaceCulling();
193                 DisableBlending();
194                 active_prog = CHUNK;
195         }
196         return chunk_prog;
197 }
198
199 DirectionalLighting &Viewport::EntityProgram() noexcept {
200         if (active_prog != ENTITY) {
201                 entity_prog.Activate();
202                 EnableDepthTest();
203                 EnableBackfaceCulling();
204                 DisableBlending();
205                 entity_prog.SetVP(cam.View(), cam.Projection());
206                 active_prog = ENTITY;
207         }
208         return entity_prog;
209 }
210
211 DirectionalLighting &Viewport::HUDProgram() noexcept {
212         if (active_prog != HUD) {
213                 entity_prog.Activate();
214                 EnableDepthTest();
215                 EnableBackfaceCulling();
216                 entity_prog.SetVP(canv.View(), canv.Projection());
217                 active_prog = HUD;
218         }
219         return entity_prog;
220 }
221
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;
227         }
228         return color_prog;
229 }
230
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;
236         }
237         return color_prog;
238 }
239
240 SkyBoxShader &Viewport::SkyBoxProgram() noexcept {
241         if (active_prog != SKY_BOX) {
242                 sky_prog.Activate();
243                 DisableBlending();
244                 DisableBackfaceCulling();
245                 EqualDepthTest();
246                 active_prog = SKY_BOX;
247         }
248         return sky_prog;
249 }
250
251 BlendedSprite &Viewport::SpriteProgram() noexcept {
252         if (active_prog != SPRITE) {
253                 sprite_prog.Activate();
254                 EnableAlphaBlending();
255                 active_prog = SPRITE;
256         }
257         return sprite_prog;
258 }
259
260
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());
266         sky_prog.Activate();
267         SkyBoxProgram().SetView(cam.View());
268 }
269
270 }