From 831523aa106dca752b351927e671ca9a3731bb62 Mon Sep 17 00:00:00 2001 From: koma Date: Thu, 28 Apr 2022 09:25:48 +0200 Subject: [PATCH] a --- Makefile | 4 +- include/app.h | 46 +++----- include/camera.h | 8 +- include/utils.h | 6 +- src/app.c | 299 ++++++++++++++++++++++------------------------- src/camera.c | 119 +++++++++++++++++-- src/game.c | 63 ---------- src/main.c | 216 +++++++++++++++++++++++++++------- src/utils.c | 19 ++- 9 files changed, 458 insertions(+), 322 deletions(-) diff --git a/Makefile b/Makefile index c43d66e..caedc36 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ all: - gcc -Iinclude/ src/app.c src/camera.c src/main.c src/game.c src/utils.c src/noise.c -lSDL2 -lGL -lm -lSDL2_image -o test -Wall + gcc -Iinclude/ src/app.c src/camera.c src/main.c src/game.c src/utils.c src/noise.c src/chunk.c -lSDL2 -lGL -lm -lSDL2_image -lGLU -o test -Wall debug: - gcc -Iinclude/ src/app.c src/camera.c src/main.c src/game.c src/utils.c src/noise.c -lSDL2 -lGL -lm -lSDL2_image -o test -Wall -g + gcc -Iinclude/ src/app.c src/camera.c src/main.c src/game.c src/utils.c src/noise.c src/chunk.c -lSDL2 -lGL -lm -lSDL2_image -o test -Wall -g diff --git a/include/app.h b/include/app.h index 8b18a43..1a007a0 100644 --- a/include/app.h +++ b/include/app.h @@ -8,9 +8,9 @@ #include #include "camera.h" #include "noise.h" +#include "chunk.h" #define SPEED 5 -#define GRAVITY 10 #define VIEWPORT_RATIO (16.0 / 9.0) #define VIEWPORT_ASPECT 50.0 @@ -21,24 +21,11 @@ typedef unsigned int uint; #define CHUNK_MAX_Y 16 #define CHUNK_MAX_Z 128 -typedef enum { - BLOCKTYPE_AIR, - BLOCKTYPE_GRASS, - BLOCKTYPE_DIRT, - BLOCKTYPE_STONE, -} Block_type; - -typedef struct -{ - Block_type type; - bool visible; -} Block; - typedef struct { - vec3f start_pos; - Block blocks[CHUNK_MAX_X][CHUNK_MAX_Y][CHUNK_MAX_Z]; - GLuint id; -} Chunk; + GLuint tid; + SDL_Surface *surface; + int type; +} Type_surface; typedef struct { bool is_running; @@ -46,26 +33,21 @@ typedef struct { SDL_GLContext context; Camera camera; double frame_time; - SDL_Surface *surface; - GLuint tid; - unsigned int chunk_count; - Chunk *chunks; + ChunkManager *cm; + Type_surface *surfaces; + unsigned int surface_count; } App; void app_generate_world(App *app, int a); -bool chunk_is_block_neighboring_block(Chunk* chunk, vec3i pos); -void chunk_set_blocks_visibility(Chunk* chunk); -void chunk_create_displayl(App *app, Chunk *chunk); -void chunk_render(Chunk* chunk); -void chunk_update(App *app, Chunk *chunk); -int app_get_current_chunk_index(App *app); +void app_load_textures(App *app); + +SDL_Surface* app_get_surface(App *app, Block_type type); + int check_intersection_block(App *app, vec3i *retv, int *chunk_index); void app_break_block(App *app); -bool is_block(Block b); - -bool app_check_collision(App *app); +bool app_check_collision(vec3f camera_pos, ChunkManager *cm); void init_app(App *app, uint w, uint h); @@ -73,8 +55,6 @@ void init_opengl(); void handle_events(App *app); -void draw_cube(App *app, float x, float y, float z); - void reshape(GLsizei width, GLsizei height); void update_app(App* app); diff --git a/include/camera.h b/include/camera.h index 7e86fc3..38e14b3 100644 --- a/include/camera.h +++ b/include/camera.h @@ -4,18 +4,18 @@ #include "utils.h" #include +#include "chunk.h" +#define GRAVITY 1 /** * Camera, as a moving point with direction */ -struct App; - typedef struct Camera { vec3f position; vec3f rotation; vec3f speed; - struct App *app; + ChunkManager *cm; } Camera; /** @@ -26,7 +26,7 @@ void init_camera(Camera* camera); /** * Update the position of the camera. */ -void update_camera(Camera* camera, struct App *app, double time); +void update_camera(Camera* camera, double time); /** * Apply the camera settings to the view transformation. diff --git a/include/utils.h b/include/utils.h index 0ac7d61..4aa1942 100644 --- a/include/utils.h +++ b/include/utils.h @@ -3,6 +3,8 @@ #include #include +#include + #define CHUNK_MAX_X 16 #define CHUNK_MAX_Y 16 #define CHUNK_MAX_Z 128 @@ -49,7 +51,7 @@ double degree_to_radian(double degree); bool is_oob(int i, int min, int max); -void mult_matrix(float *matrix, vec4f vector, vec4f *ret); +void mult_matrix(float m1[16], float m2[16], float mout[16]); bool matrix_inverse(const float m[16], float invOut[16]); @@ -58,4 +60,6 @@ void vec3f_normalize(vec3f *v); int irand_range(int min, int max); bool check_index(int x, int y, int z); + +void print_vec3f(vec3f v, const char *str); #endif /* UTILS_H */ diff --git a/src/app.c b/src/app.c index 244aee7..6476d50 100644 --- a/src/app.c +++ b/src/app.c @@ -28,45 +28,95 @@ void init_app(App *app, uint w, uint h) printf("Failed to create OpenGL context.\n"); exit(1); } - app->is_running = true; //SDL_ShowCursor(SDL_DISABLE); init_camera(&app->camera); init_opengl(); reshape(w, h); + init_camera(&app->camera); + app->camera.cm = app->cm; - init_camera(&(app->camera)); + app->surface_count = BLOCKTYPE_COUNT-2; + app->surfaces = malloc(app->surface_count+1*sizeof(Type_surface)); - app->surface = IMG_Load("assets/block.png"); - app->tid = 0; - - if(app->surface==NULL) { - printf("error\n"); - exit(1); + app_load_textures(app); + + app_generate_world(app, 10); + app->is_running = true; +} + +void app_load_textures(App *app) +{ + app->surfaces[0].type = BLOCKTYPE_GRASS; + app->surfaces[1].type = BLOCKTYPE_DIRT; + app->surfaces[2].type = BLOCKTYPE_STONE; + + app->surfaces[0].surface = IMG_Load("assets/block.png"); + //app->surfaces[1].surface = IMG_Load("assets/block.png"); + //app->surfaces[2].surface = IMG_Load("assets/block.png"); + + for (int i = 0; i < app->surface_count-2; i++) { + //app->surfaces[i].tid = i; + //if(app->surfaces[i].surface == NULL) { + // printf("error loading texture\n"); + // exit(1); + //} + //glGenTextures(1, &app->surfaces[i].tid); + //glBindTexture(GL_TEXTURE_2D, app->surfaces[i].tid); + // + //int Mode = GL_RGB; + + //if(app->surfaces[i].surface->format->BytesPerPixel == 4) { + // Mode = GL_RGBA; + //} + // + //glTexImage2D(GL_TEXTURE_2D, 0, Mode, app->surfaces[i].surface->w, + // app->surfaces[i].surface->h, 0, Mode, GL_UNSIGNED_BYTE, + // app->surfaces[i].surface->pixels); + // + //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + + //glBindTexture(GL_TEXTURE_2D, app->surfaces[i].tid); + } + app->surfaces[0].tid = 1; + if(app->surfaces[0].surface == NULL) { + printf("error loading texture\n"); + exit(1); + } + glGenTextures(1, &app->surfaces[0].tid); + glBindTexture(GL_TEXTURE_2D, app->surfaces[0].tid); + + int Mode = GL_RGB; + + if(app->surfaces[0].surface->format->BytesPerPixel == 4) { + Mode = GL_RGBA; + } + + glTexImage2D(GL_TEXTURE_2D, 0, Mode, app->surfaces[0].surface->w, + app->surfaces[0].surface->h, 0, Mode, GL_UNSIGNED_BYTE, + app->surfaces[0].surface->pixels); + + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + + glBindTexture(GL_TEXTURE_2D, app->surfaces[0].tid); +} + +SDL_Surface* app_get_surface(App *app, Block_type type) +{ + for (int i = 0; i < app->surface_count; i++) { + if (app->surfaces[i].type == type) + return app->surfaces[i].surface; } - glGenTextures(1, &app->tid); - glBindTexture(GL_TEXTURE_2D, app->tid); - - int Mode = GL_RGB; - if(app->surface->format->BytesPerPixel == 4) { - Mode = GL_RGBA; - } - - glTexImage2D(GL_TEXTURE_2D, 0, Mode, app->surface->w, app->surface->h, 0, Mode, GL_UNSIGNED_BYTE, app->surface->pixels); - - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - - glBindTexture(GL_TEXTURE_2D, app->tid); + return NULL; } void update_app(App* app) { - app->camera.speed.z -= GRAVITY * (app->frame_time/1000); - app_check_collision(app); - update_camera(&(app->camera), app, app->frame_time/1000); + update_camera(&(app->camera), app->frame_time/1000); } void init_opengl() @@ -145,8 +195,6 @@ void handle_events(App* app) camera_set_side_speed(&(app->camera), -SPEED); break; case SDL_SCANCODE_SPACE: - if (app_check_collision(app)) - app->camera.position.z+=0.3; camera_set_vertical_speed(&app->camera, SPEED); break; case SDL_SCANCODE_LSHIFT: @@ -195,52 +243,6 @@ void handle_events(App* app) } -void draw_cube(App *app, float x, float y, float z) -{ - /*x*=2; - y*=2; - z*=2; - */ - glBegin( GL_QUADS ); - - glColor3f(1, 1, 1); - glTexCoord2f(0, 0); glVertex3f(x+1.0, y, z-1.0 ); - glTexCoord2f(0, 1); glVertex3f(x, y, z-1.0); - glTexCoord2f(1, 1); glVertex3f(x, y, z); - glTexCoord2f(1, 0); glVertex3f(x+1.0, y, z); - - glColor3f(1, 1, 1); - glTexCoord2f(0, 0); glVertex3f(x+1.0, y-1.0, z-1.0); - glTexCoord2f(0, 1); glVertex3f(x, y-1.0, z-1.0); - glTexCoord2f(1, 1); glVertex3f(x, y-1.0, z); - glTexCoord2f(1, 0); glVertex3f(x+1.0, y-1.0, z); - - glColor3f(1, 1, 1); - glTexCoord2f(0, 0); glVertex3f(x+1.0, y, z); - glTexCoord2f(0, 1); glVertex3f(x, y, z); - glTexCoord2f(1, 1); glVertex3f(x, y-1.0, z); - glTexCoord2f(1, 0); glVertex3f(x+1.0, y-1.0, z); - - glColor3f(1, 1, 1); - glTexCoord2f(0, 0); glVertex3f(x, y, z-1.0); // Top-Right of back face - glTexCoord2f(0, 1); glVertex3f(x+1.0, y, z-1.0); // Top-Left of back face - glTexCoord2f(1, 1); glVertex3f(x+1.0, y-1.0, z-1.0); // Bottom-Left of back face - glTexCoord2f(1, 0); glVertex3f(x, y-1.0, z-1.0); // Bottom-Right of back face - - glColor3f(1, 1, 1); - glTexCoord2f(0, 0); glVertex3f(x, y, z); // Top-Right of left face - glTexCoord2f(0, 1); glVertex3f(x, y, z-1.0); // Top-Left of left face - glTexCoord2f(1, 1); glVertex3f(x, y-1.0, z-1.0); // Bottom-Left of left face - glTexCoord2f(1, 0); glVertex3f(x, y-1.0, z); // Bottom-Right of left face - - glColor3f(1, 1, 1); - glTexCoord2f(0, 0); glVertex3f(x+1.0, y, z); // Top-Right of left face - glTexCoord2f(0, 1); glVertex3f(x+1.0, y, z-1.0); // Top-Left of left face - glTexCoord2f(1, 1); glVertex3f(x+1.0, y-1.0, z-1.0); // Bottom-Left of left face - glTexCoord2f(1, 0); glVertex3f(x+1.0, y-1.0, z); // Bottom-Right of left face - glEnd(); - glFlush(); -} float calc_frame_time(struct timespec *start, struct timespec *end) { @@ -253,9 +255,9 @@ float calc_frame_time(struct timespec *start, struct timespec *end) void app_generate_world(App *app, int a) { - app->chunk_count = a*a; - app->chunks = malloc(a*a*sizeof(Chunk)); - memset(app->chunks, 0, a*a*sizeof(Chunk)); + app->cm->chunk_count = a*a; + app->cm->chunks = malloc(a*a*sizeof(Chunk)); + memset(app->cm->chunks, 0, a*a*sizeof(Chunk)); unsigned char ptable[512]; noise_init_ptable(ptable); @@ -276,38 +278,19 @@ void app_generate_world(App *app, int a) else if (mz > CHUNK_MAX_Z) mz = CHUNK_MAX_Z-1; for (int z = 0; z < mz; z++) - c.blocks[x][y][z].type = BLOCKTYPE_STONE; + c.blocks[x][y][z].type = BLOCKTYPE_DIRT; for (int z = mz; z < CHUNK_MAX_Z; z++) c.blocks[x][y][z].type = BLOCKTYPE_AIR; } } chunk_set_blocks_visibility(&c); - chunk_create_displayl(app, &c); + chunk_create_displayl(&c); - app->chunks[i+(j*a)] = c; + app->cm->chunks[i+(j*a)] = c; } } } -int app_get_current_chunk_index(App *app) -{ - for (int i = 0; i < app->chunk_count; i++) { - for (int x = 0; x < CHUNK_MAX_X; x++) { - for (int y = 0; y < CHUNK_MAX_Y; y++) { - for (int z = 0; z < CHUNK_MAX_Z; z++) { - if (app->camera.position.x < app->chunks[i].start_pos.x + CHUNK_MAX_X && - app->camera.position.x > app->chunks[i].start_pos.x && - app->camera.position.y < app->chunks[i].start_pos.y + CHUNK_MAX_Y && - app->camera.position.y > app->chunks[i].start_pos.y) { - return i; - } - } - } - } - } - - return -1; -} bool is_block(Block b) { @@ -316,59 +299,59 @@ bool is_block(Block b) return false; } -bool app_check_collision(App *app) -{ - int i = app_get_current_chunk_index(app); - - if (i < 0) - return false; - - vec3f c_pos = app->camera.position; - vec3i c_index = { floor(c_pos.x), floor(c_pos.y), floor(c_pos.z) }; - - //for (int x = c_index.x-1; x < c_index.x+1; x++) { - // for (int y = c_index.y-1; y < c_index.y+1; y++) { - // for (int z = c_index.z-1; y < c_index.z+1; z++) { - // if(!check_index(x, y, z)) - // continue; - // if(is_block(app->chunks[i].blocks[x][y][z])) { - // - // } - // } - // } - //} - - int x = c_index.x; - int y = c_index.y; - int z = c_index.z; - - if (check_index(x-1, y, z)) - if (is_block(app->chunks[i].blocks[x-1][y][z])) - //camera_set_side_speed(&app->camera, 0.0); - if (check_index(x+1, y, z)) - if (is_block(app->chunks[i].blocks[x+1][y][z])) - //camera_set_side_speed(&app->camera, 0.0); - if (check_index(x, y-1, z)) - if (is_block(app->chunks[i].blocks[x][y-1][z])) - //camera_set_speed(&app->camera, 0.0); - if (check_index(x, y+1, z)) - if (is_block(app->chunks[i].blocks[x][y+1][z])) - //camera_set_speed(&app->camera, 0.0); - if (check_index(x, y, z-1)) - if (is_block(app->chunks[i].blocks[x][y][z-1])) - camera_set_vertical_speed(&app->camera, 0.0); - if (check_index(x, y, z+1)) - if (is_block(app->chunks[i].blocks[x][y][z+1])) - camera_set_vertical_speed(&app->camera, 0.0); - - return false; -} +//bool app_check_collision(App *app) +//{ +// int i = app_get_current_chunk_index(app); +// +// if (i < 0) +// return false; +// +// vec3f c_pos = app->camera.position; +// vec3i c_index = { floor(c_pos.x), floor(c_pos.y), floor(c_pos.z) }; +// +// //for (int x = c_index.x-1; x < c_index.x+1; x++) { +// // for (int y = c_index.y-1; y < c_index.y+1; y++) { +// // for (int z = c_index.z-1; y < c_index.z+1; z++) { +// // if(!check_index(x, y, z)) +// // continue; +// // if(is_block(app->chunks[i].blocks[x][y][z])) { +// // +// // } +// // } +// // } +// //} +// +// int x = c_index.x; +// int y = c_index.y; +// int z = c_index.z; +// +// if (check_index(x-1, y, z)) +// if (is_block(app->chunks[i].blocks[x-1][y][z])) +// //camera_set_side_speed(&app->camera, 0.0); +// if (check_index(x+1, y, z)) +// if (is_block(app->chunks[i].blocks[x+1][y][z])) +// //camera_set_side_speed(&app->camera, 0.0); +// if (check_index(x, y-1, z)) +// if (is_block(app->chunks[i].blocks[x][y-1][z])) +// //camera_set_speed(&app->camera, 0.0); +// if (check_index(x, y+1, z)) +// if (is_block(app->chunks[i].blocks[x][y+1][z])) +// //camera_set_speed(&app->camera, 0.0); +// if (check_index(x, y, z-1)) +// if (is_block(app->chunks[i].blocks[x][y][z-1])) +// camera_set_vertical_speed(&app->camera, 0.0); +// if (check_index(x, y, z+1)) +// if (is_block(app->chunks[i].blocks[x][y][z+1])) +// camera_set_vertical_speed(&app->camera, 0.0); +// +// return false; +//} int check_intersection_block(App *app, vec3i *retv, int *chunk_index) { printf("%s\n", __func__); - int i = app_get_current_chunk_index(app); + int i = chunk_get_current_chunk_index(app->cm, app->camera.position); *chunk_index = i; vec3f c_pos = app->camera.position; @@ -380,15 +363,15 @@ int check_intersection_block(App *app, vec3i *retv, int *chunk_index) for (int x = 0; x < CHUNK_MAX_X; x++) { for (int y = 0; y < CHUNK_MAX_Y; y++) { for (int z = 0; z < CHUNK_MAX_Z; z++) { - if (app->chunks[i].blocks[x][y][z].visible) { + if (app->cm->chunks[i].blocks[x][y][z].visible) { for (float j = 0; j < 8; j+=0.2) { - vec3f ray = {(c_pos.x - app->chunks[i].start_pos.x) + c_dir.x * j, - (c_pos.y - app->chunks[i].start_pos.y) + c_dir.y * j, - (c_pos.z - app->chunks[i].start_pos.z) + c_dir.z * j}; + vec3f ray = {(c_pos.x - app->cm->chunks[i].start_pos.x) + c_dir.x * j, + (c_pos.y - app->cm->chunks[i].start_pos.y) + c_dir.y * j, + (c_pos.z - app->cm->chunks[i].start_pos.z) + c_dir.z * j}; printf("ray x: %f y: %f z: %f\n", ray.x, ray.y, ray.z); vec3i rayi = {round(ray.x/1), round(ray.y/1), round(ray.z/1)}; - if (is_block(app->chunks[i].blocks[rayi.x][rayi.y][rayi.z])) { + if (is_block(app->cm->chunks[i].blocks[rayi.x][rayi.y][rayi.z])) { retv->x = rayi.x; retv->y = rayi.y; retv->z = rayi.z; @@ -409,15 +392,15 @@ void app_break_block(App *app) int i; int ret = check_intersection_block(app, &block, &i); - printf("chunk index: %d\n", i); - printf("player x: %f y: %f z: %f\n", app->camera.position.x, app->camera.position.y, app->camera.position.z); - printf("chunk %d. min x: %f y: %f max x: %f y: %f\n", i, app->chunks[i].start_pos.x, - app->chunks[i].start_pos.y, app->chunks[i].start_pos.x+CHUNK_MAX_X, app->chunks[i].start_pos.y+CHUNK_MAX_Y); - printf("broken block x: %f y: %f z: %f\n", block.x+app->chunks[i].start_pos.x, block.y+app->chunks[i].start_pos.y, block.z+app->chunks[i].start_pos.z); + //printf("chunk index: %d\n", i); + //printf("player x: %f y: %f z: %f\n", app->camera.position.x, app->camera.position.y, app->camera.position.z); + //printf("chunk %d. min x: %f y: %f max x: %f y: %f\n", i, app->chunks[i].start_pos.x, + // app->chunks[i].start_pos.y, app->chunks[i].start_pos.x+CHUNK_MAX_X, app->chunks[i].start_pos.y+CHUNK_MAX_Y); + //printf("broken block x: %f y: %f z: %f\n", block.x+app->chunks[i].start_pos.x, block.y+app->chunks[i].start_pos.y, block.z+app->chunks[i].start_pos.z); if(ret < 0) return; - app->chunks[i].blocks[block.x][block.y][block.z].type = BLOCKTYPE_AIR; - chunk_update(app, &(app->chunks[i])); + app->cm->chunks[i].blocks[block.x][block.y][block.z].type = BLOCKTYPE_AIR; + chunk_update(&(app->cm->chunks[i])); } diff --git a/src/camera.c b/src/camera.c index 5cf5a7c..6a115a9 100644 --- a/src/camera.c +++ b/src/camera.c @@ -9,7 +9,7 @@ void init_camera(Camera* camera) { camera->position.x = 5.0; camera->position.y = 5.0; - camera->position.z = 60.0; + camera->position.z = 90.0; camera->rotation.x = 0.0; camera->rotation.y = 0.0; camera->rotation.z = 0.0; @@ -18,24 +18,123 @@ void init_camera(Camera* camera) camera->speed.z = 0.0; } -void update_camera(Camera* camera, struct App *app, double time) +vec3f handle_collision(vec3f camera_pos, ChunkManager *cm) +{ + vec3f c_max = { camera_pos.x + 0.25, camera_pos.y + 0.25, camera_pos.z + 0.25 }; + vec3f c_min = { camera_pos.x - 0.25, camera_pos.y - 0.25, camera_pos.z - 0.25 }; + vec3f b_max; + vec3f b_min; + + int i = chunk_get_current_chunk_index(cm, camera_pos); + + for (int x = floor(camera_pos.x-1); x < floor(camera_pos.x+1); x++) { + for (int y = floor(camera_pos.y-1); y < floor(camera_pos.y+1); y++) { + int z = floor(camera_pos.z+1); + if (!check_index(x, y, z)) + continue; + if(cm->chunks[i].blocks[x][y][z].visible) { + b_min = (vec3f) {x+1, y+1, z}; + b_max = (vec3f) {x+2, y+2, z+1}; + if (c_max.z > b_min.z && + c_min.z < b_max.z) { + return (vec3f) {0, 0, 1}; + } + } + } + } + + //int x = floor(camera_pos.x); + //int y = floor(camera_pos.y); + //int z = floor(camera_pos.z); + + //b_min = (vec3f) {x, y, z}; + //b_max = (vec3f) {x+1, y+1, z+1}; + // + //print_vec3f(c_max, "c_max"); + //print_vec3f(c_max, "c_min"); + //print_vec3f(c_max, "b_max"); + //print_vec3f(c_max, "b_max"); + //if (c_max.z > b_min.z && + // c_min.z < b_max.z) { + // return (vec3f) {0, 0, 1}; + //} + return (vec3f) {0, 0, 0}; +} +void update_camera(Camera* camera, double time) { - int i = app_get_current_chunk_index(app); double angle; double side_angle; angle = degree_to_radian(camera->rotation.z); side_angle = degree_to_radian(camera->rotation.z + 90.0); - float x = camera->position.x = cos(angle) * camera->speed.y * time; - float y = camera->position.y = sin(angle) * camera->speed.y * time; - float z = camera->position.z = camera->speed.z * time; + float x = camera->position.x + cos(angle) * camera->speed.y * time; + float y = camera->position.y + sin(angle) * camera->speed.y * time; + float z = camera->position.z + camera->speed.z * time; - if(is_block(app->chunks[i].blocks[(int)x][(int)y][(int)z])) + float x1 = camera->position.x + cos(side_angle) * camera->speed.x * time; + float y1 = camera->position.y + sin(side_angle) * camera->speed.x * time; + float z1 = camera->position.z + camera->speed.z * time; + + printf("x: %f\n",camera->position.x + camera->speed.z * time); + printf("x: %f\n",camera->position.y + cos(angle) * camera->speed.y * time); + printf("x: %f\n",camera->position.z + sin(angle) * camera->speed.y * time); + + //vec3f v = handle_collision(camera->position, camera->cm); - camera->position.x += cos(side_angle) * camera->speed.x * time; - camera->position.y += sin(side_angle) * camera->speed.x * time; - camera->position.z += camera->speed.z * time; + //camera->position.x += cos(angle) * camera->speed.y * time; + //camera->position.y += sin(angle) * camera->speed.y * time; + //if (v.z == 0) camera->position.z += camera->speed.z * time; + //else camera->speed.z = 0; + + //camera->position.x += cos(side_angle) * camera->speed.x * time; + //camera->position.y += sin(side_angle) * camera->speed.x * time; + //if (v.z == 0) camera->position.z += camera->speed.z * time; + //else camera->speed.z = 0; + + int i = chunk_get_current_chunk_index(camera->cm, camera->position); + vec3f cs = camera->cm->chunks[i].start_pos; + + x -= cs.x; y -= cs.y; z -= cs.z; + x1 -= cs.x; y1 -= cs.y; z1 -= cs.z; + + if(!is_block(camera->cm->chunks[i].blocks[(int)x][(int)y][(int)z-1])) { + puts("not is block"); + camera->speed.z -= GRAVITY * time; + camera->position.x += cos(angle) * camera->speed.y * time; + camera->position.y += sin(angle) * camera->speed.y * time; + camera->position.z += camera->speed.z * time; + + } else { + puts("is block"); + camera->speed.z = 0; + + while(is_block(camera->cm->chunks[i].blocks[(int)camera->position.x] + [(int)camera->position.y] + [(int)camera->position.z-1])) { + camera->position.z+=0.2; + print_vec3f(camera->position, "camera"); + } + } + + if(!is_block(camera->cm->chunks[i].blocks[(int)x1][(int)y1][(int)z1-1])) { + puts("not is block"); + camera->speed.z -= GRAVITY * time; + camera->position.x += cos(side_angle) * camera->speed.x * time; + camera->position.y += sin(side_angle) * camera->speed.x * time; + camera->position.z += camera->speed.z * time; + + } else { + puts("is block"); + camera->speed.z = 0; + + while(is_block(camera->cm->chunks[i].blocks[(int)camera->position.x] + [(int)camera->position.y] + [(int)camera->position.z-1])) { + camera->position.z+=0.2; + print_vec3f(camera->position, "camera"); + } + } } void set_view(const Camera* camera) diff --git a/src/game.c b/src/game.c index a692116..b604145 100644 --- a/src/game.c +++ b/src/game.c @@ -1,67 +1,4 @@ #include "../include/game.h" #include -bool chunk_is_block_neighboring_block(Chunk* chunk, vec3i pos) -{ - if (is_oob(pos.x-1, 0, CHUNK_MAX_X) || is_oob(pos.x+1, 0, CHUNK_MAX_X)) - return false; - if (is_oob(pos.y-1, 0, CHUNK_MAX_Y) || is_oob(pos.y+1, 0, CHUNK_MAX_Y)) - return false; - if (is_oob(pos.z-1, 0, CHUNK_MAX_Z) || is_oob(pos.z+1, 0, CHUNK_MAX_Z)) - return false; - if (chunk->blocks[pos.x-1][pos.y][pos.z].type != BLOCKTYPE_AIR && - chunk->blocks[pos.x+1][pos.y][pos.z].type != BLOCKTYPE_AIR && - chunk->blocks[pos.x][pos.y-1][pos.z].type != BLOCKTYPE_AIR && - chunk->blocks[pos.x][pos.y+1][pos.z].type != BLOCKTYPE_AIR && - chunk->blocks[pos.x][pos.y][pos.z-1].type != BLOCKTYPE_AIR && - chunk->blocks[pos.x][pos.y][pos.z+1].type != BLOCKTYPE_AIR) - return true; - - return false; -} - -void chunk_set_blocks_visibility(Chunk* chunk) -{ - for (int x = 0; x < CHUNK_MAX_X; x++) { - for (int y = 0; y < CHUNK_MAX_Y; y++) { - for (int z = 0; z < CHUNK_MAX_Z; z++) { - vec3i v = {x, y, z}; - if (chunk_is_block_neighboring_block(chunk, v)) - chunk->blocks[x][y][z].visible = false; - else - chunk->blocks[x][y][z].visible = true; - - if (chunk->blocks[x][y][z].type == BLOCKTYPE_AIR) - chunk->blocks[x][y][z].visible = false; - } - } - } -} - -void chunk_create_displayl(App *app, Chunk *chunk) -{ - chunk->id = glGenLists(1); - - glNewList(chunk->id, GL_COMPILE); - for (int x = 0; x < CHUNK_MAX_X; x++) { - for (int y = 0; y < CHUNK_MAX_Y; y++) { - for (int z = 0; z < CHUNK_MAX_Z; z++) { - if (chunk->blocks[x][y][z].visible) - draw_cube(app, chunk->start_pos.x + x, chunk->start_pos.y + y, chunk->start_pos.z + z); - } - } - } - glEndList(); -} - -void chunk_render(Chunk* chunk) -{ - glCallList(chunk->id); -} - -void chunk_update(App *app, Chunk *chunk) -{ - chunk_set_blocks_visibility(chunk); - chunk_create_displayl(app, chunk); -} diff --git a/src/main.c b/src/main.c index 7156457..e5903df 100644 --- a/src/main.c +++ b/src/main.c @@ -2,61 +2,200 @@ #include #include #include +#include +#define SHADOWMAP_SIZE 1024 int main() { + struct timespec start; + struct timespec end; App app; + ChunkManager cm; + app.cm = &cm; init_app(&app, 1600, 900); - //Chunk *chunks = malloc(25*sizeof(Chunk)); - //memset(chunks, 0, 25*sizeof(Chunk)); + ///// + glShadeModel(GL_SMOOTH); + glClearColor(0.0f, 0.0f, 0.0f, 0.0f); + glColor4f(1.0f, 1.0f, 1.0f, 1.0f); + glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); + glClearDepth(1.0f); + glDepthFunc(GL_LEQUAL); + glEnable(GL_DEPTH_TEST); + glEnable(GL_CULL_FACE); - //for (int i = 0; i < 5; i++) { - // for (int j = 0; j < 5; j++) { - // Chunk c; - // c.start_pos.x = i*CHUNK_MAX_X; - // c.start_pos.y = j*CHUNK_MAX_Y; - // c.start_pos.z = 0; - // for (int x = 0; x < CHUNK_MAX_X; x++) { - // for (int y = 0; y < CHUNK_MAX_Y; y++) { - // for (int z = 0; z < CHUNK_MAX_Z; z++) { - // if (z < 40) - // c.blocks[x][y][z].type = BLOCKTYPE_STONE; - // else - // c.blocks[x][y][z].type = BLOCKTYPE_AIR; - // } - // } - // } - // chunk_set_blocks_visibility(&c); - // chunk_create_displayl(&app, &c); - // chunks[i+(j*5)] = c; - // } - //} + GLuint shadowmap_texture; - //app.chunks = chunks; - //app.chunk_count = 25; + float light_proj_matrix[16]; + float light_view_matrix[16]; + float cam_proj_matrix[16]; + float cam_view_matrix[16]; - app_generate_world(&app, 10); + glGenTextures(1, &shadowmap_texture); + glBindTexture(GL_TEXTURE_2D, shadowmap_texture); + glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, + SHADOWMAP_SIZE, SHADOWMAP_SIZE, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL); - while(app.is_running) { - struct timespec start; - struct timespec end; + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE); + glEnable(GL_COLOR_MATERIAL); + GLfloat materialColor[] = {0.5f, 0.5f, 0.5f, 0.0f}; + glMaterialfv(GL_FRONT, GL_SPECULAR, materialColor); + glMaterialf(GL_FRONT, GL_SHININESS, 16.0f); + + + + + //reshape(1600, 900); + + ///// + while(app.is_running) { clock_gettime(CLOCK_MONOTONIC_RAW, &start); - handle_events(&app); - update_app(&app); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - glMatrixMode(GL_MODELVIEW); - set_view(&app.camera); + vec3f cd = get_camera_dir_vec3f(&app.camera); + vec3f cp = app.camera.position; + glPushMatrix(); - for (int i = 0; i < app.chunk_count; i++) { - chunk_render(&app.chunks[i]); + glLoadIdentity(); + gluPerspective(45.0f, (float)1600/900, 1.0f, 100.0f); + glGetFloatv(GL_MODELVIEW_MATRIX, cam_proj_matrix); + + glLoadIdentity(); + gluLookAt(cp.x, cp.y, cp.z, + cp.x*cd.x, cp.y*cd.y, cp.z*cd.z, + 0.0, 0.0, 1.0); + glGetFloatv(GL_MODELVIEW_MATRIX, cam_view_matrix); + + glLoadIdentity(); + gluPerspective(45.0, 1.0, 2.0, 1000.0); + glGetFloatv(GL_MODELVIEW_MATRIX, light_proj_matrix); + + glLoadIdentity(); + gluLookAt(30, 40, 30, + 0.0, 0.0, 0.0, + 0.0, 0.0, 1.0); + glGetFloatv(GL_MODELVIEW_MATRIX, light_view_matrix); + glPopMatrix(); + + + /////////////// + + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + glMatrixMode(GL_PROJECTION); + glLoadMatrixf(light_proj_matrix); + + glMatrixMode(GL_MODELVIEW); + glLoadMatrixf(light_view_matrix); + + glViewport(0, 0, SHADOWMAP_SIZE, SHADOWMAP_SIZE); + + glCullFace(GL_FRONT); + + glShadeModel(GL_FLAT); + glColorMask(0, 0, 0, 0); + + for (int i = 0; i < app.cm->chunk_count; i++) { + chunk_render(&app.cm->chunks[i]); //printf("c sp: %f\n", app.chunks[i].start_pos.x); } - // vec3f pos = app.camera.position; + glBindTexture(GL_TEXTURE_2D, shadowmap_texture); + glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, SHADOWMAP_SIZE, SHADOWMAP_SIZE); + + glCullFace(GL_BACK); + glDisable(GL_CULL_FACE); + glShadeModel(GL_SMOOTH); + glColorMask(1, 1, 1, 1); + + glMatrixMode(GL_PROJECTION); + glLoadMatrixf(cam_proj_matrix); + glMatrixMode(GL_MODELVIEW); + glLoadMatrixf(cam_view_matrix); + glViewport(0, 0, 1600, 900); + /////////////// + + handle_events(&app); + update_app(&app); + + float color1[4] = {0.2, 0.2, 0.2, 0.2}; + float color2[4] = {0, 0, 0, 0}; + float color3[4] = {1.0, 1.0, 1.0, 1.0}; + float light_pos[5] = {0, 0, 50, 0}; + glLightfv(GL_LIGHT1, GL_POSITION, light_pos); + glLightfv(GL_LIGHT1, GL_AMBIENT, color1); + glLightfv(GL_LIGHT1, GL_DIFFUSE, color1); + glLightfv(GL_LIGHT1, GL_SPECULAR, color2); + glEnable(GL_LIGHT1); + glEnable(GL_LIGHTING); + + //glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + //set_view(&app.camera); + for (int i = 0; i < app.cm->chunk_count; i++) { + chunk_render(&app.cm->chunks[i]); + //printf("c sp: %f\n", app.chunks[i].start_pos.x); + } + + glLightfv(GL_LIGHT1, GL_DIFFUSE, color3); + glLightfv(GL_LIGHT1, GL_SPECULAR, color3); + float bias[16] = {0.5f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.5f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.5f, 0.0f, + 0.5f, 0.5f, 0.5f, 1.0f}; + float tm[16]; + for (int k = 0; k < 16; k++) { + tm[k] = bias[k]*light_proj_matrix[k]*light_view_matrix[k]; + } + float v1[4] = {tm[0], tm[4], tm[8], tm[12]}; + glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR); + glTexGenfv(GL_S, GL_EYE_PLANE, v1); + glEnable(GL_TEXTURE_GEN_S); + float v2[4] = {tm[1], tm[5], tm[9], tm[13]}; + glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR); + glTexGenfv(GL_T, GL_EYE_PLANE, v2); + glEnable(GL_TEXTURE_GEN_T); + float v3[4] = {tm[2], tm[6], tm[10], tm[14]}; + glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR); + glTexGenfv(GL_R, GL_EYE_PLANE, v3); + glEnable(GL_TEXTURE_GEN_R); + float v4[4] = {tm[3], tm[7], tm[11], tm[15]}; + glTexGeni(GL_Q, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR); + glTexGenfv(GL_Q, GL_EYE_PLANE, v4); + glEnable(GL_TEXTURE_GEN_Q); + glBindTexture(GL_TEXTURE_2D, shadowmap_texture); + glEnable(GL_TEXTURE_2D); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC_ARB, GL_LEQUAL); + glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY); + glAlphaFunc(GL_GEQUAL, 0.99f); + glEnable(GL_ALPHA_TEST); + + //glMatrixMode(GL_MODELVIEW); + for (int i = 0; i < app.cm->chunk_count; i++) { + chunk_render(&app.cm->chunks[i]); + //printf("c sp: %f\n", app.chunks[i].start_pos.x); + } + + glDisable(GL_TEXTURE_2D); + + glDisable(GL_TEXTURE_GEN_S); + glDisable(GL_TEXTURE_GEN_T); + glDisable(GL_TEXTURE_GEN_R); + glDisable(GL_TEXTURE_GEN_Q); + glDisable(GL_LIGHTING); + glDisable(GL_ALPHA_TEST); + glMatrixMode(GL_PROJECTION); + glPopMatrix(); + glMatrixMode(GL_MODELVIEW); + glPopMatrix(); + //////////// + vec3f pos = app.camera.position; // vec3f dir = get_camera_dir_vec3f(&app.camera); // draw_cube(&app, pos.x + dir.x*3, pos.y + dir.y*3, pos.z + dir.z*3); @@ -64,12 +203,11 @@ int main() SDL_GL_SwapWindow(app.window); clock_gettime(CLOCK_MONOTONIC_RAW, &end); - app.frame_time = calc_frame_time(&start, &end); printf("fps: %f\n", 1000/app.frame_time); //printf("lkat pos x: %f y: %f z: %f\n", pos.x+5*dir.x, pos.y+5*dir.y, pos.z+5*dir.z); - //printf("camera x: %f y: %f z: %f\n", pos.x, pos.y, pos.z); + printf("camera x: %f y: %f z: %f\n", pos.x, pos.y, pos.z); //printf("dir x: %f y: %f z: %f\n", dir.x, dir.y, dir.z); //printf("dird x: %f y: %f z: %f\n", app.camera.rotation.x, app.camera.rotation.y, app.camera.rotation.z); } diff --git a/src/utils.c b/src/utils.c index f599af8..7385be4 100644 --- a/src/utils.c +++ b/src/utils.c @@ -15,19 +15,9 @@ bool is_oob(int i, int min, int max) return false; } -void mult_matrix(float *matrix, vec4f vector, vec4f *ret) +void mult_matrix(float m1[16], float m2[16]) { - float vec[] = {vector.x, vector.y, vector.z, vector.w}; - float ret1[4]; - for(int i = 0; i <= 12; i+=4) { - float line_sum = 0.0; - for(int j = 0; j < 4; j++){ - matrix[i+j] *= vec[j]; - line_sum += matrix[i+j]; - } - ret1[i/4] = line_sum; - } - ret->x = ret1[0]; ret->y = ret1[1]; ret->z=ret1[2]; ret->w = ret1[3]; + } bool matrix_inverse(const float m[16], float invOut[16]) @@ -184,3 +174,8 @@ bool check_index(int x, int y, int z) return false; return true; } + +void print_vec3f(vec3f v, const char *str) +{ + printf("%s x: %f y: %f z: %f\n", str, v.x, v.y, v.z); +}