From 616011ac5547730416c282db1a77e2331c1a591a Mon Sep 17 00:00:00 2001 From: Adrian Scripca Date: Sun, 7 May 2023 11:23:58 +0300 Subject: [PATCH] Implemented render of program extents. --- docs/todo.txt | 4 +-- render.cpp | 94 +++++++++++++++++++++++++++++++++++++++++++++++++-- render.h | 6 +++- 3 files changed, 99 insertions(+), 5 deletions(-) diff --git a/docs/todo.txt b/docs/todo.txt index 8c8d4a4..cce4036 100644 --- a/docs/todo.txt +++ b/docs/todo.txt @@ -49,5 +49,5 @@ Corner finding DONE - Show program extents -Render program extents - - show a blue bounding box for the program extents +DONE - Render program extents +DONE - show a bounding box for the program extents diff --git a/render.cpp b/render.cpp index 418ecc7..18cd012 100644 --- a/render.cpp +++ b/render.cpp @@ -56,6 +56,7 @@ static const char *ps_code = R"( void grbl::program_renderer::render(glm::mat4 mvp, glm::vec2 viewport_size) { + // draw model shader->bind(); shader->set_mat4(glm::value_ptr(mvp), "mvp"); shader->set_vec2(glm::value_ptr(viewport_size), "u_resolution"); @@ -63,7 +64,7 @@ void grbl::program_renderer::render(glm::mat4 mvp, glm::vec2 viewport_size) { glBindVertexArray(vao_id); glDrawArrays(GL_LINES, 0, vertices_count); - + // draw spindle auto spindle_xform = glm::translate(glm::mat4(1.0f), spindle_pos); auto spindle_view = mvp * spindle_xform; shader->set_mat4(glm::value_ptr(spindle_view), "mvp"); @@ -71,6 +72,18 @@ void grbl::program_renderer::render(glm::mat4 mvp, glm::vec2 viewport_size) { glBindVertexArray(spindle_vao_id); glDrawArrays(GL_LINES, 0, spindle_vertices_count); + // draw bounding box + 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_xform = glm::scale(glm::mat4(1.0f), {bbox_size.x, bbox_size.y, bbox_size.z}); + auto bbox_view = mvp * bbox_translation * bbox_xform; + shader->set_mat4(glm::value_ptr(bbox_view), "mvp"); + + glBindVertexArray(extents_vao_id); + glDrawArrays(GL_LINES, 0, extents_vertices_count); + shader->unbind(); // draw bit location @@ -78,11 +91,11 @@ void grbl::program_renderer::render(glm::mat4 mvp, glm::vec2 viewport_size) { void grbl::program_renderer::update(const grbl::program& pgm, const grbl::machine& cnc) { if (!initialized) { - shader = new shader_program(vs_code, ps_code); initialize_spindle_buffers(); initialize_program_buffers(); + initialize_extents_buffers(); initialized = true; } @@ -131,6 +144,7 @@ void grbl::program_renderer::initialize_spindle_buffers() { float x = sinf((i / (float) cone_granularity) * 2.0f * M_PI); float y = cosf((i / (float) cone_granularity) * 2.0f * M_PI); + // TODO: refactor this to use add_line() // from buffer_data.push_back(0); buffer_data.push_back(0); @@ -158,6 +172,82 @@ void grbl::program_renderer::initialize_spindle_buffers() { spindle_vertices_count = buffer_data.size() * sizeof(float) / sizeOfVertexInBytes; } + +static void add_vertex(std::vector& 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& 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() { + glGenBuffers(1, &extents_vbo_id); + glGenVertexArrays(1, &extents_vao_id); + + // vertex format: x, y, z, r, g, b, a + // stride: 28 bytes + const GLsizei sizeOfVertexInBytes = 28; + + // we're going to draw a simple box for the extents + // box is made up of 8 vertices and 12 lines + + glBindVertexArray(extents_vao_id); + glBindBuffer(GL_ARRAY_BUFFER, extents_vbo_id); + + glEnableVertexAttribArray(0); // vertices on stream 0 + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeOfVertexInBytes, (void *) 0); + + glEnableVertexAttribArray(1); // vertex colors on stream 1 + glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeOfVertexInBytes, (void *) (sizeof(float) * 3)); + + // unbind vao + glBindVertexArray(0); + + // we're going to make the box as unit box and scale it when rendering + // since we're going to reuse the same box for all loaded programs. + // or at least we'll try to. + + // construct box + glm::vec4 col(1, 0, 1, 1); + + + // box will range from [0,0,0] to [1,1,1] and we'll use + // translation and scaling afterward if needed to place it + + std::vector buffer_data; + // bottom plane + add_line(buffer_data, {0, 0, 0}, {1, 0, 0}, col); + add_line(buffer_data, {1, 0, 0}, {1, 1, 0}, col); + add_line(buffer_data, {1, 1, 0}, {0, 1, 0}, col); + add_line(buffer_data, {0, 1, 0}, {0, 0, 0}, col); + + // top plane + add_line(buffer_data, {0, 0, 1}, {1, 0, 1}, col); + add_line(buffer_data, {1, 0, 1}, {1, 1, 1}, col); + add_line(buffer_data, {1, 1, 1}, {0, 1, 1}, col); + add_line(buffer_data, {0, 1, 1}, {0, 0, 1}, col); + + // add vertical lines connecting the planes + add_line(buffer_data, {0, 0, 0}, {0, 0, 1}, col); + add_line(buffer_data, {1, 0, 0}, {1, 0, 1}, col); + add_line(buffer_data, {1, 1, 0}, {1, 1, 1}, col); + add_line(buffer_data, {0, 1, 0}, {0, 1, 1}, col); + + glBindBuffer(GL_ARRAY_BUFFER, extents_vbo_id); + glBufferData(GL_ARRAY_BUFFER, sizeof(float) * buffer_data.size(), buffer_data.data(), GL_STATIC_DRAW); + + extents_vertices_count = buffer_data.size() * sizeof(float) / sizeOfVertexInBytes; +} + void grbl::program_renderer::initialize_program_buffers() { glGenBuffers(1, &vbo_id); glGenVertexArrays(1, &vao_id); diff --git a/render.h b/render.h index df2316c..c29bbaa 100644 --- a/render.h +++ b/render.h @@ -27,16 +27,20 @@ private: GLuint vbo_id; GLuint vao_id; + GLuint extents_vbo_id; + GLuint extents_vao_id; + std::vector buffer_data; shader_program *shader = nullptr; bool initialized = false; glm::vec3 min_pos, max_pos, spindle_pos; - GLsizei vertices_count, spindle_vertices_count; + GLsizei vertices_count, spindle_vertices_count, extents_vertices_count; void initialize_program_buffers(); void initialize_spindle_buffers(); + void initialize_extents_buffers(); };