Migrate to NanoGUI / Eigen for GUI and linear algebra.

Also introduce a GUI play/pause button.
This commit is contained in:
David Allemang
2020-10-10 22:59:51 -04:00
parent 5e3b4defd7
commit 916e9a8906
13 changed files with 640 additions and 331 deletions

View File

@@ -6,13 +6,13 @@ set(CMAKE_CXX_STANDARD 17)
add_subdirectory(vendor/toddcox) add_subdirectory(vendor/toddcox)
add_subdirectory(vendor/yaml-cpp) add_subdirectory(vendor/yaml-cpp)
add_subdirectory(vendor/cgl)
#add_subdirectory(vendor/glfw)
set(NANOGUI_BUILD_EXAMPLE OFF CACHE BOOL " " FORCE) set(NANOGUI_BUILD_EXAMPLE OFF CACHE BOOL " " FORCE)
set(NANOGUI_BUILD_PYTHON OFF CACHE BOOL " " FORCE) set(NANOGUI_BUILD_PYTHON OFF CACHE BOOL " " FORCE)
set(NANOGUI_INSTALL OFF CACHE BOOL " " FORCE) set(NANOGUI_INSTALL OFF CACHE BOOL " " FORCE)
set(NANOGUI_USE_GLAD ON CACHE BOOL " " FORCE)
add_subdirectory(vendor/nanogui) add_subdirectory(vendor/nanogui)
set_property(TARGET glfw glfw_objects nanogui PROPERTY FOLDER "dependencies")
add_subdirectory(vis) add_subdirectory(vis)

1
vendor/cgl vendored

Submodule vendor/cgl deleted from a879278dd0

View File

@@ -12,12 +12,10 @@ add_custom_command(
COMMENT "copied preses" COMMENT "copied preses"
) )
add_executable(vis src/main.cpp) add_definitions(${NANOGUI_EXTRA_DEFS})
target_include_directories(vis PRIVATE include) include_directories(${NANOGUI_EXTRA_INCS})
target_link_libraries(vis PRIVATE tc glfw yaml-cpp cgl glad)
add_dependencies(vis shaders presets)
add_executable(vis-gui src/main-gui.cpp) add_executable(vis-gui src/main-gui.cpp)
target_include_directories(vis-gui PRIVATE include) target_include_directories(vis-gui PRIVATE include)
target_link_libraries(vis-gui PRIVATE tc nanogui) target_link_libraries(vis-gui PRIVATE tc nanogui yaml-cpp ${NANOGUI_EXTRA_LIBS})
add_dependencies(vis-gui shaders presets) add_dependencies(vis-gui shaders presets)

View File

@@ -0,0 +1,78 @@
#pragma once
#include <memory>
#include <nanogui/opengl.h>
namespace cgl {
template<class T>
class Buffer {
GLuint id{};
public:
Buffer() {
glCreateBuffers(1, &id);
}
Buffer(const T &data, GLenum usage = GL_STATIC_DRAW)
: Buffer() {
put(data, usage);
}
Buffer(const std::vector<T> &data, GLenum usage = GL_STATIC_DRAW)
: Buffer() {
put(data, usage);
}
Buffer(const Buffer &) = delete;
Buffer(Buffer &&o) noexcept {
id = std::exchange(o.id, 0);
};
~Buffer() {
glDeleteBuffers(1, &id);
id = 0;
}
operator GLuint() const {
return id;
}
[[nodiscard]] size_t size() const {
GLint res;
glGetNamedBufferParameteriv(id, GL_BUFFER_SIZE, &res);
return (size_t) res;
}
[[nodiscard]] size_t count() const {
return size() / sizeof(T);
}
void put(const T &data, GLenum usage = GL_STATIC_DRAW) {
glNamedBufferData(id, sizeof(T), &data, usage);
}
void put(const std::vector<T> &data, GLenum usage = GL_STATIC_DRAW) {
glNamedBufferData(id, sizeof(T) * data.size(), &data[0], usage);
}
void bound(GLenum target, const std::function<void()> &action) const {
glBindBuffer(target, id);
action();
glBindBuffer(target, 0);
}
std::vector<T> getSubData(size_t offset, size_t count) const {
GLintptr glOffset = offset * sizeof(T);
GLsizeiptr glSize = count * sizeof(T);
std::vector<T> data(count);
glad_glGetNamedBufferSubData(id, glOffset, glSize, data.data());
return data;
}
};
}

