diff --git a/CMakeLists.txt b/CMakeLists.txt index 33fbe53..d39ec08 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,8 +13,15 @@ include(External/json.cmake) include_directories(./include) +add_custom_target(resources DEPENDS resources_output) +add_custom_command( + OUTPUT resources_output + COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/res ${CMAKE_CURRENT_BINARY_DIR}/res + COMMENT "Copying Resources") + add_executable(vis src/main.cpp) target_link_libraries(vis glfw glad imgui eigen nlohmann_json) +add_dependencies(vis resources) add_executable(serial src/serialtest.cpp) target_link_libraries(serial eigen nlohmann_json) diff --git a/res/shaders/main.frag.glsl b/res/shaders/main.frag.glsl new file mode 100644 index 0000000..7d9b9db --- /dev/null +++ b/res/shaders/main.frag.glsl @@ -0,0 +1,11 @@ +#version 440 + +layout(location=0) uniform vec4 ucol; +layout(location=0) out vec4 col; + +void main() { + float d = 1.0 - gl_FragCoord.z; + d = (d - 0.5) / 0.7 + 0.5; + col = ucol; + col.xyz *= d; +} diff --git a/res/shaders/main.vert.glsl b/res/shaders/main.vert.glsl new file mode 100644 index 0000000..898467d --- /dev/null +++ b/res/shaders/main.vert.glsl @@ -0,0 +1,18 @@ +#version 440 + +layout(location=1) uniform float time; +layout(location=2) uniform mat4 proj; +layout(location=0) in vec3 pos; + +void main() { + float c2 = cos(time * 0.2); + float s2 = sin(time * 0.2); + float c3 = cos(time * 0.3); + float s3 = sin(time * 0.3); + + mat4 r1 = mat4(c2, -s2, 0.0, 0.0, s2, c2, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0); + + mat4 r2 = mat4(c3, 0.0, -s3, 0.0, 0.0, 1.0, 0.0, 0.0, s3, 0.0, c3, 0.0, 0.0, 0.0, 0.0, 1.0); + + gl_Position = proj * r2 * r1 * vec4(pos, 1.0); +} diff --git a/src/main.cpp b/src/main.cpp index 221a9d8..dff2f69 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -113,44 +113,28 @@ int run(GLFWwindow *window, ImGuiContext *context) { ); glVertexArrayElementBuffer(vao, ibo); - const char *vs_src = "#version 440\n" - "layout(location=1) uniform float time;" - "layout(location=2) uniform mat4 proj;" - "layout(location=0) in vec3 pos;" - "void main() {" - " float c2 = cos(time * 0.2);" - " float s2 = sin(time * 0.2);" - " float c3 = cos(time * 0.3);" - " float s3 = sin(time * 0.3);" - " mat4 r1 = mat4(" - " c2, -s2, 0.0, 0.0," - " s2, c2, 0.0, 0.0," - " 0.0, 0.0, 1.0, 0.0," - " 0.0, 0.0, 0.0, 1.0" - " );" - " mat4 r2 = mat4(" - " c3, 0.0, -s3, 0.0," - " 0.0, 1.0, 0.0, 0.0," - " s3, 0.0, c3, 0.0," - " 0.0, 0.0, 0.0, 1.0" - ");" - " gl_Position = proj * r2 * r1 * vec4(pos, 1.0);" - "}"; + std::ifstream vs_file("res/shaders/main.vert.glsl"); + std::string vs_src( + (std::istreambuf_iterator(vs_file)), + std::istreambuf_iterator() + ); + vs_file.close(); + const char *vs_str = vs_src.c_str(); + GLuint vs = glCreateShader(GL_VERTEX_SHADER); - glShaderSource(vs, 1, &vs_src, nullptr); + glShaderSource(vs, 1, &vs_str, nullptr); glCompileShader(vs); - const char *fs_src = "#version 440\n" - "layout(location=0) uniform vec4 ucol;" - "layout(location=0) out vec4 col;" - "void main() {" - " float d = 1.0 - gl_FragCoord.z;" - " d = (d - 0.5) / 0.7 + 0.5;" - " col = ucol;" - " col.xyz *= d;" - "}"; + std::ifstream fs_file("res/shaders/main.frag.glsl"); + std::string fs_src( + (std::istreambuf_iterator(fs_file)), + std::istreambuf_iterator() + ); + fs_file.close(); + const char *fs_str = fs_src.c_str(); + GLuint fs = glCreateShader(GL_FRAGMENT_SHADER); - glShaderSource(fs, 1, &fs_src, nullptr); + glShaderSource(fs, 1, &fs_str, nullptr); glCompileShader(fs); GLuint pgm = glCreateProgram(); @@ -158,6 +142,21 @@ int run(GLFWwindow *window, ImGuiContext *context) { glAttachShader(pgm, fs); glLinkProgram(pgm); + GLint link_status; + glGetProgramiv(pgm, GL_LINK_STATUS, &link_status); + if (!link_status) { + std::cerr << "Program link failed." << std::endl; + GLint vs_comp_status, fs_comp_status; + glGetShaderiv(vs, GL_COMPILE_STATUS, &vs_comp_status); + glGetShaderiv(fs, GL_COMPILE_STATUS, &fs_comp_status); + std::cerr << "vs compiled: " << std::boolalpha << (bool) vs_comp_status << std::endl; + std::cerr << "fs compiled: " << std::boolalpha << (bool) fs_comp_status << std::endl; + return EXIT_FAILURE; + } + + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + glEnable(GL_DEPTH_TEST); Eigen::Projective3f proj;