added various displays (coords, triangles count) + added 1 culling function

This commit is contained in:
Alexandre 2025-02-05 21:46:15 +01:00
parent 9585169fec
commit dec1124c43
26 changed files with 192 additions and 144 deletions

View File

@ -1,6 +1,6 @@
CC = gcc
FLAGS = -O2 -Wall -Wextra -g
LFLAGS = -lm -lncurses src/glad.c -ldl -lglfw -lcglm
LFLAGS = -lm src/glad.c -ldl -lglfw -lcglm
all: bin/back

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.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -3,7 +3,6 @@
#include <assert.h>
#include <math.h>
#include <stdbool.h>
#include <ncurses.h>
#include <unistd.h>
#include <termios.h>
#include <limits.h>
@ -231,6 +230,20 @@ teleporter* create_teleporter(
return tp ;
}
void copy_cube(cube_0* src, cube_0* dest) {
dest->red = src->red;
dest->green = src->green;
dest->blue = src->blue;
dest->hz_angle = src->hz_angle;
dest->vt_angle = src->vt_angle;
dest->x = src->x;
dest->y = src->y;
dest->z = src->z;
dest->w = src->w;
dest->h = src->h;
dest->d = src->d;
}
// ------------------------------------------------------------------------------------------------ //
double distance_pt_pt_3d(double x0, double y0, double z0, double x1, double y1, double z1) {
@ -313,54 +326,31 @@ void project_to_camera(double x0, double y0, double z0, double* rx, double* ry,
double z = z0 - camz ;
// rotate (y)
double xry = x*cos(rot_hz) + z*sin(rot_hz) ;
double xry = x*cos(rot_hz) - z*sin(rot_hz) ;
double yry = y ;
double zry = z*cos(rot_hz) - x*sin(rot_hz) ;
double zry = z*cos(rot_hz) + x*sin(rot_hz) ;
// rotate (x)
if(rx != NULL) {*rx = xry ;}
if(ry != NULL) {*ry = yry*cos(rot_vt) - zry*sin(rot_vt) ;}
if(rz != NULL) {*rz = zry*cos(rot_vt) + yry*sin(rot_vt) ;}
if(ry != NULL) {*ry = yry*cos(rot_vt) + zry*sin(rot_vt) ;}
if(rz != NULL) {*rz = zry*cos(rot_vt) - yry*sin(rot_vt) ;}
}
void project_to_cube(double x0, double y0, double z0, double* rx, double* ry, double* rz, cube_0 c) {
void project_to_cube(double x0, double y0, double z0, double* rx, double* ry, double* rz, cube_0* c) {
// align pt to (0, 0, 0)
double x = x0 - (c.x + c.w/2.0) ;
double y = y0 - (c.y + c.h/2.0) ;
double z = z0 - (c.z + c.d/2.0) ;
double x = x0 - (c->x + c->w/2.0) ;
double y = y0 - (c->y + c->h/2.0) ;
double z = z0 - (c->z + c->d/2.0) ;
// rotate (y)
double xry = x*cos(c.hz_angle) + z*sin(c.hz_angle) ;
double xry = x*cos(c->hz_angle) + z*sin(c->hz_angle) ;
double yry = y ;
double zry = z*cos(c.hz_angle) - x*sin(c.hz_angle) ;
double zry = z*cos(c->hz_angle) - x*sin(c->hz_angle) ;
// rotate (x)
if(rx != NULL) {*rx = xry ;}
if(ry != NULL) {*ry = yry*cos(c.vt_angle) - zry*sin(c.vt_angle);}
if(rz != NULL) {*rz = zry*cos(c.vt_angle) + yry*sin(c.vt_angle);}
}
double dist_to_cam_cube(double x0, double y0, double z0, cube_0 c) {
double px0, py0, pz0;
project_to_cube(x0, y0, z0, &px0, &py0, &pz0, c);
return distance_pt_pt_3d(px0 + c.x + c.w/2.0, py0 + c.y + c.h/2.0, pz0 + c.z + c.d/2.0, camx, camy, camz);
}
bool pt_equal_3D(pt_2d p1, pt_2d p2) {
return (p1.x == p2.x && p1.y == p2.y && p1.z == p2.z);
}
bool pt_equal_2D(pt_2d p1, pt_2d p2) {
return (p1.x == p2.x && p1.y == p2.y);
}
bool pt_equal_3D_eps(pt_2d p1, pt_2d p2, double epsilon) {
return (absf(p1.x - p2.x) <= epsilon && absf(p1.y - p2.y) <= epsilon && absf(p1.z - p2.z) <= epsilon);
}
bool pt_equal_2D_eps(pt_2d p1, pt_2d p2, double epsilon, bool debug0) {
if(debug0) {printf("(%lf, %lf)\n", absf(p1.x - p2.x), absf(p1.y - p2.y));}
return (absf(p1.x - p2.x) <= epsilon && absf(p1.y - p2.y) <= epsilon);
if(rx != NULL) {*rx = c->x + c->w/2.0 + xry ;}
if(ry != NULL) {*ry = c->y + c->h/2.0 + yry*cos(c->vt_angle) - zry*sin(c->vt_angle);}
if(rz != NULL) {*rz = c->z + c->d/2.0 + zry*cos(c->vt_angle) + yry*sin(c->vt_angle);}
}
// ------------------------------------------------------------------------------------------------ //

View File

@ -37,6 +37,7 @@ teleporter* create_teleporter(
double x, double y, double z, double w, double h, double d, double hz_a, double vt_a, int r, int g, int b,
int chx_dest, int chy_dest, double x_dest, double y_dest, double z_dest
);
void copy_cube(cube_0* src, cube_0* dest);
double convex_pt(double a, double b, double theta);
double distance_pt_pt_3d(double x0, double y0, double z0, double x1, double y1, double z1);
@ -58,7 +59,6 @@ void remove_entity(entity** arr, int* memlen, int* len, int index);
void add_entity(entity** arr, int* memlen, int* len, entity ent);
void project_to_camera(double x0, double y0, double z0, double* rx, double* ry, double* rz);
void project_to_cube(double x0, double y0, double z0, double* rx, double* ry, double* rz, cube_0 c);
double dist_to_cam_cube(double x0, double y0, double z0, cube_0 c);
void project_to_cube(double x0, double y0, double z0, double* rx, double* ry, double* rz, cube_0* c);
#endif

View File

@ -3,7 +3,6 @@
#include <assert.h>
#include <math.h>
#include <stdbool.h>
#include <ncurses.h>
#include <unistd.h>
#include <termios.h>
#include <limits.h>

View File

@ -3,7 +3,6 @@
#include <assert.h>
#include <math.h>
#include <stdbool.h>
#include <ncurses.h>
#include <unistd.h>
#include <termios.h>
#include <limits.h>
@ -16,6 +15,7 @@
#include "structure.h"
#include "base.h"
#include "move.h"
#include "menus.h"
#include "proj.h"
#include "entities.h"
#include "generation.h"
@ -136,18 +136,37 @@ void gl_renderCube(unsigned int shaderProgram, unsigned int fragmentShader, unsi
glUniform4f(glGetUniformLocation(fragmentShader, "u_color"), c->red/255.0f, c->green/255.0f, c->blue/255.0f, 1.0f);
glDrawArrays(GL_TRIANGLES, 0, 36);
triCount += 12;
}
double px, py, pz, pz2;
bool is_visible(cube_0* cb, double offx, double offy, double offz) {
for(int d = 0; d < 8; d++) {
project_to_cube(cb->x+cb->w*(d%2==0)+offx, cb->y+cb->h*((d/2)%2==0)+offy, cb->z+cb->d*((d/4)%2==0)+offz, &px, &py, &pz, cb);
project_to_camera(px, py, pz, NULL, NULL, &pz2);
if(pz2 > near) {
return true;
}
}
return false;
}
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++) {
gl_renderCube(shaderProgram, fragmentShader, VAO, VBO, rtd->map[k], offx, offy, offz);
if(is_visible(rtd->map[k], offx, offy, offz)) {
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);
if(is_visible(rtd->tps[k]->hitbox, offx, offy, offz)) {
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);
if(is_visible(rtd->ents[k].pos, offx, offy, offz)) {
gl_renderCube(shaderProgram, fragmentShader, VAO, VBO, rtd->ents[k].pos, offx, offy, offz);
}
}
}
}
@ -174,4 +193,12 @@ void gl_initRender(unsigned int shaderProgram, unsigned int fragmentShader, unsi
glm_lookat((vec3){(float)camx, (float)camy, (float)camz}, direction, (vec3){0.0f, 1.0f, 0.0f}, view);
glm_perspective(glm_rad((float)fov), 1500.0f / 1000.0f, 0.1f, 100.0f, projection);
}
void gl_drawData(unsigned int shaderProg) {
gl_drawInteger(shaderProg, (int)camx, 0.95f, 0.9f, 0.05f, 255, 255, 255, 0.005, -1);
gl_drawInteger(shaderProg, (int)camy, 0.95f, 0.75f, 0.05f, 255, 255, 255, 0.005, -1);
gl_drawInteger(shaderProg, (int)camz, 0.95f, 0.6f, 0.05f, 255, 255, 255, 0.005, -1);
gl_drawInteger(shaderProg, player_hp, -0.95f, 0.9f, 0.05f, 255-player_hp/4, player_hp/4, 0, 0.005f, 1);
}

