]> git.localhorst.tv Git - blank.git/blob - src/model/model.cpp
allow hsl color shifts for blocks and entities
[blank.git] / src / model / model.cpp
1 #include "BlockModel.hpp"
2 #include "EntityModel.hpp"
3 #include "OutlineModel.hpp"
4 #include "SkyBoxModel.hpp"
5 #include "SpriteModel.hpp"
6
7 #include "shapes.hpp"
8
9 #include <algorithm>
10 #include <iostream>
11
12
13 namespace blank {
14
15 void EntityModel::Update(const Buffer &buf) noexcept {
16 #ifndef NDEBUG
17         if (buf.tex_coords.size() < buf.vertices.size()) {
18                 std::cerr << "EntityModel: not enough tex coords!" << std::endl;
19         }
20         if (buf.hsl_mods.size() < buf.vertices.size()) {
21                 std::cerr << "BlockModel: not enough HSL modifiers!" << std::endl;
22         }
23         if (buf.rgb_mods.size() < buf.vertices.size()) {
24                 std::cerr << "BlockModel: not enough RGB modifiers!" << std::endl;
25         }
26         if (buf.normals.size() < buf.vertices.size()) {
27                 std::cerr << "EntityModel: not enough normals!" << std::endl;
28         }
29 #endif
30
31         vao.Bind();
32         vao.PushAttribute(ATTRIB_VERTEX, buf.vertices);
33         vao.PushAttribute(ATTRIB_TEXCOORD, buf.tex_coords);
34         vao.PushAttribute(ATTRIB_HSL, buf.hsl_mods);
35         vao.PushAttribute(ATTRIB_RGB, buf.rgb_mods);
36         vao.PushAttribute(ATTRIB_NORMAL, buf.normals);
37         vao.PushIndices(ATTRIB_INDEX, buf.indices);
38 }
39
40
41 void EntityModel::Draw() const noexcept {
42         vao.DrawTriangleElements();
43 }
44
45
46 void BlockModel::Update(const Buffer &buf) noexcept {
47 #ifndef NDEBUG
48         if (buf.tex_coords.size() < buf.vertices.size()) {
49                 std::cerr << "BlockModel: not enough tex coords!" << std::endl;
50         }
51         if (buf.hsl_mods.size() < buf.vertices.size()) {
52                 std::cerr << "BlockModel: not enough HSL modifiers!" << std::endl;
53         }
54         if (buf.rgb_mods.size() < buf.vertices.size()) {
55                 std::cerr << "BlockModel: not enough RGB modifiers!" << std::endl;
56         }
57         if (buf.lights.size() < buf.vertices.size()) {
58                 std::cerr << "BlockModel: not enough lights!" << std::endl;
59         }
60 #endif
61
62         vao.Bind();
63         vao.PushAttribute(ATTRIB_VERTEX, buf.vertices);
64         vao.PushAttribute(ATTRIB_TEXCOORD, buf.tex_coords);
65         vao.PushAttribute(ATTRIB_HSL, buf.hsl_mods);
66         vao.PushAttribute(ATTRIB_RGB, buf.rgb_mods);
67         vao.PushAttribute(ATTRIB_LIGHT, buf.lights);
68         vao.PushIndices(ATTRIB_INDEX, buf.indices);
69 }
70
71
72 void BlockModel::Draw() const noexcept {
73         vao.DrawTriangleElements();
74 }
75
76
77 void OutlineModel::Update(const Buffer &buf) noexcept {
78 #ifndef NDEBUG
79         if (buf.colors.size() < buf.vertices.size()) {
80                 std::cerr << "OutlineModel: not enough colors!" << std::endl;
81         }
82 #endif
83
84         vao.Bind();
85         vao.PushAttribute(ATTRIB_VERTEX, buf.vertices);
86         vao.PushAttribute(ATTRIB_COLOR, buf.colors);
87         vao.PushIndices(ATTRIB_INDEX, buf.indices);
88 }
89
90
91 void OutlineModel::Draw() noexcept {
92         glEnable(GL_LINE_SMOOTH);
93         glLineWidth(2.0f);
94         vao.DrawLineElements();
95 }
96
97
98 void SkyBoxModel::LoadUnitBox() {
99         Buffer buffer;
100         CuboidShape shape({{ -1, -1, -1 }, { 1, 1, 1 }});
101         shape.Vertices(buffer);
102         Update(buffer);
103 }
104
105 void SkyBoxModel::Update(const Buffer &buf) noexcept {
106         vao.Bind();
107         vao.PushAttribute(ATTRIB_VERTEX, buf.vertices);
108         vao.PushIndices(ATTRIB_INDEX, buf.indices);
109 }
110
111 void SkyBoxModel::Draw() const noexcept {
112         vao.DrawTriangleElements();
113 }
114
115
116 void SpriteModel::Buffer::LoadRect(
117         float w, float h,
118         const glm::vec2 &pivot,
119         const glm::vec2 &tex_begin,
120         const glm::vec2 &tex_end
121 ) {
122         Clear();
123         Reserve(4, 6);
124
125         vertices.emplace_back( -pivot.x,  -pivot.y, 0.0f);
126         vertices.emplace_back(w-pivot.x,  -pivot.y, 0.0f);
127         vertices.emplace_back( -pivot.x, h-pivot.y, 0.0f);
128         vertices.emplace_back(w-pivot.x, h-pivot.y, 0.0f);
129
130         coords.emplace_back(tex_begin.x, tex_begin.y);
131         coords.emplace_back(tex_end.x,   tex_begin.y);
132         coords.emplace_back(tex_begin.x, tex_end.y);
133         coords.emplace_back(tex_end.x,   tex_end.y);
134
135         indices.assign({ 0, 2, 1, 1, 2, 3 });
136 }
137
138
139 void SpriteModel::Update(const Buffer &buf) noexcept {
140 #ifndef NDEBUG
141         if (buf.coords.size() < buf.vertices.size()) {
142                 std::cerr << "SpriteModel: not enough coords!" << std::endl;
143         }
144 #endif
145
146         vao.Bind();
147         vao.PushAttribute(ATTRIB_VERTEX, buf.vertices);
148         vao.PushAttribute(ATTRIB_TEXCOORD, buf.coords);
149         vao.PushIndices(ATTRIB_INDEX, buf.indices);
150 }
151
152
153 void SpriteModel::Draw() noexcept {
154         vao.DrawTriangleElements();
155 }
156
157 }