From d7036e5b7959a0ad118d6cb537499e839dbbce60 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Fri, 19 May 2023 12:09:54 +0200 Subject: [PATCH] try to zoom (not working) --- shaders/zoom_in.fs | 6 +++++ shaders/zoom_in.vs | 10 +++++++ src/animations.c | 66 +++++++++++++++++++++++++++++++++++++++------- 3 files changed, 73 insertions(+), 9 deletions(-) diff --git a/shaders/zoom_in.fs b/shaders/zoom_in.fs index 3060f41..f8f5eda 100644 --- a/shaders/zoom_in.fs +++ b/shaders/zoom_in.fs @@ -1,4 +1,10 @@ #version 330 +uniform sampler2D tex; + +in vec2 vsoTexCoord; +out vec4 fragColor; + void main(void) { + fragColor = texture(tex, vsoTexCoord); } diff --git a/shaders/zoom_in.vs b/shaders/zoom_in.vs index 3060f41..acb688a 100644 --- a/shaders/zoom_in.vs +++ b/shaders/zoom_in.vs @@ -1,4 +1,14 @@ #version 330 +layout(location = 0) in vec3 vsiPosition; +layout(location = 2) in vec2 vsiTexCoord; + +uniform float zoomAmount; + +out vec2 vsoTexCoord; + void main(void) { + gl_Position = vec4(vsiPosition, 1.0); + + vsoTexCoord = vec2(vsiTexCoord.s * zoomAmount, vsiTexCoord.t * zoomAmount); } diff --git a/src/animations.c b/src/animations.c index a59a2fe..bc96d23 100644 --- a/src/animations.c +++ b/src/animations.c @@ -3,8 +3,10 @@ /* Mise-à-jour des animations en fonction du son */ static void update_with_audio(void (*)(int), void (*)(int), int); -static void zoom_in_init(void); -static void zoom_in_draw(void); +static GLuint _zoomin_pId = 0, _zoomin_tId = 0, _zoomin_tex[2]; +static void zoomin_init(void); +static void zoomin_draw(void (*)(int), void (*)(int), Uint32, Uint32, int); +static void zoomin_deinit(void); static void update_with_audio(void (*a0)(int), void (*a1)(int), int state) { if (a0) { @@ -19,11 +21,15 @@ static void update_with_audio(void (*a0)(int), void (*a1)(int), int state) { void zoom_in(void (*a0)(int), void (*a1)(int), Uint32 t, Uint32 et, int state) { switch (state) { case GL4DH_INIT: - zoom_in_init(); + zoomin_init(); break; case GL4DH_DRAW: - zoom_in_draw(); + zoomin_draw(a0, a1, t, et, state); + break; + + case GL4DH_FREE: + zoomin_deinit(); break; case GL4DH_UPDATE_WITH_AUDIO: @@ -35,10 +41,52 @@ void zoom_in(void (*a0)(int), void (*a1)(int), Uint32 t, Uint32 et, int state) { } } -static void zoom_in_init(void) { - static GLuint _pId = 0; - _pId = gl4duCreateProgram("shaders/zoom_in.vs", "shaders/zoom_in.fs", - NULL); +static void zoomin_init(void) { + int vp[4]; + glGetIntegerv(GL_VIEWPORT, vp); + + glGenTextures(2, _zoomin_tex); + for (int i = 0; i < 2; ++i) { + glBindTexture(GL_TEXTURE_2D, _zoomin_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/zoom_in.vs", + "shaders/zoom_in.fs", NULL); } -static void zoom_in_draw(void) {} +static void zoomin_draw(void (*a0)(int), void (*a1)(int), Uint32 t, Uint32 et, + int state) { + glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, + GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, + (GLint *)&_zoomin_tId); + + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, + _zoomin_tex[0], 0); + if (a0) { + a0(state); + } + + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, + _zoomin_tex[1], 0); + if (a1) { + a1(state); + } + + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, + _zoomin_tId, 0); + + glUseProgram(_zoomin_pId); + glUniform1f(glGetUniformLocation(_zoomin_pId, "zoomAmount"), + (GLfloat)et / (GLfloat)t); + glUniform1i(glGetUniformLocation(_zoomin_pId, "tex"), 0); +} + +static void zoomin_deinit(void) { + if (_zoomin_tex[0]) { + glDeleteTextures(2, _zoomin_tex); + _zoomin_tex[0] = _zoomin_tex[1] = 0; + } +}