ray casting

This commit is contained in:
Ákos Horváth 2022-04-07 13:54:40 +02:00
parent fa97efdce6
commit 2096c220d9
5 changed files with 84 additions and 21 deletions

View File

@ -39,7 +39,6 @@ typedef struct {
GLuint id;
} Chunk;
typedef struct {
bool is_running;
SDL_Window *window;
@ -57,6 +56,8 @@ void chunk_set_blocks_visibility(Chunk* chunk);
void chunk_create_displayl(App *app, Chunk *chunk);
void chunk_render(Chunk* chunk);
void check_intersection_block(App *app, vec3i *retv);
bool is_block(Block b);
int app_get_current_chunk_index(App *app);

View File

@ -30,6 +30,7 @@ void update_camera(Camera* camera, double time);
* Apply the camera settings to the view transformation.
*/
void set_view(const Camera* camera);
void set_view_fix(const Camera* camera);
/**
* Set the horizontal and vertical rotation of the view angle.
@ -48,4 +49,6 @@ void camera_set_vertical_speed(Camera* camera, double speed);
*/
void set_camera_side_speed(Camera* camera, double speed);
vec3f get_camera_dir_vec3f(Camera* camera);
#endif /* CAMERA_H */

View File

@ -37,7 +37,7 @@ void init_app(App *app, uint w, uint h)
init_camera(&(app->camera));
app->surface = IMG_Load("assets/block.jpg");
app->surface = IMG_Load("assets/block.png");
app->tid = 0;
if(app->surface==NULL) {
@ -305,10 +305,45 @@ bool app_check_collision(App *app)
for (int z = 0; z < CHUNK_MAX_Z; z++) {
if (!is_block(app->chunks[i].blocks[x][y][z]))
continue;
if (app->camera.position.z <= z+2)
if (app->camera.position.z <= z+5)
return true;
}
}
}
return false;
}
void check_intersection_block(App *app, vec3i *retv)
{
int i = app_get_current_chunk_index(app);
vec3f c_pos = app->camera.position;
vec3f c_dir = get_camera_dir_vec3f(&app->camera);
retv->x = 0;
retv->y = 0;
retv->z = 0;
if (i < 0)
return;
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++) {
for (int i = 0; i < 8; i+=0.1) {
vec3f ray = {c_pos.x + c_dir.x * i,
c_pos.y + c_dir.y * i,
c_pos.z + c_dir.z * i};
vec3i rayi = {floor(ray.x/1), floor(ray.y/1), floor(ray.z/1)};
if (is_block(app->chunks[i].blocks[rayi.x][rayi.y][rayi.z])) {
retv->x = ray.x;
retv->y = ray.y;
retv->z = ray.z;
}
}
}
}
}
}

View File

@ -44,7 +44,15 @@ void set_view(const Camera* camera)
glRotatef(-(camera->rotation.z - 90), 0, 0, 1.0);
glTranslatef(-camera->position.x, -camera->position.y, -camera->position.z);
}
void set_view_fix(const Camera* camera)
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(-20, 1.0, 0, 0);
glRotatef(-30, 0, 0, 1.0);
glTranslatef(-5, -5, -50);
}
void rotate_camera(Camera* camera, double horizontal, double vertical)
{
camera->rotation.z += horizontal;
@ -81,3 +89,14 @@ void set_camera_side_speed(Camera* camera, double speed)
{
camera->speed.x = speed;
}
vec3f get_camera_dif_vec3f(Camera *camera)
{
vec3f dir = {cos(degree_to_radian(camera->rotation.z)) *
cos(degree_to_radian(camera->rotation.x)),
sin(degree_to_radian(camera->rotation.z)) *
cos(degree_to_radian(camera->rotation.x)),
sin(degree_to_radian(camera->rotation.x))};
return dir;
}

View File

@ -8,11 +8,11 @@ int main()
App app;
init_app(&app, 1280, 720);
Chunk *chunks = malloc(100*sizeof(Chunk));
memset(chunks, 0, 100*sizeof(Chunk));
Chunk *chunks = malloc(25*sizeof(Chunk));
memset(chunks, 0, 25*sizeof(Chunk));
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
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;
@ -30,12 +30,12 @@ int main()
chunk_set_blocks_visibility(&c);
chunk_create_displayl(&app, &c);
chunks[i+(j*10)] = c;
chunks[i+(j*5)] = c;
}
}
app.chunks = chunks;
app.chunk_count = 100;
app.chunk_count = 25;
while(app.is_running) {
struct timespec start;
@ -49,31 +49,36 @@ int main()
glMatrixMode(GL_MODELVIEW);
set_view(&app.camera);
printf("camera x: %f y: %f z: %f\n", app.camera.position.x, app.camera.position.y, app.camera.position.z);
for (int i = 0; i < 100; i++) {
for (int i = 0; i < 25; i++) {
chunk_render(&chunks[i]);
}
vec3f dir = {cos(degree_to_radian(app.camera.rotation.z)), sin(degree_to_radian(app.camera.rotation.z)), -cos(degree_to_radian(app.camera.rotation.x))};
vec3f dir = {cos(degree_to_radian(app.camera.rotation.z)) *
cos(degree_to_radian(app.camera.rotation.x)),
sin(degree_to_radian(app.camera.rotation.z)) *
cos(degree_to_radian(app.camera.rotation.x)),
sin(degree_to_radian(app.camera.rotation.x))};
vec3f pos = {app.camera.position.x, app.camera.position.y, app.camera.position.z };
glLineWidth(20);
glLineWidth(10);
glBegin(GL_LINES);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(pos.x, pos.y, pos.z);
glVertex3f(pos.x+5*dir.x, pos.y+5*dir.y, pos.z+5*dir.z);
glVertex3f(pos.x+3*dir.x, pos.y+3*dir.y, pos.z+3*dir.z);
glEnd();
SDL_GL_SwapWindow(app.window);
clock_gettime(CLOCK_MONOTONIC_RAW, &end);
printf("fps: %f\n", 1000/calc_frame_time(&start, &end));
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("lkat x: %f y: %f z: %f\n", dir.x, dir.y, dir.z);
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("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);
}
return 0;