28
vis/include/cgl/error.hpp Normal file
View File

@@ -0,0 +1,28 @@
#pragma once
#include <stdexcept>
#include <nanogui/opengl.h>
namespace cgl {
class GlError : public std::domain_error {
public:
explicit GlError(const std::string &arg) : domain_error(arg) {}
explicit GlError(const char *string) : domain_error(string) {}
};
class ShaderError : public GlError {
public:
explicit ShaderError(const std::string &arg) : GlError(arg) {}
explicit ShaderError(const char *string) : GlError(string) {}
};
class ProgramError : public GlError {
public:
explicit ProgramError(const std::string &arg) : GlError(arg) {}
explicit ProgramError(const char *string) : GlError(string) {}
};
}

View File

@@ -0,0 +1,93 @@
#pragma once
#include <functional>
#include <string>
#include <utility>
#include <nanogui/opengl.h>
#include <cgl/error.hpp>
#include <cgl/shaderprogram.hpp>
#include <util.hpp>
namespace cgl {
class pipeline {
protected:
GLuint id{};
public:
pipeline() {
glCreateProgramPipelines(1, &id);
}
pipeline(pipeline &) = delete;
pipeline(pipeline &&o) noexcept {
id = std::exchange(o.id, 0);
}
~pipeline() {
glDeleteProgramPipelines(1, &id);
id = 0;
}
operator GLuint() const {
return id;
}
[[nodiscard]] int get(GLenum pname) const {
GLint res;
glGetProgramPipelineiv(id, pname, &res);
return (int) res;
}
[[nodiscard]] std::string get_info_log() const {
auto len = (size_t) get(GL_INFO_LOG_LENGTH);
char buffer[len];
glGetProgramPipelineInfoLog(id, len, nullptr, buffer);
return std::string(buffer);
}
pipeline &unstage(GLenum stage_bits) {
glUseProgramStages(id, stage_bits, 0);
return *this;
}
pipeline &stage(const ShaderProgram<GL_VERTEX_SHADER> &pgm) {
glUseProgramStages(id, GL_VERTEX_SHADER_BIT, pgm);
return *this;
}
pipeline &stage(const ShaderProgram<GL_TESS_CONTROL_SHADER> &pgm) {
glUseProgramStages(id, GL_TESS_CONTROL_SHADER_BIT, pgm);
return *this;
}
pipeline &stage(const ShaderProgram<GL_TESS_EVALUATION_SHADER> &pgm) {
glUseProgramStages(id, GL_TESS_EVALUATION_SHADER_BIT, pgm);
return *this;
}
pipeline &stage(const ShaderProgram<GL_GEOMETRY_SHADER> &pgm) {
glUseProgramStages(id, GL_GEOMETRY_SHADER_BIT, pgm);
return *this;
}
pipeline &stage(const ShaderProgram<GL_FRAGMENT_SHADER> &pgm) {
glUseProgramStages(id, GL_FRAGMENT_SHADER_BIT, pgm);
return *this;
}
pipeline &stage(const ShaderProgram<GL_COMPUTE_SHADER> &pgm) {
glUseProgramStages(id, GL_COMPUTE_SHADER_BIT, pgm);
return *this;
}
void bound(const std::function<void()> &action) const {
glBindProgramPipeline(id);
action();
glBindProgramPipeline(0);
}
};
}

View File

@@ -0,0 +1,65 @@
#pragma once
#include <string>
#include <utility>
#include <nanogui/opengl.h>
#include <cgl/error.hpp>
#include <cgl/shader.hpp>
#include <util.hpp>
namespace cgl {
class Program {
protected:
GLuint id{};
public:
Program() {
id = glCreateProgram();
}
Program(Program &) = delete;
Program(Program &&o) noexcept {
id = std::exchange(o.id, 0);
};
~Program() {
glDeleteProgram(id);
}
operator GLuint() const {
return id;
}
[[nodiscard]] int get(GLenum pname) const {
GLint res;
glGetProgramiv(id, pname, &res);
return (int) res;
}
[[nodiscard]] std::string get_info_log() const {
auto len = (size_t) get(GL_INFO_LOG_LENGTH);
char buffer[len];
glGetProgramInfoLog(id, len, nullptr, buffer);
return std::string(buffer);
}
template<GLenum mode>
void attach(const Shader<mode> &sh) {
glAttachShader(id, sh);
}
template<GLenum mode>
void detach(const Shader<mode> &sh) {
glDetachShader(id, sh);
}
bool link() {
glLinkProgram(id);
return (bool) get(GL_LINK_STATUS);
}
};
}

