Refact + 3d rendering

This commit is contained in:
Chewico 2026-10-20 23:01:33 +02:00
parent 2af77fa468
commit 0f4d7a721d
31 changed files with 427 additions and 167 deletions

View File

@ -88,7 +88,7 @@ MyEngine/
│ ├── include/ │ ├── include/
│ │ └── deerith/ │ │ └── deerith/
│ ├── src/ │ ├── src/
│ └── shaders/ │ └── shader_ts/
│ │
├── sandbox/ ├── sandbox/
│ ├── CMakeLists.txt │ ├── CMakeLists.txt
@ -128,7 +128,7 @@ Dependencies:
* GLFW * GLFW
* GLM * GLM
It also copies the engine shaders into the build directory automatically. It also copies the engine shader_ts into the build directory automatically.
### Sandbox ### Sandbox
@ -140,21 +140,21 @@ It links against:
DeerithClient DeerithClient
``` ```
## Shader Files ## shader_t Files
Shaders are located in: shader_ts are located in:
```text ```text
deerith_engine_client/shaders/ deerith_engine_client/shader_ts/
``` ```
CMake automatically copies them to the corresponding build directory: CMake automatically copies them to the corresponding build directory:
```text ```text
build/deerith_engine_client/shaders/ build/deerith_engine_client/shader_ts/
``` ```
This is done by the `DeerithClientShaders` custom target. This is done by the `DeerithClientshader_ts` custom target.
## Clean Build ## Clean Build

View File

@ -0,0 +1,21 @@
#pragma once
#include <glm/glm.hpp>
#include <glm/gtc/quaternion.hpp>
namespace deerith
{
class shader_t;
struct camera_t
{
glm::vec3 position{0.0f};
glm::quat rotation{1.0f, 0.0f, 0.0f, 0.0f};
float field_of_view = glm::radians(70.0f);
float near_plane = 0.1f;
float far_plane = 1000.0f;
};
void set_camera_to_shader(const camera_t &, shader_t *);
void set_transform_to_shader(const glm::vec3 &position, const glm::quat &rotation, const glm::vec3 size, shader_t *);
}

View File

