Add transition between 2 scene of a rotating cube

This commit is contained in:
Mylloon 2022-05-15 20:25:16 +02:00
parent 2d97b7a304
commit a2b6cbe0c3
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
10 changed files with 301 additions and 18 deletions

View file

@ -3,7 +3,16 @@
#include <GL4D/gl4dh.h>
// Comportement avant de lancer les animations
void initialisationAnimation(void);
// Dimensions initiales de la fenêtre
extern int dimensions[];
// Cube coloré qui tourne sur lui-même
void cube(int);
// Cube2 coloré qui tourne sur lui-même
void cube2(int);
// Effet de transition en fondu
void fondu(void (*)(int), void (*)(int), Uint32, Uint32, int);
#endif

10
shaders/cube.fs Normal file
View file

@ -0,0 +1,10 @@
#version 330
in vec4 vsoColor;
out vec4 maSortie;
in float il;
void main() {
maSortie = il * vsoColor;
}

23
shaders/cube.vs Normal file
View file

@ -0,0 +1,23 @@
#version 330
layout(location = 0) in vec3 position;
layout(location = 1) in vec3 normal;
layout(location = 2) in vec2 texCoord;
uniform mat4 proj;
uniform mat4 modview;
uniform mat4 view;
out vec4 vsoColor;
out float il;
const vec3 Ld = vec3(0., -.707, -.707);
void main() {
vec3 n = (transpose(inverse(modview)) * vec4(normal, 0.0)).xyz;
il = clamp(dot(n, -Ld), 0.0, 1.0);
gl_Position = proj * view * modview * vec4(position, 1.);
vsoColor = vec4(texCoord, 0.5, 1);
}

12
shaders/fondu.fs Normal file
View file

@ -0,0 +1,12 @@
#version 330
uniform float delta_temps;
uniform sampler2D tex0;
uniform sampler2D tex1;
in vec2 vsoTexCoord;
out vec4 fragColor;
void main(void) {
fragColor = mix(texture(tex0, vsoTexCoord), texture(tex1, vsoTexCoord), delta_temps);
}

12
shaders/fondu.vs Normal file
View file

@ -0,0 +1,12 @@
#version 330
layout (location = 0) in vec3 vsiPosition;
layout (location = 1) in vec3 vsiNormal;
layout (location = 2) in vec2 vsiTexCoord;
out vec2 vsoTexCoord;
void main(void) {
gl_Position = vec4(vsiPosition, 1.0);
vsoTexCoord = vec2(vsiTexCoord.s, vsiTexCoord.t);
}

View file

@ -1,5 +0,0 @@
#include "../includes/animations.h"
void initialisationAnimation(void) {
/* ... */
}

74
src/cube.c Normal file
View file

@ -0,0 +1,74 @@
#include "../includes/animations.h"
void cube(int etat) {
static GLuint cube = 0, proc_id = 0;
static GLclampf r = 0,
g = 0,
b = 0;
switch(etat) {
case GL4DH_INIT:
r = (rand() / (float)(RAND_MAX)) * 1.f;
g = (rand() / (float)(RAND_MAX)) * 1.f;
b = (rand() / (float)(RAND_MAX)) * 1.f;
cube = gl4dgGenCubef();
proc_id = gl4duCreateProgram("<vs>shaders/cube.vs", "<fs>shaders/cube.fs", NULL);
gl4duGenMatrix(GL_FLOAT, "modview");
gl4duGenMatrix(GL_FLOAT, "view");
gl4duGenMatrix(GL_FLOAT, "proj");
gl4duBindMatrix("proj");
gl4duLoadIdentityf();
gl4duFrustumf(-1, 1, -1, 1, 1, 1000);
// resize
GLfloat ratio_inverse;
GLuint fenetre_largeur = dimensions[0], fenetre_hauteur = dimensions[1];
glViewport(0, 0, fenetre_largeur, fenetre_hauteur);
ratio_inverse = fenetre_hauteur / (GLfloat)fenetre_largeur;
gl4duBindMatrix("proj");
gl4duLoadIdentityf();
gl4duFrustumf(-1.f, 1.f, -1.f * ratio_inverse, 1.f * ratio_inverse, 1.f, 1000.f);
glEnable(GL_DEPTH_TEST);
break;
case GL4DH_UPDATE_WITH_AUDIO:
break;
case GL4DH_DRAW:
glClearColor(r, g, b, 1.f);
static GLfloat cube_rotation = 0;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(proc_id);
gl4duBindMatrix("view");
gl4duLoadIdentityf();
gl4duLookAtf(0, 0, 5, 0, 0, 0, 0, 1, 0);
gl4duBindMatrix("modview");
gl4duLoadIdentityf();
gl4duPushMatrix();
gl4duRotatef(cube_rotation, 0, 1, 0);
gl4duSendMatrices();
gl4dgDraw(cube);
gl4duPopMatrix();
glUseProgram(0);
++cube_rotation;
break;
case GL4DH_FREE:
break;
default:
break;
}
}

74
src/cube2.c Normal file
View file

