From 2363588e88e6a15127504a101ce7f16b16b86e26 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Thu, 25 May 2023 10:14:12 +0200 Subject: [PATCH] more generic transitions impl --- shaders/zoom_in.fs | 6 ++--- src/animations.c | 64 +++++++++++++++++++++++++--------------------- 2 files changed, 38 insertions(+), 32 deletions(-) diff --git a/shaders/zoom_in.fs b/shaders/zoom_in.fs index bdec4f2..b97dd69 100644 --- a/shaders/zoom_in.fs +++ b/shaders/zoom_in.fs @@ -2,17 +2,17 @@ uniform sampler2D tex0; uniform sampler2D tex1; -uniform float zoom; +uniform float progress; in vec2 vsoTexCoord; out vec4 fragColor; void main() { vec2 center = vec2(0.5); - vec2 zoomedTexCoord = mix(center, vsoTexCoord, zoom); + vec2 zoomedTexCoord = mix(center, vsoTexCoord, progress); vec4 color1 = texture(tex0, vsoTexCoord); vec4 color2 = texture(tex1, zoomedTexCoord); - fragColor = mix(color1, color2, smoothstep(0., 1., zoom)); + fragColor = mix(color1, color2, smoothstep(0., 1., progress)); } diff --git a/src/animations.c b/src/animations.c index af04ed1..60efd9d 100644 --- a/src/animations.c +++ b/src/animations.c @@ -3,10 +3,11 @@ /* Mise-à-jour des animations en fonction du son */ static void update_with_audio(void (*)(int), void (*)(int), int); -static GLuint _zoomin_pId = 0, _zoomin_tId = 0, _zoomin_tex[2], _quadId = 0; -static void zoomin_init(void); -static void zoomin_draw(void (*)(int), void (*)(int), Uint32, Uint32, int); -static void zoomin_deinit(void); +static GLuint _transition_pId[2] = {0}, _transition_tId = 0, _quadId = 0; +static void transition_init(const char *, const int, GLuint *); +static void transition_draw(void (*)(int), void (*)(int), Uint32, Uint32, int, + int, GLuint *); +static void transition_deinit(GLuint *); static void update_with_audio(void (*a0)(int), void (*a1)(int), int state) { if (a0) { @@ -26,17 +27,19 @@ void transitionsInit(void) { void zoomIn(void (*a0)(int), void (*a1)(int), Uint32 t, Uint32 et, const int state) { + const int gpu = 0; + static GLuint tex[2]; switch (state) { case GL4DH_INIT: - zoomin_init(); + transition_init("zoom_in", gpu, tex); break; case GL4DH_DRAW: - zoomin_draw(a0, a1, t, et, state); + transition_draw(a0, a1, t, et, state, gpu, tex); break; case GL4DH_FREE: - zoomin_deinit(); + transition_deinit(tex); break; case GL4DH_UPDATE_WITH_AUDIO: @@ -48,54 +51,57 @@ void zoomIn(void (*a0)(int), void (*a1)(int), Uint32 t, Uint32 et, } } -static void zoomin_init(void) { +static void transition_init(const char *fs_shader, const int gpu, GLuint *tex) { int vp[4]; glGetIntegerv(GL_VIEWPORT, vp); - glGenTextures(2, _zoomin_tex); + glGenTextures(2, tex); for (int i = 0; i < 2; ++i) { - glBindTexture(GL_TEXTURE_2D, _zoomin_tex[i]); + glBindTexture(GL_TEXTURE_2D, tex[i]); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, vp[2], vp[3], 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); } - _zoomin_pId = gl4duCreateProgram("shaders/basic.vs", - "shaders/zoom_in.fs", NULL); + char fragment_shader[256]; + sprintf(fragment_shader, "shaders/%s.fs", fs_shader); + _transition_pId[gpu] = + gl4duCreateProgram("shaders/basic.vs", fragment_shader, NULL); } -static void zoomin_draw(void (*a0)(int), void (*a1)(int), Uint32 t, Uint32 et, - int state) { +static void transition_draw(void (*a0)(int), void (*a1)(int), Uint32 t, + Uint32 et, int state, int gpu, GLuint *tex) { glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, - (GLint *)&_zoomin_tId); + (GLint *)&_transition_tId); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, - _zoomin_tex[0], 0); + tex[0], 0); if (a0) { a0(state); } glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, - _zoomin_tex[1], 0); + tex[1], 0); if (a1) { a1(state); } glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, - _zoomin_tId, 0); + _transition_tId, 0); glDisable(GL_DEPTH_TEST); glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, _zoomin_tex[0]); + glBindTexture(GL_TEXTURE_2D, tex[0]); glActiveTexture(GL_TEXTURE1); - glBindTexture(GL_TEXTURE_2D, _zoomin_tex[1]); + glBindTexture(GL_TEXTURE_2D, tex[1]); - glUseProgram(_zoomin_pId); - GLfloat zoom = (GLfloat)et / (GLfloat)t; - glUniform1f(glGetUniformLocation(_zoomin_pId, "zoom"), (1.f - zoom)); - glUniform1i(glGetUniformLocation(_zoomin_pId, "tex1"), 0); - glUniform1i(glGetUniformLocation(_zoomin_pId, "tex0"), 1); + glUseProgram(_transition_pId[gpu]); + GLfloat progress = (GLfloat)et / (GLfloat)t; + glUniform1f(glGetUniformLocation(_transition_pId[gpu], "progress"), + (1.f - progress)); + glUniform1i(glGetUniformLocation(_transition_pId[gpu], "tex1"), 0); + glUniform1i(glGetUniformLocation(_transition_pId[gpu], "tex0"), 1); gl4dgDraw(_quadId); glActiveTexture(GL_TEXTURE1); @@ -104,10 +110,10 @@ static void zoomin_draw(void (*a0)(int), void (*a1)(int), Uint32 t, Uint32 et, glBindTexture(GL_TEXTURE_2D, 0); } -static void zoomin_deinit(void) { - if (_zoomin_tex[0]) { - glDeleteTextures(2, _zoomin_tex); - _zoomin_tex[0] = _zoomin_tex[1] = 0; +static void transition_deinit(GLuint *tex) { + if (tex[0]) { + glDeleteTextures(2, tex); + tex[0] = tex[1] = 0; } }