added face culling

This commit is contained in:
Alexandre 2025-02-02 11:50:42 +01:00
parent 38ac44b448
commit f2c45b45cd
10 changed files with 108 additions and 62 deletions

BIN
bin/back

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,19 +100,19 @@ 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;
@ -138,6 +139,7 @@ void gl_renderCube(unsigned int shaderProgram, unsigned int fragmentShader, unsi
} }
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_renderAll(unsigned int shaderProgram, unsigned int fragmentShader, unsigned int VAO, unsigned int VBO, room* rtd, double offx, double offy, double offz) {
if(rtd != NULL) {
for(int k = 0; k < rtd->map_size; k++) { for(int k = 0; k < rtd->map_size; k++) {
gl_renderCube(shaderProgram, fragmentShader, VAO, VBO, rtd->map[k], offx, offy, offz); gl_renderCube(shaderProgram, fragmentShader, VAO, VBO, rtd->map[k], offx, offy, offz);
} }
@ -148,6 +150,15 @@ void gl_renderAll(unsigned int shaderProgram, unsigned int fragmentShader, unsig
gl_renderCube(shaderProgram, fragmentShader, VAO, VBO, rtd->ents[k].pos, offx, offy, offz); gl_renderCube(shaderProgram, fragmentShader, VAO, VBO, rtd->ents[k].pos, offx, offy, offz);
} }
} }
}
void gl_renderNearbyChunks(unsigned int shaderProgram, unsigned int fragmentShader, unsigned int VAO, unsigned int VBO, int render_distance) {
for(int w = -render_distance; w <= render_distance; w++) {
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);
}
}
}
vec3 dir0; vec3 dir0;
vec3 direction; vec3 direction;

View File

@ -3,6 +3,7 @@
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_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, room* rtd, double offx, double offy, double offz); 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, current_room, 0.0, 0.0, 0.0); //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

@ -41,6 +41,9 @@ 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;
@ -50,6 +53,8 @@ void init_csts() {
draw_type = 0; draw_type = 0;
player_hp = 1000; player_hp = 1000;
fade_dmg = 0; fade_dmg = 0;
room_width = 16.0;
room_depth = 16.0;
stop_evetything = false; stop_evetything = false;
tan_fov = tan((fov * 3.14159 / 180.0) / 2.0); tan_fov = tan((fov * 3.14159 / 180.0) / 2.0);
} }
@ -83,7 +88,7 @@ bool is_colliding(float dtime) {
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;
} }
} }
@ -104,3 +109,27 @@ bool is_colliding(float dtime) {
} }
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

@ -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 ;