]> git.localhorst.tv Git - blank.git/blob - src/graphics/viewport.cpp
chat state
[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 glm::vec2 Viewport::GetPosition(const glm::vec2 &off, Gravity grav) const noexcept {
169         return align(grav, canv.Size(), off + canv.Offset());
170 }
171
172 void Viewport::SetCursor(const glm::vec3 &pos) noexcept {
173         cursor[3].x = pos.x;
174         cursor[3].y = pos.y;
175         cursor[3].z = pos.z;
176 }
177
178 void Viewport::SetCursor(const glm::vec3 &pos, Gravity grav) noexcept {
179         glm::vec2 p(GetPosition(glm::vec2(pos), grav));
180         cursor[3].x = p.x;
181         cursor[3].y = p.y;
182         cursor[3].z = pos.z;
183 }
184
185 void Viewport::MoveCursor(const glm::vec3 &d) noexcept {
186         cursor[3].x += d.x;
187         cursor[3].y += d.y;
188         cursor[3].z += d.z;
189 }
190
191
192 BlockLighting &Viewport::ChunkProgram() noexcept {
193         if (active_prog != CHUNK) {
194                 chunk_prog.Activate();
195                 EnableDepthTest();
196                 EnableBackfaceCulling();
197                 DisableBlending();
198                 active_prog = CHUNK;
199         }
200         return chunk_prog;
201 }
202
203 DirectionalLighting &Viewport::EntityProgram() noexcept {
204         if (active_prog != ENTITY) {
205                 entity_prog.Activate();
206                 EnableDepthTest();
207                 EnableBackfaceCulling();
208                 DisableBlending();
209                 entity_prog.SetVP(cam.View(), cam.Projection());
210                 active_prog = ENTITY;
211         }
212         return entity_prog;
213 }
214
215 DirectionalLighting &Viewport::HUDProgram() noexcept {
216         if (active_prog != HUD) {
217                 entity_prog.Activate();
218                 EnableDepthTest();
219                 EnableBackfaceCulling();
220                 entity_prog.SetVP(canv.View(), canv.Projection());
221                 active_prog = HUD;
222         }
223         return entity_prog;
224 }
225
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;
231         }
232         return color_prog;
233 }
234
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;
240         }
241         return color_prog;
242 }
243
244 SkyBoxShader &Viewport::SkyBoxProgram() noexcept {
245         if (active_prog != SKY_BOX) {
246                 sky_prog.Activate();
247                 DisableBlending();
248                 DisableBackfaceCulling();
249                 EqualDepthTest();
250                 active_prog = SKY_BOX;
251         }
252         return sky_prog;
253 }
254
255 BlendedSprite &Viewport::SpriteProgram() noexcept {
256         if (active_prog != SPRITE) {
257                 sprite_prog.Activate();
258                 EnableAlphaBlending();
259                 active_prog = SPRITE;
260         }
261         return sprite_prog;
262 }
263
264
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());
270         sky_prog.Activate();
271         SkyBoxProgram().SetView(cam.View());
272 }
273
274 }