Some rendering clean-up.
This commit is contained in:
+42
-80
@@ -4,7 +4,6 @@
|
|||||||
#include "glm/vec3.hpp"
|
#include "glm/vec3.hpp"
|
||||||
#include "glm/vec4.hpp"
|
#include "glm/vec4.hpp"
|
||||||
#include "glm/gtc/type_ptr.hpp"
|
#include "glm/gtc/type_ptr.hpp"
|
||||||
//#include <GL/glext.h>
|
|
||||||
|
|
||||||
static const char *vs_code = R"(
|
static const char *vs_code = R"(
|
||||||
#version 330
|
#version 330
|
||||||
@@ -54,6 +53,27 @@ static const char *ps_code = R"(
|
|||||||
}
|
}
|
||||||
)";
|
)";
|
||||||
|
|
||||||
|
static void add_vertex(std::vector<float>& buffer_data, glm::vec3 v, glm::vec4 col) {
|
||||||
|
buffer_data.push_back(v.x);
|
||||||
|
buffer_data.push_back(v.y);
|
||||||
|
buffer_data.push_back(v.z);
|
||||||
|
|
||||||
|
buffer_data.push_back(col.r);
|
||||||
|
buffer_data.push_back(col.g);
|
||||||
|
buffer_data.push_back(col.b);
|
||||||
|
buffer_data.push_back(col.a);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void add_line(std::vector<float>& buffer_data, glm::vec3 from, glm::vec4 from_col, glm::vec3 to, glm::vec4 to_col) {
|
||||||
|
add_vertex(buffer_data, from, from_col);
|
||||||
|
add_vertex(buffer_data, to, to_col);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void add_line(std::vector<float>& buffer_data, glm::vec3 from, glm::vec3 to, glm::vec4 col) {
|
||||||
|
add_vertex(buffer_data, from, col);
|
||||||
|
add_vertex(buffer_data, to, col);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void grbl::program_renderer::render(glm::mat4 mvp, glm::vec2 viewport_size) {
|
void grbl::program_renderer::render(glm::mat4 mvp, glm::vec2 viewport_size) {
|
||||||
// draw model
|
// draw model
|
||||||
@@ -74,19 +94,16 @@ void grbl::program_renderer::render(glm::mat4 mvp, glm::vec2 viewport_size) {
|
|||||||
|
|
||||||
// draw bounding box
|
// draw bounding box
|
||||||
auto bbox_size = max_pos - min_pos;
|
auto bbox_size = max_pos - min_pos;
|
||||||
// need to add proper translation
|
|
||||||
|
|
||||||
auto bbox_translation = glm::translate(glm::mat4(1.0f), {min_pos.x, min_pos.y, min_pos.z});
|
auto bbox_translation = glm::translate(glm::mat4(1.0f), min_pos);
|
||||||
auto bbox_xform = glm::scale(glm::mat4(1.0f), {bbox_size.x, bbox_size.y, bbox_size.z});
|
auto bbox_scale = glm::scale(glm::mat4(1.0f), bbox_size);
|
||||||
auto bbox_view = mvp * bbox_translation * bbox_xform;
|
auto bbox_view = mvp * bbox_translation * bbox_scale;
|
||||||
shader->set_mat4(glm::value_ptr(bbox_view), "mvp");
|
shader->set_mat4(glm::value_ptr(bbox_view), "mvp");
|
||||||
|
|
||||||
glBindVertexArray(extents_vao_id);
|
glBindVertexArray(extents_vao_id);
|
||||||
glDrawArrays(GL_LINES, 0, extents_vertices_count);
|
glDrawArrays(GL_LINES, 0, extents_vertices_count);
|
||||||
|
|
||||||
shader->unbind();
|
shader->unbind();
|
||||||
|
|
||||||
// draw bit location
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void grbl::program_renderer::update(const grbl::program& pgm, const grbl::machine& cnc) {
|
void grbl::program_renderer::update(const grbl::program& pgm, const grbl::machine& cnc) {
|
||||||
@@ -113,80 +130,45 @@ void grbl::program_renderer::update(const grbl::program& pgm, const grbl::machin
|
|||||||
spindle_pos = glm::vec3(work_pos[0], work_pos[1], work_pos[2]);
|
spindle_pos = glm::vec3(work_pos[0], work_pos[1], work_pos[2]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void grbl::program_renderer::initialize_spindle_buffers() {
|
void grbl::program_renderer::initialize_spindle_buffers() {
|
||||||
glGenBuffers(1, &spindle_vbo_id);
|
glGenBuffers(1, &spindle_vbo_id);
|
||||||
glGenVertexArrays(1, &spindle_vao_id);
|
glGenVertexArrays(1, &spindle_vao_id);
|
||||||
|
|
||||||
// vertex format: x, y, z, r, g, b, a
|
// vertex format: x, y, z, r, g, b, a
|
||||||
// stride: 28 bytes
|
// stride: 28 bytes
|
||||||
const GLsizei sizeOfVertexInBytes = 28;
|
const GLsizei size_of_vertex_in_bytes = 28;
|
||||||
|
|
||||||
glBindVertexArray(spindle_vao_id);
|
glBindVertexArray(spindle_vao_id);
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, spindle_vbo_id);
|
glBindBuffer(GL_ARRAY_BUFFER, spindle_vbo_id);
|
||||||
|
|
||||||
glEnableVertexAttribArray(0); // vertices on stream 0
|
glEnableVertexAttribArray(0); // vertices on stream 0
|
||||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeOfVertexInBytes, (void *) 0);
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, size_of_vertex_in_bytes, (void *) nullptr);
|
||||||
|
|
||||||
glEnableVertexAttribArray(1); // vertex colors on stream 1
|
glEnableVertexAttribArray(1); // vertex colors on stream 1
|
||||||
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeOfVertexInBytes, (void *) (sizeof(float) * 3));
|
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, size_of_vertex_in_bytes, (void *) (sizeof(float) * 3));
|
||||||
|
|
||||||
// unbind vao
|
// unbind vao
|
||||||
glBindVertexArray(0);
|
glBindVertexArray(0);
|
||||||
|
|
||||||
// construct spindle model
|
// construct spindle model
|
||||||
glm::vec4 col(1, 1, 0, 1); // yellow
|
glm::vec4 col(1, 1, 0, 1);
|
||||||
|
|
||||||
std::vector<float> buffer_data;
|
std::vector<float> buffer_data;
|
||||||
|
|
||||||
const float spindle_length = 3.5;
|
const float spindle_length = 3.5;
|
||||||
const size_t cone_granularity = 20;
|
const size_t cone_granularity = 20;
|
||||||
for (int i = 0; i < cone_granularity; i++) {
|
for (int i = 0; i < cone_granularity; i++) {
|
||||||
float x = sinf((i / (float) cone_granularity) * 2.0f * M_PI);
|
float x = sinf((i / (float) cone_granularity) * 2.0f * glm::pi<float>());
|
||||||
float y = cosf((i / (float) cone_granularity) * 2.0f * M_PI);
|
float y = cosf((i / (float) cone_granularity) * 2.0f * glm::pi<float>());
|
||||||
|
|
||||||
// TODO: refactor this to use add_line()
|
add_line(buffer_data, {0, 0, 0}, {x, y, spindle_length}, col);
|
||||||
// from
|
|
||||||
buffer_data.push_back(0);
|
|
||||||
buffer_data.push_back(0);
|
|
||||||
buffer_data.push_back(0);
|
|
||||||
|
|
||||||
buffer_data.push_back(col.r);
|
|
||||||
buffer_data.push_back(col.g);
|
|
||||||
buffer_data.push_back(col.b);
|
|
||||||
buffer_data.push_back(col.a);
|
|
||||||
|
|
||||||
// to
|
|
||||||
buffer_data.push_back(x);
|
|
||||||
buffer_data.push_back(y);
|
|
||||||
buffer_data.push_back(spindle_length);
|
|
||||||
|
|
||||||
buffer_data.push_back(col.r);
|
|
||||||
buffer_data.push_back(col.g);
|
|
||||||
buffer_data.push_back(col.b);
|
|
||||||
buffer_data.push_back(col.a);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, spindle_vbo_id);
|
glBindBuffer(GL_ARRAY_BUFFER, spindle_vbo_id);
|
||||||
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * buffer_data.size(), buffer_data.data(), GL_STATIC_DRAW);
|
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * buffer_data.size(), buffer_data.data(), GL_STATIC_DRAW);
|
||||||
|
|
||||||
spindle_vertices_count = buffer_data.size() * sizeof(float) / sizeOfVertexInBytes;
|
spindle_vertices_count = buffer_data.size() * sizeof(float) / size_of_vertex_in_bytes;
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void add_vertex(std::vector<float>& buffer_data, glm::vec3 v, glm::vec4 col) {
|
|
||||||
buffer_data.push_back(v.x);
|
|
||||||
buffer_data.push_back(v.y);
|
|
||||||
buffer_data.push_back(v.z);
|
|
||||||
|
|
||||||
buffer_data.push_back(col.r);
|
|
||||||
buffer_data.push_back(col.g);
|
|
||||||
buffer_data.push_back(col.b);
|
|
||||||
buffer_data.push_back(col.a);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void add_line(std::vector<float>& buffer_data, glm::vec3 from, glm::vec3 to, glm::vec4 col) {
|
|
||||||
add_vertex(buffer_data, from, col);
|
|
||||||
add_vertex(buffer_data, to, col);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void grbl::program_renderer::initialize_extents_buffers() {
|
void grbl::program_renderer::initialize_extents_buffers() {
|
||||||
@@ -195,7 +177,7 @@ void grbl::program_renderer::initialize_extents_buffers() {
|
|||||||
|
|
||||||
// vertex format: x, y, z, r, g, b, a
|
// vertex format: x, y, z, r, g, b, a
|
||||||
// stride: 28 bytes
|
// stride: 28 bytes
|
||||||
const GLsizei sizeOfVertexInBytes = 28;
|
const GLsizei size_of_vertex_in_bytes = 28;
|
||||||
|
|
||||||
// we're going to draw a simple box for the extents
|
// we're going to draw a simple box for the extents
|
||||||
// box is made up of 8 vertices and 12 lines
|
// box is made up of 8 vertices and 12 lines
|
||||||
@@ -204,10 +186,10 @@ void grbl::program_renderer::initialize_extents_buffers() {
|
|||||||
glBindBuffer(GL_ARRAY_BUFFER, extents_vbo_id);
|
glBindBuffer(GL_ARRAY_BUFFER, extents_vbo_id);
|
||||||
|
|
||||||
glEnableVertexAttribArray(0); // vertices on stream 0
|
glEnableVertexAttribArray(0); // vertices on stream 0
|
||||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeOfVertexInBytes, (void *) 0);
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, size_of_vertex_in_bytes, (void *) 0);
|
||||||
|
|
||||||
glEnableVertexAttribArray(1); // vertex colors on stream 1
|
glEnableVertexAttribArray(1); // vertex colors on stream 1
|
||||||
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeOfVertexInBytes, (void *) (sizeof(float) * 3));
|
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, size_of_vertex_in_bytes, (void *) (sizeof(float) * 3));
|
||||||
|
|
||||||
// unbind vao
|
// unbind vao
|
||||||
glBindVertexArray(0);
|
glBindVertexArray(0);
|
||||||
@@ -245,7 +227,7 @@ void grbl::program_renderer::initialize_extents_buffers() {
|
|||||||
glBindBuffer(GL_ARRAY_BUFFER, extents_vbo_id);
|
glBindBuffer(GL_ARRAY_BUFFER, extents_vbo_id);
|
||||||
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * buffer_data.size(), buffer_data.data(), GL_STATIC_DRAW);
|
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * buffer_data.size(), buffer_data.data(), GL_STATIC_DRAW);
|
||||||
|
|
||||||
extents_vertices_count = buffer_data.size() * sizeof(float) / sizeOfVertexInBytes;
|
extents_vertices_count = buffer_data.size() * sizeof(float) / size_of_vertex_in_bytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
void grbl::program_renderer::initialize_program_buffers() {
|
void grbl::program_renderer::initialize_program_buffers() {
|
||||||
@@ -273,12 +255,10 @@ GLsizei grbl::program_renderer::build_vbo(const grbl::program& pgm) {
|
|||||||
static auto movement_re = std::regex(R"(([gG]0*1?\s+|[xXyYzZ]\s*[0-9\.\-]+))");
|
static auto movement_re = std::regex(R"(([gG]0*1?\s+|[xXyYzZ]\s*[0-9\.\-]+))");
|
||||||
bool is_tool_on = false;
|
bool is_tool_on = false;
|
||||||
|
|
||||||
|
|
||||||
buffer_data.clear();
|
|
||||||
|
|
||||||
glm::vec3 tool_pos;
|
glm::vec3 tool_pos;
|
||||||
min_pos = max_pos = tool_pos = glm::vec3(0);
|
min_pos = max_pos = tool_pos = glm::vec3(0);
|
||||||
|
|
||||||
|
std::vector<float> buffer_data;
|
||||||
for (auto& i: pgm.instructions) {
|
for (auto& i: pgm.instructions) {
|
||||||
if (i.type == grbl::instruction_type::gcode) {
|
if (i.type == grbl::instruction_type::gcode) {
|
||||||
|
|
||||||
@@ -331,25 +311,7 @@ GLsizei grbl::program_renderer::build_vbo(const grbl::program& pgm) {
|
|||||||
to_color = glm::vec4(0, 1, 0, 1);
|
to_color = glm::vec4(0, 1, 0, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// from
|
add_line(buffer_data, tool_pos, from_color, new_pos, to_color);
|
||||||
buffer_data.push_back(tool_pos.x);
|
|
||||||
buffer_data.push_back(tool_pos.y);
|
|
||||||
buffer_data.push_back(tool_pos.z);
|
|
||||||
|
|
||||||
buffer_data.push_back(from_color.r);
|
|
||||||
buffer_data.push_back(from_color.g);
|
|
||||||
buffer_data.push_back(from_color.b);
|
|
||||||
buffer_data.push_back(from_color.a);
|
|
||||||
|
|
||||||
// to
|
|
||||||
buffer_data.push_back(new_pos.x);
|
|
||||||
buffer_data.push_back(new_pos.y);
|
|
||||||
buffer_data.push_back(new_pos.z);
|
|
||||||
|
|
||||||
buffer_data.push_back(to_color.r);
|
|
||||||
buffer_data.push_back(to_color.g);
|
|
||||||
buffer_data.push_back(to_color.b);
|
|
||||||
buffer_data.push_back(to_color.a);
|
|
||||||
|
|
||||||
// calculate extents
|
// calculate extents
|
||||||
min_pos.x = std::min(min_pos.x, new_pos.x);
|
min_pos.x = std::min(min_pos.x, new_pos.x);
|
||||||
@@ -365,8 +327,8 @@ GLsizei grbl::program_renderer::build_vbo(const grbl::program& pgm) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const GLsizei sizeOfVertexInBytes = 28;
|
const GLsizei size_of_vertex_in_bytes = 28;
|
||||||
auto number_of_vertices = buffer_data.size() * sizeof(float) / sizeOfVertexInBytes;
|
auto number_of_vertices = buffer_data.size() * sizeof(float) / size_of_vertex_in_bytes;
|
||||||
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, vbo_id);
|
glBindBuffer(GL_ARRAY_BUFFER, vbo_id);
|
||||||
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * buffer_data.size(), buffer_data.data(), GL_STATIC_DRAW);
|
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * buffer_data.size(), buffer_data.data(), GL_STATIC_DRAW);
|
||||||
@@ -391,7 +353,7 @@ std::string get_program_info_log(GLuint id) {
|
|||||||
char error_log[log_length]; // length includes the NULL character
|
char error_log[log_length]; // length includes the NULL character
|
||||||
glGetProgramInfoLog(id, log_length, &log_length, &error_log[0]);
|
glGetProgramInfoLog(id, log_length, &log_length, &error_log[0]);
|
||||||
|
|
||||||
return std::string(error_log, log_length);
|
return {error_log, (size_t) log_length};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user