From 9989933853900578512c185db5add8f1b5a445f4 Mon Sep 17 00:00:00 2001 From: David Allemang Date: Fri, 10 Jan 2020 15:59:23 -0500 Subject: [PATCH] load shaders from files. --- vis/include/util.hpp | 65 ++++++++++++++++++++++++++++- vis/shaders/keep | 0 vis/shaders/one-color.fs.glsl | 12 ++++++ vis/shaders/ortho.vs.glsl | 15 +++++++ vis/shaders/stereo.vs.glsl | 15 +++++++ vis/shaders/w-axis-hue.fs.glsl | 11 +++++ vis/src/main.cpp | 74 +++++----------------------------- 7 files changed, 126 insertions(+), 66 deletions(-) delete mode 100644 vis/shaders/keep create mode 100644 vis/shaders/one-color.fs.glsl create mode 100644 vis/shaders/ortho.vs.glsl create mode 100644 vis/shaders/stereo.vs.glsl create mode 100644 vis/shaders/w-axis-hue.fs.glsl diff --git a/vis/include/util.hpp b/vis/include/util.hpp index 260f6d6..f038e0c 100644 --- a/vis/include/util.hpp +++ b/vis/include/util.hpp @@ -1,10 +1,30 @@ #pragma once +#include +#include +#include #include #include + #include -#include +class gl_error : public std::domain_error { +public: + explicit gl_error(const std::string &arg) : domain_error(arg) {} + explicit gl_error(const char *string) : domain_error(string) {} +}; + +class shader_error : public gl_error { +public: + explicit shader_error(const std::string &arg) : gl_error(arg) {} + explicit shader_error(const char *string) : gl_error(string) {} +}; + +class program_error : public gl_error { +public: + explicit program_error(const std::string &arg) : gl_error(arg) {} + explicit program_error(const char *string) : gl_error(string) {} +}; void utilShaderSource(GLuint shader, const std::vector &sources) { char const *ptrs[sources.size()]; @@ -39,4 +59,47 @@ std::string utilInfo() { << " OpenGL version: " << glGetString(GL_VERSION) << std::endl << " Shading version: " << glGetString(GL_SHADING_LANGUAGE_VERSION) << std::endl; return ss.str(); +} + +std::string utilReadFile(const std::string &filename) { + std::ifstream in(filename, std::ios::in | std::ios::binary); + if (in) { + std::ostringstream contents; + contents << in.rdbuf(); + in.close(); + return (contents.str()); + } + throw std::system_error(errno, std::generic_category()); +} + +GLuint utilCompileFiles(const GLenum type, const std::vector &files) { + std::vector sources; + sources.reserve(files.size()); + for (const auto &file : files) { + sources.push_back(utilReadFile(file)); + } + + GLuint shader = glCreateShader(type); + utilShaderSource(shader, sources); + glCompileShader(shader); + + int success; + glGetShaderiv(shader, GL_COMPILE_STATUS, &success); + if (success) return shader; + + throw shader_error(utilShaderInfoLog(shader)); +} + +GLuint utilLinkProgram(const std::vector &shaders) { + GLuint program = glCreateProgram(); + for (const auto &shader : shaders) { + glAttachShader(program, shader); + } + glLinkProgram(program); + + int success; + glGetProgramiv(program, GL_LINK_STATUS, &success); + if (success)return program; + + throw program_error(utilProgramInfoLog(program)); } \ No newline at end of file diff --git a/vis/shaders/keep b/vis/shaders/keep deleted file mode 100644 index e69de29..0000000 diff --git a/vis/shaders/one-color.fs.glsl b/vis/shaders/one-color.fs.glsl new file mode 100644 index 0000000..d21060f --- /dev/null +++ b/vis/shaders/one-color.fs.glsl @@ -0,0 +1,12 @@ +#version 430 + +layout(location=2) uniform vec3 c; + +in vec4 vpos; + +out vec4 color; + +void main() { + float d = smoothstep(-2, 2, vpos.z); + color = vec4(c * d, 1); +} diff --git a/vis/shaders/ortho.vs.glsl b/vis/shaders/ortho.vs.glsl new file mode 100644 index 0000000..dbaf89d --- /dev/null +++ b/vis/shaders/ortho.vs.glsl @@ -0,0 +1,15 @@ +#version 430 + +layout(location=0) uniform mat4 proj; +layout(location=1) uniform mat4 view; + +layout(location=0) in vec4 pos; + +out vec4 vpos; + +void main() { + int i = gl_VertexID; + vpos = view * pos; + gl_Position = proj * vec4(vpos.xyz / (1), 1); + gl_PointSize = 5; +} diff --git a/vis/shaders/stereo.vs.glsl b/vis/shaders/stereo.vs.glsl new file mode 100644 index 0000000..c67588e --- /dev/null +++ b/vis/shaders/stereo.vs.glsl @@ -0,0 +1,15 @@ +#version 430 + +layout(location=0) uniform mat4 proj; +layout(location=1) uniform mat4 view; + +layout(location=0) in vec4 pos; + +out vec4 vpos; + +void main() { + int i = gl_VertexID; + vpos = view * pos; + gl_Position = proj * vec4(vpos.xyz / (1 - vpos.w), 1); + gl_PointSize = 5; +} diff --git a/vis/shaders/w-axis-hue.fs.glsl b/vis/shaders/w-axis-hue.fs.glsl new file mode 100644 index 0000000..99646e4 --- /dev/null +++ b/vis/shaders/w-axis-hue.fs.glsl @@ -0,0 +1,11 @@ +#version 430 + +in vec4 vpos; + +out vec4 color; + +void main() { + float d = smoothstep(-2, 2, vpos.z); + vec3 off = 1.04 * vec3(0, 2, 4) + 2 * vec3(vpos.w); + color = vec4(d * cos(off), 1); +} diff --git a/vis/src/main.cpp b/vis/src/main.cpp index 7db9397..920a033 100644 --- a/vis/src/main.cpp +++ b/vis/src/main.cpp @@ -44,78 +44,22 @@ int main(int argc, char *argv[]) { std::cout << utilInfo(); - const std::string VS_SOURCE = - "#version 430\n" - "layout(location=0) uniform mat4 proj;" - "layout(location=1) uniform mat4 view;" - "" - "layout(location=0) in vec4 pos;" - "" - "out vec4 vpos;" - "" - "void main() {" - " int i = gl_VertexID;" - " vpos = view * pos;" - " gl_Position = proj * vec4(vpos.xyz / (1), 1);" - // " gl_Position = proj * vec4(vpos.xyz / (1 - vpos.w), 1);" - " gl_PointSize = 5;" - "}"; + GLuint pgm; - const std::string FS_SOURCE = - "#version 430\n" - "layout(location=2) uniform vec3 c;" - "" - "in vec4 vpos;" - "" - "out vec4 color;" - "" - "void main() {" - " float d = smoothstep(-2, 2, vpos.z);" - " vec3 off = 1.04 * vec3(0, 2, 4) + 2 * vec3(vpos.w);" - " color = vec4(c * d, 1);" - "}"; + try { + GLuint vs = utilCompileFiles(GL_VERTEX_SHADER, {"shaders/ortho.vs.glsl"}); + GLuint fs = utilCompileFiles(GL_FRAGMENT_SHADER, {"shaders/w-axis-hue.fs.glsl"}); - //region init shaders - GLuint vs = glCreateShader(GL_VERTEX_SHADER); - utilShaderSource(vs, {VS_SOURCE}); - glCompileShader(vs); - - GLuint fs = glCreateShader(GL_FRAGMENT_SHADER); - utilShaderSource(fs, {FS_SOURCE}); - - GLuint pgm = glCreateProgram(); - glAttachShader(pgm, vs); - glAttachShader(pgm, fs); - glLinkProgram(pgm); - - GLint status; - - glGetShaderiv(vs, GL_COMPILE_STATUS, &status); - if (!status) { - std::cerr << utilShaderInfoLog(vs) << "\n=========\n" << std::endl; - } - - glGetShaderiv(fs, GL_COMPILE_STATUS, &status); - if (!status) { - std::cerr << utilShaderInfoLog(fs) << "\n=========\n" << std::endl; - } - - glGetProgramiv(pgm, GL_LINK_STATUS, &status); - if (!status) { - std::cerr << utilProgramInfoLog(pgm) << "\n=========\n" << std::endl; + pgm = utilLinkProgram({vs, fs}); + } catch (const gl_error &e) { + std::cerr << e.what() << std::endl; glfwTerminate(); - return EXIT_FAILURE; + exit(EXIT_FAILURE); } - //endregion - auto group = tc::group::H(3); + auto group = tc::schlafli({5, 3, 3}); auto res = group.solve(); auto mirrors = mirror(group); - std::cout << "Solved " << res.size() << std::endl; - std::cout << "Mirror lengths:" << std::endl; - for (const auto &m : mirrors) { - std::cout << glm::length(m) << " (" << m.x << " " << m.y << " " << m.z << " " << m.w << ")" << std::endl; - } auto corners = plane_intersections(mirrors); auto start = barycentric(corners, {1.00, 1.00, 1.00, 1.00});