@ -3,18 +3,33 @@
namespace deerith namespace deerith
{ {
class Mesh2D class mesh_2d_t
{ {
public: public:
Mesh2D(const glm::vec2 *vertices, mesh_2d_t(const glm::vec2 *vertices, size_t vertex_size, const unsigned int *indices, size_t index_count);
size_t vertex_size,
const unsigned int *indices,
size_t index_count);
void bind() const; void bind() const;
void draw() const; void draw() const;
~Mesh2D(); ~mesh_2d_t();
private:
unsigned int vao;
unsigned int vbo;
unsigned int ebo;
size_t index_count;
};
class mesh_3d_t
{
public:
mesh_3d_t(const glm::vec3 *vertices, size_t vertex_size, const unsigned int *indices, size_t index_count);
void bind() const;
void draw() const;
~mesh_3d_t();
private: private:
unsigned int vao; unsigned int vao;

View File

@ -3,8 +3,14 @@
namespace deerith namespace deerith
{ {
void define_primitive_viewspace(glm::vec2 start_viewspace, glm::vec2 end_viewspace); class shader_t;
void define_primitive_color(glm::vec4 color); class mesh_3d_t;
void set_primitive_viewspace(glm::vec2 start_viewspace, glm::vec2 end_viewspace);
void set_primitive_color(glm::vec4 color);
void draw_primitive_rectangle(glm::vec2 start, glm::vec2 end); void draw_primitive_rectangle(glm::vec2 start, glm::vec2 end);
shader_t *get_lit_shader();
mesh_3d_t *get_cube_mesh();
} }

View File

@ -4,12 +4,12 @@
namespace deerith namespace deerith
{ {
class Shader class shader_t
{ {
public: public:
unsigned int id; unsigned int id;
Shader(const char *vertexPath, const char *fragmentPath); shader_t(const char *vertexPath, const char *fragmentPath);
void use() const; void use() const;

View File

@ -1,9 +1,13 @@
#pragma once #pragma once
#include <deerith/engine.h> #include <deerith/engine.h>
#include <deerith/graphics/mesh.h>
namespace deerith namespace deerith
{ {
struct chunk_render_t struct chunk_render_t
{ {
std::unique_ptr<mesh_2d_t> chunk_mesh;
void draw();
}; };
} }

View File

@ -0,0 +1,10 @@
#version 330 core
uniform vec4 u_color = vec4(0.75, 0.8, 0.9, 1.0);
out vec4 frag_color;
void main()
{
frag_color = u_color;
}

View File

@ -0,0 +1,12 @@
#version 330 core
layout (location = 0) in vec3 a_position;
uniform mat4 u_model;
uniform mat4 u_view;
uniform mat4 u_projection;
void main()
{
gl_Position = u_projection * u_view * u_model * vec4(a_position, 1.0);
}

View File

@ -1,6 +1,5 @@
#include <deerith/engine.h> #include <deerith/engine.h>
#include <deerith/core/log.h> #include <deerith/core/log.h>
#include <deerith/core/core.h>
#include "network/network_internals.h" #include "network/network_internals.h"

View File

@ -0,0 +1,35 @@
#include <deerith/graphics/camera.h>
#include <deerith/graphics/shader.h>
#include <deerith/graphics/window.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/quaternion.hpp>
void deerith::set_camera_to_shader(const camera_t &camera, shader_t *shader)
{
const glm::mat4 projection = glm::perspective(
camera.field_of_view,
get_window_aspect_ratio(),
camera.near_plane,
camera.far_plane);
const glm::mat4 rotation = glm::mat4_cast(camera.rotation);
const glm::mat4 translation = glm::translate(glm::mat4(1.0f), -camera.position);
const glm::mat4 view = glm::scale(glm::mat4(1.0f), glm::vec3(1.0f, 1.0f, -1.0f)) * glm::transpose(rotation) * translation;
shader->set_matrix("u_projection", projection);
shader->set_matrix("u_view", view);
}
void deerith::set_transform_to_shader(const glm::vec3 &position, const glm::quat &rotation, const glm::vec3 size, shader_t *shader)
{
glm::mat4 translation = glm::translate(glm::mat4(1.0f), position);
glm::mat4 rotation_matrix = glm::mat4_cast(rotation);
glm::mat4 scale = glm::scale(glm::mat4(1.0f), size);
glm::mat4 model = translation * rotation_matrix * scale;
shader->set_matrix("u_model", model);
}

View File

@ -1,7 +1,7 @@
#include <deerith/graphics/mesh.h> #include <deerith/graphics/mesh.h>
#include <glad/gl.h> #include <glad/gl.h>
deerith::Mesh2D::Mesh2D( deerith::mesh_2d_t::mesh_2d_t(
const glm::vec2 *vertices, const glm::vec2 *vertices,
std::size_t vertex_count, std::size_t vertex_count,
const unsigned int *indices, const unsigned int *indices,
@ -42,19 +42,83 @@ deerith::Mesh2D::Mesh2D(
glBindVertexArray(0); glBindVertexArray(0);
} }
deerith::Mesh2D::~Mesh2D() deerith::mesh_2d_t::~mesh_2d_t()
{ {
glDeleteVertexArrays(1, &vao); glDeleteVertexArrays(1, &vao);
glDeleteBuffers(1, &vbo); glDeleteBuffers(1, &vbo);
glDeleteBuffers(1, &ebo); glDeleteBuffers(1, &ebo);
} }
void deerith::Mesh2D::bind() const void deerith::mesh_2d_t::bind() const
{ {
glBindVertexArray(vao); glBindVertexArray(vao);
} }
void deerith::Mesh2D::draw() const void deerith::mesh_2d_t::draw() const
{
glBindVertexArray(vao);
glDrawElements(
GL_TRIANGLES,
static_cast<GLsizei>(index_count),
GL_UNSIGNED_INT,
nullptr);
}
deerith::mesh_3d_t::mesh_3d_t(
const glm::vec3 *vertices,
std::size_t vertex_count,
const unsigned int *indices,
std::size_t index_count)
: index_count(index_count)
{
glGenVertexArrays(1, &vao);
glGenBuffers(1, &vbo);
glGenBuffers(1, &ebo);
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(
GL_ARRAY_BUFFER,
vertex_count * sizeof(glm::vec3),
vertices,
GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
glBufferData(
GL_ELEMENT_ARRAY_BUFFER,
index_count * sizeof(unsigned int),
indices,
GL_STATIC_DRAW);
// Position: vec2
glVertexAttribPointer(
0,
3,
GL_FLOAT,
GL_FALSE,
3 * sizeof(float),
nullptr);
glEnableVertexAttribArray(0);
glBindVertexArray(0);
}
deerith::mesh_3d_t::~mesh_3d_t()
{
glDeleteVertexArrays(1, &vao);
glDeleteBuffers(1, &vbo);
glDeleteBuffers(1, &ebo);
}
void deerith::mesh_3d_t::bind() const
{
glBindVertexArray(vao);
}
void deerith::mesh_3d_t::draw() const
{ {
glBindVertexArray(vao); glBindVertexArray(vao);

View File

@ -15,4 +15,68 @@ namespace deerith
const unsigned int rectangle_indices[6] = { const unsigned int rectangle_indices[6] = {
0, 2, 1, 0, 2, 1,
1, 2, 3}; 1, 2, 3};
const glm::vec3 cube_vertices[8] = {
// Front face (Z = 1)
{0, 0, 1}, // 0: Bottom-left
{1, 0, 1}, // 1: Bottom-right
{0, 1, 1}, // 2: Top-left
{1, 1, 1}, // 3: Top-right
// Back face (Z = 0)
{0, 0, 0}, // 4: Bottom-left
{1, 0, 0}, // 5: Bottom-right
{0, 1, 0}, // 6: Top-left
{1, 1, 0}, // 7: Top-right
};
const unsigned int cube_indices[36] = {
// Front (+Z)
0,
2,
1,
1,
2,
3,
// Back (-Z)
5,
7,
4,
4,
7,
6,
// Left (-X)
4,
6,
0,
0,
6,
2,
// Right (+X)
1,
3,
5,
5,
3,
7,
// Top (+Y)
2,
6,
3,
3,
6,
7,
// Bottom (-Y)
4,
0,
5,
5,
0,
1,
};
} }

View File

@ -1,7 +1,8 @@
#include "primitive_internals.h" #include "primitive_internals.h"
#include "deerith/graphics/primitive.h" #include "deerith/graphics/primitive.h"
#include "deerith/graphics/shader.h" #include <deerith/graphics/shader.h>
#include "deerith/graphics/mesh.h" #include "deerith/graphics/mesh.h"
#include <deerith/engine.h>
#include "glm/glm.hpp" #include "glm/glm.hpp"
#include "glm/mat4x4.hpp" #include "glm/mat4x4.hpp"
@ -9,24 +10,31 @@
#include <glm/gtc/matrix_transform.hpp> #include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp> #include <glm/gtc/type_ptr.hpp>
#include <deerith/core/log.h>
#include <memory>
#include <glad/gl.h> #include <glad/gl.h>
namespace deerith namespace deerith
{ {
std::unique_ptr<Shader> primitive_shader; std::unique_ptr<shader_t> primitive_shader;
std::unique_ptr<Mesh2D> primitive_rectangle_mesh; std::unique_ptr<mesh_2d_t> primitive_rectangle_mesh;
std::unique_ptr<shader_t> lit_shader;
std::unique_ptr<mesh_3d_t> cube_mesh;
} }
void deerith::init_primitives() void deerith::init_primitives()
{ {
primitive_shader = std::make_unique<Shader>( primitive_shader = std::make_unique<shader_t>(
"../deerith_engine_client/shaders/primitive.vert.glsl", "../deerith_engine_client/shaders/primitive.vert.glsl",
"../deerith_engine_client/shaders/primitive.frag.glsl"); "../deerith_engine_client/shaders/primitive.frag.glsl");
primitive_rectangle_mesh = std::make_unique<Mesh2D>(rectangle_vertices, 4, rectangle_indices, 6); primitive_rectangle_mesh = std::make_unique<mesh_2d_t>(rectangle_vertices, 4, rectangle_indices, 6);
lit_shader = std::make_unique<shader_t>(
"../deerith_engine_client/shaders/lit.vert.glsl",
"../deerith_engine_client/shaders/lit.frag.glsl");
cube_mesh = std::make_unique<mesh_3d_t>(cube_vertices, 8, cube_indices, 36); // 6 faces * 2 * 3
} }
void deerith::shutdown_primitives() void deerith::shutdown_primitives()
@ -35,15 +43,19 @@ void deerith::shutdown_primitives()
primitive_shader.reset(); primitive_shader.reset();
primitive_rectangle_mesh.release(); primitive_rectangle_mesh.release();
primitive_rectangle_mesh.reset(); primitive_rectangle_mesh.reset();
lit_shader.release();
lit_shader.reset();
cube_mesh.release();
cube_mesh.reset();
} }
void deerith::define_primitive_viewspace(glm::vec2 start_viewspace, glm::vec2 end_viewspace) void deerith::set_primitive_viewspace(glm::vec2 start_viewspace, glm::vec2 end_viewspace)
{ {
float width = end_viewspace.x - start_viewspace.x; float width = end_viewspace.x - start_viewspace.x;
float height = end_viewspace.y - start_viewspace.y; float height = end_viewspace.y - start_viewspace.y;
deerith_assert(width > 0, "define_primitive_viewspace width must be > 0"); deerith_assert(width > 0, "set_primitive_viewspace width must be > 0");
deerith_assert(height > 0, "define_primitive_viewspace height must be > 0"); deerith_assert(height > 0, "set_primitive_viewspace height must be > 0");
glm::mat4 m_projection = glm::ortho( glm::mat4 m_projection = glm::ortho(
start_viewspace.x, start_viewspace.x,
@ -56,7 +68,7 @@ void deerith::define_primitive_viewspace(glm::vec2 start_viewspace, glm::vec2 en
primitive_shader->set_matrix("uViewProjection", m_projection); primitive_shader->set_matrix("uViewProjection", m_projection);
} }
void deerith::define_primitive_color(glm::vec4 color) void deerith::set_primitive_color(glm::vec4 color)
{ {
primitive_shader->set_vec4("uColor", color); primitive_shader->set_vec4("uColor", color);
} }
@ -76,3 +88,13 @@ void deerith::draw_primitive_rectangle(glm::vec2 start, glm::vec2 end)
primitive_shader->set_matrix("uTransform", model); primitive_shader->set_matrix("uTransform", model);
primitive_rectangle_mesh->draw(); primitive_rectangle_mesh->draw();
} }
deerith::shader_t *deerith::get_lit_shader()
{
return lit_shader.get();
}
deerith::mesh_3d_t *deerith::get_cube_mesh()
{
return cube_mesh.get();
}

View File

@ -21,12 +21,10 @@ std::filesystem::path get_executable_directory()
if (length == -1) if (length == -1)
throw std::runtime_error("Failed to get executable path"); throw std::runtime_error("Failed to get executable path");
return std::filesystem::path( return std::filesystem::path(std::string(buffer.data(), length)).parent_path();
std::string(buffer.data(), length))
.parent_path();
} }
deerith::Shader::Shader(const char *vertexPath, const char *fragmentPath) deerith::shader_t::shader_t(const char *vertexPath, const char *fragmentPath)
{ {
std::string vertexCode; std::string vertexCode;
std::string fragmentCode; std::string fragmentCode;
@ -74,57 +72,57 @@ deerith::Shader::Shader(const char *vertexPath, const char *fragmentPath)
const char *vertexSource = vertexCode.c_str(); const char *vertexSource = vertexCode.c_str();
const char *fragmentSource = fragmentCode.c_str(); const char *fragmentSource = fragmentCode.c_str();
unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER); unsigned int vertex_shader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource( glShaderSource(
vertexShader, vertex_shader,
1, 1,
&vertexSource, &vertexSource,
nullptr); nullptr);
glCompileShader(vertexShader); glCompileShader(vertex_shader);
int success; int success;
char infoLog[1024]; char infoLog[1024];
glGetShaderiv( glGetShaderiv(
vertexShader, vertex_shader,
GL_COMPILE_STATUS, GL_COMPILE_STATUS,
&success); &success);
if (!success) if (!success)
{ {
glGetShaderInfoLog( glGetShaderInfoLog(
vertexShader, vertex_shader,
sizeof(infoLog), sizeof(infoLog),
nullptr, nullptr,
infoLog); infoLog);
std::cerr std::cerr
<< "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << "ERROR::shader_t::VERTEX::COMPILATION_FAILED\n"
<< infoLog << infoLog
<< std::endl; << std::endl;
} }
unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); unsigned int fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource( glShaderSource(
fragmentShader, fragment_shader,
1, 1,
&fragmentSource, &fragmentSource,
nullptr); nullptr);
glCompileShader(fragmentShader); glCompileShader(fragment_shader);
glGetShaderiv( glGetShaderiv(
fragmentShader, fragment_shader,
GL_COMPILE_STATUS, GL_COMPILE_STATUS,
&success); &success);
if (!success) if (!success)
{ {
glGetShaderInfoLog( glGetShaderInfoLog(
fragmentShader, fragment_shader,
sizeof(infoLog), sizeof(infoLog),
nullptr, nullptr,
infoLog); infoLog);
@ -137,8 +135,8 @@ deerith::Shader::Shader(const char *vertexPath, const char *fragmentPath)
id = glCreateProgram(); id = glCreateProgram();
glAttachShader(id, vertexShader); glAttachShader(id, vertex_shader);
glAttachShader(id, fragmentShader); glAttachShader(id, fragment_shader);
glLinkProgram(id); glLinkProgram(id);
@ -156,21 +154,21 @@ deerith::Shader::Shader(const char *vertexPath, const char *fragmentPath)
infoLog); infoLog);
std::cerr std::cerr
<< "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << "ERROR::shader_t::PROGRAM::LINKING_FAILED\n"
<< infoLog << infoLog
<< std::endl; << std::endl;
} }
glDeleteShader(vertexShader); glDeleteShader(vertex_shader);
glDeleteShader(fragmentShader); glDeleteShader(fragment_shader);
} }
void deerith::Shader::use() const void deerith::shader_t::use() const
{ {
glUseProgram(id); glUseProgram(id);
} }
void deerith::Shader::set_bool(const char *name, bool value) const void deerith::shader_t::set_bool(const char *name, bool value) const
{ {
this->use(); this->use();
glUniform1i( glUniform1i(
@ -178,7 +176,7 @@ void deerith::Shader::set_bool(const char *name, bool value) const
static_cast<int>(value)); static_cast<int>(value));
} }
void deerith::Shader::set_int(const char *name, int value) const void deerith::shader_t::set_int(const char *name, int value) const
{ {
this->use(); this->use();
glUniform1i( glUniform1i(
@ -186,7 +184,7 @@ void deerith::Shader::set_int(const char *name, int value) const
value); value);
} }
void deerith::Shader::set_float(const char *name, float value) const void deerith::shader_t::set_float(const char *name, float value) const
{ {
this->use(); this->use();
glUniform1f( glUniform1f(
@ -194,7 +192,7 @@ void deerith::Shader::set_float(const char *name, float value) const
value); value);
} }
void deerith::Shader::set_matrix( void deerith::shader_t::set_matrix(
const char *name, const char *name,
const glm::mat4 &value) const glm::mat4 &value)
{ {
@ -206,7 +204,7 @@ void deerith::Shader::set_matrix(
glm::value_ptr(value)); glm::value_ptr(value));
} }
void deerith::Shader::set_vec3( void deerith::shader_t::set_vec3(
const char *name, const char *name,
const glm::vec3 &value) const glm::vec3 &value)
{ {
@ -217,7 +215,7 @@ void deerith::Shader::set_vec3(
glm::value_ptr(value)); glm::value_ptr(value));
} }
void deerith::Shader::set_vec4( void deerith::shader_t::set_vec4(
const char *name, const char *name,
const glm::vec4 &value) const glm::vec4 &value)
{ {

View File

@ -3,7 +3,7 @@
#include <deerith/graphics/window.h> #include <deerith/graphics/window.h>
#include <deerith/network/router.h> #include <deerith/network/router.h>
#include <deerith/network/packets.h> #include <deerith/network/packets.h>
#include <deerith/world/entity.h> #include <deerith/world/components.h>
#include "world/world_internals.h" #include "world/world_internals.h"
@ -23,7 +23,7 @@ void deerith::process_player_input()
movment = glm::vec2(0, 0); movment = glm::vec2(0, 0);
} }
entity_sync_client_position_t &entity_sync_position = get_component<entity_sync_client_position_t>(self_entity); component_player_sync_input_t &entity_sync_position = get_component<component_player_sync_input_t>(self_entity);
entity_sync_position.move_input = movment; entity_sync_position.move_input = movment;
entity_sync_position.position = entity_sync_position.position.read_value() + movment * 0.1f; entity_sync_position.position = entity_sync_position.position.read_value() + movment * 0.1f;
@ -33,9 +33,9 @@ void deerith::send_player_input()
{ {
if (self_entity == entt::null) if (self_entity == entt::null)
return; return;
entity_sync_client_position_t &entity_sync_position = get_component<entity_sync_client_position_t>(self_entity); component_player_sync_input_t &entity_sync_position = get_component<component_player_sync_input_t>(self_entity);
sync_entity_player_position_packet_t packet{}; packet_update_player_input_t packet{};
packet.input_transport.load(entity_sync_position.move_input); packet.input_transport.load(entity_sync_position.move_input);
packet.position_transport.load(entity_sync_position.position); packet.position_transport.load(entity_sync_position.position);

View File

@ -1,7 +1,7 @@
#include <deerith/engine.h> #include <deerith/engine.h>
#include <deerith/network/router.h> #include <deerith/network/router.h>
#include <deerith/network/packets.h> #include <deerith/network/packets.h>
#include <deerith/world/entity.h> #include <deerith/world/components.h>
#include <deerith/world.h> #include <deerith/world.h>
#include "world/world_internals.h" #include "world/world_internals.h"
@ -9,29 +9,29 @@
void deerith::setup_base_protocols() void deerith::setup_base_protocols()
{ {
router_add_packet_processor<sync_world_tick_packet_t>(); router_add_packet_processor<packet_sync_world_tick_t>();
router_add_packet_processor<create_player_entity_packet_t>(); router_add_packet_processor<packet_create_player_entity_t>();
router_add_packet_processor<create_entity_packet_t>(); router_add_packet_processor<packet_create_entity_t>();
router_add_packet_processor<sync_entities_coordinates_packet_t>(); router_add_packet_processor<packet_update_entities_coordinates_t>();
router_add_packet_processor<destroy_entity_packet_t>(); router_add_packet_processor<packet_destroy_entity_t>();
} }
template <> template <>
void deerith::process_packet(deerith::sync_world_tick_packet_t &packet, client_t *client) void deerith::process_packet(deerith::packet_sync_world_tick_t &packet, client_t *client)
{ {
deerith_trace("tick set to {}", packet.tick_id); deerith_trace("tick set to {}", packet.tick_id);
set_tick(packet.tick_id); set_tick(packet.tick_id);
} }
template <> template <>
void deerith::process_packet(deerith::create_player_entity_packet_t &packet, client_t *client) void deerith::process_packet(deerith::packet_create_player_entity_t &packet, client_t *client)
{ {
self_entity = create_entity(); self_entity = create_entity();
add_component<entity_sync_client_position_t>(self_entity); add_component<component_player_sync_input_t>(self_entity);
} }
template <> template <>
void deerith::process_packet(deerith::create_entity_packet_t &packet, client_t *client) void deerith::process_packet(deerith::packet_create_entity_t &packet, client_t *client)
{ {
deerith_trace("creating entity with network id {}", packet.network_sync_id); deerith_trace("creating entity with network id {}", packet.network_sync_id);
@ -46,17 +46,17 @@ void deerith::process_packet(deerith::create_entity_packet_t &packet, client_t *
entity = create_entity(); entity = create_entity();
} }
entity_network_id_t &entity_network_id = add_entity_network_with_id(entity, packet.network_sync_id); component_network_id_t &entity_network_id = add_entity_network_with_id(entity, packet.network_sync_id);
if (packet.add_entity_sync_coordinates) if (packet.add_entity_sync_coordinates)
{ {
deerith_trace("-> adding entity sync coordinates to entity"); deerith_trace("-> adding entity sync coordinates to entity");
entity_sync_coordinates_t &sync_coords = add_component<entity_sync_coordinates_t>(entity); component_sync_coordinates_t &sync_coords = add_component<component_sync_coordinates_t>(entity);
} }
} }
template <> template <>
void deerith::process_packet(deerith::sync_entities_coordinates_packet_t &packet, client_t *client) void deerith::process_packet(deerith::packet_update_entities_coordinates_t &packet, client_t *client)
{ {
for (int i = 0; i < packet.entities_ids.size(); i++) for (int i = 0; i < packet.entities_ids.size(); i++)
{ {
@ -66,14 +66,14 @@ void deerith::process_packet(deerith::sync_entities_coordinates_packet_t &packet
} }
entity_t entity = get_network_entity_with_id(packet.entities_ids[i]); entity_t entity = get_network_entity_with_id(packet.entities_ids[i]);
entity_sync_coordinates_t &sync_coords = get_component<entity_sync_coordinates_t>(entity); component_sync_coordinates_t &sync_coords = get_component<component_sync_coordinates_t>(entity);
packet.positions_to_update[i].recive(sync_coords.position); packet.positions_to_update[i].recive(sync_coords.position);
} }
} }
template <> template <>
void deerith::process_packet(deerith::destroy_entity_packet_t &packet, client_t *client) void deerith::process_packet(deerith::packet_destroy_entity_t &packet, client_t *client)
{ {
deerith_trace("destroying entity : {}", packet.network_sync_id); deerith_trace("destroying entity : {}", packet.network_sync_id);
entity_t specific_entity = get_network_entity_with_id(packet.network_sync_id); entity_t specific_entity = get_network_entity_with_id(packet.network_sync_id);

View File

@ -1,28 +1,58 @@
#include <deerith/world.h> #include <deerith/world.h>
#include <deerith/world/entity.h> #include <deerith/world/components.h>
#include <deerith/graphics/camera.h>
#include <deerith/graphics/mesh.h>
#include <deerith/graphics/shader.h>
#include <deerith/graphics/primitive.h> #include <deerith/graphics/primitive.h>
#include <glm/glm.hpp>
#include <glm/gtc/quaternion.hpp>
void deerith::render_world() void deerith::render_world()
{ {
define_primitive_viewspace(glm::vec2(-10, -10), glm::vec2(10, 10)); set_primitive_viewspace(glm::vec2(-10, -10), glm::vec2(10, 10));
auto entities_view = get_entity_registry().view<entity_sync_coordinates_t>(entt::exclude<entity_sync_client_position_t>); auto entities_view = get_entity_registry().view<component_sync_coordinates_t>(entt::exclude<component_player_sync_input_t>);
for (auto [entity, sync_coords] : entities_view.each()) for (auto [entity, sync_coords] : entities_view.each())
{ {
glm::vec2 chords; glm::vec2 chords;
if (sync_coords.position.read_synced(chords)) if (sync_coords.position.read_synced(chords))
{ {
define_primitive_color(glm::vec4(1, 1, 1, 1)); camera_t camera;
draw_primitive_rectangle(chords - glm::vec2(0.5f, 0.5f), chords + glm::vec2(0.5f, 0.5f)); camera.position = glm::vec3(0, 0, -10);
glm::vec3 position = glm::vec3(chords.x, 0, chords.y);
glm::quat rotation = glm::identity<glm::quat>();
glm::vec3 scale = glm::vec3(1, 1, 1);
set_camera_to_shader(camera, get_lit_shader());
set_transform_to_shader(position, rotation, scale, get_lit_shader());
get_lit_shader()->set_vec4("u_color", glm::vec4(1, 0.4f, 0.6f, 1));
get_lit_shader()->use();
get_cube_mesh()->draw();
} }
} }
// we should expect only one registry // we should expect only one registry
auto self_view = get_entity_registry().view<entity_sync_coordinates_t, entity_sync_client_position_t>(); auto self_view = get_entity_registry().view<component_sync_coordinates_t, component_player_sync_input_t>();
for (auto [entity, sync_coords, client_sync_coords] : self_view.each()) for (auto [entity, sync_coords, client_sync_coords] : self_view.each())
{ {
define_primitive_color(glm::vec4(1, 0.5f, 0.6f, 1));
draw_primitive_rectangle(client_sync_coords.position.read_value() - glm::vec2(0.5f, 0.5f), client_sync_coords.position.read_value() + glm::vec2(0.5f, 0.5f)); camera_t camera;
camera.position = glm::vec3(0, 0, -10);
get_lit_shader()->set_vec4("u_color", glm::vec4(0.8f, 0.8f, 0.8f, 1));
glm::vec2 user_pos = client_sync_coords.position.read_value();
glm::vec3 position = glm::vec3(user_pos.x, 0, user_pos.y);
glm::quat rotation = glm::identity<glm::quat>();
glm::vec3 scale = glm::vec3(1, 1, 1);
set_camera_to_shader(camera, get_lit_shader());
set_transform_to_shader(position, rotation, scale, get_lit_shader());
get_lit_shader()->use();
get_cube_mesh()->draw();
} }
} }

View File

@ -1,6 +0,0 @@
#pragma once
namespace deerith
{
}

View File

@ -1,7 +1,6 @@
#pragma once #pragma once
#include <cstdint> #include <cstdint>
#include <deerith/core/log.h> #include <deerith/core/log.h>
#include <deerith/core/core.h>
#define world_SIMULATION_TICK_FREQ 50 #define world_SIMULATION_TICK_FREQ 50
#define NETWORK_RELATIVE_TICK 2 #define NETWORK_RELATIVE_TICK 2

View File

@ -1,12 +1,12 @@
#pragma once #pragma once
#include <deerith/engine.h> #include <deerith/engine.h>
#include <deerith/world/entity.h> #include <deerith/world/components.h>
#include <vector> #include <vector>
namespace deerith namespace deerith
{ {
struct sync_world_tick_packet_t struct packet_sync_world_tick_t
{ {
static constexpr uint16_t packet_id = 1; static constexpr uint16_t packet_id = 1;
static constexpr bool reliable = true; static constexpr bool reliable = true;
@ -20,13 +20,13 @@ namespace deerith
} }
}; };
struct create_player_entity_packet_t struct packet_create_player_entity_t
{ {
static constexpr uint16_t packet_id = 2; static constexpr uint16_t packet_id = 2;
static constexpr bool reliable = true; static constexpr bool reliable = true;
}; };
struct sync_entity_player_position_packet_t struct packet_update_player_input_t
{ {
static constexpr uint16_t packet_id = 3; static constexpr uint16_t packet_id = 3;
static constexpr bool reliable = false; static constexpr bool reliable = false;
@ -42,7 +42,7 @@ namespace deerith
} }
}; };
struct create_entity_packet_t struct packet_create_entity_t
{ {
static constexpr uint16_t packet_id = 4; static constexpr uint16_t packet_id = 4;
static constexpr bool reliable = true; static constexpr bool reliable = true;
@ -60,9 +60,23 @@ namespace deerith
} }
}; };
struct sync_entities_coordinates_packet_t struct packet_destroy_entity_t
{ {
static constexpr uint16_t packet_id = 5; static constexpr uint16_t packet_id = 5;
static constexpr bool reliable = true;
id_t network_sync_id = 0;
template <typename Archive>
void serialize(Archive &archive)
{
archive(network_sync_id);
}
};
struct packet_update_entities_coordinates_t
{
static constexpr uint16_t packet_id = 6;
static constexpr bool reliable = false; static constexpr bool reliable = false;
std::vector<id_t> entities_ids; std::vector<id_t> entities_ids;
@ -76,17 +90,4 @@ namespace deerith
} }
}; };
struct destroy_entity_packet_t
{
static constexpr uint16_t packet_id = 6;
static constexpr bool reliable = true;
id_t network_sync_id = 0;
template <typename Archive>
void serialize(Archive &archive)
{
archive(network_sync_id);
}
};
} }

