Compare commits

..

2 Commits

Author SHA1 Message Date
Alexandre f2c45b45cd added face culling 2025-02-02 11:50:42 +01:00
Alexandre 38ac44b448 removed unnecessary for() loop 2025-02-02 11:21:49 +01:00
12 changed files with 112 additions and 72 deletions

BIN
bin/back

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -73,24 +73,25 @@ mat4 model, view, projection;
mat4 scale; mat4 scale;
float vertices[108]; float vertices[108];
// -x ; +x ; -y ; +y ; -z ; +z //
void init_vertices() { void init_vertices() {
vertices[0] = -0.5f; vertices[1] = -0.5f; vertices[2] = -0.5f; vertices[0] = -0.5f; vertices[1] = -0.5f; vertices[2] = -0.5f;
vertices[3] = -0.5f; vertices[4] = 0.5f; vertices[5] = -0.5f; vertices[3] = -0.5f; vertices[4] = 0.5f; vertices[5] = -0.5f;
vertices[6] = -0.5f; vertices[7] = 0.5f; vertices[8] = 0.5f; vertices[6] = -0.5f; vertices[7] = 0.5f; vertices[8] = 0.5f;
vertices[9] = -0.5f; vertices[10] = -0.5f; vertices[11] = -0.5f; vertices[9] = -0.5f; vertices[10] = -0.5f; vertices[11] = -0.5f;
vertices[12] = -0.5f; vertices[13] = -0.5f; vertices[14] = 0.5f; vertices[12] = -0.5f; vertices[13] = 0.5f; vertices[14] = 0.5f;
vertices[15] = -0.5f; vertices[16] = 0.5f; vertices[17] = 0.5f; vertices[15] = -0.5f; vertices[16] = -0.5f; vertices[17] = 0.5f;
vertices[18] = 0.5f; vertices[19] = -0.5f; vertices[20] = -0.5f; vertices[18] = 0.5f; vertices[19] = -0.5f; vertices[20] = -0.5f;
vertices[21] = 0.5f; vertices[22] = 0.5f; vertices[23] = -0.5f; vertices[21] = 0.5f; vertices[22] = 0.5f; vertices[23] = 0.5f;
vertices[24] = 0.5f; vertices[25] = 0.5f; vertices[26] = 0.5f; vertices[24] = 0.5f; vertices[25] = 0.5f; vertices[26] = -0.5f;
vertices[27] = 0.5f; vertices[28] = -0.5f; vertices[29] = -0.5f; vertices[27] = 0.5f; vertices[28] = -0.5f; vertices[29] = -0.5f;
vertices[30] = 0.5f; vertices[31] = -0.5f; vertices[32] = 0.5f; vertices[30] = 0.5f; vertices[31] = -0.5f; vertices[32] = 0.5f;
vertices[33] = 0.5f; vertices[34] = 0.5f; vertices[35] = 0.5f; vertices[33] = 0.5f; vertices[34] = 0.5f; vertices[35] = 0.5f;
vertices[36] = -0.5f; vertices[37] = -0.5f; vertices[38] = -0.5f; vertices[36] = -0.5f; vertices[37] = -0.5f; vertices[38] = -0.5f;
vertices[39] = 0.5f; vertices[40] = -0.5f; vertices[41] = -0.5f; vertices[39] = 0.5f; vertices[40] = -0.5f; vertices[41] = 0.5f;
vertices[42] = 0.5f; vertices[43] = -0.5f; vertices[44] = 0.5f; vertices[42] = 0.5f; vertices[43] = -0.5f; vertices[44] = -0.5f;
vertices[45] = -0.5f; vertices[46] = -0.5f; vertices[47] = -0.5f; vertices[45] = -0.5f; vertices[46] = -0.5f; vertices[47] = -0.5f;
vertices[48] = -0.5f; vertices[49] = -0.5f; vertices[50] = 0.5f; vertices[48] = -0.5f; vertices[49] = -0.5f; vertices[50] = 0.5f;
vertices[51] = 0.5f; vertices[52] = -0.5f; vertices[53] = 0.5f; vertices[51] = 0.5f; vertices[52] = -0.5f; vertices[53] = 0.5f;
@ -99,31 +100,31 @@ void init_vertices() {
vertices[57] = 0.5f; vertices[58] = 0.5f; vertices[59] = -0.5f; vertices[57] = 0.5f; vertices[58] = 0.5f; vertices[59] = -0.5f;
vertices[60] = 0.5f; vertices[61] = 0.5f; vertices[62] = 0.5f; vertices[60] = 0.5f; vertices[61] = 0.5f; vertices[62] = 0.5f;
vertices[63] = -0.5f; vertices[64] = 0.5f; vertices[65] = -0.5f; vertices[63] = -0.5f; vertices[64] = 0.5f; vertices[65] = -0.5f;
vertices[66] = -0.5f; vertices[67] = 0.5f; vertices[68] = 0.5f; vertices[66] = 0.5f; vertices[67] = 0.5f; vertices[68] = 0.5f;
vertices[69] = 0.5f; vertices[70] = 0.5f; vertices[71] = 0.5f; vertices[69] = -0.5f; vertices[70] = 0.5f; vertices[71] = 0.5f;
vertices[72] = -0.5f; vertices[73] = -0.5f; vertices[74] = -0.5f; vertices[72] = -0.5f; vertices[73] = -0.5f; vertices[74] = -0.5f;
vertices[75] = 0.5f; vertices[76] = -0.5f; vertices[77] = -0.5f; vertices[75] = 0.5f; vertices[76] = -0.5f; vertices[77] = -0.5f;
vertices[78] = 0.5f; vertices[79] = 0.5f; vertices[80] = -0.5f; vertices[78] = 0.5f; vertices[79] = 0.5f; vertices[80] = -0.5f;
vertices[81] = -0.5f; vertices[82] = -0.5f; vertices[83] = -0.5f; vertices[81] = -0.5f; vertices[82] = -0.5f; vertices[83] = -0.5f;
vertices[84] = -0.5f; vertices[85] = 0.5f; vertices[86] = -0.5f; vertices[84] = 0.5f; vertices[85] = 0.5f; vertices[86] = -0.5f;
vertices[87] = 0.5f; vertices[88] = 0.5f; vertices[89] = -0.5f; vertices[87] = -0.5f; vertices[88] = 0.5f; vertices[89] = -0.5f;
vertices[90] = -0.5f; vertices[91] = -0.5f; vertices[92] = 0.5f; vertices[90] = -0.5f; vertices[91] = -0.5f; vertices[92] = 0.5f;
vertices[93] = 0.5f; vertices[94] = -0.5f; vertices[95] = 0.5f; vertices[93] = 0.5f; vertices[94] = 0.5f; vertices[95] = 0.5f;
vertices[96] = 0.5f; vertices[97] = 0.5f; vertices[98] = 0.5f; vertices[96] = 0.5f; vertices[97] = -0.5f; vertices[98] = 0.5f;
vertices[99] = -0.5f; vertices[100] = -0.5f; vertices[101] = 0.5f; vertices[99] = -0.5f; vertices[100] = -0.5f; vertices[101] = 0.5f;
vertices[102] = -0.5f; vertices[103] = 0.5f; vertices[104] = 0.5f; vertices[102] = -0.5f; vertices[103] = 0.5f; vertices[104] = 0.5f;
vertices[105] = 0.5f; vertices[106] = 0.5f; vertices[107] = 0.5f; vertices[105] = 0.5f; vertices[106] = 0.5f; vertices[107] = 0.5f;
} }
void gl_renderSurface(unsigned int shaderProgram, unsigned int fragmentShader, unsigned int VAO, unsigned int VBO, cube_0* c, int sf) { void gl_renderCube(unsigned int shaderProgram, unsigned int fragmentShader, unsigned int VAO, unsigned int VBO, cube_0* c, double offx, double offy, double offz) {
glm_mat4_identity(model); glm_mat4_identity(model);
glm_mat4_identity(scale); glm_mat4_identity(scale);
scale[0][0] = (float)(c->w); scale[0][0] = (float)(c->w);
scale[1][1] = (float)(c->h); scale[1][1] = (float)(c->h);
scale[2][2] = (float)(c->d); scale[2][2] = (float)(c->d);
glm_translate(model, (vec3){(float)(c->x+c->w/2.0), (float)(c->y+c->h/2.0), (float)(c->z+c->d/2.0)}); glm_translate(model, (vec3){(float)(c->x+c->w/2.0+offx), (float)(c->y+c->h/2.0+offy), (float)(c->z+c->d/2.0+offz)});
glm_rotate(model, (float)(-c->hz_angle), (vec3){0.0f, 1.0f, 0.0f}); glm_rotate(model, (float)(-c->hz_angle), (vec3){0.0f, 1.0f, 0.0f});
glm_rotate(model, (float)(c->vt_angle), (vec3){1.0f, 0.0f, 0.0f}); glm_rotate(model, (float)(c->vt_angle), (vec3){1.0f, 0.0f, 0.0f});
@ -137,21 +138,25 @@ void gl_renderSurface(unsigned int shaderProgram, unsigned int fragmentShader, u
glDrawArrays(GL_TRIANGLES, 0, 36); glDrawArrays(GL_TRIANGLES, 0, 36);
} }
void gl_renderCube(unsigned int shaderProgram, unsigned int fragmentShader, unsigned int VAO, unsigned int VBO, cube_0* c) { void gl_renderAll(unsigned int shaderProgram, unsigned int fragmentShader, unsigned int VAO, unsigned int VBO, room* rtd, double offx, double offy, double offz) {
for(int k = 0; k < 6; k++) { if(rtd != NULL) {
gl_renderSurface(shaderProgram, fragmentShader, VAO, VBO, c, k); for(int k = 0; k < rtd->map_size; k++) {
gl_renderCube(shaderProgram, fragmentShader, VAO, VBO, rtd->map[k], offx, offy, offz);
}
for(int k = 0; k < rtd->tps_size; k++) {
gl_renderCube(shaderProgram, fragmentShader, VAO, VBO, rtd->tps[k]->hitbox, offx, offy, offz);
}
for(int k = 0; k < rtd->ent_len; k++) {
gl_renderCube(shaderProgram, fragmentShader, VAO, VBO, rtd->ents[k].pos, offx, offy, offz);
}
} }
} }
void gl_renderAll(unsigned int shaderProgram, unsigned int fragmentShader, unsigned int VAO, unsigned int VBO) { void gl_renderNearbyChunks(unsigned int shaderProgram, unsigned int fragmentShader, unsigned int VAO, unsigned int VBO, int render_distance) {
for(int k = 0; k < current_room->map_size; k++) { for(int w = -render_distance; w <= render_distance; w++) {
gl_renderCube(shaderProgram, fragmentShader, VAO, VBO, current_room->map[k]); for(int h = -render_distance; h <= render_distance; h++) {
} gl_renderAll(shaderProgram, fragmentShader, VAO, VBO, hashtbl_find_opt(visited, player_chx+w, player_chy+h), (2.0*room_width)*w, 0.0, (2.0*room_depth)*h);
for(int k = 0; k < current_room->tps_size; k++) { }
gl_renderCube(shaderProgram, fragmentShader, VAO, VBO, current_room->tps[k]->hitbox);
}
for(int k = 0; k < current_room->ent_len; k++) {
gl_renderCube(shaderProgram, fragmentShader, VAO, VBO, current_room->ents[k].pos);
} }
} }

View File

@ -1,8 +1,9 @@
#ifndef DISPLAY_H #ifndef DISPLAY_H
#define DISPLAY_H #define DISPLAY_H
void gl_renderCube(unsigned int shaderProgram, unsigned int fragmentShader, unsigned int VAO, unsigned int VBO, cube_0* c); void gl_renderCube(unsigned int shaderProgram, unsigned int fragmentShader, unsigned int VAO, unsigned int VBO, cube_0* c, double offx, double offy, double offz);
void gl_renderAll(unsigned int shaderProgram, unsigned int fragmentShader, unsigned int VAO, unsigned int VBO); void gl_renderAll(unsigned int shaderProgram, unsigned int fragmentShader, unsigned int VAO, unsigned int VBO, room* rtd, double offx, double offy, double offz);
void gl_renderNearbyChunks(unsigned int shaderProgram, unsigned int fragmentShader, unsigned int VAO, unsigned int VBO, int render_distance);
void gl_initRender(unsigned int shaderProgram, unsigned int fragmentShader, unsigned int VAO, unsigned int VBO); void gl_initRender(unsigned int shaderProgram, unsigned int fragmentShader, unsigned int VAO, unsigned int VBO);
void init_vertices(); void init_vertices();

View File

@ -140,6 +140,8 @@ int main_alt() {
} }
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glCullFace(GL_FRONT);
glDepthFunc(GL_LESS); glDepthFunc(GL_LESS);
//printf("%f\n", glDepthRange); //printf("%f\n", glDepthRange);
@ -227,19 +229,19 @@ int main_alt() {
glBindVertexArray(VAO); glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO); glBindBuffer(GL_ARRAY_BUFFER, VBO);
gl_initRender(shaderProgram, shaderProgram, VAO, VBO); gl_initRender(shaderProgram, shaderProgram, VAO, VBO);
gl_renderAll(shaderProgram, shaderProgram, VAO, VBO); //gl_renderAll(shaderProgram, shaderProgram, VAO, VBO, current_room, 0.0, 0.0, 0.0);
gl_renderNearbyChunks(shaderProgram, shaderProgram, VAO, VBO, 1);
gl_renderProj(shaderProgram, shaderProgram, VAO, VBO); gl_renderProj(shaderProgram, shaderProgram, VAO, VBO);
//printf("01\n");
//fflush(stdout);
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.) // glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
// ------------------------------------------------------------------------------- // -------------------------------------------------------------------------------
finish = clock(); finish = clock();
processInput(window, ((float)finish - (float)origin)/CLOCKS_PER_SEC); processInput(window, ((float)finish - (float)origin)/CLOCKS_PER_SEC);
teleport_on_edge();
update_entities(((float)finish - (float)origin)/CLOCKS_PER_SEC); update_entities(((float)finish - (float)origin)/CLOCKS_PER_SEC);
updateProj(((float)finish - (float)origin)/CLOCKS_PER_SEC); updateProj(((float)finish - (float)origin)/CLOCKS_PER_SEC);
printf("%f\n", 1.0f/(((float)origin - (float)finish)/CLOCKS_PER_SEC)); printf("%f\n", 1.0f/(((float)finish - (float)origin)/CLOCKS_PER_SEC));
//printf("%lf, %lf, %lf\n", camx, camy, camz);
usleep(interval); usleep(interval);