View File

@ -8,4 +8,6 @@ void gl_renderNearbyChunks(unsigned int shaderProgram, unsigned int fragmentShad
void gl_initRender(unsigned int shaderProgram, unsigned int fragmentShader, unsigned int VAO, unsigned int VBO);
void init_vertices();
void gl_drawData(unsigned int shaderProg);
#endif

View File

@ -3,7 +3,6 @@
#include <assert.h>
#include <math.h>
#include <stdbool.h>
#include <ncurses.h>
#include <unistd.h>
#include <termios.h>
#include <limits.h>

View File

@ -3,7 +3,6 @@
#include <assert.h>
#include <math.h>
#include <stdbool.h>
#include <ncurses.h>
#include <unistd.h>
#include <termios.h>
#include <limits.h>
@ -424,6 +423,35 @@ void parse_rooms(int n_rooms) {
free(name);
}
int divider = 8;
void generate_terrain(room* r) {
int rsize = 4*(room_width/divider)*(room_width/divider)*(room_depth/divider)*(room_depth/divider); // floor size (with 1x1 cubes)
cube** newMap = malloc(sizeof(cube_0*)*(rsize+r->map_size));
for(int k = 0; k < rsize+r->map_size; k++) {
newMap[k] = malloc(sizeof(cube_0));
}
for(int k = 0; k < r->map_size; k++) {
copy_cube(r->map[k], newMap[k]);
free(r->map[k]);
}
free(r->map);
// use whatever noise funtcion here
int i = r->map_size;
for(int l = -room_width; l < room_width; l+=divider) {
for(int d = -room_depth; d < room_depth; d+=divider) {
newMap[i] = create_cube_0((double)l, -20.0, (double)d, (double)divider, 1.0, (double)divider, 0.0, 0.0,32, 128, 32);
i+=1;
}
}
r->map = newMap;
r->map_size += rsize;
}
void generate_nearby_chunks(int render_dist) {
for(int w = -render_dist; w <= render_dist; w++) {
for(int h = -render_dist; h <= render_dist; h++) {
@ -438,6 +466,7 @@ void generate_nearby_chunks(int render_dist) {
printf("chose %d\n", k);
room* new = malloc(sizeof(room));
copy_room(pool[k].area, new, player_chx + w, player_chy + h);
generate_terrain(new);
hashtbl_add(visited, player_chx + w, player_chy + h, new);
k = pool_size+1;
}

View File

@ -3,7 +3,6 @@
#include <assert.h>
#include <math.h>
#include <stdbool.h>
#include <ncurses.h>
#include <unistd.h>
#include <termios.h>
#include <limits.h>

View File

@ -3,7 +3,6 @@
#include <assert.h>
#include <math.h>
#include <stdbool.h>
#include <ncurses.h>
#include <unistd.h>
#include <termios.h>
#include <limits.h>
@ -22,6 +21,7 @@
#include "generation.h"
double sim_time ;
int triCount;
void processInput(GLFWwindow *window, float dtime) {
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
@ -293,15 +293,17 @@ int main_alt() {
clock_t finish = clock();
clock_t origin = clock();
while(!glfwWindowShouldClose(window)) {
while(!glfwWindowShouldClose(window) && player_hp >= 0) {
// input
// -----
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
triCount = 0;
origin = clock();
generate_nearby_chunks(1);
// draw the map
glUseProgram(shaderProgram);
glBindVertexArray(VAO);
@ -309,11 +311,14 @@ int main_alt() {
gl_renderNearbyChunks(shaderProgram, shaderProgram, VAO, VBO, 1);
gl_renderProj(shaderProgram, shaderProgram, VAO, VBO);
// draw data
glUseProgram(shaderProgramR);
glBindVertexArray(RVAO);
finish = clock();
gl_drawInteger(shaderProgramR, (int)(1.0f/(((float)finish - (float)origin)/CLOCKS_PER_SEC)), -0.5f, 0.85f, 0.05, 32, 255, 32, 0.005, 1);
gl_drawInteger(shaderProgramR, (int)(1.0f/(((float)finish - (float)origin)/CLOCKS_PER_SEC)), 0.9f, -0.85f, 0.05, 32, 255, 32, 0.005, -1);
gl_drawInteger(shaderProgramR, triCount, 0.0f, 0.92f, 0.04f, 128, 128, 128, 0.005f, 1);
gl_drawData(shaderProgramR);
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
// -------------------------------------------------------------------------------
@ -350,5 +355,6 @@ int main_alt() {
int main(int argc, char** argv) {
srand(time(NULL));
triCount = 0;
return main_alt();
}

View File

@ -3,7 +3,6 @@
#include <assert.h>
#include <math.h>
#include <stdbool.h>
#include <ncurses.h>
#include <unistd.h>
#include <termios.h>
#include <limits.h>
@ -71,10 +70,9 @@ void gl_drawDigit(unsigned int fragShader, int n, float x, float y, float size,
gl_drawRect(fragShader, x+size/2-width, y-size-width, 2*width, size+2*width, r, g, b);
gl_drawRect(fragShader, x-size/2-width, y-size-width, size+2*width, 2*width, r, g, b);
} else if(n == 4) {
gl_drawRect(fragShader, x-size/2-width, y-size-width, size+2*width, 2*width, r, g, b);
gl_drawRect(fragShader, x-size/2-width, y-size-width, 2*width, 2*size+2*width, r, g, b);
gl_drawRect(fragShader, x-size/2-width, y+size-width, size+2*width, 2*width, r, g, b);
gl_drawRect(fragShader, x+size/2-width, y-size-width, 2*width, 2*size+2*width, r, g, b);
gl_drawRect(fragShader, x-size/2-width, y-width, size+2*width, 2*width, r, g, b);
gl_drawRect(fragShader, x-size/2-width, y-width, 2*width, size+2*width, r, g, b);
} else if(n == 5) {
gl_drawRect(fragShader, x-size/2-width, y+size-width, size+2*width, 2*width, r, g, b);
gl_drawRect(fragShader, x-size/2-width, y-width, 2*width, size+2*width, r, g, b);
@ -111,14 +109,24 @@ void gl_drawDigit(unsigned int fragShader, int n, float x, float y, float size,
void gl_drawInteger(unsigned int fragShader, int n, float x, float y, float size, int r, int g, int b, float width, int side) {
int left = n;
float curx = x;
if(left < 0) {
left *= (-1);
}
if(side == 1) {
curx += ((ln_baseN(n, 10)-1)*(size+4*width));
curx += ((ln_baseN(abs(n), 10)-1)*(size+4*width));
}
while(left > 0) {
gl_drawDigit(fragShader, left%10, curx, y, size, r, g, b, width);
curx -= (size+4*width);
left = left/10;
}
if(n == 0) {
gl_drawDigit(fragShader, 0, curx, y, size, r, g, b, width);
curx -= (size+4*width);
}
if(n < 0) {
gl_drawRect(fragShader, curx-size/2-width, y, size+2*width, 2*width, r, g, b);
}
}
void gl_initDrawRect(unsigned int shaderProgram) {

View File

@ -6,7 +6,6 @@ void initMenus();
void gl_drawRect(unsigned int fragShader, float x, float y, float w, float h, int r, int g, int b);
void gl_initDrawRect(unsigned int shaderProgram);
void gl_drawDigit(unsigned int fragShader, int n, float x, float y, float size, int r, int g, int b, float width);
void gl_drawInteger(unsigned int fragShader, int n, float x, float y, float size, int r, int g, int b, float width, int side);
#endif

View File

@ -3,7 +3,6 @@
#include <assert.h>
#include <math.h>
#include <stdbool.h>
#include <ncurses.h>
#include <unistd.h>
#include <termios.h>
#include <limits.h>

View File

@ -3,7 +3,6 @@
#include <assert.h>
#include <math.h>
#include <stdbool.h>
#include <ncurses.h>
#include <unistd.h>
#include <termios.h>
#include <limits.h>
@ -62,20 +61,6 @@ void appendProj(double x, double y, double z, double w, double h, double d, doub
}
}
void copy_cube(cube_0* src, cube_0* dest) {
dest->red = src->red;
dest->green = src->green;
dest->blue = src->blue;
dest->hz_angle = src->hz_angle;
dest->vt_angle = src->vt_angle;
dest->x = src->x;
dest->y = src->y;
dest->z = src->z;
dest->w = src->w;
dest->h = src->h;
dest->d = src->d;
}
void removeProj(int k) {
if(k >= 0 && k < bullets_id) {
copy_cube(bullets[bullets_id-1].pos, bullets[k].pos);
@ -113,6 +98,10 @@ void updateProj(float dtime) {
if(*(bullets[k].ttl) <= 0.0 || is_colliding_with_map(bullets[k].pos) || is_colliding_with_tp(bullets[k].pos)) {
removeProj(k);
k -= 1;
} else if(distance_pt_cube_0_3d(camx, camy, camz, bullets[k].pos) <= 0.1) {
player_hp -= bullets[k].damage;
removeProj(k);
k -= 1;
} else {
bullets[k].vel->x += ((double)dtime)*bullets[k].acc->x;
bullets[k].vel->y += ((double)dtime)*bullets[k].acc->y;

View File

@ -5,123 +5,125 @@ typedef struct pt_2d {
double x;
double y;
double z;
} pt_2d ;
} pt_2d;
struct cube_0 {
int red; int green; int blue ;
int red; int green; int blue;
double x;
double y;
double z;
double w;
double h;
double d;
double hz_angle ;
double vt_angle ;
} ;
typedef struct cube_0 cube_0 ;
typedef cube_0* cube ;
double hz_angle;
double vt_angle;
};
typedef struct cube_0 cube_0;
typedef cube_0* cube;
typedef struct teleporter {
cube_0* hitbox ;
int dest_chx ; // in the pool, these are offsets
int dest_chy ;
double dest_x ;
double dest_y ;
double dest_z ;
} teleporter ;
cube_0* hitbox;
int dest_chx; // in the pool, these are offsets
int dest_chy;
double dest_x;
double dest_y;
double dest_z;
} teleporter;
typedef struct entity {
cube_0* pos ;
void (*updatePos)(double x, double y, double z, double w, double h, double d, double hz_angle, double vt_angle, float dtime, cube_0* ret) ;
cube_0* pos;
// act as velocity function
void (*updatePos)(double x, double y, double z, double w, double h, double d, double hz_angle, double vt_angle, float dtime, cube_0* ret);
void (*onHit)(float dtime, int* hp, int* dmg, cube_0* ret) ;
// triggers when object is hit
void (*onHit)(float dtime, int* hp, int* dmg, cube_0* ret);
void (*onDeath)(float dtime) ;
// triggers when <hitpoints> goes negative
// triggers when <hitpoints> goes negative (death)
void (*onDeath)(float dtime);
int damage ;
int* hitpoints ;
} entity ;
int damage;
int* hitpoints;
} entity;
struct room {
// (0, 0, 0) = bottom, left and down
int chunk_x ;
int chunk_y ;
cube_0** map ;
int map_size ;
teleporter** tps ;
int tps_size ;
entity* ents ;
int ent_len ;
int ent_memlen ;
} ;
typedef struct room room ;
int chunk_x;
int chunk_y;
cube_0** map;
int map_size;
teleporter** tps;
int tps_size;
entity* ents;
int ent_len;
int ent_memlen;
};
typedef struct room room;
struct cell {
int chx ;
int chy ;
room* area ;
struct cell* next ;
} ;
int chx;
int chy;
room* area;
struct cell* next;
};
typedef struct cell cell ;
typedef struct cell* linkedList ;
typedef struct cell cell;
typedef struct cell* linkedList;
struct hashtbl_0 {
int tabLength ;
int insertedElts ;
int maxInserted ;
linkedList* tab ;
} ;
typedef struct hashtbl_0 hashtbl_0 ;
typedef hashtbl_0* hashtbl ;
int tabLength;
int insertedElts;
int maxInserted;
linkedList* tab;
};
typedef struct hashtbl_0 hashtbl_0;
typedef hashtbl_0* hashtbl;
// ------------------------------------------------ //
extern double sim_time ;
extern double sim_time;
extern double camx ;
extern double camy ;
extern double camz ;
extern double camx;
extern double camy;
extern double camz;
extern double rot_hz ;
extern double rot_vt ;
extern double rot_hz;
extern double rot_vt;
extern double tan_fov ;
extern double tan_fov;
extern bool has_changed ;
extern bool has_changed;
extern hashtbl visited ;
extern room* current_room ;
extern hashtbl visited;
extern room* current_room;
extern int player_chx ;
extern int player_chy ;
extern int player_chx;
extern int player_chy;
extern int* drawOrder ;
extern int* drawOrder;
extern int coins ;
extern int coins;
extern int draw_type ;
extern int draw_type;
extern int player_hp ;
extern int player_hp;
extern int fade_dmg ;
extern int fade_dmg;
extern bool stop_evetything ;
extern double room_width ;
extern bool stop_evetything;
extern double room_width;
// no height yet (requires a rewrite oh hashtbl)
extern double room_depth ;
extern double room_depth;
// ---------------------------------------------------------------------------------------------------- //
extern double sensitivity ;
extern double fov ;
extern double speed ;
extern double min_dist ;
extern double sensitivity;
extern double fov;
extern double speed;
extern double min_dist;
// ---------------------------------------------------------------------------------------------------- //
extern float vertices[108];
extern float rectDefault[18];
extern int triCount ;
#endif