View File

@@ -0,0 +1,80 @@
#pragma once
#include <string>
#include <utility>
#include <nanogui/opengl.h>
#include <cgl/error.hpp>
#include <util.hpp>
namespace cgl {
template<GLenum mode>
class Shader {
protected:
GLuint id{};
public:
Shader() {
id = glCreateShader(mode);
}
Shader(const std::string &src) : Shader() {
set_source(src);
if (!compile())
throw ShaderError(get_info_log());
}
static Shader<mode> file(const std::string &name) {
return Shader<mode>(utilReadFile(name));
}
Shader(Shader &) = delete;
Shader(Shader &&o) noexcept {
id = std::exchange(o.id, 0);
};
~Shader() {
glDeleteShader(id);
}
operator GLuint() const {
return id;
}
[[nodiscard]] int get(GLenum pname) const {
GLint res;
glGetShaderiv(id, pname, &res);
return (int) res;
}
[[nodiscard]] std::string get_info_log() const {
auto len = (size_t) get(GL_INFO_LOG_LENGTH);
char buffer[len];
glGetShaderInfoLog(id, len, nullptr, buffer);
return std::string(buffer);
}
void set_source(const std::string &src) {
const char *c_src = src.c_str();
glShaderSource(id, 1, &c_src, nullptr);
}
bool compile() {
glCompileShader(id);
return (bool) get(GL_COMPILE_STATUS);
}
};
namespace sh {
using vert = Shader<GL_VERTEX_SHADER>;
using tcs = Shader<GL_TESS_CONTROL_SHADER>;
using tes = Shader<GL_TESS_EVALUATION_SHADER>;
using geom = Shader<GL_GEOMETRY_SHADER>;
using frag = Shader<GL_FRAGMENT_SHADER>;
using comp = Shader<GL_COMPUTE_SHADER>;
}
}

View File

@@ -0,0 +1,46 @@
#pragma once
#include <string>
#include <utility>
#include <nanogui/opengl.h>
#include <cgl/error.hpp>
#include <cgl/shader.hpp>
#include <cgl/program.hpp>
#include <util.hpp>
namespace cgl{
template<GLenum mode>
class ShaderProgram : public Program {
public:
ShaderProgram() : Program() {
glProgramParameteri(id, GL_PROGRAM_SEPARABLE, GL_TRUE);
}
ShaderProgram(const std::string &src) : ShaderProgram() {
Shader<mode> sh(src);
attach(sh);
if (!link())
throw ShaderError(get_info_log());
detach(sh);
}
static ShaderProgram<mode> file(const std::string &name) {
return ShaderProgram<mode>(utilReadFile(name));
}
};
namespace pgm {
using vert = ShaderProgram<GL_VERTEX_SHADER>;
using tcs = ShaderProgram<GL_TESS_CONTROL_SHADER>;
using tes = ShaderProgram<GL_TESS_EVALUATION_SHADER>;
using geom = ShaderProgram<GL_GEOMETRY_SHADER>;
using frag = ShaderProgram<GL_FRAGMENT_SHADER>;
using comp = ShaderProgram<GL_COMPUTE_SHADER>;
}
}

View File

