Use Voronoi instead of a second cube

This commit is contained in:
Mylloon 2022-05-16 13:21:24 +02:00
parent 15a2a8913e
commit 5581b9cadb
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
6 changed files with 127 additions and 79 deletions

View file

@ -9,8 +9,8 @@ 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);
// Voronoi
void voronoi(int);
// Effet de transition en fondu
void fondu(void (*)(int), void (*)(int), Uint32, Uint32, int);

31
shaders/voronoi.fs Normal file
View file

@ -0,0 +1,31 @@
/* TP6 */
#version 330
uniform int nb_sites;
uniform vec2 coords[500];
uniform vec4 colors[500];
uniform sampler2D tex;
in vec2 vsoTexCoord;
out vec4 fragColor;
void main() {
float dmin = distance(vsoTexCoord, coords[0]);
int imin = 0;
for(int i = 1; i < nb_sites; i++) {
float d = distance(vsoTexCoord, coords[i]);
if(d < dmin) {
imin = i;
dmin = d;
}
}
// Affichage d'un point au milieu d'une aire terminée
if(dmin < 0.003) {
fragColor = vec4(0, 0, 0, 1);
} else {
fragColor = colors[imin];
}
}

14
shaders/voronoi.vs Normal file
View file

@ -0,0 +1,14 @@
/* TP6 */
#version 330
layout(location = 0) in vec3 position;
layout(location = 1) in vec3 normal;
layout(location = 2) in vec2 texCoord;
out vec2 vsoTexCoord;
void main() {
gl_Position = vec4(position, 1.);
vsoTexCoord = vec2(texCoord.x, 1 - texCoord.y);
}

View file

@ -1,73 +0,0 @@
#include "../includes/animations.h"
/* TP6 */
void cube2(int etat) {
static GLuint cube = 0, proc_id = 0;
static GLclampf rgb[3] = { 0 };
switch(etat) {
case GL4DH_INIT:
for(int i = 0; i < 3; ++i) {
rgb[i] = rand() / (float)RAND_MAX;
}
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;
int 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(rgb[0], rgb[1], rgb[2], 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;
}
}

76
src/voronoi.c Normal file
View file

@ -0,0 +1,76 @@
#include "../includes/animations.h"
/* TP6 */
// Initialise le diagramme de Voronoi
void diag_init(int, GLfloat **, GLfloat **);
void voronoi(int etat) {
static GLuint quad = 0, proc_id = 0;
static GLfloat *coords = NULL, *colors = NULL;
static int sites_affiches = 1;
const int nb_sites = 400;
switch(etat) {
case GL4DH_INIT:
quad = gl4dgGenQuadf();
proc_id = gl4duCreateProgram("<vs>shaders/voronoi.vs", "<fs>shaders/voronoi.fs", NULL);
diag_init(nb_sites, &coords, &colors);
break;
case GL4DH_UPDATE_WITH_AUDIO:
break;
case GL4DH_DRAW:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(proc_id);
if(sites_affiches > nb_sites) {
sites_affiches = 1;
}
glUniform2fv(glGetUniformLocation(proc_id, "coords"), sites_affiches, coords);
glUniform1i(glGetUniformLocation(proc_id, "nb_sites"), sites_affiches);
glUniform4fv(glGetUniformLocation(proc_id, "colors"), nb_sites, colors);
gl4dgDraw(quad);
glUseProgram(0);
++sites_affiches;
break;
case GL4DH_FREE:
if(coords != NULL) {
free(coords);
coords = NULL;
}
if(colors != NULL) {
free(colors);
colors = NULL;
}
break;
default:
break;
}
}
void diag_init(int n, GLfloat **coords, GLfloat **colors) {
*coords = malloc((GLuint64)n * 2 * sizeof(**coords));
assert(*coords);
*colors = malloc((GLuint64)n * 4 * sizeof(GLfloat));
assert(*colors);
for(int i = 0; i < n; ++i) {
(*coords)[i * 2] = (gl4dmSURand() + 1) / 2;
(*coords)[i * 2 + 1] = (gl4dmSURand() + 1) / 2;
(*colors)[i * 4] = (rand() % 256) / 255.f;
(*colors)[i * 4 + 1] = (rand() % 256) / 255.f;
(*colors)[i * 4 + 2] = (rand() % 256) / 255.f;
(*colors)[i * 4 + 3] = 1.;
}
}

View file

@ -7,8 +7,8 @@ int dimensions[] = {1280, 720};
// Animations
GL4DHanime animations[] = {
{ 5000, cube, NULL, NULL },
{ 2500, cube, cube2, fondu },
{ 5000, cube2, NULL, NULL },
{ 2500, cube, voronoi, fondu },
{ 5000, voronoi, NULL, NULL },
{ 0, NULL, NULL, NULL }
};