transfered from codeberg
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
#include "private_world_renderer.h"
|
||||
|
||||
#include "chunk.h"
|
||||
#include "chunk_renderer.h"
|
||||
#include "frustum.h"
|
||||
#include "hashmap.h"
|
||||
#include "player.h"
|
||||
#include "player_renderer.h"
|
||||
#include "world.h"
|
||||
|
||||
#include "raymath.h"
|
||||
|
||||
/**
|
||||
* Iterates through the world chunks to perform distance culling and frustum
|
||||
* culling before invoking the individual chunk and player renderers.
|
||||
*/
|
||||
void _draw_games_3D_elements(struct world_struct *world,
|
||||
struct player_struct *player,
|
||||
const struct frustum_plane_struct *frustum_planes)
|
||||
{
|
||||
draw_player(player);
|
||||
|
||||
float render_distance_threshold = (float)((render_distance + 1) * chunk_size);
|
||||
float half_chunk_dimension = (float)chunk_size / 2.0f;
|
||||
|
||||
pthread_mutex_lock(&world->mutex);
|
||||
|
||||
size_t hashmap_iterator = 0;
|
||||
void *current_item;
|
||||
|
||||
while (hashmap_iter(world->active_chunks, &hashmap_iterator, ¤t_item))
|
||||
{
|
||||
worlds_chunk_hashmap_entry *current_entry = current_item;
|
||||
chunk_struct *current_chunk = current_entry->chunk_pointer;
|
||||
|
||||
if (current_chunk == NULL)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (atomic_load(¤t_chunk->build_state) !=
|
||||
state_chunk_finished_uploading)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
Vector3 chunk_center_position = {
|
||||
(float)current_entry->chunk_position->x + half_chunk_dimension,
|
||||
(float)current_entry->chunk_position->y + half_chunk_dimension,
|
||||
(float)current_entry->chunk_position->z + half_chunk_dimension
|
||||
};
|
||||
|
||||
float distance_to_player_camera = Vector3Distance(
|
||||
player->camera.position,
|
||||
chunk_center_position
|
||||
);
|
||||
|
||||
if (distance_to_player_camera > render_distance_threshold)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
draw_chunk(current_chunk, frustum_planes);
|
||||
}
|
||||
|
||||
pthread_mutex_unlock(&world->mutex);
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
#ifndef private_world_renderer_h
|
||||
#define private_world_renderer_h
|
||||
|
||||
struct world_struct;
|
||||
struct player_struct;
|
||||
struct frustum_plane_struct;
|
||||
|
||||
/**
|
||||
* Iterates through the world chunks to perform distance culling and frustum
|
||||
* culling before invoking the individual chunk and player renderers.
|
||||
*/
|
||||
void _draw_games_3D_elements(struct world_struct* world,
|
||||
struct player_struct* player,
|
||||
const struct frustum_plane_struct* frustum);
|
||||
|
||||
|
||||
#endif
|
||||
40
source_code/renderers_module/world_renderer/world_renderer.c
Normal file
40
source_code/renderers_module/world_renderer/world_renderer.c
Normal file
@@ -0,0 +1,40 @@
|
||||
#include "world_renderer.h"
|
||||
#include "_internal/private_world_renderer.h"
|
||||
|
||||
#include "constants.h"
|
||||
#include "frustum.h"
|
||||
#include "player.h"
|
||||
#include "world.h"
|
||||
|
||||
#include "raymath.h"
|
||||
|
||||
/**
|
||||
* Prepares the 3D rendering context by calculating the view-projection matrix,
|
||||
* extracting the current viewing frustum, and invoking the 3D element pass.
|
||||
*/
|
||||
void draw_world_3D(struct world_struct *world, struct player_struct *player)
|
||||
{
|
||||
struct frustum_plane_struct frustum_planes[number_of_planes_for_a_cube];
|
||||
|
||||
Matrix view_matrix = GetCameraMatrix(player->camera);
|
||||
float aspect_ratio = (float)GetScreenWidth() / (float)GetScreenHeight();
|
||||
float near_plane_clipping_distance = 0.01f;
|
||||
float far_plane_clipping_distance = 1000.0f;
|
||||
|
||||
Matrix projection_matrix = MatrixPerspective(
|
||||
player->camera.fovy * DEG2RAD,
|
||||
aspect_ratio,
|
||||
near_plane_clipping_distance,
|
||||
far_plane_clipping_distance
|
||||
);
|
||||
|
||||
Matrix view_projection_matrix = MatrixMultiply(view_matrix, projection_matrix);
|
||||
|
||||
extract_frustum_planes(frustum_planes, view_projection_matrix);
|
||||
|
||||
BeginMode3D(player->camera);
|
||||
|
||||
_draw_games_3D_elements(world, player, frustum_planes);
|
||||
|
||||
EndMode3D();
|
||||
}
|
||||
13
source_code/renderers_module/world_renderer/world_renderer.h
Normal file
13
source_code/renderers_module/world_renderer/world_renderer.h
Normal file
@@ -0,0 +1,13 @@
|
||||
#ifndef world_renderer_h
|
||||
#define world_renderer_h
|
||||
|
||||
struct world_struct;
|
||||
struct player_struct;
|
||||
|
||||
/**
|
||||
* Prepares the 3D rendering context by calculating the view-projection matrix,
|
||||
* extracting the current viewing frustum, and invoking the 3D element pass.
|
||||
*/
|
||||
void draw_world_3D(struct world_struct* world, struct player_struct* player);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user