transfered from codeberg
This commit is contained in:
25
source_code/renderers_module/chunk_renderer/chunk_renderer.c
Normal file
25
source_code/renderers_module/chunk_renderer/chunk_renderer.c
Normal file
@@ -0,0 +1,25 @@
|
||||
#include "chunk_renderer.h"
|
||||
|
||||
#include "chunk.h"
|
||||
#include "chunk_position_to_vector_3/chunk_position_to_vector_3.h"
|
||||
#include "frustum.h"
|
||||
|
||||
#include "raylib.h"
|
||||
|
||||
|
||||
/**
|
||||
* Renders a single chunk to the screen, performing frustum culling
|
||||
* using the provided array of clipping planes.
|
||||
*/
|
||||
void draw_chunk(struct chunk_struct* chunk,
|
||||
const struct frustum_plane_struct* frustum)
|
||||
{
|
||||
if (!is_axis_aligned_bounding_box_in_frustum(frustum, chunk->bounds))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
DrawModel(chunk->model, chunk_position_to_vector_3(chunk->position), 1.0f, WHITE);
|
||||
}
|
||||
|
||||
|
||||
14
source_code/renderers_module/chunk_renderer/chunk_renderer.h
Normal file
14
source_code/renderers_module/chunk_renderer/chunk_renderer.h
Normal file
@@ -0,0 +1,14 @@
|
||||
#ifndef chunk_renderer_h
|
||||
#define chunk_renderer_h
|
||||
|
||||
struct chunk_struct;
|
||||
struct frustum_plane_struct;
|
||||
|
||||
/**
|
||||
* Renders a single chunk to the screen, performing frustum culling
|
||||
* using the provided array of clipping planes.
|
||||
*/
|
||||
void draw_chunk(struct chunk_struct* chunk,
|
||||
const struct frustum_plane_struct* frustum_planes);
|
||||
|
||||
#endif
|
||||
19
source_code/renderers_module/game_renderer/game_renderer.c
Normal file
19
source_code/renderers_module/game_renderer/game_renderer.c
Normal file
@@ -0,0 +1,19 @@
|
||||
#include "game_renderer.h"
|
||||
|
||||
#include "user_interface.h"
|
||||
#include "world_renderer.h"
|
||||
|
||||
#include "raylib.h"
|
||||
|
||||
/**
|
||||
* Orchestrates the entire frame rendering sequence by clearing the buffers,
|
||||
* invoking the 3D world pass, and overlaying the 2D user interface.
|
||||
*/
|
||||
void draw_game(struct world_struct *world, struct player_struct *player)
|
||||
{
|
||||
BeginDrawing();
|
||||
ClearBackground((Color){ 120, 180, 180, 255 });
|
||||
draw_world_3D(world, player);
|
||||
draw_ui_2D(player);
|
||||
EndDrawing();
|
||||
}
|
||||
13
source_code/renderers_module/game_renderer/game_renderer.h
Normal file
13
source_code/renderers_module/game_renderer/game_renderer.h
Normal file
@@ -0,0 +1,13 @@
|
||||
#ifndef game_renderer_h
|
||||
#define game_renderer_h
|
||||
|
||||
struct world_struct;
|
||||
struct player_struct;
|
||||
|
||||
/**
|
||||
* Orchestrates the entire frame rendering sequence by clearing the buffers,
|
||||
* invoking the 3D world pass, and overlaying the 2D user interface.
|
||||
*/
|
||||
void draw_game(struct world_struct *world, struct player_struct *player);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,25 @@
|
||||
#include "player_renderer.h"
|
||||
|
||||
#include "player.h"
|
||||
|
||||
#include "raylib.h"
|
||||
|
||||
/**
|
||||
* Renders the third-person representation of the player character,
|
||||
* including the 3D model, animations, and equipped items.
|
||||
*/
|
||||
void draw_player(struct player_struct *player) {
|
||||
highlight_block_being_watched(player);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Renders a 3D wireframe or highlight effect around the voxel
|
||||
* currently targeted by the player's view ray.
|
||||
*/
|
||||
void highlight_block_being_watched(struct player_struct *player) {
|
||||
if (player->should_highlight_block)
|
||||
{
|
||||
DrawCubeWires(player->block_being_watched, 1.02f, 1.02f, 1.02f, BLACK);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
#ifndef player_renderer_h
|
||||
#define player_renderer_h
|
||||
|
||||
struct player_struct;
|
||||
|
||||
/**
|
||||
* Renders the third-person representation of the player character,
|
||||
* including the 3D model, animations, and equipped items.
|
||||
*/
|
||||
void draw_player(struct player_struct *player);
|
||||
|
||||
/**
|
||||
* Renders a 3D wireframe or highlight effect around the voxel
|
||||
* currently targeted by the player's view ray.
|
||||
*/
|
||||
void highlight_block_being_watched(struct player_struct *player);
|
||||
|
||||
#endif
|
||||
@@ -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