2026-11-03 18:56:57 +01:00

82 lines
2.7 KiB
C++

#include <deerith/world/components.h>
#include <deerith/client/world.h>
#include <deerith/client/graphics/camera.h>
#include <deerith/client/graphics/mesh.h>
#include <deerith/client/graphics/shader.h>
#include <deerith/client/graphics/primitive.h>
#include <deerith/client/internals/world.h>
#include <glm/glm.hpp>
#include <glm/gtc/quaternion.hpp>
namespace deerith
{
extern std::unique_ptr<mesh_3d_t> test_mesh;
}
void deerith::render_world()
{
process_player_input();
load_clusters_to_gpu();
get_lit_shader()->set_vec3("u_color", glm::vec3(0.8f, 0.8f, 0.8f));
set_primitive_viewspace(glm::vec2(-10, -10), glm::vec2(10, 10));
camera_t camera;
auto self_view = get_entity_registry().view<component_sync_coordinates_t, component_client_sync_character_t>();
for (auto [entity, sync_coords, client_sync_coords] : self_view.each())
{
camera.position = client_sync_coords.position.read_value();
float yaw = glm::radians(client_sync_coords.player_look.x);
float pitch = glm::radians(client_sync_coords.player_look.y);
glm::quat yaw_rotation = glm::angleAxis(yaw, glm::vec3(0, 1, 0));
glm::quat pitch_rotation = glm::angleAxis(pitch, glm::vec3(1, 0, 0));
camera.rotation = glm::normalize(yaw_rotation * pitch_rotation);
set_camera_to_shader(camera, get_lit_shader());
}
get_lit_shader()->use();
get_lit_shader()->set_vec3("u_color", glm::vec3(0.8f, 0.8f, 0.8f));
set_transform_to_shader(glm::vec3(0, 0, 0), glm::identity<glm::quat>(), glm::vec3(1, 1, 1), get_lit_shader());
get_grid_mesh()->draw_lines();
auto entities_view = get_entity_registry().view<component_sync_coordinates_t>(entt::exclude<component_client_sync_character_t>);
for (auto [entity, sync_coords] : entities_view.each())
{
glm::vec3 chords;
if (sync_coords.position.read_synced(chords))
{
glm::vec3 position = chords;
glm::quat rotation = glm::identity<glm::quat>();
glm::vec3 scale = glm::vec3(1, 1, 1);
get_lit_shader()->set_vec4("u_color", glm::vec4(1, 0.4f, 0.6f, 1));
set_transform_to_shader(position, rotation, scale, get_lit_shader());
get_lit_shader()->use();
get_cube_mesh()->draw();
}
}
// we should expect only one registry
if (test_mesh)
{
get_lit_shader()->set_vec3("u_color", glm::vec3(0.9f, 0.9f, 0.9f));
glm::vec3 position = glm::vec3(0, 0, 0);
glm::quat rotation = glm::identity<glm::quat>();
glm::vec3 scale = glm::vec3(1, 1, 1);
set_transform_to_shader(position, rotation, scale, get_lit_shader());
get_lit_shader()->use();
test_mesh->draw();
}
}