View File

@ -1,5 +1,4 @@
#pragma once #pragma once
#include <deerith/engine.h> #include <deerith/engine.h>
#include <deerith/world/world_core.h> #include <deerith/world/world_core.h>
@ -8,7 +7,7 @@
#include <cstdint> #include <cstdint>
#include <cereal/types/array.hpp> #include <cereal/types/array.hpp>
#include <deerith/core/serialization_ext.h> #include <deerith/network/serialization_ext.h>
#define SYNC_VARIABLE_STORAGE 30 #define SYNC_VARIABLE_STORAGE 30
#define BUFFER_SIZE (NETWORK_DELAY + SYNC_VARIABLE_STORAGE) #define BUFFER_SIZE (NETWORK_DELAY + SYNC_VARIABLE_STORAGE)

View File

@ -1,10 +0,0 @@
#pragma once
#include <deerith/world/character_sync_interface.h>
namespace deerith
{
struct character_t
{
};
}

View File

@ -5,24 +5,24 @@
namespace deerith namespace deerith
{ {
// struct for linking a entity to a client, mostly in the server // struct for linking a entity to a client, mostly in the server
struct entity_link_to_client_t struct component_client_link_t
{ {
client_t *client = nullptr; client_t *client = nullptr;
}; };
struct entity_network_id_t struct component_network_id_t
{ {
id_t network_id; id_t network_id;
}; };
// struct for linking a entity to a client // struct for linking a entity to a client
struct entity_sync_client_position_t struct component_player_sync_input_t
{ {
sync_variable_t<glm::vec2> move_input; sync_variable_t<glm::vec2> move_input;
sync_variable_t<glm::vec2> position; sync_variable_t<glm::vec2> position;
}; };
struct entity_sync_coordinates_t struct component_sync_coordinates_t
{ {
sync_variable_t<glm::vec2> position; sync_variable_t<glm::vec2> position;
}; };

View File

@ -4,7 +4,7 @@
namespace deerith namespace deerith
{ {
struct entity_network_id_t; struct component_network_id_t;
using entity_registry_t = entt::registry; using entity_registry_t = entt::registry;
using entity_t = entt::entity; using entity_t = entt::entity;
@ -35,8 +35,8 @@ namespace deerith
return get_entity_registry().all_of<T>(ent); return get_entity_registry().all_of<T>(ent);
} }
entity_network_id_t &add_entity_network_id(entity_t ent); component_network_id_t &add_entity_network_id(entity_t ent);
entity_network_id_t &add_entity_network_with_id(entity_t ent, id_t id); component_network_id_t &add_entity_network_with_id(entity_t ent, id_t id);
entity_t get_network_entity_with_id(id_t id); entity_t get_network_entity_with_id(id_t id);
bool exists_network_entity_with_id(id_t id); bool exists_network_entity_with_id(id_t id);
} }

