181 lines
4.9 KiB
C
181 lines
4.9 KiB
C
#include "../includes/animations.h"
|
|
|
|
static GLuint _pId = 0;
|
|
static GLuint _planId = 0;
|
|
|
|
struct manifestant {
|
|
GLuint corps, tete, jambe_g, jambe_d, bras_g, bras_d;
|
|
// Offsets
|
|
GLfloat ox, oz;
|
|
};
|
|
|
|
#define HEROS_NUMBER 30 // should change in fragment shader too
|
|
static struct manifestant _herosId[HEROS_NUMBER];
|
|
|
|
static const char *matrix_proj = "proj";
|
|
static const char *matrix_model = "model";
|
|
static const char *matrix_view = "view";
|
|
|
|
static void init(void);
|
|
static void draw(void);
|
|
|
|
void manif(int state) {
|
|
switch (state) {
|
|
case GL4DH_INIT:
|
|
init();
|
|
break;
|
|
|
|
case GL4DH_DRAW:
|
|
draw();
|
|
break;
|
|
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
static void init(void) {
|
|
_planId = gl4dgGenQuadf();
|
|
|
|
// Last Offset X and Z
|
|
for (int i = 0; i < HEROS_NUMBER; ++i) {
|
|
// Head and body
|
|
_herosId[i].tete = gl4dgGenSpheref(7, 7);
|
|
_herosId[i].corps = gl4dgGenCubef();
|
|
|
|
// Arms
|
|
_herosId[i].bras_d = gl4dgGenCubef();
|
|
_herosId[i].bras_g = gl4dgGenCubef();
|
|
|
|
// Legs
|
|
_herosId[i].jambe_g = gl4dgGenCubef();
|
|
_herosId[i].jambe_d = gl4dgGenCubef();
|
|
|
|
// Coordinates
|
|
_herosId[i].ox = ((GLfloat)rand() / (GLfloat)RAND_MAX) * -50;
|
|
_herosId[i].oz = 30 - ((GLfloat)rand() / (GLfloat)RAND_MAX) * 50;
|
|
}
|
|
_pId =
|
|
gl4duCreateProgram("<vs>shaders/manif.vs", "<fs>shaders/manif.fs", NULL);
|
|
|
|
gl4duGenMatrix(GL_FLOAT, matrix_proj);
|
|
gl4duGenMatrix(GL_FLOAT, matrix_model);
|
|
gl4duGenMatrix(GL_FLOAT, matrix_view);
|
|
|
|
glEnable(GL_DEPTH_TEST);
|
|
}
|
|
|
|
static void draw(void) {
|
|
static double t0 = 0;
|
|
double dt = get_dt(&t0, GL_TRUE);
|
|
|
|
glClearColor(0.2f, 0.2f, 0.8f, 1); // couleur ciel
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
glUseProgram(_pId);
|
|
|
|
const GLfloat couleur_plan[] = {0.3f, 0.3f, 0.3f, 1},
|
|
couleur_heros[] = {1, 1, 0, 1};
|
|
GLint couleur_gpu = glGetUniformLocation(_pId, "couleur");
|
|
static float deplacement = 0;
|
|
|
|
bindAndLoadf(matrix_proj);
|
|
GLfloat ratio = (GLfloat)_dims[0] / (GLfloat)_dims[1];
|
|
gl4duFrustumf(-1, 1, -ratio, ratio, 1, 1000);
|
|
|
|
bindAndLoadf(matrix_view);
|
|
const GLfloat distance = 5; // distance from the scene
|
|
gl4duLookAtf(0, distance, distance, 0, 0, 0, 0, 1, 0);
|
|
// gl4duRotatef(-90, 0, 1, 0); // rotation camera
|
|
|
|
bindAndLoadf(matrix_model);
|
|
gl4duRotatef(-90, 1, 0, 0);
|
|
gl4duScalef(4 * distance, 2 * distance, 1);
|
|
|
|
gl4duSendMatrices();
|
|
glUniform4fv(couleur_gpu, 1, couleur_plan);
|
|
gl4dgDraw(_planId);
|
|
|
|
GLfloat lumpos[HEROS_NUMBER][4], lumcolor[HEROS_NUMBER][4];
|
|
glUniform4fv(couleur_gpu, 1, couleur_heros);
|
|
GLfloat part_mult = 4;
|
|
for (int i = 0; i < HEROS_NUMBER; ++i) {
|
|
// Torchs
|
|
// Position
|
|
GLfloat lumpos_i[] = {(_herosId[i].ox / 10) + (deplacement / 10), 1,
|
|
((_herosId[i].oz / 20) - 1)};
|
|
rotate_vec(lumpos_i, ((float)sin(deplacement) + 1));
|
|
memcpy(lumpos[i], lumpos_i, sizeof(lumpos_i));
|
|
|
|
// Color
|
|
const GLfloat lumcolor_red[] = {1, 0, 0, 1};
|
|
memcpy(lumcolor[i], lumcolor_red, sizeof(lumcolor_red));
|
|
|
|
GLfloat oscillation = ((float)sin(2 * deplacement) + 1);
|
|
|
|
// Draw corps
|
|
bindAndLoadf(matrix_model);
|
|
gl4duScalef(0.1f, 0.5f, 0.2f);
|
|
gl4duTranslatef(0, 3, -1);
|
|
move(_herosId[i].ox + deplacement, 0, _herosId[i].oz);
|
|
|
|
gl4duSendMatrices();
|
|
gl4dgDraw(_herosId[i].corps);
|
|
|
|
// Draw head
|
|
bindAndLoadf(matrix_model);
|
|
gl4duScalef(0.1f, 0.2f, 0.2f);
|
|
gl4duTranslatef(0, 10.9f, -1);
|
|
move(_herosId[i].ox + deplacement, 0, _herosId[i].oz);
|
|
|
|
gl4duSendMatrices();
|
|
gl4dgDraw(_herosId[i].tete);
|
|
|
|
// Draw leg left
|
|
bindAndLoadf(matrix_model);
|
|
gl4duScalef(0.1f, 1, 0.05f);
|
|
gl4duTranslatef(0, 0, -1);
|
|
move(_herosId[i].ox + deplacement, 0, _herosId[i].oz * part_mult);
|
|
gl4duRotatef(-oscillation * 90, 0, 0, 1);
|
|
|
|
gl4duSendMatrices();
|
|
gl4dgDraw(_herosId[i].jambe_g);
|
|
|
|
// Draw leg right
|
|
bindAndLoadf(matrix_model);
|
|
gl4duScalef(0.1f, 1, 0.05f);
|
|
gl4duTranslatef(0, 0, -7);
|
|
move(_herosId[i].ox + deplacement, 0, _herosId[i].oz * part_mult);
|
|
gl4duRotatef(oscillation * 90, 0, 0, 1);
|
|
|
|
gl4duSendMatrices();
|
|
gl4dgDraw(_herosId[i].jambe_d);
|
|
|
|
// Draw arm left
|
|
bindAndLoadf(matrix_model);
|
|
gl4duScalef(0.1f, 0.4f, 0.05f);
|
|
gl4duTranslatef(0, 4, 1);
|
|
move(_herosId[i].ox + deplacement, 0, _herosId[i].oz * part_mult);
|
|
gl4duRotatef(oscillation * 90, 0, 0, 1);
|
|
|
|
gl4duSendMatrices();
|
|
gl4dgDraw(_herosId[i].bras_g);
|
|
|
|
// Draw arm right
|
|
bindAndLoadf(matrix_model);
|
|
gl4duScalef(0.1f, 0.4f, 0.05f);
|
|
gl4duTranslatef(0, 4, -8);
|
|
move(_herosId[i].ox + deplacement, 0, _herosId[i].oz * part_mult);
|
|
gl4duRotatef(-oscillation * 90, 0, 0, 1);
|
|
|
|
gl4duSendMatrices();
|
|
gl4dgDraw(_herosId[i].bras_d);
|
|
}
|
|
|
|
/* On devrait donner HEROS_NUMBER mais pour éviter de ne plus rien voir avec
|
|
* les lumières, on en donne que 20 */
|
|
glUniform4fv(glGetUniformLocation(_pId, "lum_pos"), 20, (float *)lumpos);
|
|
glUniform4fv(glGetUniformLocation(_pId, "lum_color"), 20, (float *)lumcolor);
|
|
|
|
deplacement += (float)(0.4 * M_PI * dt);
|
|
glUseProgram(0);
|
|
}
|