mirror of
https://github.com/allemangD/toddcox-visualize.git
synced 2025-11-10 12:02:47 -05:00
load shaders from files.
This commit is contained in:
@@ -1,10 +1,30 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <cerrno>
|
||||||
|
#include <fstream>
|
||||||
|
#include <sstream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include <glad/glad.h>
|
#include <glad/glad.h>
|
||||||
|
|
||||||
#include <sstream>
|
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<std::string> &sources) {
|
void utilShaderSource(GLuint shader, const std::vector<std::string> &sources) {
|
||||||
char const *ptrs[sources.size()];
|
char const *ptrs[sources.size()];
|
||||||
@@ -39,4 +59,47 @@ std::string utilInfo() {
|
|||||||
<< " OpenGL version: " << glGetString(GL_VERSION) << std::endl
|
<< " OpenGL version: " << glGetString(GL_VERSION) << std::endl
|
||||||
<< " Shading version: " << glGetString(GL_SHADING_LANGUAGE_VERSION) << std::endl;
|
<< " Shading version: " << glGetString(GL_SHADING_LANGUAGE_VERSION) << std::endl;
|
||||||
return ss.str();
|
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<std::string> &files) {
|
||||||
|
std::vector<std::string> 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<GLuint> &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));
|
||||||
}
|
}
|
||||||
12
vis/shaders/one-color.fs.glsl
Normal file
12
vis/shaders/one-color.fs.glsl
Normal file
@@ -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);
|
||||||
|
}
|
||||||
15
vis/shaders/ortho.vs.glsl
Normal file
15
vis/shaders/ortho.vs.glsl
Normal file
@@ -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;
|
||||||
|
}
|
||||||
15
vis/shaders/stereo.vs.glsl
Normal file
15
vis/shaders/stereo.vs.glsl
Normal file
@@ -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;
|
||||||
|
}
|
||||||
11
vis/shaders/w-axis-hue.fs.glsl
Normal file
11
vis/shaders/w-axis-hue.fs.glsl
Normal file
@@ -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);
|
||||||
|
}
|
||||||
@@ -44,78 +44,22 @@ int main(int argc, char *argv[]) {
|
|||||||
|
|
||||||
std::cout << utilInfo();
|
std::cout << utilInfo();
|
||||||
|
|
||||||
const std::string VS_SOURCE =
|
GLuint pgm;
|
||||||
"#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;"
|
|
||||||
"}";
|
|
||||||
|
|
||||||
const std::string FS_SOURCE =
|
try {
|
||||||
"#version 430\n"
|
GLuint vs = utilCompileFiles(GL_VERTEX_SHADER, {"shaders/ortho.vs.glsl"});
|
||||||
"layout(location=2) uniform vec3 c;"
|
GLuint fs = utilCompileFiles(GL_FRAGMENT_SHADER, {"shaders/w-axis-hue.fs.glsl"});
|
||||||
""
|
|
||||||
"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);"
|
|
||||||
"}";
|
|
||||||
|
|
||||||
//region init shaders
|
pgm = utilLinkProgram({vs, fs});
|
||||||
GLuint vs = glCreateShader(GL_VERTEX_SHADER);
|
} catch (const gl_error &e) {
|
||||||
utilShaderSource(vs, {VS_SOURCE});
|
std::cerr << e.what() << std::endl;
|
||||||
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;
|
|
||||||
glfwTerminate();
|
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 res = group.solve();
|
||||||
auto mirrors = mirror(group);
|
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 corners = plane_intersections(mirrors);
|
||||||
auto start = barycentric(corners, {1.00, 1.00, 1.00, 1.00});
|
auto start = barycentric(corners, {1.00, 1.00, 1.00, 1.00});
|
||||||
|
|||||||
Reference in New Issue
Block a user