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