From b1a2c8fb701a79905ef49adff5f6ce02ce065a1d Mon Sep 17 00:00:00 2001 From: Mylloon Date: Fri, 12 May 2023 16:42:51 +0200 Subject: [PATCH] add legs and arms --- includes/utils.h | 4 +- src/manif.c | 99 ++++++++++++++++++++++++++++++++++++++---------- src/utils.c | 15 +------- 3 files changed, 80 insertions(+), 38 deletions(-) diff --git a/includes/utils.h b/includes/utils.h index 3dc15ee..b0657a0 100644 --- a/includes/utils.h +++ b/includes/utils.h @@ -4,8 +4,6 @@ #include #include -enum Movement { TETE, CORPS, JAMBE_G, JAMBE_D, BRAS_G, BRAS_D }; - // Récupère un delta-temps double get_dt(double *, GLboolean); @@ -25,6 +23,6 @@ void bindAndLoadf(const char *name); * +z = move closer * * -z = move away */ -void move(GLfloat x, GLfloat y, GLfloat z, int type); +void move(GLfloat x, GLfloat y, GLfloat z); #endif diff --git a/src/manif.c b/src/manif.c index 307148e..26f68ef 100644 --- a/src/manif.c +++ b/src/manif.c @@ -4,12 +4,12 @@ static GLuint _pId = 0; static GLuint _planId = 0; struct manifestant { - GLuint corps, tete; + GLuint corps, tete, jambe_g, jambe_d, bras_g, bras_d; // Offsets GLfloat ox, oz; }; -#define HEROS_NUMBER 70 // should change in fragment shader too +#define HEROS_NUMBER 1 // should change in fragment shader too static struct manifestant _herosId[HEROS_NUMBER]; static const char *matrix_proj = "proj"; @@ -39,13 +39,21 @@ static void init(void) { // 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(); - _herosId[i].tete = gl4dgGenSpheref(20, 20); + + // 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) * -7; _herosId[i].oz = ((GLfloat)rand() / (GLfloat)RAND_MAX) * 10; - - /* printf("Generating #%d\t=> %f:%f\n", _herosId[i].corps, - (double)_herosId[i].ox, (double)_herosId[i].oz); */ } _pId = gl4duCreateProgram("shaders/manif.vs", "shaders/manif.fs", NULL); @@ -70,21 +78,14 @@ static void draw(void) { GLint couleur_gpu = glGetUniformLocation(_pId, "couleur"); static double deplacement = 0; - const GLfloat lumpos[][4] = {{1, (GLfloat)(2 + 2 * sin(deplacement)), 1, 1}, - {-1, (GLfloat)(2 + 2 * sin(deplacement)), 1, 1}}, - lumcolor[][4] = {{1, 0, 0, 1}, {0, 0, 1, 1}}; - - glUniform4fv(glGetUniformLocation(_pId, "lum_pos"), 2, (float *)lumpos); - glUniform4fv(glGetUniformLocation(_pId, "lum_color"), 2, (float *)lumcolor); - 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; + const GLfloat distance = 2; gl4duLookAtf(0, distance, distance, 0, 0, 0, 0, 1, 0); - // gl4duRotatef(-30, 0, 1, 0); + // gl4duRotatef(-30, 0, 1, 0); // rotation caméra bindAndLoadf(matrix_model); gl4duRotatef(-90, 1, 0, 0); @@ -94,29 +95,85 @@ static void draw(void) { glUniform4fv(couleur_gpu, 1, couleur_plan); gl4dgDraw(_planId); + GLfloat lumpos[1][4], lumcolor[1][4]; for (int i = 0; i < HEROS_NUMBER; ++i) { - bindAndLoadf(matrix_model); - gl4duTranslatef(0, 1, -1); - gl4duScalef(0.1f, 0.7f, 0.2f); + // Torchs + // Position + const GLfloat lumpos_i[] = {0, 1, -0.5f, 0}; + memcpy(lumpos[i], lumpos_i, sizeof(lumpos_i)); - // Apply hero position - move(_herosId[i].ox + (float)deplacement, 0, _herosId[i].oz, CORPS); + // Color + /* const GLfloat lumcolor_red[] = {1, 0, 0, 1}; */ + const GLfloat lumcolor_blue[] = {0, 0, 1, 1}; + memcpy(lumcolor[i], /* rand() % 2 == 0 ? lumcolor_red : */ lumcolor_blue, + sizeof(lumcolor_blue)); // Draw corps + bindAndLoadf(matrix_model); + gl4duTranslatef(0, 1.2f, -1); + gl4duScalef(0.1f, 0.5f, 0.2f); + move(_herosId[i].ox + (float)deplacement, 0, _herosId[i].oz); + gl4duSendMatrices(); glUniform4fv(couleur_gpu, 1, couleur_heros); gl4dgDraw(_herosId[i].corps); + // Draw head bindAndLoadf(matrix_model); - move(_herosId[i].ox + (float)deplacement, 0, _herosId[i].oz, TETE); gl4duTranslatef(0, 1.9f, -1); gl4duScalef(0.1f, 0.2f, 0.2f); + move(_herosId[i].ox + (float)deplacement, 0, _herosId[i].oz); gl4duSendMatrices(); glUniform4fv(couleur_gpu, 1, couleur_heros); gl4dgDraw(_herosId[i].tete); + + // Draw leg left + bindAndLoadf(matrix_model); + gl4duTranslatef(0, 0.3f, -1.1f); + gl4duScalef(0.1f, 0.4f, 0.05f); + move(_herosId[i].ox + (float)deplacement, 0, _herosId[i].oz); + + gl4duSendMatrices(); + glUniform4fv(couleur_gpu, 1, couleur_heros); + gl4dgDraw(_herosId[i].jambe_g); + + // Draw leg right + bindAndLoadf(matrix_model); + gl4duTranslatef(0, 0.3f, -0.9f); + gl4duScalef(0.1f, 0.4f, 0.05f); + move(_herosId[i].ox + (float)deplacement, 0, _herosId[i].oz); + + gl4duSendMatrices(); + glUniform4fv(couleur_gpu, 1, couleur_heros); + gl4dgDraw(_herosId[i].jambe_d); + + // Draw arm left + bindAndLoadf(matrix_model); + gl4duTranslatef(0, 1.25f, -1.2f); + gl4duScalef(0.1f, 0.4f, 0.05f); + move(_herosId[i].ox + (float)deplacement, 0, _herosId[i].oz); + + gl4duSendMatrices(); + glUniform4fv(couleur_gpu, 1, couleur_heros); + gl4dgDraw(_herosId[i].bras_g); + + // Draw arm right + bindAndLoadf(matrix_model); + gl4duTranslatef(0, 1.25f, -0.8f); + gl4duScalef(0.1f, 0.4f, 0.05f); + move(_herosId[i].ox + (float)deplacement, 0, _herosId[i].oz); + + gl4duSendMatrices(); + glUniform4fv(couleur_gpu, 1, couleur_heros); + gl4dgDraw(_herosId[i].bras_d); } + glUniform4fv(glGetUniformLocation(_pId, "lum_pos"), HEROS_NUMBER, + (float *)lumpos); + glUniform4fv(glGetUniformLocation(_pId, "lum_color"), HEROS_NUMBER, + (float *)lumcolor); + deplacement += 0.4 * M_PI * dt; glUseProgram(0); } diff --git a/src/utils.c b/src/utils.c index ddae0d9..e3cc442 100644 --- a/src/utils.c +++ b/src/utils.c @@ -14,17 +14,4 @@ void bindAndLoadf(const char *name) { gl4duLoadIdentityf(); } -void move(GLfloat x, GLfloat y, GLfloat z, int type) { - switch (type) { - case CORPS: - x *= 10; - z *= 5; - break; - case TETE: - break; - default: - printf("Undefined yet\n"); - break; - } - gl4duTranslatef(x, y, z); -} +void move(GLfloat x, GLfloat y, GLfloat z) { gl4duTranslatef(x, y, z); }