Introduced machine state handlers (check and run still need work).

Added proper init handler, fetching settings and parameters and exposing them to UI.
Added spindle rendering.
This commit is contained in:
2023-05-04 14:15:33 +03:00
parent a63d58a1ec
commit cacbe1b8aa
5 changed files with 842 additions and 200 deletions
+132 -75
View File
@@ -63,6 +63,14 @@ void grbl::program_renderer::render(glm::mat4 mvp, glm::vec2 viewport_size) {
glBindVertexArray(vao_id);
glDrawArrays(GL_LINES, 0, vertices_count);
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");
glBindVertexArray(spindle_vao_id);
glDrawArrays(GL_LINES, 0, spindle_vertices_count);
shader->unbind();
// draw bit location
@@ -73,24 +81,8 @@ void grbl::program_renderer::update(const grbl::program& pgm, const grbl::machin
shader = new shader_program(vs_code, ps_code);
glGenBuffers(1, &vbo_id);
glGenVertexArrays(1, &vao_id);
// vertex format: x, y, z, r, g, b, a
// stride: 28 bytes
const GLsizei sizeOfVertexInBytes = 28;
glBindVertexArray(vao_id);
glBindBuffer(GL_ARRAY_BUFFER, 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);
initialize_spindle_buffers();
initialize_program_buffers();
initialized = true;
}
@@ -98,6 +90,88 @@ void grbl::program_renderer::update(const grbl::program& pgm, const grbl::machin
// update program with machine status
// build vbo and vao
vertices_count = build_vbo(pgm);
auto x = cnc.get_status().work_pos;
spindle_pos = glm::vec3(x[0], x[1], x[2]);
}
void grbl::program_renderer::initialize_spindle_buffers() {
glGenBuffers(1, &spindle_vbo_id);
glGenVertexArrays(1, &spindle_vao_id);
// vertex format: x, y, z, r, g, b, a
// stride: 28 bytes
const GLsizei sizeOfVertexInBytes = 28;
glBindVertexArray(spindle_vao_id);
glBindBuffer(GL_ARRAY_BUFFER, spindle_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);
// construct spindle model
glm::vec4 col(1, 1, 0, 1); // yellow
std::vector<float> buffer_data;
const float spindle_length = 3.5;
const size_t cone_granularity = 20;
for (int i = 0; i < cone_granularity; i++) {
float x = sinf((i / (float) cone_granularity) * 2.0f * M_PI);
float y = cosf((i / (float) cone_granularity) * 2.0f * M_PI);
// 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);
glBufferData(GL_ARRAY_BUFFER, sizeOfVertexInBytes * buffer_data.size(), buffer_data.data(), GL_STATIC_DRAW);
spindle_vertices_count = buffer_data.size() * sizeof(float) / sizeOfVertexInBytes;
}
void grbl::program_renderer::initialize_program_buffers() {
glGenBuffers(1, &vbo_id);
glGenVertexArrays(1, &vao_id);
// vertex format: x, y, z, r, g, b, a
// stride: 28 bytes
const GLsizei sizeOfVertexInBytes = 28;
glBindVertexArray(vao_id);
glBindBuffer(GL_ARRAY_BUFFER, 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);
}
GLsizei grbl::program_renderer::build_vbo(const grbl::program& pgm) {
@@ -203,85 +277,68 @@ GLsizei grbl::program_renderer::build_vbo(const grbl::program& pgm) {
return number_of_vertices;
}
void check_compile_error(GLuint shader_id) {
GLint isCompiled = 0;
glGetShaderiv(shader_id, GL_COMPILE_STATUS, &isCompiled);
if (!isCompiled) {
GLint maxLength = 0;
glGetShaderiv(shader_id, GL_INFO_LOG_LENGTH, &maxLength);
std::string get_shader_info_log(GLuint id) {
GLint log_length = 0;
glGetShaderiv(id, GL_INFO_LOG_LENGTH, &log_length);
//The maxLength includes the NULL character
char errorLog[maxLength];
glGetShaderInfoLog(shader_id, maxLength, &maxLength, &errorLog[0]);
char error_log[log_length]; // length includes the NULL character
glGetShaderInfoLog(id, log_length, &log_length, &error_log[0]);
std::cerr << "Shader compile error: " << "(id: " << shader_id << ", code: " << isCompiled << ", bytes: "
<< maxLength << ") - " << std::string(errorLog, maxLength);
//Provide the infolog in whatever manor you deem best.
//Exit with failure.
glDeleteShader(shader_id); //Don't leak the shader.
} else {
std::cout << "Shader " << shader_id << " successfully compiled";
}
return std::string(error_log, log_length);
}
void check_link_error(GLuint program_id) {
std::string get_program_info_log(GLuint id) {
GLint log_length = 0;
glGetProgramiv(id, GL_INFO_LOG_LENGTH, &log_length);
char error_log[log_length]; // length includes the NULL character
glGetProgramInfoLog(id, log_length, &log_length, &error_log[0]);
return std::string(error_log, log_length);
}
bool check_compile_error(GLuint shader_id) {
GLint is_compiled = 0;
glGetShaderiv(shader_id, GL_COMPILE_STATUS, &is_compiled);
if (is_compiled == GL_TRUE)
return true;
std::cerr << "Shader compile error: " << "(id: " << shader_id << ") - " << get_shader_info_log(shader_id) << std::endl;
glDeleteShader(shader_id);
return false;
}
bool check_link_error(GLuint program_id) {
GLint is_linked = 0;
glGetShaderiv(program_id, GL_LINK_STATUS, &is_linked);
if (!is_linked) {
GLint maxLength = 0;
glGetShaderiv(program_id, GL_INFO_LOG_LENGTH, &maxLength);
glGetProgramiv(program_id, GL_LINK_STATUS, &is_linked);
if (is_linked == GL_TRUE)
return true;
//The maxLength includes the NULL character
char errorLog[maxLength];
glGetShaderInfoLog(program_id, maxLength, &maxLength, &errorLog[0]);
std::cerr << "Shader program link error: " << "(id: " << program_id << ", code: " << is_linked << ", bytes: "
<< maxLength << ") - " << std::string(errorLog, maxLength);
//Provide the info log in whatever manner you deem best.
//Exit with failure.
glDeleteShader(program_id); //Don't leak the program shader.
} else {
std::cout << "Shader program " << program_id << " successfully linked";
}
std::cerr << "Shader program link error: " << "(id: " << program_id << ") - " << get_program_info_log(program_id) << std::endl;
glDeleteProgram(program_id);
return false;
}
grbl::shader_program::shader_program(const char *vs_content, const char *ps_content)
: shader_ids{0, 0},
program_id{0} {
shader_ids[0] = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(shader_ids[0], 1, &vs_content, NULL);
glShaderSource(shader_ids[0], 1, &vs_content, nullptr);
glCompileShader(shader_ids[0]);
int success;
char infoLog[512];
glGetShaderiv(shader_ids[0], GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(shader_ids[0], 512, NULL, infoLog);
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
}
check_compile_error(shader_ids[0]);
shader_ids[1] = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(shader_ids[1], 1, &ps_content, NULL);
glShaderSource(shader_ids[1], 1, &ps_content, nullptr);
glCompileShader(shader_ids[1]);
glGetShaderiv(shader_ids[1], GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(shader_ids[1], 512, NULL, infoLog);
std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
}
check_compile_error(shader_ids[1]);
program_id = glCreateProgram();
glAttachShader(program_id, shader_ids[0]);
glAttachShader(program_id, shader_ids[1]);
glLinkProgram(program_id);
glGetProgramiv(program_id, GL_LINK_STATUS, &success);
if (!success) {
glGetProgramInfoLog(program_id, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::PROGRAM::COMPILATION_FAILED\n" << infoLog << std::endl;
}
check_link_error(program_id);
glUseProgram(program_id);
}