View File

@ -1,5 +1,5 @@
#include <deerith/world/world_core.h> #include <deerith/world/world_core.h>
#include <deerith/world/entity.h> #include <deerith/world/components.h>
#include <vector> #include <vector>
@ -15,14 +15,14 @@ entt::registry &deerith::get_entity_registry()
return entity_registry; return entity_registry;
} }
deerith::entity_network_id_t &deerith::add_entity_network_id(entity_t ent) deerith::component_network_id_t &deerith::add_entity_network_id(entity_t ent)
{ {
while (network_entities.contains(sequential_network_id)) while (network_entities.contains(sequential_network_id))
{ {
sequential_network_id++; sequential_network_id++;
} }
entity_network_id_t &network_id = add_component<entity_network_id_t>(ent); component_network_id_t &network_id = add_component<component_network_id_t>(ent);
network_id.network_id = sequential_network_id; network_id.network_id = sequential_network_id;
network_entities[sequential_network_id] = ent; network_entities[sequential_network_id] = ent;
@ -30,9 +30,9 @@ deerith::entity_network_id_t &deerith::add_entity_network_id(entity_t ent)
return network_id; return network_id;
} }
deerith::entity_network_id_t &deerith::add_entity_network_with_id(entity_t ent, id_t id) deerith::component_network_id_t &deerith::add_entity_network_with_id(entity_t ent, id_t id)
{ {
entity_network_id_t &network_id = add_component<entity_network_id_t>(ent); component_network_id_t &network_id = add_component<component_network_id_t>(ent);
network_id.network_id = id; network_id.network_id = id;
network_entities[id] = ent; network_entities[id] = ent;

View File

@ -1,7 +1,4 @@
#include <deerith/engine.h> #include <deerith/engine.h>
#include <deerith/core/core.h>
#include <deerith/core/log.h>
#include "network/network_internals.h" #include "network/network_internals.h"

View File

@ -1,6 +1,6 @@
#include <deerith/engine.h> #include <deerith/engine.h>
#include <deerith/world.h> #include <deerith/world.h>
#include <deerith/world/entity.h> #include <deerith/world/components.h>
#include <deerith/network/packets.h> #include <deerith/network/packets.h>
#include <deerith/network/router.h> #include <deerith/network/router.h>
#include <deerith/network/client.h> #include <deerith/network/client.h>
@ -14,14 +14,14 @@ namespace deerith
} }
void deerith::load_base_protocols() void deerith::load_base_protocols()
{ {
router_add_packet_processor<sync_entity_player_position_packet_t>(); router_add_packet_processor<packet_update_player_input_t>();
} }
template <> template <>
void deerith::process_packet(sync_entity_player_position_packet_t &packet, client_t *client) void deerith::process_packet(packet_update_player_input_t &packet, client_t *client)
{ {
entity_t entity = client->entity_id; entity_t entity = client->entity_id;
entity_sync_client_position_t &sync_client = get_component<entity_sync_client_position_t>(entity); component_player_sync_input_t &sync_client = get_component<component_player_sync_input_t>(entity);
packet.input_transport.recive(sync_client.move_input); packet.input_transport.recive(sync_client.move_input);
packet.position_transport.recive(sync_client.position); packet.position_transport.recive(sync_client.position);
@ -30,25 +30,25 @@ void deerith::process_packet(sync_entity_player_position_packet_t &packet, clien
void deerith::client_connects(client_t *client) void deerith::client_connects(client_t *client)
{ {
// sync world tick // sync world tick
sync_world_tick_packet_t sync_world_tick; packet_sync_world_tick_t sync_world_tick;
sync_world_tick.tick_id = get_world_tick(); sync_world_tick.tick_id = get_world_tick();
router_send_packet(sync_world_tick, client); router_send_packet(sync_world_tick, client);
// create entity player entity // create entity player entity
entity_t entity = create_entity(); entity_t entity = create_entity();
entity_sync_client_position_t &position_sync = add_component<entity_sync_client_position_t>(entity); component_player_sync_input_t &position_sync = add_component<component_player_sync_input_t>(entity);
create_player_entity_packet_t create_player_entity; packet_create_player_entity_t create_player_entity;
router_send_packet(create_player_entity, client); router_send_packet(create_player_entity, client);
// create the server representation of the entity // create the server representation of the entity
add_entity_network_id(entity); add_entity_network_id(entity);
add_component<entity_sync_coordinates_t>(entity); add_component<component_sync_coordinates_t>(entity);
send_create_entity_to_all_clients(entity); send_create_entity_to_all_clients(entity);
// we do this the last to avoid send_create_entity_to_all_clients send_create_actual_entities_to_client create the same entity for the player // we do this the last to avoid send_create_entity_to_all_clients send_create_actual_entities_to_client create the same entity for the player
entity_link_to_client_t &link = add_component<entity_link_to_client_t>(entity); component_client_link_t &link = add_component<component_client_link_t>(entity);
client->entity_id = entity; client->entity_id = entity;
link.client = client; link.client = client;
@ -57,7 +57,7 @@ void deerith::client_connects(client_t *client)
void deerith::send_create_entity_to_all_clients(deerith::entity_t ent) void deerith::send_create_entity_to_all_clients(deerith::entity_t ent)
{ {
auto view = get_entity_registry().view<entity_link_to_client_t>(); auto view = get_entity_registry().view<component_client_link_t>();
for (auto [entity, link_to_client] : view.each()) for (auto [entity, link_to_client] : view.each())
{ {
send_create_entity_to_client(ent, link_to_client.client); send_create_entity_to_client(ent, link_to_client.client);
@ -66,20 +66,20 @@ void deerith::send_create_entity_to_all_clients(deerith::entity_t ent)
void deerith::send_create_entity_to_client(entity_t ent, client_t *client) void deerith::send_create_entity_to_client(entity_t ent, client_t *client)
{ {
entity_network_id_t &entity_network_id = get_component<entity_network_id_t>(ent); component_network_id_t &entity_network_id = get_component<component_network_id_t>(ent);
create_entity_packet_t create_entity; packet_create_entity_t create_entity;
create_entity.network_sync_id = entity_network_id.network_id; create_entity.network_sync_id = entity_network_id.network_id;
// in case its the self entity we let it know to the client // in case its the self entity we let it know to the client
if (has_component<entity_link_to_client_t>(ent)) if (has_component<component_client_link_t>(ent))
{ {
entity_link_to_client_t &link = get_component<entity_link_to_client_t>(ent); component_client_link_t &link = get_component<component_client_link_t>(ent);
create_entity.is_self_client = link.client == client; create_entity.is_self_client = link.client == client;
deerith_trace("entity could be self {}", create_entity.is_self_client); deerith_trace("entity could be self {}", create_entity.is_self_client);
} }
if (has_component<entity_sync_coordinates_t>(ent)) if (has_component<component_sync_coordinates_t>(ent))
{ {
create_entity.add_entity_sync_coordinates = true; create_entity.add_entity_sync_coordinates = true;
} }
@ -97,12 +97,12 @@ void deerith::client_disconnects(client_t *client)
void deerith::send_delete_entity_to_all_clients(entity_t ent) void deerith::send_delete_entity_to_all_clients(entity_t ent)
{ {
destroy_entity_packet_t destroy_entity; packet_destroy_entity_t destroy_entity;
entity_network_id_t &network_id = get_component<entity_network_id_t>(ent); component_network_id_t &network_id = get_component<component_network_id_t>(ent);
destroy_entity.network_sync_id = network_id.network_id; destroy_entity.network_sync_id = network_id.network_id;
auto view = get_entity_registry().view<entity_link_to_client_t>(); auto view = get_entity_registry().view<component_client_link_t>();
for (auto [entity, link_to_client] : view.each()) for (auto [entity, link_to_client] : view.each())
{ {
router_send_packet(destroy_entity, link_to_client.client); router_send_packet(destroy_entity, link_to_client.client);
@ -111,7 +111,7 @@ void deerith::send_delete_entity_to_all_clients(entity_t ent)
void deerith::send_create_actual_entities_to_client(client_t *client) void deerith::send_create_actual_entities_to_client(client_t *client)
{ {
auto entities_view = get_entity_registry().view<entity_network_id_t>(); auto entities_view = get_entity_registry().view<component_network_id_t>();
for (auto [ent, network_id] : entities_view.each()) for (auto [ent, network_id] : entities_view.each())
{ {

View File

@ -5,7 +5,7 @@
#include "network/network_internals.h" #include "network/network_internals.h"
#include "world/world_internals.h" #include "world/world_internals.h"
#include <deerith/world/entity.h> #include <deerith/world/components.h>
#include <deerith/network/packets.h> #include <deerith/network/packets.h>
#include <csignal> #include <csignal>

View File

@ -1,6 +1,6 @@
#pragma once #pragma once
#include <deerith/network/client.h> #include <deerith/network/client.h>
#include <deerith/world/entity.h> #include <deerith/world/components.h>
namespace deerith namespace deerith
{ {

View File

@ -1,13 +1,13 @@
#include "world/world_internals.h" #include "world/world_internals.h"
#include <deerith/world.h> #include <deerith/world.h>
#include <deerith/world/entity.h> #include <deerith/world/components.h>
#include <deerith/network/packets.h> #include <deerith/network/packets.h>
#include <deerith/network/router.h> #include <deerith/network/router.h>
void deerith::update_players_characters() void deerith::update_players_characters()
{ {
auto view = get_entity_registry().view<entity_sync_client_position_t, entity_link_to_client_t, entity_sync_coordinates_t>(); auto view = get_entity_registry().view<component_player_sync_input_t, component_client_link_t, component_sync_coordinates_t>();
for (auto [entity, position_input, client, sync_coords] : view.each()) for (auto [entity, position_input, client, sync_coords] : view.each())
{ {
@ -25,9 +25,9 @@ void deerith::update_players_characters()
void deerith::send_entity_data() void deerith::send_entity_data()
{ {
sync_entities_coordinates_packet_t sync_entities_coordinates; packet_update_entities_coordinates_t sync_entities_coordinates;
auto coordinates_view = get_entity_registry().view<entity_network_id_t, entity_sync_coordinates_t>(); auto coordinates_view = get_entity_registry().view<component_network_id_t, component_sync_coordinates_t>();
for (auto [entity, network_id, sync_coords] : coordinates_view.each()) for (auto [entity, network_id, sync_coords] : coordinates_view.each())
{ {
sync_entities_coordinates.entities_ids.push_back(network_id.network_id); sync_entities_coordinates.entities_ids.push_back(network_id.network_id);
@ -36,7 +36,7 @@ void deerith::send_entity_data()
position_trasport.load(sync_coords.position); position_trasport.load(sync_coords.position);
} }
auto clients_view = get_entity_registry().view<entity_link_to_client_t>(); auto clients_view = get_entity_registry().view<component_client_link_t>();
for (auto [entity, client] : clients_view.each()) for (auto [entity, client] : clients_view.each())
{ {
router_send_packet(sync_entities_coordinates, client.client); router_send_packet(sync_entities_coordinates, client.client);