]> git.localhorst.tv Git - blank.git/blob - src/hud.cpp
recycle chunks flagged for deletion
[blank.git] / src / hud.cpp
1 #include "hud.hpp"
2
3 #include "init.hpp"
4 #include "shape.hpp"
5
6 #include <glm/gtc/matrix_transform.hpp>
7
8
9 namespace blank {
10
11 HUD::HUD()
12 : block()
13 , block_transform(1.0f)
14 , block_visible(false)
15 , crosshair()
16 , crosshair_transform(1.0f)
17 , near(100.0f)
18 , far(-100.0f)
19 , projection(glm::ortho(0.0f, 1.0f, 1.0f, 0.0f, near, far))
20 , view(glm::translate(glm::mat4(1.0f), glm::vec3(-0.5f, -0.5f, 0))) {
21         block_transform = glm::translate(block_transform, glm::vec3(50.0f, 50.0f, 0.0f));
22         block_transform = glm::scale(block_transform, glm::vec3(50.0f));
23         block_transform = glm::rotate(block_transform, 3.5f, glm::vec3(1.0f, 0.0f, 0.0f));
24         block_transform = glm::rotate(block_transform, 0.85f, glm::vec3(0.0f, 1.0f, 0.0f));
25
26         crosshair.vertices = std::vector<glm::vec3>({
27                 { -10.0f,   0.0f, 0.0f }, { 10.0f,  0.0f, 0.0f },
28                 {   0.0f, -10.0f, 0.0f }, {  0.0f, 10.0f, 0.0f },
29         });
30         crosshair.indices = std::vector<OutlineModel::Index>({
31                 0, 1, 2, 3
32         });
33         crosshair.colors.resize(4, { 10.0f, 10.0f, 10.0f });
34         crosshair.Invalidate();
35 }
36
37
38 void HUD::Viewport(float width, float height) {
39         Viewport(0, 0, width, height);
40 }
41
42 void HUD::Viewport(float x, float y, float width, float height) {
43         projection = glm::ortho(x, width, height, y, near, far);
44         crosshair_transform = glm::translate(glm::mat4(1.0f), glm::vec3(width * 0.5f, height * 0.5f, 0.0f));
45 }
46
47
48 void HUD::Display(const BlockType &type) {
49         block.Clear();
50         type.FillModel(block);
51         block_visible = type.visible;
52 }
53
54
55 void HUD::Render(DirectionalLighting &program) {
56         if (block_visible) {
57                 program.SetLightDirection({ 1.0f, 3.0f, 5.0f });
58                 GLContext::ClearDepthBuffer();
59                 program.SetMVP(block_transform, view, projection);
60                 block.Draw();
61                 program.SetM(crosshair_transform);
62                 crosshair.Draw();
63         }
64 }
65
66 }