@@ -0,0 +1,75 @@
#pragma once
#include <functional>
#include <string>
#include <utility>
#include <nanogui/opengl.h>
#include <cgl/error.hpp>
#include <cgl/buffer.hpp>
namespace cgl {
class VertexArray {
GLuint id{};
public:
VertexArray() {
glCreateVertexArrays(1, &id);
}
VertexArray(VertexArray &) = delete;
VertexArray(VertexArray &&o) noexcept {
id = std::exchange(o.id, 0);
}
~VertexArray() {
glDeleteVertexArrays(1, &id);
id = 0;
}
operator GLuint() const {
return id;
}
void bound(const std::function<void()> &action) const {
glBindVertexArray(id);
action();
glBindVertexArray(0);
}
template<class T>
void pointer(
GLuint index,
const Buffer<T> &buf,
unsigned size,
GLenum type,
bool normalized = false,
unsigned stride = 0
) const {
bound([&]() {
glEnableVertexAttribArray(index);
buf.bound(GL_ARRAY_BUFFER, [&]() {
glVertexAttribPointer(index, size, type, normalized, stride, nullptr);
});
});
}
template<class T>
void ipointer(
GLuint index,
const Buffer<T> &buf,
unsigned size,
GLenum type,
unsigned stride = 0
) const {
bound([&]() {
glEnableVertexAttribArray(index);
buf.bound(GL_ARRAY_BUFFER, [&]() {
glVertexAttribIPointer(index, size, type, stride, nullptr);
});
});
}
};
}

View File

