Skip to content

Commit

Permalink
quick WIP for GLSL transpose for the shakycam effect
Browse files Browse the repository at this point in the history
  • Loading branch information
sz3 committed Feb 20, 2021
1 parent 9b90e78 commit 0de861b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
32 changes: 27 additions & 5 deletions src/lib/gui/gl_2d_display.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,13 @@ class gl_2d_display

// pass in rotation matrix
GLuint rotateUniform = glGetUniformLocation(prog, "rot");
std::array<GLfloat, 4> vals = rotation_matrix();
glUniformMatrix2fv(rotateUniform, 1, false, vals.data());
std::array<GLfloat, 4> rot = rotation_matrix();
glUniformMatrix2fv(rotateUniform, 1, false, rot.data());

// pass in transform vector
GLuint transformUniform = glGetUniformLocation(prog, "tform");
std::pair<float, float> tform = shake_transform();
glUniform2f(transformUniform, tform.first, tform.second);

// Draw
glDrawArrays(GL_TRIANGLES, 0, 6);
Expand All @@ -87,6 +92,12 @@ class gl_2d_display
_rotation = 0;
}

void shake(unsigned i=1)
{
if (i == 0 or ++_shake >= 8)
_shake = 0;
}

std::array<GLfloat, 4> rotation_matrix() const
{
// just using sin and cos is probably better?
Expand All @@ -95,15 +106,23 @@ class gl_2d_display
default:
case 0:
return {-1, 0, 0, 1};
case 1: // right 90
return {0, 1, 1, 0};
case 2: // right 180
case 1: // right 180
return {1, 0, 0, -1};
case 2: // right 90
return {0, 1, 1, 0};
case 3: // right 270
return {0, -1, -1, 0};
}
}

std::pair<float, float> shake_transform() const
{
static constexpr std::array<std::pair<float, float>, 8> SHAKE_POS = {{
{0, 0}, {-.008, -.008}, {0, 0}, {.008, .008}, {0, 0}, {-.008, .008}, {0, 0}, {.008, -.008}
}};
return SHAKE_POS[_shake];
}

protected:
static std::shared_ptr<cimbar::gl_program> create()
{
Expand All @@ -116,13 +135,15 @@ class gl_2d_display
*/
static const std::string VERTEX_SHADER_SRC = R"(#version 300 es
uniform mat2 rot;
uniform vec2 tform;
in vec4 vert;
out vec2 texCoord;
void main() {
gl_Position = vec4(vert.x, vert.y, 0.0f, 1.0f);
vec2 ori = vec2(vert.x, vert.y);
ori *= rot;
texCoord = vec2(1.0f - ori.x, 1.0f - ori.y) / 2.0;
texCoord += tform;
})";

static const std::string FRAGMENT_SHADER_SRC = R"(#version 300 es
Expand All @@ -145,6 +166,7 @@ class gl_2d_display
GLuint _vao;
unsigned _i = 0;
unsigned _rotation = 0;
unsigned _shake = 0;
};

}
6 changes: 6 additions & 0 deletions src/lib/gui/window_glfw.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ class window_glfw : public window_interface<window_glfw>
_display->rotate(i);
}

void shake(unsigned i=1)
{
if (_display)
_display->shake(i);
}

void clear()
{
if (_display)
Expand Down

0 comments on commit 0de861b

Please sign in to comment.