View File

@ -17,41 +17,46 @@
#include "move.h" #include "move.h"
// ---------------------------------------------------------------------------------------------------- // // ---------------------------------------------------------------------------------------------------- //
double sensitivity = 0.06 ; double sensitivity = 0.06;
double fov = 90.0 ; double fov = 90.0;
double speed = 0.22 ; double speed = 0.22;
double min_dist = 0.7 ; double min_dist = 0.7;
// ---------------------------------------------------------------------------------------------------- // // ---------------------------------------------------------------------------------------------------- //
int player_hp ; int player_hp;
bool stop_evetything; bool stop_evetything;
double camx ; double camx;
double camy ; double camy;
double camz ; double camz;
double rot_hz ; double rot_hz;
double rot_vt ; double rot_vt;
double tan_fov ; double tan_fov;
int draw_type ; int draw_type;
int fade_dmg ; int fade_dmg;
bool has_changed ; bool has_changed;
double room_width;
double room_depth;
void init_csts() { void init_csts() {
camx = 2.0 ; camx = 2.0;
camy = 5.0 ; camy = 5.0;
camz = 2.0 ; camz = 2.0;
rot_hz = 0.0 ; rot_hz = 0.0;
rot_vt = 0.0 ; rot_vt = 0.0;
draw_type = 0 ; draw_type = 0;
player_hp = 1000 ; player_hp = 1000;
fade_dmg = 0; fade_dmg = 0;
stop_evetything = false ; room_width = 16.0;
tan_fov = tan((fov * 3.14159 / 180.0) / 2.0) ; room_depth = 16.0;
stop_evetything = false;
tan_fov = tan((fov * 3.14159 / 180.0) / 2.0);
} }
void set_player_coords(int old_chx, int old_chy) { void set_player_coords(int old_chx, int old_chy) {
@ -62,33 +67,33 @@ void set_player_coords(int old_chx, int old_chy) {
camy = current_room->tps[k]->hitbox->y + current_room->tps[k]->hitbox->h +1.5; camy = current_room->tps[k]->hitbox->y + current_room->tps[k]->hitbox->h +1.5;
camz = current_room->tps[k]->hitbox->z + current_room->tps[k]->hitbox->d/2.0; camz = current_room->tps[k]->hitbox->z + current_room->tps[k]->hitbox->d/2.0;
} }
return ; return;
} }
} }
} }
bool is_colliding(float dtime) { bool is_colliding(float dtime) {
for(int k = 0; k < current_room->map_size; k++) { for(int k = 0; k < current_room->map_size; k++) {
double dist = distance_pt_cube_0_3d(camx, camy, camz, current_room->map[k]) ; double dist = distance_pt_cube_0_3d(camx, camy, camz, current_room->map[k]);
if(dist <= min_dist) { if(dist <= min_dist) {
return true ; return true;
} }
} }
for(int k = 0; k < current_room->tps_size; k++) { for(int k = 0; k < current_room->tps_size; k++) {
double dist = distance_pt_cube_0_3d(camx, camy, camz, current_room->tps[k]->hitbox) ; double dist = distance_pt_cube_0_3d(camx, camy, camz, current_room->tps[k]->hitbox);
if(dist <= min_dist) { if(dist <= min_dist) {
int old_chx = player_chx ; int old_chx = player_chx;
int old_chy = player_chy ; int old_chy = player_chy;
player_chx = current_room->tps[k]->dest_chx ; player_chx = current_room->tps[k]->dest_chx;
player_chy = current_room->tps[k]->dest_chy ; player_chy = current_room->tps[k]->dest_chy;
current_room = hashtbl_find_opt(visited, player_chx, player_chy); current_room = hashtbl_find_opt(visited, player_chx, player_chy);
set_player_coords(old_chx, old_chy); set_player_coords(old_chx, old_chy);
//resetProj(); resetProj();
return true ; return true;
} }
} }
for(int k = 0; k < current_room->ent_len; k++) { for(int k = 0; k < current_room->ent_len; k++) {
double dist = distance_pt_cube_0_3d(camx, camy, camz, current_room->ents[k].pos) ; double dist = distance_pt_cube_0_3d(camx, camy, camz, current_room->ents[k].pos);
if(dist <= min_dist) { if(dist <= min_dist) {
if(current_room->ents[k].onHit != NULL) { if(current_room->ents[k].onHit != NULL) {
(*current_room->ents[k].onHit)(dtime, current_room->ents[k].hitpoints, &current_room->ents[k].damage, &(*(current_room->ents[k].pos))); (*current_room->ents[k].onHit)(dtime, current_room->ents[k].hitpoints, &current_room->ents[k].damage, &(*(current_room->ents[k].pos)));
@ -99,8 +104,32 @@ bool is_colliding(float dtime) {
remove_entity(&current_room->ents, &current_room->ent_memlen, &current_room->ent_len, k); remove_entity(&current_room->ents, &current_room->ent_memlen, &current_room->ent_len, k);
} }
} }
return true ; return true;
} }
} }
return false ; return false;
}
void teleport_on_edge() {
if(camx >= room_width) {
camx -= 2.0*room_width;
player_chx += 1;
current_room = hashtbl_find_opt(visited, player_chx, player_chy);
resetProj();
} else if(camx <= -room_width) {
camx += 2.0*room_width;
player_chx -= 1;
current_room = hashtbl_find_opt(visited, player_chx, player_chy);
resetProj();
} else if(camz >= room_depth) {
camz -= 2.0*room_depth;
player_chy += 1;
current_room = hashtbl_find_opt(visited, player_chx, player_chy);
resetProj();
} else if(camz <= -room_depth) {
camz += 2.0*room_depth;
player_chy -= 1;
current_room = hashtbl_find_opt(visited, player_chx, player_chy);
resetProj();
}
} }

