diff --git a/images/kirby.jpg b/images/kirby.jpg new file mode 100644 index 0000000..7d15e76 Binary files /dev/null and b/images/kirby.jpg differ diff --git a/includes/animations.h b/includes/animations.h index 2b6e8cd..f83ba3a 100644 --- a/includes/animations.h +++ b/includes/animations.h @@ -14,4 +14,7 @@ void voronoi(int); // Formes en jouant avec caméra et polygones void polygone(int); +// Kirby +void kirby(int); + #endif diff --git a/shaders/kirby.fs b/shaders/kirby.fs new file mode 100644 index 0000000..124b2b0 --- /dev/null +++ b/shaders/kirby.fs @@ -0,0 +1,10 @@ +#version 330 + +uniform sampler2D tex; + +in vec2 vsoTexCoord; +out vec4 fragColor; + +void main() { + fragColor = texture(tex, vsoTexCoord); +} diff --git a/shaders/kirby.vs b/shaders/kirby.vs new file mode 100644 index 0000000..7247f2b --- /dev/null +++ b/shaders/kirby.vs @@ -0,0 +1,15 @@ +#version 330 + +layout(location = 0) in vec3 pos; +layout(location = 1) in vec3 normal; +layout(location = 2) in vec2 texCoord; + +uniform mat4 proj; +uniform mat4 view; + +out vec2 vsoTexCoord; + +void main() { + gl_Position = proj * view * vec4(pos, 1.); + vsoTexCoord = vec2(texCoord.x, 1 - texCoord.y); +} diff --git a/src/kirby.c b/src/kirby.c new file mode 100644 index 0000000..dbae5db --- /dev/null +++ b/src/kirby.c @@ -0,0 +1,91 @@ +/* TP 5 */ + +#include "../includes/animations.h" + +#include + +void kirby(int etat) { + static GLuint boule = 0, proc_id = 0, tex_id = 0; + static GLfloat musique[3] = {0}; + + switch(etat) { + case GL4DH_INIT: + boule = gl4dgGenSpheref(20, 20); + + SDL_Surface *image = IMG_Load("images/kirby.jpg"); + SDL_Surface *surface = SDL_CreateRGBSurface(0, image->w, image->h, 32, R_MASK, G_MASK, B_MASK, A_MASK); + SDL_BlitSurface(image, NULL, surface, NULL); + SDL_FreeSurface(image); + + glGenTextures(1, &tex_id); + glBindTexture(GL_TEXTURE_2D, tex_id); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, surface->w, surface->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels); + SDL_FreeSurface(surface); + glBindTexture(GL_TEXTURE_2D, 0); + + proc_id = gl4duCreateProgram("shaders/kirby.vs", "shaders/kirby.fs", NULL); + + gl4duGenMatrix(GL_FLOAT, "proj"); + gl4duGenMatrix(GL_FLOAT, "view"); + + glEnable(GL_DEPTH_TEST); + break; + + case GL4DH_DRAW: + glClearColor(.66f, .17f, .24f, 1.f); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + glUseProgram(proc_id); + + gl4duBindMatrix("proj"); + gl4duLoadIdentityf(); + gl4duFrustumf(-1.f, 1.f, -.57f, .57f, 1.f, 1000.f); + + gl4duBindMatrix("view"); + gl4duLoadIdentityf(); + + gl4duLookAtf(2.f, 0.f, 2.f, 0.f, 0.f, 0.f, 0.f, 1.f, 0.f); + + gl4duPushMatrix(); + + gl4duRotatef(135, 0, 1, 0); + + GLfloat s = moyenneMusique() * 5000.f; + if(musique[0] != 0 || musique[1] != 0) { + s = (s - musique[0]) / (musique[1] - musique[0]) + .2f; + if(s > musique[2]) { + s -= s / 20.f; + } + if(s < musique[2]) { + s += s / 20.f; + } + } + musique[1] = musique[1] < s ? s : musique[1]; + musique[0] = musique[0] > s ? s : musique[0]; + musique[2] = s; + + gl4duPushMatrix(); + if(musique[1] != musique[0]) { + gl4duScalef(s, s, s); + } + + glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); + gl4duSendMatrices(); + + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, tex_id); + glUniform1i(glGetUniformLocation(proc_id, "tex"), 0); + + gl4dgDraw(boule); + gl4duPopMatrix(); + + glUseProgram(0); + break; + + default: + break; + } +} diff --git a/window.c b/window.c index bb696bb..2c2e2b8 100644 --- a/window.c +++ b/window.c @@ -26,6 +26,7 @@ int main(int argc, char *argv[]) { { 6800, cube, NULL, NULL }, { 8500, voronoi, NULL, NULL }, { 6000, polygone, NULL, NULL }, + { 5000, kirby, NULL, NULL }, { 0, NULL, NULL, NULL } };