Skip to content

Commit

Permalink
File resources (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
p3p committed May 2, 2024
1 parent afe7683 commit 96ed053
Show file tree
Hide file tree
Showing 14 changed files with 429 additions and 140 deletions.
6 changes: 3 additions & 3 deletions src/MarlinSimulator/hardware/KinematicSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ void KinematicSystem::collect_steppers() {
#endif
}

CartesianKinematicSystem::CartesianKinematicSystem(std::function<void(kinematic_state)> on_kinematic_update) : KinematicSystem(on_kinematic_update) {
CartesianKinematicSystem::CartesianKinematicSystem(std::function<void(kinematic_state&)> on_kinematic_update) : KinematicSystem(on_kinematic_update) {
collect_steppers();

srand(time(0));
Expand Down Expand Up @@ -200,7 +200,7 @@ void CartesianKinematicSystem::kinematic_update() {
std::static_pointer_cast<StepperDriver>(steppers[AxisIndex::Z])->steps() / steps_per_unit[2] * (((INVERT_Z_DIR * 2) - 1) * -1.0)
};

std::vector<double> extruder {};
extruder.clear();
for (size_t i = 0; i < EXTRUDERS; ++i) {
extruder.push_back(std::static_pointer_cast<StepperDriver>(steppers[AxisIndex::E0 + i])->steps() / steps_per_unit[3 + (i * distinct_e_factors)] * (((extruder_invert_dir[i] * 2) - 1) * -1.0));
}
Expand Down Expand Up @@ -341,7 +341,7 @@ glm::vec3 DeltaKinematicSystem::forward_kinematics(const double z1, const double
};
}

DeltaKinematicSystem::DeltaKinematicSystem(std::function<void(kinematic_state)> on_kinematic_update) : KinematicSystem(on_kinematic_update) {
DeltaKinematicSystem::DeltaKinematicSystem(std::function<void(kinematic_state&)> on_kinematic_update) : KinematicSystem(on_kinematic_update) {

#ifdef DELTA_HEIGHT
delta_height = DELTA_HEIGHT;
Expand Down
9 changes: 5 additions & 4 deletions src/MarlinSimulator/hardware/KinematicSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,28 @@ struct kinematic_state {

class KinematicSystem : public VirtualPrinter::Component {
public:
KinematicSystem(std::function<void(kinematic_state)> on_kinematic_update) : VirtualPrinter::Component("Kinematic System"), on_kinematic_update(on_kinematic_update) {};
KinematicSystem(std::function<void(kinematic_state&)> on_kinematic_update) : VirtualPrinter::Component("Kinematic System"), on_kinematic_update(on_kinematic_update) {};

virtual void kinematic_update() = 0;
void collect_steppers();

std::vector<glm::vec3> hardware_offset {};
std::vector<std::shared_ptr<VirtualPrinter::Component>> steppers;
kinematic_state state{};
std::function<void(kinematic_state)> on_kinematic_update;
std::function<void(kinematic_state&)> on_kinematic_update;
};

class CartesianKinematicSystem : public KinematicSystem {
public:
CartesianKinematicSystem(std::function<void(kinematic_state)> on_kinematic_update);
CartesianKinematicSystem(std::function<void(kinematic_state&)> on_kinematic_update);
virtual void ui_widget() override;
virtual void kinematic_update() override;
std::vector<double> extruder {};
};

class DeltaKinematicSystem : public KinematicSystem {
public:
DeltaKinematicSystem(std::function<void(kinematic_state)> on_kinematic_update);
DeltaKinematicSystem(std::function<void(kinematic_state&)> on_kinematic_update);
virtual void ui_widget() override;
virtual void kinematic_update() override;

Expand Down
6 changes: 3 additions & 3 deletions src/MarlinSimulator/hardware/SDCard.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ class SDCard: public SPISlavePeripheral {
virtual ~SDCard() {};

void update() {}

const std::string file_dialog_key = "ChooseSDFileDlgKey";
void ui_widget() {
if (ImGui::Button("Select Image (FAT32)")) {
ImGuiFileDialog::Instance()->OpenDialog("ChooseFileDlgKey", "Choose File", "FAT32 Disk Image(*.img){.img},.*", ".");
ImGuiFileDialog::Instance()->OpenDialog(file_dialog_key, "Choose File", "FAT32 Disk Image(*.img){.img},.*", ".");
}
if (ImGuiFileDialog::Instance()->Display("ChooseFileDlgKey", ImGuiWindowFlags_NoDocking)) {
if (ImGuiFileDialog::Instance()->Display(file_dialog_key, ImGuiWindowFlags_NoDocking)) {
if (ImGuiFileDialog::Instance()->IsOk()) {
image_filename = ImGuiFileDialog::Instance()->GetFilePathName();
sd_present = image_exists();
Expand Down
84 changes: 83 additions & 1 deletion src/MarlinSimulator/renderer/gl.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,40 @@
#pragma once

#include <variant>

#include <gl.h>
#include <glm/glm.hpp>

namespace renderer {

template<uint32_t V> struct gl_enum_to_type;
template<> struct gl_enum_to_type<GL_FLOAT> { using value_type = float; };
template<> struct gl_enum_to_type<GL_FLOAT_VEC2> { using value_type = glm::vec2; };
template<> struct gl_enum_to_type<GL_FLOAT_VEC3> { using value_type = glm::vec3; };
template<> struct gl_enum_to_type<GL_FLOAT_VEC4> { using value_type = glm::vec4; };
template<> struct gl_enum_to_type<GL_DOUBLE> { using value_type = double; };
template<> struct gl_enum_to_type<GL_INT> { using value_type = int; };
template<> struct gl_enum_to_type<GL_INT_VEC2> { using value_type = glm::ivec2; };
template<> struct gl_enum_to_type<GL_INT_VEC3> { using value_type = glm::ivec3; };
template<> struct gl_enum_to_type<GL_INT_VEC4> { using value_type = glm::ivec4; };
template<> struct gl_enum_to_type<GL_UNSIGNED_INT> { using value_type = unsigned int; };
template<> struct gl_enum_to_type<GL_UNSIGNED_INT_VEC2> { using value_type = glm::uvec2; };
template<> struct gl_enum_to_type<GL_UNSIGNED_INT_VEC3> { using value_type = glm::uvec3; };
template<> struct gl_enum_to_type<GL_UNSIGNED_INT_VEC4> { using value_type = glm::uvec4; };
template<> struct gl_enum_to_type<GL_BOOL> { using value_type = bool; };
template<> struct gl_enum_to_type<GL_BOOL_VEC2> { using value_type = glm::bvec2; };
template<> struct gl_enum_to_type<GL_BOOL_VEC3> { using value_type = glm::bvec3; };
template<> struct gl_enum_to_type<GL_BOOL_VEC4> { using value_type = glm::bvec4; };
template<> struct gl_enum_to_type<GL_FLOAT_MAT2> { using value_type = glm::mat2; };
template<> struct gl_enum_to_type<GL_FLOAT_MAT3> { using value_type = glm::mat3; };
template<> struct gl_enum_to_type<GL_FLOAT_MAT4> { using value_type = glm::mat4; };
template<> struct gl_enum_to_type<GL_FLOAT_MAT2x3> { using value_type = glm::mat2x3; };
template<> struct gl_enum_to_type<GL_FLOAT_MAT2x4> { using value_type = glm::mat2x4; };
template<> struct gl_enum_to_type<GL_FLOAT_MAT3x2> { using value_type = glm::mat3x2; };
template<> struct gl_enum_to_type<GL_FLOAT_MAT3x4> { using value_type = glm::mat3x4; };
template<> struct gl_enum_to_type<GL_FLOAT_MAT4x2> { using value_type = glm::mat4x2; };
template<> struct gl_enum_to_type<GL_FLOAT_MAT4x3> { using value_type = glm::mat4x3; };

template<typename T> struct type_to_gl_enum;
template<> struct type_to_gl_enum<float> { static constexpr uint32_t value = GL_FLOAT; };
template<> struct type_to_gl_enum<glm::vec2> { static constexpr uint32_t value = GL_FLOAT_VEC2; };
Expand Down Expand Up @@ -37,6 +67,58 @@ template<> struct type_to_gl_enum<int16_t> { static constexpr uint32_t value = G
template<> struct type_to_gl_enum<uint16_t>{ static constexpr uint32_t value = GL_UNSIGNED_SHORT; };
template<> struct type_to_gl_enum<int8_t>{ static constexpr uint32_t value = GL_BYTE; };

template<uint32_t V> struct gl_uniform;
template<> struct gl_uniform<GL_FLOAT> { using value_type = decltype(glUniform1fv); static constexpr bool is_matrix = false; gl_enum_to_type<GL_FLOAT>::value_type *value; value_type invoke = glUniform1fv; };
template<> struct gl_uniform<GL_FLOAT_VEC2> { using value_type = decltype(glUniform2fv); static constexpr bool is_matrix = false; gl_enum_to_type<GL_FLOAT_VEC2>::value_type *value; value_type invoke = glUniform2fv; };
template<> struct gl_uniform<GL_FLOAT_VEC3> { using value_type = decltype(glUniform3fv); static constexpr bool is_matrix = false; gl_enum_to_type<GL_FLOAT_VEC3>::value_type *value; value_type invoke = glUniform3fv; };
template<> struct gl_uniform<GL_FLOAT_VEC4> { using value_type = decltype(glUniform4fv); static constexpr bool is_matrix = false; gl_enum_to_type<GL_FLOAT_VEC4>::value_type *value; value_type invoke = glUniform4fv; };
template<> struct gl_uniform<GL_INT> { using value_type = decltype(glUniform1iv); static constexpr bool is_matrix = false; gl_enum_to_type<GL_INT>::value_type *value; value_type invoke = glUniform1iv; };
template<> struct gl_uniform<GL_INT_VEC2> { using value_type = decltype(glUniform2iv); static constexpr bool is_matrix = false; gl_enum_to_type<GL_INT_VEC2>::value_type *value; value_type invoke = glUniform2iv; };
template<> struct gl_uniform<GL_INT_VEC3> { using value_type = decltype(glUniform3iv); static constexpr bool is_matrix = false; gl_enum_to_type<GL_INT_VEC3>::value_type *value; value_type invoke = glUniform3iv; };
template<> struct gl_uniform<GL_INT_VEC4> { using value_type = decltype(glUniform4iv); static constexpr bool is_matrix = false; gl_enum_to_type<GL_INT_VEC4>::value_type *value; value_type invoke = glUniform4iv; };
template<> struct gl_uniform<GL_UNSIGNED_INT> { using value_type = decltype(glUniform1uiv); static constexpr bool is_matrix = false; gl_enum_to_type<GL_UNSIGNED_INT>::value_type *value; value_type invoke = glUniform1uiv; };
template<> struct gl_uniform<GL_UNSIGNED_INT_VEC2> { using value_type = decltype(glUniform2uiv); static constexpr bool is_matrix = false; gl_enum_to_type<GL_UNSIGNED_INT_VEC2>::value_type *value; value_type invoke = glUniform2uiv; };
template<> struct gl_uniform<GL_UNSIGNED_INT_VEC3> { using value_type = decltype(glUniform3uiv); static constexpr bool is_matrix = false; gl_enum_to_type<GL_UNSIGNED_INT_VEC3>::value_type *value; value_type invoke = glUniform3uiv; };
template<> struct gl_uniform<GL_UNSIGNED_INT_VEC4> { using value_type = decltype(glUniform4uiv); static constexpr bool is_matrix = false; gl_enum_to_type<GL_UNSIGNED_INT_VEC4>::value_type *value; value_type invoke = glUniform4uiv; };
template<> struct gl_uniform<GL_BOOL> { using value_type = decltype(glUniform1uiv); static constexpr bool is_matrix = false; gl_enum_to_type<GL_BOOL>::value_type *value; value_type invoke = glUniform1uiv; };
template<> struct gl_uniform<GL_BOOL_VEC2> { using value_type = decltype(glUniform2uiv); static constexpr bool is_matrix = false; gl_enum_to_type<GL_BOOL_VEC2>::value_type *value; value_type invoke = glUniform2uiv; };
template<> struct gl_uniform<GL_BOOL_VEC3> { using value_type = decltype(glUniform3uiv); static constexpr bool is_matrix = false; gl_enum_to_type<GL_BOOL_VEC3>::value_type *value; value_type invoke = glUniform3uiv; };
template<> struct gl_uniform<GL_BOOL_VEC4> { using value_type = decltype(glUniform4uiv); static constexpr bool is_matrix = false; gl_enum_to_type<GL_BOOL_VEC4>::value_type *value; value_type invoke = glUniform4uiv; };
template<> struct gl_uniform<GL_FLOAT_MAT2> { using value_type = decltype(glUniformMatrix2fv); static constexpr bool is_matrix = true; gl_enum_to_type<GL_FLOAT_MAT2>::value_type *value; value_type invoke = glUniformMatrix2fv; };
template<> struct gl_uniform<GL_FLOAT_MAT3> { using value_type = decltype(glUniformMatrix3fv); static constexpr bool is_matrix = true; gl_enum_to_type<GL_FLOAT_MAT3>::value_type *value; value_type invoke = glUniformMatrix3fv; };
template<> struct gl_uniform<GL_FLOAT_MAT4> { using value_type = decltype(glUniformMatrix4fv); static constexpr bool is_matrix = true; gl_enum_to_type<GL_FLOAT_MAT4>::value_type *value; value_type invoke = glUniformMatrix4fv; };
template<> struct gl_uniform<GL_FLOAT_MAT2x3> { using value_type = decltype(glUniformMatrix2x3fv); static constexpr bool is_matrix = true; gl_enum_to_type<GL_FLOAT_MAT2x3>::value_type *value; value_type invoke = glUniformMatrix2x3fv; };
template<> struct gl_uniform<GL_FLOAT_MAT2x4> { using value_type = decltype(glUniformMatrix2x4fv); static constexpr bool is_matrix = true; gl_enum_to_type<GL_FLOAT_MAT2x4>::value_type *value; value_type invoke = glUniformMatrix2x4fv; };
template<> struct gl_uniform<GL_FLOAT_MAT3x2> { using value_type = decltype(glUniformMatrix3x2fv); static constexpr bool is_matrix = true; gl_enum_to_type<GL_FLOAT_MAT3x2>::value_type *value; value_type invoke = glUniformMatrix3x2fv; };
template<> struct gl_uniform<GL_FLOAT_MAT3x4> { using value_type = decltype(glUniformMatrix3x4fv); static constexpr bool is_matrix = true; gl_enum_to_type<GL_FLOAT_MAT3x4>::value_type *value; value_type invoke = glUniformMatrix3x4fv; };
template<> struct gl_uniform<GL_FLOAT_MAT4x2> { using value_type = decltype(glUniformMatrix4x2fv); static constexpr bool is_matrix = true; gl_enum_to_type<GL_FLOAT_MAT4x2>::value_type *value; value_type invoke = glUniformMatrix4x2fv; };
template<> struct gl_uniform<GL_FLOAT_MAT4x3> { using value_type = decltype(glUniformMatrix4x3fv); static constexpr bool is_matrix = true; gl_enum_to_type<GL_FLOAT_MAT4x3>::value_type *value; value_type invoke = glUniformMatrix4x3fv; };
using gl_uniform_t = std::variant<
gl_uniform<GL_FLOAT>, gl_uniform<GL_FLOAT_VEC2>, gl_uniform<GL_FLOAT_VEC3>,
gl_uniform<GL_FLOAT_VEC4>, gl_uniform<GL_INT>, gl_uniform<GL_INT_VEC2>,
gl_uniform<GL_INT_VEC3>, gl_uniform<GL_INT_VEC4>, gl_uniform<GL_UNSIGNED_INT>,
gl_uniform<GL_UNSIGNED_INT_VEC2>, gl_uniform<GL_UNSIGNED_INT_VEC3>, gl_uniform<GL_UNSIGNED_INT_VEC4>,
gl_uniform<GL_BOOL>, gl_uniform<GL_BOOL_VEC2>, gl_uniform<GL_BOOL_VEC3>, gl_uniform<GL_BOOL_VEC4>,
gl_uniform<GL_FLOAT_MAT2>, gl_uniform<GL_FLOAT_MAT3>, gl_uniform<GL_FLOAT_MAT4>,
gl_uniform<GL_FLOAT_MAT2x3>, gl_uniform<GL_FLOAT_MAT2x4>, gl_uniform<GL_FLOAT_MAT3x2>,
gl_uniform<GL_FLOAT_MAT3x4>, gl_uniform<GL_FLOAT_MAT4x2>, gl_uniform<GL_FLOAT_MAT4x3>
>;

template<uint32_t V> struct gl_texture_parameter;
template<> struct gl_texture_parameter<GL_FLOAT> { using value_type = decltype(glTexParameterfv); value_type invoke = glTexParameterfv; };
template<> struct gl_texture_parameter<GL_INT> { using value_type = decltype(glTexParameteriv); value_type invoke = glTexParameteriv; };
template<> struct gl_texture_parameter<GL_UNSIGNED_INT> { using value_type = decltype(glTexParameterIuiv); value_type invoke = glTexParameterIuiv; };

template<uint32_t V> struct gl_texture_image;
template<> struct gl_texture_image<1> { using value_type = decltype(glTexImage1D); value_type invoke = glTexImage1D; };
template<> struct gl_texture_image<2> { using value_type = decltype(glTexImage2D); value_type invoke = glTexImage2D; };
template<> struct gl_texture_image<3> { using value_type = decltype(glTexImage3D); value_type invoke = glTexImage3D; };

template<uint32_t V> struct gl_texture_subimage;
template<> struct gl_texture_subimage<1> { using value_type = decltype(glTexSubImage1D); value_type invoke = glTexSubImage1D; };
template<> struct gl_texture_subimage<2> { using value_type = decltype(glTexSubImage2D); value_type invoke = glTexSubImage2D; };
template<> struct gl_texture_subimage<3> { using value_type = decltype(glTexSubImage3D); value_type invoke = glTexSubImage3D; };

struct data_descriptor_element_t {
public:
template <typename T> static constexpr data_descriptor_element_t build(){ return data_descriptor_element_t(T::length(), type_to_gl_enum<typename T::value_type>::value, sizeof(T)); };
Expand All @@ -47,7 +129,7 @@ struct data_descriptor_element_t {
constexpr data_descriptor_element_t(const uint32_t elements, const uint32_t gl_enum, const uint32_t length) : elements{elements}, gl_enum{gl_enum}, length{length} {};
};

enum class Primitive : uint32_t {
enum class GeometryPrimitive : uint32_t {
POINTS = GL_POINTS,
LINE_STRIP = GL_LINE_STRIP,
LINE_LOOP = GL_LINE_LOOP,
Expand Down
19 changes: 19 additions & 0 deletions src/MarlinSimulator/renderer/renderer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "renderer.h"

namespace renderer {

template<> void ShaderProgramInstance::set_uniform<float>(std::string name, float* value) {
using gl_uniform_type = gl_uniform<type_to_gl_enum<float>::value>;
if (m_program->m_uniforms.count(name)) {
auto uniform = m_program->m_uniforms[name];
m_uniforms[uniform.location] = shader_uniform_t {
uniform,
gl_uniform_type {value},
[](shader_attr_t& uniform_desc, gl_uniform_t& uniform_data) {
gl_uniform_type().invoke(uniform_desc.location, uniform_desc.size, std::get<gl_uniform_type>(uniform_data).value);
},
};
}
}

}
Loading

0 comments on commit 96ed053

Please sign in to comment.