1 #include "BlockModel.hpp"
3 #include "OutlineModel.hpp"
10 Model::Model() noexcept
14 glGenVertexArrays(1, &va);
15 glGenBuffers(ATTRIB_COUNT, handle);
18 Model::~Model() noexcept {
19 glDeleteBuffers(ATTRIB_COUNT, handle);
20 glDeleteVertexArrays(1, &va);
23 Model::Model(Model &&other) noexcept
25 , count(other.count) {
27 for (int i = 0; i < ATTRIB_COUNT; ++i) {
28 handle[i] = other.handle[i];
33 Model &Model::operator =(Model &&other) noexcept {
34 std::swap(va, other.va);
35 for (int i = 0; i < ATTRIB_COUNT; ++i) {
36 std::swap(handle[i], other.handle[i]);
42 void Model::Update(const Buffer &buf) noexcept {
43 glBindVertexArray(va);
44 glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_VERTEX]);
45 glBufferData(GL_ARRAY_BUFFER, buf.vertices.size() * sizeof(glm::vec3), buf.vertices.data(), GL_STATIC_DRAW);
46 glEnableVertexAttribArray(ATTRIB_VERTEX);
47 glVertexAttribPointer(
48 ATTRIB_VERTEX, // location (for shader)
51 GL_FALSE, // normalized
57 if (buf.colors.size() < buf.vertices.size()) {
58 std::cerr << "Model: not enough colors!" << std::endl;
61 glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_COLOR]);
62 glBufferData(GL_ARRAY_BUFFER, buf.colors.size() * sizeof(glm::vec3), buf.colors.data(), GL_STATIC_DRAW);
63 glEnableVertexAttribArray(ATTRIB_COLOR);
64 glVertexAttribPointer(
65 ATTRIB_COLOR, // location (for shader)
68 GL_FALSE, // normalized
74 if (buf.normals.size() < buf.vertices.size()) {
75 std::cerr << "Model: not enough normals!" << std::endl;
78 glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_NORMAL]);
79 glBufferData(GL_ARRAY_BUFFER, buf.normals.size() * sizeof(glm::vec3), buf.normals.data(), GL_STATIC_DRAW);
80 glEnableVertexAttribArray(ATTRIB_NORMAL);
81 glVertexAttribPointer(
82 ATTRIB_NORMAL, // location (for shader)
85 GL_FALSE, // normalized
90 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, handle[ATTRIB_INDEX]);
91 glBufferData(GL_ELEMENT_ARRAY_BUFFER, buf.indices.size() * sizeof(Index), buf.indices.data(), GL_STATIC_DRAW);
92 count = buf.indices.size();
96 void Model::Draw() const noexcept {
97 glBindVertexArray(va);
101 GL_UNSIGNED_INT, // type
107 BlockModel::BlockModel() noexcept
111 glGenVertexArrays(1, &va);
112 glGenBuffers(ATTRIB_COUNT, handle);
115 BlockModel::~BlockModel() noexcept {
116 glDeleteBuffers(ATTRIB_COUNT, handle);
117 glDeleteVertexArrays(1, &va);
120 BlockModel::BlockModel(BlockModel &&other) noexcept
122 , count(other.count) {
124 for (int i = 0; i < ATTRIB_COUNT; ++i) {
125 handle[i] = other.handle[i];
130 BlockModel &BlockModel::operator =(BlockModel &&other) noexcept {
131 std::swap(va, other.va);
132 for (int i = 0; i < ATTRIB_COUNT; ++i) {
133 std::swap(handle[i], other.handle[i]);
139 void BlockModel::Update(const Buffer &buf) noexcept {
140 glBindVertexArray(va);
141 glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_VERTEX]);
142 glBufferData(GL_ARRAY_BUFFER, buf.vertices.size() * sizeof(glm::vec3), buf.vertices.data(), GL_STATIC_DRAW);
143 glEnableVertexAttribArray(ATTRIB_VERTEX);
144 glVertexAttribPointer(
145 ATTRIB_VERTEX, // location (for shader)
148 GL_FALSE, // normalized
154 if (buf.colors.size() < buf.vertices.size()) {
155 std::cerr << "BlockModel: not enough colors!" << std::endl;
158 glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_COLOR]);
159 glBufferData(GL_ARRAY_BUFFER, buf.colors.size() * sizeof(glm::vec3), buf.colors.data(), GL_STATIC_DRAW);
160 glEnableVertexAttribArray(ATTRIB_COLOR);
161 glVertexAttribPointer(
162 ATTRIB_COLOR, // location (for shader)
165 GL_FALSE, // normalized
171 if (buf.lights.size() < buf.vertices.size()) {
172 std::cerr << "BlockModel: not enough lights!" << std::endl;
175 glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_LIGHT]);
176 glBufferData(GL_ARRAY_BUFFER, buf.lights.size() * sizeof(float), buf.lights.data(), GL_STATIC_DRAW);
177 glEnableVertexAttribArray(ATTRIB_LIGHT);
178 glVertexAttribPointer(
179 ATTRIB_LIGHT, // location (for shader)
182 GL_FALSE, // normalized
187 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, handle[ATTRIB_INDEX]);
188 glBufferData(GL_ELEMENT_ARRAY_BUFFER, buf.indices.size() * sizeof(Index), buf.indices.data(), GL_STATIC_DRAW);
189 count = buf.indices.size();
193 void BlockModel::Draw() const noexcept {
194 glBindVertexArray(va);
198 GL_UNSIGNED_INT, // type
203 OutlineModel::OutlineModel() noexcept
210 glGenVertexArrays(1, &va);
211 glGenBuffers(ATTRIB_COUNT, handle);
214 OutlineModel::~OutlineModel() noexcept {
215 glDeleteBuffers(ATTRIB_COUNT, handle);
216 glDeleteVertexArrays(1, &va);
220 void OutlineModel::Clear() noexcept {
227 void OutlineModel::Reserve(int v, int i) {
234 void OutlineModel::Update() noexcept {
235 glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_VERTEX]);
236 glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), vertices.data(), GL_STATIC_DRAW);
237 glEnableVertexAttribArray(ATTRIB_VERTEX);
238 glVertexAttribPointer(
239 ATTRIB_VERTEX, // location (for shader)
242 GL_FALSE, // normalized
248 if (colors.size() < vertices.size()) {
249 std::cerr << "OutlineModel: not enough colors!" << std::endl;
250 colors.resize(vertices.size(), { 1, 0, 1 });
253 glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_COLOR]);
254 glBufferData(GL_ARRAY_BUFFER, colors.size() * sizeof(glm::vec3), colors.data(), GL_STATIC_DRAW);
255 glEnableVertexAttribArray(ATTRIB_COLOR);
256 glVertexAttribPointer(
257 ATTRIB_COLOR, // location (for shader)
260 GL_FALSE, // normalized
265 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, handle[ATTRIB_INDEX]);
266 glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(Index), indices.data(), GL_STATIC_DRAW);
272 void OutlineModel::Draw() noexcept {
273 glBindVertexArray(va);
279 glEnable(GL_LINE_SMOOTH);
284 indices.size(), // count
285 GL_UNSIGNED_SHORT, // type