Move shader sources to files.

This commit is contained in:
David Allemang
2022-02-13 15:59:47 -05:00
committed by David Allemang
parent 34b1a31704
commit ae10f00127
4 changed files with 69 additions and 34 deletions

View File

@@ -13,8 +13,15 @@ include(External/json.cmake)
include_directories(./include) 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) add_executable(vis src/main.cpp)
target_link_libraries(vis glfw glad imgui eigen nlohmann_json) target_link_libraries(vis glfw glad imgui eigen nlohmann_json)
add_dependencies(vis resources)
add_executable(serial src/serialtest.cpp) add_executable(serial src/serialtest.cpp)
target_link_libraries(serial eigen nlohmann_json) target_link_libraries(serial eigen nlohmann_json)

View File

@@ -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;
}

View File

@@ -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);
}

View File

@@ -113,44 +113,28 @@ int run(GLFWwindow *window, ImGuiContext *context) {
); );
glVertexArrayElementBuffer(vao, ibo); glVertexArrayElementBuffer(vao, ibo);
const char *vs_src = "#version 440\n" std::ifstream vs_file("res/shaders/main.vert.glsl");
"layout(location=1) uniform float time;" std::string vs_src(
"layout(location=2) uniform mat4 proj;" (std::istreambuf_iterator<char>(vs_file)),
"layout(location=0) in vec3 pos;" std::istreambuf_iterator<char>()
"void main() {" );
" float c2 = cos(time * 0.2);" vs_file.close();
" float s2 = sin(time * 0.2);" const char *vs_str = vs_src.c_str();
" 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);"
"}";
GLuint vs = glCreateShader(GL_VERTEX_SHADER); GLuint vs = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vs, 1, &vs_src, nullptr); glShaderSource(vs, 1, &vs_str, nullptr);
glCompileShader(vs); glCompileShader(vs);
const char *fs_src = "#version 440\n" std::ifstream fs_file("res/shaders/main.frag.glsl");
"layout(location=0) uniform vec4 ucol;" std::string fs_src(
"layout(location=0) out vec4 col;" (std::istreambuf_iterator<char>(fs_file)),
"void main() {" std::istreambuf_iterator<char>()
" float d = 1.0 - gl_FragCoord.z;" );
" d = (d - 0.5) / 0.7 + 0.5;" fs_file.close();
" col = ucol;" const char *fs_str = fs_src.c_str();
" col.xyz *= d;"
"}";
GLuint fs = glCreateShader(GL_FRAGMENT_SHADER); GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fs, 1, &fs_src, nullptr); glShaderSource(fs, 1, &fs_str, nullptr);
glCompileShader(fs); glCompileShader(fs);
GLuint pgm = glCreateProgram(); GLuint pgm = glCreateProgram();
@@ -158,6 +142,21 @@ int run(GLFWwindow *window, ImGuiContext *context) {
glAttachShader(pgm, fs); glAttachShader(pgm, fs);
glLinkProgram(pgm); 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); glEnable(GL_DEPTH_TEST);
Eigen::Projective3f proj; Eigen::Projective3f proj;