This repository has been archived on 2023-05-27. You can view files and clone it, but cannot push or open issues or pull requests.
api8/src/manif.c

181 lines
4.9 KiB
C
Raw Normal View History

2023-05-08 09:07:48 +02:00
#include "../includes/animations.h"
static GLuint _pId = 0;
2023-05-08 10:23:45 +02:00
static GLuint _planId = 0;
2023-05-08 10:38:45 +02:00
2023-05-12 13:50:12 +02:00
struct manifestant {
2023-05-12 16:42:51 +02:00
GLuint corps, tete, jambe_g, jambe_d, bras_g, bras_d;
2023-05-12 13:50:12 +02:00
// Offsets
GLfloat ox, oz;
};
2023-05-19 11:25:07 +02:00
#define HEROS_NUMBER 30 // should change in fragment shader too
2023-05-12 13:50:12 +02:00
static struct manifestant _herosId[HEROS_NUMBER];
static const char *matrix_proj = "proj";
static const char *matrix_model = "model";
static const char *matrix_view = "view";
2023-05-08 09:07:48 +02:00
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) {
2023-05-08 10:23:45 +02:00
_planId = gl4dgGenQuadf();
2023-05-12 13:50:12 +02:00
// Last Offset X and Z
for (int i = 0; i < HEROS_NUMBER; ++i) {
2023-05-12 16:42:51 +02:00
// Head and body
_herosId[i].tete = gl4dgGenSpheref(7, 7);
2023-05-12 13:50:12 +02:00
_herosId[i].corps = gl4dgGenCubef();
2023-05-12 16:42:51 +02:00
// Arms
_herosId[i].bras_d = gl4dgGenCubef();
_herosId[i].bras_g = gl4dgGenCubef();
// Legs
_herosId[i].jambe_g = gl4dgGenCubef();
_herosId[i].jambe_d = gl4dgGenCubef();
// Coordinates
2023-05-12 17:46:25 +02:00
_herosId[i].ox = ((GLfloat)rand() / (GLfloat)RAND_MAX) * -50;
_herosId[i].oz = 30 - ((GLfloat)rand() / (GLfloat)RAND_MAX) * 50;
2023-05-12 13:50:12 +02:00
}
2023-05-08 09:08:09 +02:00
_pId =
gl4duCreateProgram("<vs>shaders/manif.vs", "<fs>shaders/manif.fs", NULL);
2023-05-08 09:07:48 +02:00
2023-05-08 10:38:45 +02:00
gl4duGenMatrix(GL_FLOAT, matrix_proj);
gl4duGenMatrix(GL_FLOAT, matrix_model);
gl4duGenMatrix(GL_FLOAT, matrix_view);
2023-05-08 09:07:48 +02:00
}
static void draw(void) {
2023-05-08 13:41:32 +02:00
static double t0 = 0;
double dt = get_dt(&t0, GL_TRUE);
2023-05-19 13:14:48 +02:00
glEnable(GL_DEPTH_TEST);
2023-05-08 16:46:22 +02:00
glClearColor(0.2f, 0.2f, 0.8f, 1); // couleur ciel
2023-05-08 09:07:48 +02:00
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
2023-05-08 10:23:45 +02:00
glUseProgram(_pId);
2023-05-08 09:07:48 +02:00
2023-05-08 11:29:12 +02:00
const GLfloat couleur_plan[] = {0.3f, 0.3f, 0.3f, 1},
2023-05-08 12:16:29 +02:00
couleur_heros[] = {1, 1, 0, 1};
2023-05-08 15:06:42 +02:00
GLint couleur_gpu = glGetUniformLocation(_pId, "couleur");
2023-05-12 18:14:54 +02:00
static float deplacement = 0;
2023-05-08 11:29:12 +02:00
2023-05-08 15:06:42 +02:00
bindAndLoadf(matrix_proj);
2023-05-08 10:23:45 +02:00
GLfloat ratio = (GLfloat)_dims[0] / (GLfloat)_dims[1];
gl4duFrustumf(-1, 1, -ratio, ratio, 1, 1000);
2023-05-08 15:06:42 +02:00
bindAndLoadf(matrix_view);
2023-05-12 18:17:35 +02:00
const GLfloat distance = 5; // distance from the scene
2023-05-08 15:26:47 +02:00
gl4duLookAtf(0, distance, distance, 0, 0, 0, 0, 1, 0);
2023-05-12 17:46:25 +02:00
// gl4duRotatef(-90, 0, 1, 0); // rotation camera
2023-05-08 10:23:45 +02:00
2023-05-08 15:06:42 +02:00
bindAndLoadf(matrix_model);
2023-05-08 10:23:45 +02:00
gl4duRotatef(-90, 1, 0, 0);
gl4duScalef(4 * distance, 2 * distance, 1);
gl4duSendMatrices();
2023-05-08 15:06:42 +02:00
glUniform4fv(couleur_gpu, 1, couleur_plan);
2023-05-08 10:23:45 +02:00
gl4dgDraw(_planId);
2023-05-08 09:07:48 +02:00
2023-05-12 17:46:25 +02:00
GLfloat lumpos[HEROS_NUMBER][4], lumcolor[HEROS_NUMBER][4];
glUniform4fv(couleur_gpu, 1, couleur_heros);
GLfloat part_mult = 4;
2023-05-12 13:50:12 +02:00
for (int i = 0; i < HEROS_NUMBER; ++i) {
2023-05-12 16:42:51 +02:00
// Torchs
// Position
2023-05-19 11:13:37 +02:00
GLfloat lumpos_i[] = {(_herosId[i].ox / 10) + (deplacement / 10), 1,
((_herosId[i].oz / 20) - 1)};
2023-05-19 11:21:58 +02:00
rotate_vec(lumpos_i, ((float)sin(deplacement) + 1));
2023-05-12 16:42:51 +02:00
memcpy(lumpos[i], lumpos_i, sizeof(lumpos_i));
2023-05-12 13:50:12 +02:00
2023-05-12 16:42:51 +02:00
// Color
2023-05-12 18:14:54 +02:00
const GLfloat lumcolor_red[] = {1, 0, 0, 1};
2023-05-12 17:46:25 +02:00
memcpy(lumcolor[i], lumcolor_red, sizeof(lumcolor_red));
2023-05-12 13:50:12 +02:00
2023-05-19 11:21:58 +02:00
GLfloat oscillation = ((float)sin(2 * deplacement) + 1);
2023-05-12 13:50:12 +02:00
// Draw corps
2023-05-12 16:42:51 +02:00
bindAndLoadf(matrix_model);
gl4duScalef(0.1f, 0.5f, 0.2f);
2023-05-12 17:46:25 +02:00
gl4duTranslatef(0, 3, -1);
2023-05-12 18:14:54 +02:00
move(_herosId[i].ox + deplacement, 0, _herosId[i].oz);
2023-05-12 16:42:51 +02:00
2023-05-12 13:50:12 +02:00
gl4duSendMatrices();
gl4dgDraw(_herosId[i].corps);
2023-05-12 16:42:51 +02:00
// Draw head
2023-05-12 13:50:12 +02:00
bindAndLoadf(matrix_model);
gl4duScalef(0.1f, 0.2f, 0.2f);
2023-05-12 17:46:25 +02:00
gl4duTranslatef(0, 10.9f, -1);
2023-05-12 18:14:54 +02:00
move(_herosId[i].ox + deplacement, 0, _herosId[i].oz);
2023-05-12 13:50:12 +02:00
gl4duSendMatrices();
gl4dgDraw(_herosId[i].tete);
2023-05-12 16:42:51 +02:00
// Draw leg left
bindAndLoadf(matrix_model);
2023-05-12 17:46:25 +02:00
gl4duScalef(0.1f, 1, 0.05f);
gl4duTranslatef(0, 0, -1);
2023-05-12 18:14:54 +02:00
move(_herosId[i].ox + deplacement, 0, _herosId[i].oz * part_mult);
2023-05-19 11:21:58 +02:00
gl4duRotatef(-oscillation * 90, 0, 0, 1);
2023-05-12 16:42:51 +02:00
gl4duSendMatrices();
gl4dgDraw(_herosId[i].jambe_g);
// Draw leg right
bindAndLoadf(matrix_model);
2023-05-12 17:46:25 +02:00
gl4duScalef(0.1f, 1, 0.05f);
gl4duTranslatef(0, 0, -7);
2023-05-12 18:14:54 +02:00
move(_herosId[i].ox + deplacement, 0, _herosId[i].oz * part_mult);
2023-05-19 11:21:58 +02:00
gl4duRotatef(oscillation * 90, 0, 0, 1);
2023-05-12 16:42:51 +02:00
gl4duSendMatrices();
gl4dgDraw(_herosId[i].jambe_d);
// Draw arm left
bindAndLoadf(matrix_model);
gl4duScalef(0.1f, 0.4f, 0.05f);
2023-05-12 17:46:25 +02:00
gl4duTranslatef(0, 4, 1);
2023-05-12 18:14:54 +02:00
move(_herosId[i].ox + deplacement, 0, _herosId[i].oz * part_mult);
2023-05-19 11:21:58 +02:00
gl4duRotatef(oscillation * 90, 0, 0, 1);
2023-05-12 16:42:51 +02:00
gl4duSendMatrices();
gl4dgDraw(_herosId[i].bras_g);
// Draw arm right
bindAndLoadf(matrix_model);
gl4duScalef(0.1f, 0.4f, 0.05f);
2023-05-12 17:46:25 +02:00
gl4duTranslatef(0, 4, -8);
2023-05-12 18:14:54 +02:00
move(_herosId[i].ox + deplacement, 0, _herosId[i].oz * part_mult);
2023-05-19 11:21:58 +02:00
gl4duRotatef(-oscillation * 90, 0, 0, 1);
2023-05-12 16:42:51 +02:00
gl4duSendMatrices();
gl4dgDraw(_herosId[i].bras_d);
2023-05-12 13:50:12 +02:00
}
2023-05-08 10:38:45 +02:00
2023-05-19 11:25:07 +02:00
/* 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);
2023-05-12 16:42:51 +02:00
2023-05-12 18:14:54 +02:00
deplacement += (float)(0.4 * M_PI * dt);
2023-05-08 09:07:48 +02:00
glUseProgram(0);
}