, blockType()
, cam()
, chunk()
+, outline()
+, outline_visible(false)
+, outline_transform(1.0f)
, light_position(17.0f, 17.0f, 17.0f)
, light_color(1.0f, 1.0f, 1.0f)
, light_power(250.0f)
chunk.BlockAt(glm::vec3(2, 2, 1)) = Block(blockType[2]);
chunk.Invalidate();
+ outline.vertices = std::vector<glm::vec3>({
+ { 0.0f, 0.0f, 0.0f }, { 1.0f, 0.0f, 0.0f },
+ { 1.0f, 0.0f, 0.0f }, { 1.0f, 1.0f, 0.0f },
+ { 1.0f, 1.0f, 0.0f }, { 0.0f, 1.0f, 0.0f },
+ { 0.0f, 1.0f, 0.0f }, { 0.0f, 0.0f, 0.0f },
+ { 0.0f, 0.0f, 0.0f }, { 0.0f, 0.0f, 1.0f },
+ { 1.0f, 0.0f, 0.0f }, { 1.0f, 0.0f, 1.0f },
+ { 1.0f, 1.0f, 0.0f }, { 1.0f, 1.0f, 1.0f },
+ { 0.0f, 1.0f, 0.0f }, { 0.0f, 1.0f, 1.0f },
+ { 0.0f, 0.0f, 1.0f }, { 1.0f, 0.0f, 1.0f },
+ { 1.0f, 0.0f, 1.0f }, { 1.0f, 1.0f, 1.0f },
+ { 1.0f, 1.0f, 1.0f }, { 0.0f, 1.0f, 1.0f },
+ { 0.0f, 1.0f, 1.0f }, { 0.0f, 0.0f, 1.0f },
+ });
+ outline.colors.resize(24, { -1, -1, -1 });
+ outline.Invalidate();
+
m_handle = program.UniformLocation("M");
v_handle = program.UniformLocation("V");
mv_handle = program.UniformLocation("MV");
float dist;
if (chunk.Intersection(aim, glm::mat4(1.0f), &blkid, &dist)) {
glm::vec3 pos = Chunk::ToCoords(blkid);
- std::cout << "pointing at: <" << pos.x << ", " << pos.y << ", " << pos.z << ">, "
- "distance: " << dist << std::endl;
+ outline_visible = true;
+ outline_transform = glm::translate(glm::mat4(1.0f), pos);
} else {
- std::cout << "pointing at: nothing" << std::endl;
+ outline_visible = false;
}
}
glUniform1f(light_power_handle, light_power);
chunk.Draw();
+ if (outline_visible) {
+ mv = cam.View() * outline_transform;
+ mvp = cam.MakeMVP(outline_transform);
+ glUniformMatrix4fv(m_handle, 1, GL_FALSE, &m[0][0]);
+ glUniformMatrix4fv(mv_handle, 1, GL_FALSE, &mv[0][0]);
+ glUniformMatrix4fv(mvp_handle, 1, GL_FALSE, &mvp[0][0]);
+ outline.Draw();
+ }
window.Flip();
}
#include "model.hpp"
+#include <iostream>
+
namespace blank {
glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_VERTEX]);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), vertices.data(), GL_STATIC_DRAW);
+#ifndef NDEBUG
+ if (colors.size() < vertices.size()) {
+ std::cerr << "Model: not enough colors!" << std::endl;
+ colors.resize(vertices.size(), { 1, 0, 1 });
+ }
+#endif
glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_COLOR]);
glBufferData(GL_ARRAY_BUFFER, colors.size() * sizeof(glm::vec3), colors.data(), GL_STATIC_DRAW);
+#ifndef NDEBUG
+ if (normals.size() < vertices.size()) {
+ std::cerr << "Model: not enough normals!" << std::endl;
+ normals.resize(vertices.size(), { 0, 1, 0 });
+ }
+#endif
glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_NORMAL]);
glBufferData(GL_ARRAY_BUFFER, normals.size() * sizeof(glm::vec3), normals.data(), GL_STATIC_DRAW);
glDisableVertexAttribArray(ATTRIB_VERTEX);
}
+
+OutlineModel::OutlineModel()
+: vertices()
+, colors()
+, handle{}
+, dirty(false) {
+ glGenBuffers(ATTRIB_COUNT, handle);
+}
+
+OutlineModel::~OutlineModel() {
+ glDeleteBuffers(ATTRIB_COUNT, handle);
+}
+
+
+void OutlineModel::Clear() {
+ vertices.clear();
+ colors.clear();
+ Invalidate();
+}
+
+void OutlineModel::Reserve(int s) {
+ vertices.reserve(s);
+ colors.reserve(s);
+}
+
+
+void OutlineModel::Update() {
+ glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_VERTEX]);
+ glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), vertices.data(), GL_STATIC_DRAW);
+
+#ifndef NDEBUG
+ if (colors.size() < vertices.size()) {
+ std::cerr << "OutlineModel: not enough colors!" << std::endl;
+ colors.resize(vertices.size(), { 1, 0, 1 });
+ }
+#endif
+ glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_COLOR]);
+ glBufferData(GL_ARRAY_BUFFER, colors.size() * sizeof(glm::vec3), colors.data(), GL_STATIC_DRAW);
+
+ dirty = false;
+}
+
+
+void OutlineModel::Draw() {
+ if (dirty) {
+ Update();
+ }
+
+ glEnableVertexAttribArray(ATTRIB_VERTEX);
+ glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_VERTEX]);
+ glVertexAttribPointer(
+ ATTRIB_VERTEX, // location (for shader)
+ 3, // size
+ GL_FLOAT, // type
+ GL_FALSE, // normalized
+ 0, // stride
+ nullptr // offset
+ );
+
+ glEnableVertexAttribArray(ATTRIB_COLOR);
+ glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_COLOR]);
+ glVertexAttribPointer(
+ ATTRIB_COLOR, // location (for shader)
+ 3, // size
+ GL_FLOAT, // type
+ GL_FALSE, // normalized
+ 0, // stride
+ nullptr // offset
+ );
+
+ glLineWidth(2.0f);
+
+ glDrawArrays(
+ GL_LINES, // how
+ 0, // start
+ vertices.size() // len
+ );
+
+ glDisableVertexAttribArray(ATTRIB_COLOR);
+ glDisableVertexAttribArray(ATTRIB_VERTEX);
+}
+
}