diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 5aaae27..8f68652 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -2,4 +2,5 @@ add_executable(memo memotest.cpp) target_link_libraries(memo PRIVATE tc vis-util) add_executable(geom geomtest.cpp) -target_link_libraries(geom PRIVATE tc vis-util) \ No newline at end of file +target_link_libraries(geom PRIVATE tc vis-util) + diff --git a/vis/CMakeLists.txt b/vis/CMakeLists.txt index 488387d..12f2dd9 100644 --- a/vis/CMakeLists.txt +++ b/vis/CMakeLists.txt @@ -1,7 +1,3 @@ -add_executable(vis src/main.cpp) -target_include_directories(vis PRIVATE include) -target_link_libraries(vis PRIVATE tc glad glm glfw) - add_custom_target(shaders ALL DEPENDS shader_output) add_custom_command( OUTPUT shader_output @@ -9,7 +5,10 @@ add_custom_command( COMMENT "copied shaders" ) -add_dependencies(vis shaders) - add_library(vis-util INTERFACE) target_include_directories(vis-util INTERFACE include) + +add_executable(vis src/main.cpp) +target_include_directories(vis PRIVATE include) +target_link_libraries(vis PRIVATE tc glad glm glfw) +add_dependencies(vis shaders) diff --git a/vis/include/util.hpp b/vis/include/util.hpp index 0019c21..49ec0d1 100644 --- a/vis/include/util.hpp +++ b/vis/include/util.hpp @@ -29,6 +29,26 @@ public: explicit program_error(const char *string) : gl_error(string) {} }; +template +T utilGetShader(GLuint shader) { + GLint res; + glGetShaderiv(shader, prop, &res); + return static_cast(res); +} + +#define getShaderInfoLogLength utilGetShader +#define getShaderCompileStatus utilGetShader + +template +T utilGetProgram(GLuint program) { + GLint res; + glGetProgramiv(program, prop, &res); + return static_cast(res); +} + +#define getProgramInfoLogLength utilGetProgram +#define getProgramLinkStatus utilGetProgram + void utilShaderSource(GLuint shader, const std::vector &sources) { char const *ptrs[sources.size()]; for (size_t i = 0; i < sources.size(); ++i) { @@ -37,17 +57,15 @@ void utilShaderSource(GLuint shader, const std::vector &sources) { glShaderSource(shader, sources.size(), ptrs, nullptr); } -std::string utilShaderInfoLog(GLuint shader) { - int len; - glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &len); +std::string getShaderInfoLog(GLuint shader) { + int len = getShaderInfoLogLength(shader); char buffer[len]; glGetShaderInfoLog(shader, len, nullptr, buffer); return std::string(buffer); } -std::string utilProgramInfoLog(GLuint program) { - int len; - glGetProgramiv(program, GL_INFO_LOG_LENGTH, &len); +std::string getProgramInfoLog(GLuint program) { + int len = getProgramInfoLogLength(program); char buffer[len]; glGetProgramInfoLog(program, len, nullptr, buffer); return std::string(buffer); @@ -86,11 +104,9 @@ GLuint utilCompileFiles(const GLenum type, const std::vector &files utilShaderSource(shader, sources); glCompileShader(shader); - int success; - glGetShaderiv(shader, GL_COMPILE_STATUS, &success); - if (success) return shader; + if (getShaderCompileStatus(shader)) return shader; - throw shader_error(utilShaderInfoLog(shader)); + throw shader_error(getShaderInfoLog(shader)); } GLuint utilLinkProgram(const std::vector &shaders) { @@ -100,9 +116,26 @@ GLuint utilLinkProgram(const std::vector &shaders) { } glLinkProgram(program); - int success; - glGetProgramiv(program, GL_LINK_STATUS, &success); - if (success)return program; + if (getProgramLinkStatus(program)) return program; - throw program_error(utilProgramInfoLog(program)); + throw program_error(getProgramInfoLog(program)); +} + +GLuint utilCreateShaderProgram(GLenum type, const std::vector &src) { + std::vector c_str(src.size()); + std::transform(src.begin(), src.end(), c_str.begin(), [](auto &str) { + return str.c_str(); + }); + + GLuint program = glCreateShaderProgramv(type, src.size(), &c_str[0]); + + if (getProgramLinkStatus(program)) return program; + + throw program_error(getProgramInfoLog(program)); +} + +GLuint utilCreateShaderProgramFile(GLenum type, const std::vector &files) { + std::vector sources(files.size()); + std::transform(files.begin(), files.end(), sources.begin(), utilReadFile); + return utilCreateShaderProgram(type, sources); } diff --git a/vis/shaders/ortho.vs.glsl b/vis/shaders/ortho.vs.glsl index dbaf89d..94accfa 100644 --- a/vis/shaders/ortho.vs.glsl +++ b/vis/shaders/ortho.vs.glsl @@ -1,5 +1,9 @@ #version 430 +out gl_PerVertex { + vec4 gl_Position; +}; + layout(location=0) uniform mat4 proj; layout(location=1) uniform mat4 view; @@ -8,8 +12,6 @@ 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/src/main.cpp b/vis/src/main.cpp index ec1a6c1..302542c 100644 --- a/vis/src/main.cpp +++ b/vis/src/main.cpp @@ -18,13 +18,6 @@ __attribute__((unused)) __declspec(dllexport) int NvOptimusEnablement = 0x000000 } #endif -float floatmod(float x, float m) { - if (x < 0) { - x += (float)((int)((-x)/m) + 1)*m; - } - return x - ((float)((int)(x / m)))*m; -} - int main(int argc, char *argv[]) { //region init window if (!glfwInit()) { @@ -50,24 +43,17 @@ int main(int argc, char *argv[]) { std::cout << utilInfo(); - GLuint pgm; + GLuint pipe; + glCreateProgramPipelines(1, &pipe); + + GLuint vs, fs; try { -// GLuint vs = utilCompileFiles(GL_VERTEX_SHADER, {"shaders/ortho.vs.glsl"}); -// GLuint vs = utilCompileFiles(GL_VERTEX_SHADER, {"shaders/stereo.vs.glsl"}); -// GLuint fs = utilCompileFiles(GL_FRAGMENT_SHADER, {"shaders/one-color.fs.glsl"}); -// GLuint fs = utilCompileFiles(GL_FRAGMENT_SHADER, {"shaders/w-axis-hue.fs.glsl"}); + vs = utilCreateShaderProgramFile(GL_VERTEX_SHADER, {"shaders/ortho.vs.glsl"}); + fs = utilCreateShaderProgramFile(GL_FRAGMENT_SHADER, {"shaders/one-color.fs.glsl"}); -// pgm = utilLinkProgram({vs, fs}); - - pgm = utilLinkProgram({ - utilCompileFiles(GL_VERTEX_SHADER, {"shaders/stereo-proper.vs.glsl"}), -// utilCompileFiles(GL_VERTEX_SHADER, {"shaders/ortho.vs.glsl"}), -// utilCompileFiles(GL_VERTEX_SHADER, {"shaders/stereo.vs.glsl"}), -// utilCompileFiles(GL_GEOMETRY_SHADER, {"shaders/stereo-proper.gm.glsl"}), - utilCompileFiles(GL_FRAGMENT_SHADER, {"shaders/one-color.fs.glsl"}), -// utilCompileFiles(GL_FRAGMENT_SHADER, {"shaders/w-axis-hue.fs.glsl"}) - }); + glUseProgramStages(pipe, GL_VERTEX_SHADER_BIT, vs); + glUseProgramStages(pipe, GL_FRAGMENT_SHADER_BIT, fs); } catch (const gl_error &e) { std::cerr << e.what() << std::endl; glfwTerminate(); @@ -75,16 +61,12 @@ int main(int argc, char *argv[]) { } auto group = tc::group::H(3); -// auto group = tc::group::B(5); -// auto group = tc::group::A(5); GeomGen gg(group); auto res = gg.solve(); auto mirrors = mirror(group); auto corners = plane_intersections(mirrors); -// auto start = barycentric(corners, {.75f, .75f, .75f, .75f}); auto start = barycentric(corners, {1.00f, 0.1f, 0.01f, 0.005f}); -// auto start = barycentric(corners, {1, 1, 0.05, 1}); auto points = res.path.walk(start, mirrors, reflect); GLuint vbo; @@ -128,11 +110,11 @@ int main(int argc, char *argv[]) { float _ts[numts]; float _ts_temp[numts]; for (int i = 0; i < numts; ++i) { - _ts[i] = ((float)i - (numts/2.f))*0.1f; + _ts[i] = ((float) i - (numts / 2.f)) * 0.1f; } - float* ts = _ts; - float* ts_temp = _ts_temp; - float* swap_t; + float *ts = _ts; + float *ts_temp = _ts_temp; + float *swap_t; float alpha = 0.0001; float beta = 0.0002; @@ -143,7 +125,7 @@ int main(int argc, char *argv[]) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - glUseProgram(pgm); + glBindProgramPipeline(pipe); glEnable(GL_PROGRAM_POINT_SIZE); glEnable(GL_POINT_SMOOTH); glEnable(GL_DEPTH_TEST); @@ -154,19 +136,16 @@ int main(int argc, char *argv[]) { auto aspect = (float) width / (float) height; auto pheight = 1.4f; auto pwidth = aspect * pheight; -// glm::mat4 proj = glm::ortho(-pwidth, pwidth, -pheight, pheight, -2.0f, 2.0f); glm::mat4 proj = glm::ortho(-pwidth, pwidth, -pheight, pheight, -10.0f, 10.0f); - glUniformMatrix4fv(0, 1, false, glm::value_ptr(proj)); + glProgramUniformMatrix4fv(vs, 0, 1, false, glm::value_ptr(proj)); auto st = (float) glfwGetTime() / 8; auto t = st / 7; -// printf("ts: [%f", ts[0]); - ts_temp[0] = ts[0] + alpha * (float)std::cos(rates[0] * t) + beta * (-ts[0]); - for (size_t i = 1; i < numts; i++){ -// printf( ", %f", ts[i]); - ts_temp[i] = ts[i] + alpha * (float)std::cos(rates[i] * t) + beta * (-ts[i]); + ts_temp[0] = ts[0] + alpha * (float) std::cos(rates[0] * t) + beta * (-ts[0]); + for (size_t i = 1; i < numts; i++) { + ts_temp[i] = ts[i] + alpha * (float) std::cos(rates[i] * t) + beta * (-ts[i]); } -// printf("]\n"); + swap_t = ts; ts = ts_temp; ts_temp = swap_t; @@ -177,13 +156,12 @@ int main(int argc, char *argv[]) { view *= utilRotate(1, 2, st * ts[3]); // view *= utilRotate(1, 3, st * ts[4]); // view *= utilRotate(2, 3, st * ts[5]); - glUniformMatrix4fv(1, 1, false, glm::value_ptr(view)); + glProgramUniformMatrix4fv(vs, 1, 1, false, glm::value_ptr(view)); //endregion - glUniform3f(2, 1.0f, 1.0f, 1.0f); // glDrawArrays(GL_POINTS, 0, points.size()); - glUniform3f(2, 1.0f, 1.0f, 1.0f); + glProgramUniform3f(fs, 2, 1.0f, 1.0f, 1.0f); for (int i = 0; i < group.ngens; ++i) { auto ibo = edge_ibo[i]; auto count = edge_count[i];