@ -0,0 +1,74 @@
#include "../includes/animations.h"
void cube2(int etat) {
static GLuint cube = 0, proc_id = 0;
static GLclampf r = 0,
g = 0,
b = 0;
switch(etat) {
case GL4DH_INIT:
r = (rand() / (float)(RAND_MAX)) * 1.f;
g = (rand() / (float)(RAND_MAX)) * 1.f;
b = (rand() / (float)(RAND_MAX)) * 1.f;
cube = gl4dgGenCubef();
proc_id = gl4duCreateProgram("<vs>shaders/cube.vs", "<fs>shaders/cube.fs", NULL);
gl4duGenMatrix(GL_FLOAT, "modview");
gl4duGenMatrix(GL_FLOAT, "view");
gl4duGenMatrix(GL_FLOAT, "proj");
gl4duBindMatrix("proj");
gl4duLoadIdentityf();
gl4duFrustumf(-1, 1, -1, 1, 1, 1000);
// resize
GLfloat ratio_inverse;
GLuint fenetre_largeur = dimensions[0], fenetre_hauteur = dimensions[1];
glViewport(0, 0, fenetre_largeur, fenetre_hauteur);
ratio_inverse = fenetre_hauteur / (GLfloat)fenetre_largeur;
gl4duBindMatrix("proj");
gl4duLoadIdentityf();
gl4duFrustumf(-1.f, 1.f, -1.f * ratio_inverse, 1.f * ratio_inverse, 1.f, 1000.f);
glEnable(GL_DEPTH_TEST);
break;
case GL4DH_UPDATE_WITH_AUDIO:
break;
case GL4DH_DRAW:
glClearColor(r, g, b, 1.f);
static GLfloat cube_rotation = 0;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(proc_id);
gl4duBindMatrix("view");
gl4duLoadIdentityf();
gl4duLookAtf(0, 0, 5, 0, 0, 0, 0, 1, 0);
gl4duBindMatrix("modview");
gl4duLoadIdentityf();
gl4duPushMatrix();
gl4duRotatef(cube_rotation, 0, 1, 0);
gl4duSendMatrices();
gl4dgDraw(cube);
gl4duPopMatrix();
glUseProgram(0);
++cube_rotation;
break;
case GL4DH_FREE:
break;
default:
break;
}
}

79
src/fondu.c Normal file
View file

@ -0,0 +1,79 @@
#include "../includes/animations.h"
/* Fondu de Farès BELHADJ */
void fondu(void (* ancienne_anim)(int), void (* nouvelle_anim)(int), Uint32 temps_total, Uint32 temps_ecoule, int etat) {
GLint viewport[4];
GLint tex_id = 0;
static GLuint ecran[2], proc_id = 0, quad = 0;
switch(etat) {
case GL4DH_INIT:
quad = gl4dgGenQuadf();
glGetIntegerv(GL_VIEWPORT, viewport);
glGenTextures(2, ecran);
for(int i = 0; i < 2; ++i) {
glBindTexture(GL_TEXTURE_2D, ecran[i]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, viewport[2], viewport[3], 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
}
proc_id = gl4duCreateProgram("<vs>shaders/fondu.vs", "<fs>shaders/fondu.fs", NULL);
break;
case GL4DH_UPDATE_WITH_AUDIO:
break;
case GL4DH_DRAW:
// Récupération ID texture
glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &tex_id);
// Dessine les deux animations
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, ecran[0], 0);
if(ancienne_anim) {
ancienne_anim(etat);
}
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, ecran[1], 0);
if(nouvelle_anim) {
nouvelle_anim(etat);
}
// Mix des deux écrans
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex_id, 0);
glDisable(GL_DEPTH);
glUseProgram(proc_id);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, ecran[0]);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, ecran[1]);
glUniform1f(glGetUniformLocation(proc_id, "delta_temps"), temps_ecoule / (GLfloat)temps_total);
glUniform1i(glGetUniformLocation(proc_id, "tex0"), 0);
glUniform1i(glGetUniformLocation(proc_id, "tex1"), 1);
gl4dgDraw(quad);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, 0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, 0);
break;
case GL4DH_FREE:
if(ecran[0] || ecran[1]) {
glDeleteTextures(2, ecran);
ecran[0] = ecran[1] = 0;
}
break;
default:
break;
}
}

View file

@ -2,25 +2,24 @@
#include "includes/animations.h"
// Dimensions initiales de la fenêtre
static int dimensions[] = {1280, 720};
int dimensions[] = {1280, 720};
// Animations
static GL4DHanime animations[] = {
{0, NULL, NULL, NULL}
{ 5000, cube, NULL, NULL },
{ 2500, cube, cube2, fondu },
{ 5000, cube2, NULL, NULL },
{ 0, NULL, NULL, NULL }
};
// Comportement à la fermeture du programme
static void fermeture(void);
// Comportement au démarrage du programme
void initialisation(void);
int main(int argc, char *argv[]) {
if(!gl4duwCreateWindow(argc, argv, "Démo Anri KENNEL", 10, 10, dimensions[0], dimensions[1], GL4DW_SHOWN)) {
return 1;
}
initialisation();
gl4dhInit(animations, dimensions[0], dimensions[1], NULL);
atexit(fermeture);
gl4duwDisplayFunc(gl4dhDraw);
@ -29,10 +28,6 @@ int main(int argc, char *argv[]) {
return 0;
}
void initialisation(void) {
gl4dhInit(animations, dimensions[0], dimensions[1], initialisationAnimation);
}
void fermeture(void) {
gl4duClean(GL4DU_ALL);
}