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