View File

@ -4,4 +4,6 @@
void init_csts(); void init_csts();
bool is_colliding(float dtime); bool is_colliding(float dtime);
void teleport_on_edge();
#endif #endif

View File

@ -96,7 +96,7 @@ void removeProj(int k) {
void gl_renderProj(unsigned int shaderProgram, unsigned int fragmentShader, unsigned int VAO, unsigned int VBO) { void gl_renderProj(unsigned int shaderProgram, unsigned int fragmentShader, unsigned int VAO, unsigned int VBO) {
for(int k = 0; k < bullets_id; k++) { for(int k = 0; k < bullets_id; k++) {
gl_renderCube(shaderProgram, fragmentShader, VAO, VBO, bullets[k].pos); gl_renderCube(shaderProgram, fragmentShader, VAO, VBO, bullets[k].pos, 0.0, 0.0, 0.0);
} }
} }

View File

@ -110,8 +110,9 @@ extern int player_hp ;
extern int fade_dmg ; extern int fade_dmg ;
extern bool stop_evetything ; extern bool stop_evetything ;
extern double room_width ;
extern double shrink_distance ; // no height yet (requires a rewrite oh hashtbl)
extern double room_depth ;
// ---------------------------------------------------------------------------------------------------- // // ---------------------------------------------------------------------------------------------------- //
extern double sensitivity ; extern double sensitivity ;