@@ -6,8 +6,12 @@
#include <vector> #include <vector>
#include <algorithm> #include <algorithm>
template<unsigned N> #include <nanogui/glutil.h>
using vec = std::array<float, N>;
template<int N>
using vec = Eigen::Matrix<float, N, 1>;
template<int N>
using mat = Eigen::Matrix<float, N, N>;
using vec1 = vec<1>; using vec1 = vec<1>;
using vec2 = vec<2>; using vec2 = vec<2>;
@@ -15,75 +19,12 @@ using vec3 = vec<3>;
using vec4 = vec<4>; using vec4 = vec<4>;
using vec5 = vec<5>; using vec5 = vec<5>;
template<unsigned N>
using mat = std::array<std::array<float, N>, N>;
using mat1 = mat<1>; using mat1 = mat<1>;
using mat2 = mat<2>; using mat2 = mat<2>;
using mat3 = mat<3>; using mat3 = mat<3>;
using mat4 = mat<4>; using mat4 = mat<4>;
using mat5 = mat<5>; using mat5 = mat<5>;
template<class V>
V operator*(V a, const float &b) {
for (auto &e : a) e *= b;
return a;
}
template<class V>
V operator*(const float &b, V a) {
for (auto &e : a) e *= b;
return a;
}
template<class V>
V operator/(V a, const float &b) {
for (auto &e : a) e /= b;
return a;
}
template<class V>
V operator+(const V &a, V b) {
for (int i = 0; i < a.size(); ++i) {
a[i] += b[i];
}
return a;
}
template<class V>
V operator-(V a, const V &b) {
for (int i = 0; i < a.size(); ++i) {
a[i] -= b[i];
}
return a;
}
template<class V>
void operator-=(V &a, const V &b) {
for (int i = 0; i < a.size(); ++i) {
a[i] -= b[i];
}
}
template<class V>
void operator+=(V &a, const V &b) {
for (int i = 0; i < a.size(); ++i) {
a[i] += b[i];
}
}
template<class V>
float length(const V &a) {
float sum = 0;
for (const auto &e : a) sum += e * e;
return sqrtf(sum);
}
template<class V>
V normalized(const V &a) {
return a / length(a);
}
template<class V> template<class V>
float dot(int n, const V &a, const V &b) { float dot(int n, const V &a, const V &b) {
float sum = 0; float sum = 0;
@@ -93,39 +34,6 @@ float dot(int n, const V &a, const V &b) {
return sum; return sum;
} }
template<class V>
float dot(const V &a, const V &b) {
float sum = 0;
for (int i = 0; i < a.size(); ++i) {
sum += a[i] * b[i];
}
return sum;
}
vec5 mul(vec5 v, mat5 m) {
vec5 r{};
for (int i = 0; i < 5; ++i)
for (int j = 0; j < 5; ++j)
r[i] += m[i][j] * v[j];
return r;
}
mat5 mul(mat5 a, mat5 b) {
mat5 r{};
for (int i = 0; i < 5; ++i) {
for (int j = 0; j < 5; ++j) {
for (int k = 0; k < 5; ++k) {
r[i][j] += a[i][k] * b[k][j];
}
}
}
return r;
}
template<unsigned N> template<unsigned N>
std::vector<vec<N>> mirror(const tc::Group &group) { std::vector<vec<N>> mirror(const tc::Group &group) {
std::vector<std::vector<float>> mirrors; std::vector<std::vector<float>> mirrors;
@@ -152,7 +60,7 @@ std::vector<vec<N>> mirror(const tc::Group &group) {
std::vector<vec<N>> res; std::vector<vec<N>> res;
for (const auto &v : mirrors) { for (const auto &v : mirrors) {
vec<N> rv{}; vec<N> rv = vec<N>::Zero();
// ortho proj // ortho proj
for (int i = 0; i < std::min(v.size(), (size_t) N); ++i) { for (int i = 0; i < std::min(v.size(), (size_t) N); ++i) {
@@ -184,7 +92,7 @@ vec<N> ortho(const vec<N + 1> &v) {
template<class V> template<class V>
V project(const V &vec, const V &target) { V project(const V &vec, const V &target) {
return dot(vec, target) / dot(target, target) * target; return vec.dot(target) / target.dot(target) * target;
} }
template<class V> template<class V>
@@ -200,14 +108,14 @@ V gram_schmidt_last(std::vector<V> vecs) {
} }
} }
return normalized(vecs[vecs.size() - 1]); return vecs[vecs.size() - 1].normalized();
} }
template<class V, class C> template<class V, class C>
V barycentric(const std::vector<V> &basis, const C &coords) { V barycentric(const std::vector<V> &basis, const C &coords) {
V res{}; V res = V::Zero();
int N = std::min(basis.size(), coords.size()); int N = std::min((int) basis.size(), (int) coords.rows());
for (int i = 0; i < N; ++i) { for (int i = 0; i < N; ++i) {
res += basis[i] * coords[i]; res += basis[i] * coords[i];
} }
@@ -226,29 +134,22 @@ std::vector<V> plane_intersections(std::vector<V> normals) {
return results; return results;
} }
template<unsigned N>
mat<N> identity() {
mat<N> res{};
for (int i = 0; i < N; ++i)
res[i][i] = 1;
return res;
}
template<unsigned N> template<unsigned N>
mat<N> rot(int u, int v, float theta) { mat<N> rot(int u, int v, float theta) {
auto res = identity<N>(); mat<N> res = mat<N>::Identity();
res[u][u] = std::cos(theta); res(u, u) = std::cos(theta);
res[u][v] = std::sin(theta); res(u, v) = std::sin(theta);
res[v][u] = -std::sin(theta); res(v, u) = -std::sin(theta);
res[v][v] = std::cos(theta); res(v, v) = std::cos(theta);
return res; return res;
} }
mat4 ortho(float left, float right, float bottom, float top, float front, float back) { mat4 ortho(float left, float right, float bottom, float top, float front, float back) {
return { mat<4> res = mat4();
res <<
2 / (right - left), 0, 0, -(right + left) / (right - left), 2 / (right - left), 0, 0, -(right + left) / (right - left),
0, 2 / (top - bottom), 0, -(top + bottom) / (top - bottom), 0, 2 / (top - bottom), 0, -(top + bottom) / (top - bottom),
0, 0, 2 / (front - back), -(front + back) / (front - back), 0, 0, 2 / (front - back), -(front + back) / (front - back),
0, 0, 0, 1, 0, 0, 0, 1;
}; return res;
} }

View File

@@ -1,13 +1,13 @@
#pragma once #pragma once
#include <nanogui/opengl.h>
#include <cerrno> #include <cerrno>
#include <fstream> #include <fstream>
#include <sstream> #include <sstream>
#include <string> #include <string>
#include <vector> #include <vector>
#include <glad/glad.h>
std::string utilInfo() { std::string utilInfo() {
std::stringstream ss; std::stringstream ss;
ss ss
@@ -19,6 +19,10 @@ std::string utilInfo() {
return ss.str(); return ss.str();
} }
std::string utilGetString(GLenum name) {
return reinterpret_cast<char const*>(glGetString(name));
}
std::string utilReadFile(const std::string &filename) { std::string utilReadFile(const std::string &filename) {
std::ifstream in(filename, std::ios::in | std::ios::binary); std::ifstream in(filename, std::ios::in | std::ios::binary);
if (in) { if (in) {

View File

@@ -12,236 +12,178 @@
*/ */
#include <nanogui/opengl.h> #include <nanogui/opengl.h>
#include <nanogui/nanogui.h>
#include <nanogui/glutil.h> #include <nanogui/glutil.h>
#include <nanogui/screen.h>
#include <nanogui/window.h>
#include <nanogui/layout.h>
#include <nanogui/label.h>
#include <nanogui/checkbox.h>
#include <nanogui/button.h>
#include <nanogui/toolbutton.h>
#include <nanogui/popupbutton.h>
#include <nanogui/combobox.h>
#include <nanogui/progressbar.h>
#include <nanogui/entypo.h>
#include <nanogui/messagedialog.h>
#include <nanogui/textbox.h>
#include <nanogui/slider.h>
#include <nanogui/imagepanel.h>
#include <nanogui/imageview.h>
#include <nanogui/vscrollpanel.h>
#include <nanogui/colorwheel.h>
#include <nanogui/graph.h>
#include <nanogui/tabwidget.h>
#include <nanogui/glcanvas.h>
#include <iostream> #include <iostream>
#include <string> #include <string>
// Includes for the GLTexture class. #include <geometry.hpp>
#include <cstdint> #include <solver.hpp>
#include <memory> #include <rendering.hpp>
#include <utility> #include <mirror.hpp>
#include <util.hpp>
#include <tc/groups.hpp>
#if defined(__GNUC__) struct Matrices {
# pragma GCC diagnostic ignored "-Wmissing-field-initializers" mat4 proj = mat4::Identity();
#endif mat4 view = mat4::Identity();
#if defined(_WIN32)
# pragma warning(push)
# pragma warning(disable: 4457 4456 4005 4312)
#endif
#if defined(_WIN32) Matrices() = default;
# pragma warning(pop)
#endif
#if defined(_WIN32)
# if defined(APIENTRY)
# undef APIENTRY
# endif
# include <windows.h>
#endif
using std::cout; Matrices(mat4 proj, mat4 view) : proj(std::move(proj)), view(std::move(view)) {}
using std::cerr;
using std::endl;
using std::string;
using std::vector;
using std::pair;
using std::to_string;
static Matrices build(const nanogui::Screen &screen) {
auto aspect = (float) screen.width() / (float) screen.height();
auto pheight = 1.4f;
auto pwidth = aspect * pheight;
mat4 proj = ortho(-pwidth, pwidth, -pheight, pheight, -10.0f, 10.0f);
class MyGLCanvas : public nanogui::GLCanvas { // if (!glfwGetKey(window, GLFW_KEY_LEFT_SHIFT)) {
public: // state.st += state.time_delta / 8;
MyGLCanvas(Widget *parent) : nanogui::GLCanvas(parent), mRotation(nanogui::Vector3f(0.25f, 0.5f, 0.33f)) { // }
using namespace nanogui;
mShader.init( auto view = mat4::Identity();
/* An identifying name */ return Matrices(proj, view);
"a_simple_shader", }
/* Vertex shader */
"#version 330\n"
"uniform mat4 modelViewProj;\n"
"in vec3 position;\n"
"in vec3 color;\n"
"out vec4 frag_color;\n"
"void main() {\n"
" frag_color = 3.0 * modelViewProj * vec4(color, 1.0);\n"
" gl_Position = modelViewProj * vec4(position, 1.0);\n"
"}",
/* Fragment shader */
"#version 330\n"
"out vec4 color;\n"
"in vec4 frag_color;\n"
"void main() {\n"
" color = frag_color;\n"
"}"
);
MatrixXu indices(3, 12); /* Draw a cube */
indices.col( 0) << 0, 1, 3;
indices.col( 1) << 3, 2, 1;
indices.col( 2) << 3, 2, 6;
indices.col( 3) << 6, 7, 3;
indices.col( 4) << 7, 6, 5;
indices.col( 5) << 5, 4, 7;
indices.col( 6) << 4, 5, 1;
indices.col( 7) << 1, 0, 4;
indices.col( 8) << 4, 0, 3;
indices.col( 9) << 3, 7, 4;
indices.col(10) << 5, 6, 2;
indices.col(11) << 2, 1, 5;
MatrixXf positions(3, 8);
positions.col(0) << -1, 1, 1;
positions.col(1) << -1, 1, -1;
positions.col(2) << 1, 1, -1;
positions.col(3) << 1, 1, 1;
positions.col(4) << -1, -1, 1;
positions.col(5) << -1, -1, -1;
positions.col(6) << 1, -1, -1;
positions.col(7) << 1, -1, 1;
MatrixXf colors(3, 12);
colors.col( 0) << 1, 0, 0;
colors.col( 1) << 0, 1, 0;
colors.col( 2) << 1, 1, 0;
colors.col( 3) << 0, 0, 1;
colors.col( 4) << 1, 0, 1;
colors.col( 5) << 0, 1, 1;
colors.col( 6) << 1, 1, 1;
colors.col( 7) << 0.5, 0.5, 0.5;
colors.col( 8) << 1, 0, 0.5;
colors.col( 9) << 1, 0.5, 0;
colors.col(10) << 0.5, 1, 0;
colors.col(11) << 0.5, 1, 0.5;
mShader.bind();
mShader.uploadIndices(indices);
mShader.uploadAttrib("position", positions);
mShader.uploadAttrib("color", colors);
}
~MyGLCanvas() {
mShader.free();
}
void setRotation(nanogui::Vector3f vRotation) {
mRotation = vRotation;
}
virtual void drawGL() override {
using namespace nanogui;
mShader.bind();
Matrix4f mvp;
mvp.setIdentity();
float fTime = (float)glfwGetTime();
mvp.topLeftCorner<3,3>() = Eigen::Matrix3f(Eigen::AngleAxisf(mRotation[0]*fTime, Vector3f::UnitX()) *
Eigen::AngleAxisf(mRotation[1]*fTime, Vector3f::UnitY()) *
Eigen::AngleAxisf(mRotation[2]*fTime, Vector3f::UnitZ())) * 0.25f;
mShader.setUniform("modelViewProj", mvp);
glEnable(GL_DEPTH_TEST);
/* Draw 12 triangles starting at index 0 */
mShader.drawIndexed(GL_TRIANGLES, 0, 12);
glDisable(GL_DEPTH_TEST);
}
private:
nanogui::GLShader mShader;
Eigen::Vector3f mRotation;
}; };
template<class C>
std::vector<vec4> points(const tc::Group &group, const C &coords, const float time) {
auto cosets = group.solve();
auto mirrors = mirror<5>(group);
auto corners = plane_intersections(mirrors);
auto start = barycentric(corners, coords);
auto higher = cosets.path.walk<vec5, vec5>(start, mirrors, reflect<vec5>);
mat5 r = mat5::Identity();
r *= rot<5>(0, 2, time * .21f);
// r *= rot<5>(1, 4, time * .27f);
r *= rot<5>(0, 3, time * .17f);
r *= rot<5>(1, 3, time * .25f);
r *= rot<5>(2, 3, time * .12f);
std::transform(higher.begin(), higher.end(), higher.begin(), [&](vec5 v) { return r * v; });
std::vector<vec4> lower(higher.size());
std::transform(higher.begin(), higher.end(), lower.begin(), stereo<4>);
return lower;
}
template<int N, class T, class C>
Prop<4, vec4> make_slice(
const tc::Group &g,
const C &coords,
vec3 color,
T all_sg_gens,
const std::vector<std::vector<int>> &exclude
) {
Prop<N, vec4> res{};
// res.vbo.put(points(g, coords));
res.ibo.put(merge<N>(hull<N>(g, all_sg_gens, exclude)));
res.vao.ipointer(0, res.ibo, 4, GL_UNSIGNED_INT);
return res;
}
class ExampleApplication : public nanogui::Screen { class ExampleApplication : public nanogui::Screen {
public: public:
ExampleApplication() : nanogui::Screen(Eigen::Vector2i(800, 600), "NanoGUI Test", false) { vec5 root;
using namespace nanogui; std::unique_ptr<tc::Group> group;
std::unique_ptr<Prop<4, vec4>> prop;
std::unique_ptr<cgl::Buffer<Matrices>> ubo;
std::unique_ptr<SliceRenderer<4, vec4>> ren;
Window *window = new Window(this, "GLCanvas Demo"); float glfw_time = 0;
window->setPosition(Vector2i(15, 15)); float last_frame = 0;
window->setLayout(new GroupLayout()); float frame_time = 0;
float time = 0;
mCanvas = new MyGLCanvas(window); bool paused = false;
mCanvas->setBackgroundColor({100, 100, 100, 255});
mCanvas->setSize({400, 400});
Widget *tools = new Widget(window); ExampleApplication() : nanogui::Screen(
tools->setLayout(new BoxLayout(Orientation::Horizontal, Eigen::Vector2i(1920, 1080),
Alignment::Middle, 0, 5)); "Coset Visualization",
true, false,
8, 8, 24, 8,
4,
4, 5) {
using namespace nanogui;
Button *b0 = new Button(tools, "Random Color"); Window *window = new Window(this, "Sample Window");
b0->setCallback([this]() { mCanvas->setBackgroundColor(Vector4i(rand() % 256, rand() % 256, rand() % 256, 255)); }); window->setPosition(Vector2i(15, 15));
window->setFixedWidth(250);
window->setLayout(new BoxLayout(Orientation::Vertical));
Button *b1 = new Button(tools, "Random Rotation"); auto pause = new ToolButton(window, ENTYPO_ICON_CONTROLLER_PAUS);
b1->setCallback([this]() { mCanvas->setRotation(nanogui::Vector3f((rand() % 100) / 100.0f, (rand() % 100) / 100.0f, (rand() % 100) / 100.0f)); }); pause->setFlags(Button::ToggleButton);
pause->setChangeCallback([&](bool value) { this->paused = value; });
performLayout(); performLayout();
}
virtual bool keyboardEvent(int key, int scancode, int action, int modifiers) { std::cout << utilInfo();
if (Screen::keyboardEvent(key, scancode, action, modifiers))
return true; std::vector<int> symbol = {5, 3, 3, 2};
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) { root << .80, .02, .02, .02, .02;
setVisible(false);
return true; group = std::make_unique<tc::Group>(tc::schlafli(symbol));
auto gens = generators(*group);
std::vector<std::vector<int>> exclude = {{0, 1, 2}};
auto combos = Combos<int>(gens, 3);
prop = std::make_unique<Prop<4, vec4>>(make_slice<4>(*group, root, {}, combos, exclude));
ubo = std::make_unique<cgl::Buffer<Matrices>>();
glBindBufferBase(GL_UNIFORM_BUFFER, 1, *ubo);
ren = std::make_unique<SliceRenderer<4, vec4>>();
} }
return false;
}
virtual void draw(NVGcontext *ctx) { void drawContents() override {
/* Draw the user interface */ glEnable(GL_DEPTH_TEST);
Screen::draw(ctx);
} glEnable(GL_BLEND);
private: glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
MyGLCanvas *mCanvas;
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glViewport(0, 0, width(), height());
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glfw_time = (float) glfwGetTime();
frame_time = glfw_time - last_frame;
last_frame = glfw_time;
if (!paused) time += frame_time;
std::get<0>(prop->vbos).put(points(*group, root, time));
Matrices mats = Matrices::build(*this);
ubo->put(mats);
ren->draw(*prop);
}
}; };
int main(int /* argc */, char ** /* argv */) { int main(int /* argc */, char ** /* argv */) {
try { try {
nanogui::init(); nanogui::init();
/* scoped variables */ { /* scoped variables */ {
nanogui::ref<ExampleApplication> app = new ExampleApplication(); nanogui::ref<ExampleApplication> app = new ExampleApplication();
app->drawAll(); app->drawAll();
app->setVisible(true); app->setVisible(true);
nanogui::mainloop(); nanogui::mainloop(1);
}
nanogui::shutdown();
} catch (const std::runtime_error &e) {
std::string error_msg = std::string("Caught a fatal error: ") + std::string(e.what());
std::cerr << error_msg << std::endl;
return -1;
} }
nanogui::shutdown(); return 0;
} catch (const std::runtime_error &e) {
std::string error_msg = std::string("Caught a fatal error: ") + std::string(e.what());
#if defined(_WIN32)
MessageBoxA(nullptr, error_msg.c_str(), NULL, MB_ICONERROR | MB_OK);
#else
std::cerr << error_msg << endl;
#endif
return -1;
}
return 0;
} }