Add 4d navball navigation

This commit is contained in:
2022-02-16 10:01:52 -05:00
parent c302cc03a5
commit d3dad55801
3 changed files with 52 additions and 49 deletions

View File

@@ -2,39 +2,11 @@
layout(location=1) uniform float time; layout(location=1) uniform float time;
layout(location=2) uniform mat4 proj; layout(location=2) uniform mat4 proj;
layout(location=3) uniform mat4 rot;
layout(location=0) in vec4 pos; layout(location=0) in vec4 pos;
void main() { void main() {
float c2 = cos(time * 0.2); vec4 pos = rot * pos;
float s2 = sin(time * 0.2);
float c3 = cos(time * 0.3);
float s3 = sin(time * 0.3);
float c4 = cos(time * 0.25);
float s4 = sin(time * 0.25);
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
);
mat4 r3 = mat4(
1.0, 0.0, 0.0, 0.0,
0.0, c4, 0.0, -s4,
0.0, 0.0, 1.0, 0.0,
0.0, s4, 0.0, c4
);
vec4 pos = r2 * r1 * r3 * pos;
gl_Position = proj * vec4(pos.xyz / (1 - pos.w), 1.0); gl_Position = proj * vec4(pos.xyz / (1 - pos.w), 1.0);
} }

View File

@@ -2,17 +2,12 @@
layout(location=1) uniform float time; layout(location=1) uniform float time;
layout(location=2) uniform mat4 proj; layout(location=2) uniform mat4 proj;
layout(location=3) uniform mat4 rot;
layout(location=0) in vec3 pos; layout(location=0) in vec3 pos;
void main() { void main() {
float c2 = cos(time * 0.2); mat3 rot3 = mat3(rot);
float s2 = sin(time * 0.2); vec3 pos3 = rot3 * pos;
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); gl_Position = proj * vec4(pos3, 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

@@ -12,10 +12,21 @@
#include <ml/meshlib_json.hpp> #include <ml/meshlib_json.hpp>
struct State { struct State {
float bg[4] = {0.45f, 0.55f, 0.60f, 1.00f}; Eigen::Vector4f bg{0.45f, 0.55f, 0.60f, 1.00f};
float fg[4] = {0.19f, 0.86f, 0.33f, 1.00f}; Eigen::Vector4f fg{0.71f, 0.53f, 0.94f, 1.00f};
Eigen::Vector4f wf{0.95f, 0.95f, 0.95f, 1.00f};
Eigen::Matrix4f rot = Eigen::Matrix4f::Identity();
}; };
Eigen::Matrix4f rotor(int u, int v, float rad) {
Eigen::Matrix4f res = Eigen::Matrix4f::Identity();
res(u, u) = res(v, v) = cosf(rad);
res(u, v) = res(v, u) = sinf(rad);
res(u, v) *= -1;
return res;
}
void show_overlay(State &state) { void show_overlay(State &state) {
static std::string gl_vendor = (const char *) glGetString(GL_VENDOR); static std::string gl_vendor = (const char *) glGetString(GL_VENDOR);
static std::string gl_renderer = (const char *) glGetString(GL_RENDERER); static std::string gl_renderer = (const char *) glGetString(GL_RENDERER);
@@ -56,8 +67,31 @@ void show_overlay(State &state) {
ImGui::Separator(); ImGui::Separator();
ImGui::ColorEdit3("Background", state.bg, ImGuiColorEditFlags_Float); ImGui::ColorEdit3("Background", state.bg.data(), ImGuiColorEditFlags_Float);
ImGui::ColorEdit3("Foreground", state.fg, ImGuiColorEditFlags_Float); ImGui::ColorEdit3("Foreground", state.fg.data(), ImGuiColorEditFlags_Float);
ImGui::ColorEdit3("Wireframe", state.wf.data(), ImGuiColorEditFlags_Float);
if (io.MouseDown[0] && !io.WantCaptureMouse) {
Eigen::Matrix4f rot = Eigen::Matrix4f::Identity();
Eigen::Vector2f del{io.MouseDelta.x, io.MouseDelta.y};
del /= 200.0f;
if (io.KeyShift) {
del /= 5.0f;
}
if (io.KeyCtrl) {
Eigen::Matrix4f rx = rotor(0, 3, -del.x());
Eigen::Matrix4f ry = rotor(1, 3, del.y());
rot = rx * ry;
} else {
Eigen::Matrix4f rx = rotor(0, 2, -del.x());
Eigen::Matrix4f ry = rotor(1, 2, del.y());
rot = rx * ry;
}
state.rot = rot * state.rot;
}
ImGui::End(); ImGui::End();
} }
@@ -239,9 +273,10 @@ int run(GLFWwindow *window, ImGuiContext *context) {
glUseProgram(pgm); glUseProgram(pgm);
glBindVertexArray(vao); glBindVertexArray(vao);
glUniform4fv(0, 1, state.fg); glUniform4fv(0, 1, state.fg.data());
glUniform1f(1, (GLfloat) glfwGetTime()); glUniform1f(1, (GLfloat) glfwGetTime());
glUniformMatrix4fv(2, 1, false, proj.data()); glUniformMatrix4fv(2, 1, false, proj.data());
glUniformMatrix4fv(3, 1, false, state.rot.data());
glDrawElements(GL_TRIANGLES, (GLsizei) dynamic.cells().size(), GL_UNSIGNED_INT, nullptr); glDrawElements(GL_TRIANGLES, (GLsizei) dynamic.cells().size(), GL_UNSIGNED_INT, nullptr);
glBindVertexArray(0); glBindVertexArray(0);
glUseProgram(0); glUseProgram(0);
@@ -251,6 +286,7 @@ int run(GLFWwindow *window, ImGuiContext *context) {
glUniform4fv(0, 1, state.fg); glUniform4fv(0, 1, state.fg);
glUniform1f(1, (GLfloat) glfwGetTime()); glUniform1f(1, (GLfloat) glfwGetTime());
glUniformMatrix4fv(2, 1, false, proj.data()); glUniformMatrix4fv(2, 1, false, proj.data());
glUniformMatrix4fv(3, 1, false, state.rot.data());
glDrawElements(GL_LINES, (GLsizei) wire_dynamic.cells().size(), GL_UNSIGNED_INT, nullptr); glDrawElements(GL_LINES, (GLsizei) wire_dynamic.cells().size(), GL_UNSIGNED_INT, nullptr);
glBindVertexArray(0); glBindVertexArray(0);
glUseProgram(0); glUseProgram(0);