added two jiggle effects and command for toggling them

This commit is contained in:
frank 2021-09-08 00:31:46 -04:00
parent ffede89153
commit 5aab43896c
4 changed files with 70 additions and 8 deletions

View File

@ -16,7 +16,8 @@
"print-video-memory-size": ["CTRL", "v"],
"print-frame-length-history": ["CTRL", "SHIFT", "h"],
"toggle-camera": ["CTRL", "c"],
"toggle-item": ["CTRL", "i"]
"toggle-item": ["CTRL", "i"],
"effect": ["CTRL", "e"]
},
"recording":
{
@ -44,7 +45,7 @@
{
"json-save": true,
"json-save-directory": "local/scans",
"barcode": "071146005280",
"barcode": "",
"capture-device": "/dev/video0"
},
"api":

View File

@ -36,7 +36,6 @@ Pudding::Pudding()
set_pudding_model(1.0f, 1.6f, 10, 12, -.6, .6, .25);
/* use gl context so we can draw 3D */
load_gl_context();
initialize_camera();
}
/* Assign vertices, colors and texture UV coordinates to the pudding model */
@ -221,7 +220,9 @@ void Pudding::load_gl_context()
glBindAttribLocation(mvp_program, 3, "in_color");
glBindAttribLocation(mvp_program, 4, "vertex_uv");
link_shader(mvp_program);
mvp_id = glGetUniformLocation(mvp_program, "mvp");
mvp_uniform_location = glGetUniformLocation(mvp_program, "mvp");
time_uniform_location = glGetUniformLocation(mvp_program, "time");
effect_uniform_location = glGetUniformLocation(mvp_program, "effect");
log_gl_errors();
}
@ -287,6 +288,13 @@ void Pudding::respond(SDL_Event& event)
{
show_item = !show_item;
}
/* The effect command switches the active effect to the next in the list, wrapping around at the end */
else if (get_delegate().compare(event, "effect"))
{
effect_id = ++effect_id % Effect::COUNT;
glUseProgram(mvp_program);
glUniform1i(effect_uniform_location, effect_id);
}
}
/* Build an Item object by submitting the upc parameter to multiple APIs and taking
@ -669,13 +677,14 @@ void Pudding::update()
glDrawArrays(GL_TRIANGLES, 0, 6);
/* draw pudding model using MVP shader */
glUseProgram(mvp_program);
glUniform1f(time_uniform_location, SDL_GetTicks() / 1000.0f);
/* calculate the transformation matrix for displaying pudding in viewport */
model = glm::rotate(model, weight(get_configuration()["pudding"]["rotation-speed"].get<float>()), Y_UNIT_NORMAL_3D);
projection = glm::perspective(
glm::radians(40.0f * 1 / viewport_box.aspect()), viewport_box.aspect(), 0.1f, 100.0f);
mvp = projection * VIEW_MATRIX * model;
/* pass the mvp matrix to the shader */
glUniformMatrix4fv(mvp_id, 1, GL_FALSE, &mvp[0][0]);
glUniformMatrix4fv(mvp_uniform_location, 1, GL_FALSE, &mvp[0][0]);
/* disable rectangle attributes and enable pudding attributes */
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);

View File

@ -27,6 +27,16 @@ class Pudding : public Game
private:
/* Defines for effect IDs that will be passed to the shader program. Since COUNT is last and every value
* is the default integer, it will define the number of effects available */
enum Effect
{
NONE,
SNAKE,
WOBBLE,
COUNT
};
typedef Game super;
const std::string OPEN_FOOD_API_URL = "https://world.openfoodfacts.org/api/v0/product/";
const std::string NUTRONIX_API_URL = "https://trackapi.nutritionix.com/v2/search/item?upc=";
@ -46,11 +56,13 @@ private:
cv::VideoCapture capture;
cv::Mat capture_frame;
zbar::ImageScanner image_scanner;
GLuint flat_program, mvp_program, video_capture_texture_id, mvp_id, background_texture_id;
GLuint flat_program, mvp_program, video_capture_texture_id, mvp_uniform_location, background_texture_id, time_uniform_location,
effect_uniform_location;
glm::mat4 projection, model = glm::mat4(1.0f), mvp;
std::vector<glm::vec3> pudding_vertices, pudding_colors;
std::vector<glm::vec2> pudding_uv;
bool show_item;
int effect_id = Effect::NONE;
void set_pudding_model(float, float, int, int = 1, float = -1, float = 1, float = 0.3f);
void load_gl_context();

View File

@ -1,17 +1,57 @@
#version 130
#define PI 3.1415926535897932384626433832795
#define AMPLITUDE 0.2
#define PERIOD .5
#define WAVELENGTH 2.5
#define EFFECT_NONE 0
#define EFFECT_SNAKE 1
#define EFFECT_WOBBLE 2
in vec3 in_position;
in vec3 in_color;
in vec2 vertex_uv;
uniform mat4 mvp;
uniform float time;
uniform int effect = EFFECT_NONE;
out vec3 ex_color;
out float x;
out vec2 uv;
void main(void)
/* Offset X-coordinate according to the time step and Y-coordinate to create a wobble effect when run on
* flattened coordinates (after projection matrix has been applied) */
void wobble()
{
gl_Position = mvp * vec4(in_position, 1);
gl_Position.x += sin(time * 10.0) * in_position.y / 2.0;
}
/* Contort the X-coordinate according the time step and Y-coordinate using the sine function. This contorts
* the model into a sine wave along the Y-axis. It also moves the sine wave along the Y-axis using the time
* step uniform. The shape can be edited by changing the defintions for amplitude (maximum distance from
* Y-axis), wavelength (length in GL model coordinates between peaks), and period (amount of time in
* seconds it takes to loop through one wavelength cycle). */
void snake()
{
gl_Position = vec4(
in_position.x + AMPLITUDE * sin(2.0 * PI / PERIOD * (time + in_position.y * PERIOD / WAVELENGTH)), in_position.yz, 1);
}
void main()
{
if (effect == EFFECT_SNAKE)
{
snake();
}
else
{
gl_Position = vec4(in_position, 1);
}
gl_Position = mvp * gl_Position;
ex_color = in_color;
x = 1.8 - abs(in_position[0]);
if (effect == EFFECT_WOBBLE)
{
wobble();
}
uv = vertex_uv;
}