fixed projectiles not spawning in correct chunks + collisions now use all rendered chunks

This commit is contained in:
Alexandre 2025-02-23 18:26:31 +01:00
parent e17ebbffd4
commit b7973fbbda
13 changed files with 86 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.

Binary file not shown.

View File

@ -34,6 +34,8 @@ int loc_proj;
int loc_frag;
int loc_tex;
int loc_tex_tr;
void init_draworder() {
drawOrder = malloc(sizeof(int)*6) ;
}
@ -79,6 +81,7 @@ double right = 1.0 ;
mat4 model, view, projection;
mat4 scale;
mat2 texShift;
float vertices[180];
// -x ; +x ; -y ; +y ; -z ; +z //
@ -126,7 +129,7 @@ void init_vertices() {
vertices[175] = 0.5f; vertices[176] = 0.5f; vertices[177] = 0.5f; vertices[178] = 1.0; vertices[179] = 0.0;
}
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(cube_0* c, double offx, double offy, double offz) {
glm_mat4_identity(model);
glm_mat4_identity(scale);
scale[0][0] = (float)(c->w);
@ -136,12 +139,17 @@ void gl_renderCube(unsigned int shaderProgram, unsigned int fragmentShader, unsi
glm_rotate(model, (float)(c->vt_angle), (vec3){1.0f, 0.0f, 0.0f});
glm_rotate(model, (float)(c->hz_angle), (vec3){0.0f, 1.0f, 0.0f});
glm_mat2_identity(texShift);
texShift[0][0] = (float)(maxd(maxd(c->w, c->h), c->d)/mind(mind(c->w, c->h), c->d));
texShift[1][1] = texShift[0][0];
glUniformMatrix4fv(loc_scale, 1, GL_FALSE, (float*)scale);
glUniformMatrix4fv(loc_model, 1, GL_FALSE, (float*)model);
glUniformMatrix4fv(loc_view, 1, GL_FALSE, (float*)view);
glUniformMatrix4fv(loc_proj, 1, GL_FALSE, (float*)projection);
glUniform4f(loc_frag, c->red/255.0f, c->green/255.0f, c->blue/255.0f, 1.0f);
glUniformMatrix2fv(loc_tex_tr, 1, GL_FALSE, (float*)texShift);
glDrawArrays(GL_TRIANGLES, 0, 36);
//glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0);
@ -166,41 +174,45 @@ void gl_resetTexture() {
glBindTexture(GL_TEXTURE_2D, textures[0]);
}
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(room* rtd, double offx, double offy, double offz) {
//printf("------------------------\n");
if(rtd != NULL) {
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, textures[5]);
for(int k = 0; k < rtd->map_size; k++) {
if(is_visible(rtd->map[k], offx, offy, offz)) {
gl_renderCube(shaderProgram, fragmentShader, VAO, VBO, rtd->map[k], offx, offy, offz);
gl_renderCube(rtd->map[k], offx, offy, offz);
}
}
glBindTexture(GL_TEXTURE_2D, textures[6]);
for(int k = 0; k < rtd->tps_size; k++) {
if(is_visible(rtd->tps[k]->hitbox, offx, offy, offz)) {
gl_renderCube(shaderProgram, fragmentShader, VAO, VBO, rtd->tps[k]->hitbox, offx, offy, offz);
gl_renderCube(rtd->tps[k]->hitbox, offx, offy, offz);
}
}
for(int k = 0; k < rtd->ent_len; k++) {
if(is_visible(rtd->ents[k]->pos, offx, offy, offz)) {
//printf("%d\n", rtd->ents[k]->tex);
//printf("r %d\n", rtd->ents[k]->entity_type); fflush(stdout);
if(rtd->ents[k]->entity_type != 9 || rtd->ents[k]->metai1) {
glBindTexture(GL_TEXTURE_2D, textures[rtd->ents[k]->tex]);
} else {
//printf(">>%d<<\n", rtd->ents[k]->tex2);
glBindTexture(GL_TEXTURE_2D, textures[rtd->ents[k]->tex2]);
}
gl_renderCube(shaderProgram, fragmentShader, VAO, VBO, rtd->ents[k]->pos, offx, offy, offz);
gl_renderCube(rtd->ents[k]->pos, offx, offy, offz);
//printf("OK\n"); fflush(stdout);
}
}
}
//printf("+++++++++++++++\n");
}
void gl_renderNearbyChunks(unsigned int shaderProgram, unsigned int fragmentShader, unsigned int VAO, unsigned int VBO, int render_distance) {
void gl_renderNearbyChunks(int render_distance) {
for(int w = -render_distance; w <= render_distance; w++) {
for(int h = -render_distance; h <= render_distance; h++) {
//printf("(%d %d -> %d)", w, h, hashtbl_find_opt(visited, player_chx+w, player_chy+h)->ent_len);
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);
gl_renderAll(hashtbl_find_opt(visited, player_chx+w, player_chy+h), (2.0*room_width)*w, 0.0, (2.0*room_depth)*h);
}
}
//printf("\n\n");
@ -226,6 +238,8 @@ void gl_initRender(unsigned int shaderProgram, unsigned int fragmentShader, unsi
loc_view = glGetUniformLocation(shaderProgram, "view");
loc_proj = glGetUniformLocation(shaderProgram, "projection");
loc_tex_tr = glGetUniformLocation(fragmentShader, "translation");
loc_frag = glGetUniformLocation(fragmentShader, "u_color");
}

View File

@ -1,9 +1,8 @@
#ifndef DISPLAY_H
#define DISPLAY_H
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_renderNearbyChunks(unsigned int shaderProgram, unsigned int fragmentShader, unsigned int VAO, unsigned int VBO, int render_distance);
void gl_renderCube(cube_0* c, double offx, double offy, double offz);
void gl_renderNearbyChunks(int render_distance);
void gl_initRender(unsigned int shaderProgram, unsigned int fragmentShader, unsigned int VAO, unsigned int VBO);
void init_vertices();

View File

@ -44,6 +44,7 @@ bool is_colliding_with_tp(cube_0* cb) {
// ------------------------------------------------------------------------------------------------------------------------------------------------ //
static double choffx, choffz;
void update_entity(entity* ent, float dtime) {
(*ent->updatePos)(ent->pos->x, ent->pos->y, ent->pos->z, ent->pos->w, ent->pos->h, ent->pos->d, ent->pos->hz_angle, ent->pos->vt_angle, dtime, ent, ent->pos);
}
@ -62,6 +63,8 @@ void update_entities(float dtime, room* rtd) {
void update_nearby_entities(float dtime, int render_distance) {
for(int w = -render_distance; w <= render_distance; w++) {
for(int h = -render_distance; h <= render_distance; h++) {
choffx = 2*room_width*w;
choffz = 2*room_depth*h;
update_entities(dtime, hashtbl_find_opt(visited, player_chx+w, player_chy+h));
}
}
@ -76,14 +79,14 @@ void speen(double x, double y, double z, double w, double h, double d, double hz
void speen2(double x, double y, double z, double w, double h, double d, double hz_angle, double vt_angle, float dtime, entity* ent, cube_0* ret) {
ret->hz_angle += ((double)dtime)*2.5;
if((int)(5.0*ret->hz_angle) != (int)(5.0*(ret->hz_angle - ((double)dtime)*22.5))) {
double dx = (x+w/2 - camx);
double dy = (y+h/2 - camy);
double dz = (z+d/2 - camz);
double dx = (x+w/2 - (camx-choffx));
double dy = (y+h/2 - (camy));
double dz = (z+d/2 - (camz-choffz));
double total = sqrt(dx*dx + dy*dy + dz*dz);
dx = 110.0*dx/total;
dy = 110.0*dy/total;
dz = 110.0*dz/total;
appendProj(x+w/2, y+h/2, z+d/2, 0.1, 0.1, 0.1, -dx, -dy, -dz, 0.0, 0.0, 0.0, 255, 0, 0, 10, 3.0);
appendProj(x+w/2+choffx, y+h/2, z+d/2+choffz, 0.1, 0.1, 0.1, -dx, -dy, -dz, 0.0, 0.0, 0.0, 255, 0, 0, 10, 3.0);
}
}
@ -214,9 +217,9 @@ void translatePlayerLine(float dtime, int* hp, int* dmg, entity* ent, cube_0* re
}
void go_to_player(double x, double y, double z, double w, double h, double d, double hz_angle, double vt_angle, float dtime, entity* ent, cube_0* ret) {
double dx = (x+w/2 - camx);
double dy = (y+h/2 - camy);
double dz = (z+d/2 - camz);
double dx = (x+w/2 - (camx-choffx));
double dy = (y+h/2 - (camy));
double dz = (z+d/2 - (camz-choffz));
double total = sqrt(dx*dx + dy*dy + dz*dz);
dx = 11.0*dx/total;
dy = 11.0*dy/total;

View File

@ -278,12 +278,13 @@ const char *vertexShaderSource = "#version 330 core\n"
// Fragment Shader Source
const char *fragmentShaderSource = "#version 330 core\n"
"uniform vec4 u_color;\n"
"uniform mat2 translation;\n"
"out vec4 FragColor;\n"
"in vec2 texCoord;\n"
"uniform sampler2D tex0;\n"
"void main() {\n"
// " FragColor = u_color;\n"
" FragColor = texture(tex0, texCoord) * u_color;\n"
" FragColor = texture(tex0, translation * texCoord) * u_color;\n"
"}\0";
const char *vertexShaderSourceR = "#version 330 core\n"
@ -546,9 +547,13 @@ int main_alt() {
// draw the map
glBindVertexArray(VAO);
//printf("1\n"); fflush(stdout);
gl_initRender(shaderProgram, shaderProgram, VAO, VBO);
gl_renderNearbyChunks(shaderProgram, shaderProgram, VAO, VBO, 1);
gl_renderProj(shaderProgram, shaderProgram, VAO, VBO);
//printf("2\n"); fflush(stdout);
gl_renderNearbyChunks(1);
//printf("3\n"); fflush(stdout);
gl_renderProj();
//printf("4\n"); fflush(stdout);
// draw data
glUseProgram(shaderProgramR);

View File

@ -209,15 +209,6 @@ void updateF(cube_0* cb, double dtime) {
}
bool is_colliding(float dtime) {
for(int k = 0; k < current_room->map_size; k++) {
double dist = distance_pt_cube_0_3d_infinite(camx, camy, camz, current_room->map[k]);
if(dist <= min_dist) {
if(updateForces) {
updateF(current_room->map[k], (double)dtime);
}
return true;
}
}
for(int w = -1; w <= 1; w++) {
for(int h = -1; h <= 1; h++) {
room* vstd = hashtbl_find_opt(visited, player_chx+w, player_chy+h);
@ -231,50 +222,52 @@ bool is_colliding(float dtime) {
return true;
}
}
}
}
}
for(int k = 0; k < current_room->tps_size; k++) {
double dist = distance_pt_cube_0_3d_infinite(camx, camy, camz, current_room->tps[k]->hitbox);
if(dist <= min_dist) {
if(updateForces) {
updateF(current_room->tps[k]->hitbox, (double)dtime);
}
int old_chx = player_chx;
int old_chy = player_chy;
player_chx = current_room->tps[k]->dest_chx;
player_chy = current_room->tps[k]->dest_chy;
current_room = hashtbl_find_opt(visited, player_chx, player_chy);
set_player_coords(old_chx, old_chy);
resetProj();
return true;
}
}
for(int k = 0; k < current_room->ent_len; k++) {
//printf("%d -> %d\n", k, current_room->ents[k]->entity_type);
if(current_room->ents[k]->entity_type != 9 || /* entityType == 9 */current_room->ents[k]->metai1) {
double dist = distance_pt_cube_0_3d_infinite(camx, camy, camz, current_room->ents[k]->pos);
//printf("%lf vs %lf\n", dist, min_dist);
if(dist <= min_dist) {
bool exists = true;
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], &(*(current_room->ents[k]->pos)));
if(*(current_room->ents[k]->hitpoints) <= 0) {
if(current_room->ents[k]->onDeath != NULL) {
(*current_room->ents[k]->onDeath)(dtime);
for(int k = 0; k < vstd->tps_size; k++) {
double dist = distance_pt_cube_0_3d_infinite(camx-2*room_width*w, camy, camz-2*room_depth*h, vstd->tps[k]->hitbox);
if(dist <= min_dist) {
if(updateForces) {
updateF(vstd->tps[k]->hitbox, (double)dtime);
}
remove_entity(current_room->ents, &current_room->ent_memlen, &current_room->ent_len, k);
is_clipping = false;
exists = false;
int old_chx = player_chx;
int old_chy = player_chy;
player_chx = vstd->tps[k]->dest_chx;
player_chy = vstd->tps[k]->dest_chy;
vstd = hashtbl_find_opt(visited, player_chx, player_chy);
set_player_coords(old_chx, old_chy);
resetProj();
return true;
}
}
if(exists && updateForces && current_room->ents[k]->entity_type != 0) {
updateF(current_room->ents[k]->pos, (double)dtime);
for(int k = 0; k < vstd->ent_len; k++) {
//printf("%d -> %d\n", k, vstd->ents[k]->entity_type);
if(vstd->ents[k]->entity_type != 9 || /* entityType == 9 */vstd->ents[k]->metai1) {
double dist = distance_pt_cube_0_3d_infinite(camx-2*room_width*w, camy, camz-2*room_depth*h, vstd->ents[k]->pos);
//printf("%lf vs %lf\n", dist, min_dist);
if(dist <= min_dist) {
bool exists = true;
if(vstd->ents[k]->onHit != NULL) {
(*vstd->ents[k]->onHit)(dtime, vstd->ents[k]->hitpoints, &vstd->ents[k]->damage, vstd->ents[k], &(*(vstd->ents[k]->pos)));
if(*(vstd->ents[k]->hitpoints) <= 0) {
if(vstd->ents[k]->onDeath != NULL) {
(*vstd->ents[k]->onDeath)(dtime);
}
remove_entity(vstd->ents, &vstd->ent_memlen, &vstd->ent_len, k);
is_clipping = false;
exists = false;
}
}
if(exists && updateForces && vstd->ents[k]->entity_type != 0) {
updateF(vstd->ents[k]->pos, (double)dtime);
}
if(exists && (vstd->ents[k]->entity_type == 9 || vstd->ents[k]->entity_type == 4 || vstd->ents[k]->entity_type == 5)) {
is_clipping = false;
}
return true;
}
}
}
if(exists && (current_room->ents[k]->entity_type == 9 || current_room->ents[k]->entity_type == 4 || current_room->ents[k]->entity_type == 5)) {
is_clipping = false;
}
return true;
}
}
}

View File

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

View File

@ -15,7 +15,7 @@ void free_proj();
void resetProj();
void appendProj(double x, double y, double z, double w, double h, double d, double vx, double vy, double vz, double ax, double ay, double az, int red, int green, int blue, int dmg, double ttl);
void removeProj(int k);
void gl_renderProj(unsigned int shaderProgram, unsigned int fragmentShader, unsigned int VAO, unsigned int VBO);
void gl_renderProj();
void updateProj(float dtime);
#endif