feat: initial DX12 foundation framework
This commit is contained in:
128
Engine/EngineAPI/Camera.h
Normal file
128
Engine/EngineAPI/Camera.h
Normal file
@@ -0,0 +1,128 @@
|
||||
/**
|
||||
* @file Camera.h
|
||||
* @brief 相机句柄与视图/投影参数接口定义。
|
||||
* @details
|
||||
* camera 提供对渲染相机实例的统一访问,覆盖:
|
||||
* - 透视/正交两类投影参数设置;
|
||||
* - 视图矩阵、投影矩阵及其逆矩阵查询;
|
||||
* - 近远裁剪面、宽高与绑定实体 ID 读取。
|
||||
*/
|
||||
#pragma once
|
||||
#include "CommonHeader.h"
|
||||
|
||||
namespace XEngine::graphics {
|
||||
|
||||
|
||||
/**
|
||||
* @brief 相机强类型标识符。
|
||||
*/
|
||||
DEFINE_TYPED_ID(camera_id);
|
||||
|
||||
/**
|
||||
* @brief 渲染相机句柄对象。
|
||||
*/
|
||||
class camera
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* @brief 投影模式枚举。
|
||||
*/
|
||||
enum type :u32
|
||||
{
|
||||
perspective,
|
||||
orthographic,
|
||||
};
|
||||
|
||||
constexpr explicit camera(camera_id id) : _id{ id } {}
|
||||
constexpr camera() = default;
|
||||
constexpr camera_id get_id() const { return _id; }
|
||||
constexpr bool is_valid() const { return id::is_valid(_id); }
|
||||
|
||||
/**
|
||||
* @brief 设置相机上方向。
|
||||
*/
|
||||
void up(math::v3 up) const;
|
||||
/**
|
||||
* @brief 设置透视投影视场角。
|
||||
*/
|
||||
void field_of_view(f32 fov) const;
|
||||
/**
|
||||
* @brief 设置视口宽高比。
|
||||
*/
|
||||
void aspect_ratio(f32 aspect_ratio) const;
|
||||
/**
|
||||
* @brief 设置正交视口宽度。
|
||||
*/
|
||||
void view_width(f32 width) const;
|
||||
/**
|
||||
* @brief 设置正交视口高度。
|
||||
*/
|
||||
void view_height(f32 height) const;
|
||||
/**
|
||||
* @brief 设置近远裁剪面。
|
||||
*/
|
||||
void range(f32 near_z, f32 far_z) const;
|
||||
|
||||
/**
|
||||
* @brief 获取视图矩阵。
|
||||
*/
|
||||
math::m4x4 view() const;
|
||||
/**
|
||||
* @brief 获取投影矩阵。
|
||||
*/
|
||||
math::m4x4 projection() const;
|
||||
/**
|
||||
* @brief 获取投影矩阵逆矩阵。
|
||||
*/
|
||||
math::m4x4 inverse_projection() const;
|
||||
/**
|
||||
* @brief 获取视图投影矩阵。
|
||||
*/
|
||||
math::m4x4 view_projection() const;
|
||||
/**
|
||||
* @brief 获取视图投影逆矩阵。
|
||||
*/
|
||||
math::m4x4 inverse_view_projection() const;
|
||||
/**
|
||||
* @brief 获取上方向向量。
|
||||
*/
|
||||
math::v3 up() const;
|
||||
|
||||
/**
|
||||
* @brief 获取近裁剪面。
|
||||
*/
|
||||
f32 near_z() const;
|
||||
/**
|
||||
* @brief 获取远裁剪面。
|
||||
*/
|
||||
f32 far_z() const;
|
||||
/**
|
||||
* @brief 获取视场角。
|
||||
*/
|
||||
f32 field_of_view() const;
|
||||
/**
|
||||
* @brief 获取宽高比。
|
||||
*/
|
||||
f32 aspect_ratio() const;
|
||||
/**
|
||||
* @brief 获取视口宽度。
|
||||
*/
|
||||
f32 view_width() const;
|
||||
/**
|
||||
* @brief 获取视口高度。
|
||||
*/
|
||||
f32 view_height() const;
|
||||
/**
|
||||
* @brief 获取投影类型。
|
||||
*/
|
||||
type projection_type() const;
|
||||
/**
|
||||
* @brief 获取绑定实体 ID。
|
||||
*/
|
||||
id::id_type entity_id() const;
|
||||
private:
|
||||
camera_id _id{ id::invalid_id };
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
191
Engine/EngineAPI/GameEntity.h
Normal file
191
Engine/EngineAPI/GameEntity.h
Normal file
@@ -0,0 +1,191 @@
|
||||
/**
|
||||
* @file GameEntity.h
|
||||
* @brief 实体句柄、脚本基类与脚本注册机制定义。
|
||||
* @details
|
||||
* 该文件是引擎 ECS 对外脚本接口的核心入口:
|
||||
* - game_entity::entity 提供实体句柄及常用变换访问;
|
||||
* - script::entity_script 定义脚本生命周期与变换写接口;
|
||||
* - script::detail 提供脚本工厂注册、按名称哈希检索与宏封装。
|
||||
*/
|
||||
#pragma once
|
||||
#include "..\Components\ComponentsCommon.h"
|
||||
#include "TransformComponent.h"
|
||||
#include "ScriptComponent.h"
|
||||
|
||||
namespace XEngine{
|
||||
namespace game_entity {
|
||||
|
||||
/**
|
||||
* @brief 实体强类型标识符。
|
||||
*/
|
||||
DEFINE_TYPED_ID(entity_id);
|
||||
|
||||
/**
|
||||
* @brief 运行时实体句柄封装。
|
||||
*/
|
||||
class entity {
|
||||
public:
|
||||
/**
|
||||
* @brief 由实体 ID 构造句柄。
|
||||
*/
|
||||
constexpr explicit entity(entity_id id) : _id{ id } {}
|
||||
/**
|
||||
* @brief 构造无效实体句柄。
|
||||
*/
|
||||
constexpr entity() : _id{ id::invalid_id } {}
|
||||
/**
|
||||
* @brief 获取实体 ID。
|
||||
*/
|
||||
[[nodiscard]] constexpr entity_id get_id() const { return _id; }
|
||||
/**
|
||||
* @brief 判断实体句柄是否有效。
|
||||
*/
|
||||
[[nodiscard]] constexpr bool is_valid() const { return id::is_valid(_id); }
|
||||
|
||||
/**
|
||||
* @brief 获取实体变换组件句柄。
|
||||
*/
|
||||
[[nodiscard]] transform::component transform() const;
|
||||
/**
|
||||
* @brief 获取实体脚本组件句柄。
|
||||
*/
|
||||
[[nodiscard]] script::component script() const;
|
||||
|
||||
[[nodiscard]] math::v4 rotation() const { return transform().rotation(); }
|
||||
[[nodiscard]] math::v3 orientation() const { return transform().orientation(); }
|
||||
[[nodiscard]] math::v3 position() const { return transform().position(); }
|
||||
[[nodiscard]] math::v3 scale() const { return transform().scale(); }
|
||||
private:
|
||||
entity_id _id;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
namespace script {
|
||||
/**
|
||||
* @brief 实体脚本基类。
|
||||
* @details
|
||||
* 派生类可覆盖 begin_play/update,并通过受保护接口修改实体变换。
|
||||
*/
|
||||
class entity_script : public game_entity::entity {
|
||||
public:
|
||||
/**
|
||||
* @brief 析构脚本实例。
|
||||
*/
|
||||
virtual ~entity_script() = default;
|
||||
/**
|
||||
* @brief 脚本启动回调。
|
||||
*/
|
||||
virtual void begin_play() {}
|
||||
/**
|
||||
* @brief 脚本逐帧更新回调。
|
||||
* @param dt 帧时间步长。
|
||||
*/
|
||||
virtual void update(float) {}
|
||||
|
||||
protected:
|
||||
constexpr explicit entity_script(game_entity::entity entity)
|
||||
: game_entity::entity{ entity.get_id() }{}
|
||||
|
||||
void set_rotation(math::v4 rotation_quaternion) const {set_rotation(this, rotation_quaternion);}
|
||||
void set_orientation(math::v3 orientation_vector) const {set_orientation(this, orientation_vector);}
|
||||
void set_position(math::v3 position) const {set_position(this, position);}
|
||||
void set_scale(math::v3 scale) const { set_scale(this, scale); }
|
||||
|
||||
static void set_rotation(const game_entity::entity *const entity, math::v4 rotation_quaternion);
|
||||
static void set_orientation(const game_entity::entity *const entity, math::v3 orientation_vector);
|
||||
static void set_position(const game_entity::entity *const entity, math::v3 position);
|
||||
static void set_scale(const game_entity::entity *const entity, math::v3 scale);
|
||||
|
||||
};
|
||||
|
||||
|
||||
namespace detail {
|
||||
|
||||
/**
|
||||
* @brief 脚本实例智能指针类型。
|
||||
*/
|
||||
using script_ptr = std::unique_ptr<entity_script>;
|
||||
#ifdef USESTDFUNC
|
||||
/**
|
||||
* @brief 基于 std::function 的脚本工厂签名。
|
||||
*/
|
||||
using script_creator = std::function<script_ptr(game_entity::entity entity)>;
|
||||
#else
|
||||
/**
|
||||
* @brief 基于函数指针的脚本工厂签名。
|
||||
*/
|
||||
using script_creator = script_ptr(*)(game_entity::entity entity);
|
||||
#endif // USESTDFUNC
|
||||
/**
|
||||
* @brief 脚本名哈希器类型。
|
||||
*/
|
||||
using string_hash = std::hash<std::string>;
|
||||
|
||||
/**
|
||||
* @brief 注册脚本工厂到脚本注册表。
|
||||
* @param tag 脚本哈希标签。
|
||||
* @param creator 脚本工厂函数。
|
||||
* @return 注册结果码。
|
||||
*/
|
||||
u8 register_script(size_t tag, script_creator creator);
|
||||
#ifdef USE_WITH_EDITOR
|
||||
extern "C" __declspec(dllexport)
|
||||
#endif
|
||||
/**
|
||||
* @brief 按哈希标签查询脚本工厂。
|
||||
* @param tag 脚本名哈希。
|
||||
* @return 对应脚本工厂函数。
|
||||
*/
|
||||
script_creator get_script_creator(size_t tag);
|
||||
|
||||
/**
|
||||
* @brief 默认脚本工厂模板函数。
|
||||
* @tparam script_class 脚本类型。
|
||||
* @param entity 绑定实体。
|
||||
* @return 新创建脚本实例。
|
||||
*/
|
||||
template<class script_class>
|
||||
script_ptr create_script(game_entity::entity entity)
|
||||
{
|
||||
assert(entity.is_valid());
|
||||
return std::make_unique<script_class>(entity);
|
||||
}
|
||||
|
||||
|
||||
#ifdef USE_WITH_EDITOR
|
||||
/**
|
||||
* @brief 向编辑器导出脚本名称。
|
||||
* @param name 脚本类名。
|
||||
* @return 写入结果码。
|
||||
*/
|
||||
u8 add_script_name(const char* name);
|
||||
|
||||
#define REGISTER_SCRIPT(TYPE) \
|
||||
namespace { \
|
||||
u8 _reg_##TYPE{ \
|
||||
XEngine::script::detail::register_script( \
|
||||
XEngine::script::detail::string_hash()(#TYPE), \
|
||||
&XEngine::script::detail::create_script<TYPE>) }; \
|
||||
const u8 _name_##TYPE \
|
||||
{ XEngine::script::detail::add_script_name(#TYPE) }; \
|
||||
}
|
||||
|
||||
|
||||
#else
|
||||
#define REGISTER_SCRIPT(TYPE) \
|
||||
class TYPE; \
|
||||
namespace { \
|
||||
u8 _reg_##TYPE{ \
|
||||
XEngine::script::detail::register_script( \
|
||||
XEngine::script::detail::string_hash()(#TYPE), \
|
||||
&XEngine::script::detail::create_script<TYPE>) }; \
|
||||
}
|
||||
|
||||
#endif // USE_WITH_EDITOR
|
||||
|
||||
|
||||
}//namespace detail
|
||||
}//namespace script
|
||||
|
||||
}
|
||||
97
Engine/EngineAPI/Light.h
Normal file
97
Engine/EngineAPI/Light.h
Normal file
@@ -0,0 +1,97 @@
|
||||
/**
|
||||
* @file Light.h
|
||||
* @brief 光源句柄与光照参数访问接口定义。
|
||||
* @details
|
||||
* light 封装渲染层光源实例的轻量句柄语义,支持:
|
||||
* - 启停、强度、颜色等常用光照属性读写;
|
||||
* - 查询光源类型与绑定实体;
|
||||
* - 通过 light_set_key 区分不同光照集合。
|
||||
*/
|
||||
#pragma once
|
||||
#include "CommonHeader.h"
|
||||
|
||||
namespace XEngine::graphics {
|
||||
|
||||
|
||||
/**
|
||||
* @brief 光源强类型标识符。
|
||||
*/
|
||||
DEFINE_TYPED_ID(light_id);
|
||||
|
||||
/**
|
||||
* @brief 光源句柄对象。
|
||||
*/
|
||||
class light
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* @brief 光源类型枚举。
|
||||
*/
|
||||
enum type :u32
|
||||
{
|
||||
directioinal,
|
||||
point,
|
||||
spot,
|
||||
|
||||
count
|
||||
};
|
||||
|
||||
constexpr explicit light(light_id id, u64 light_set_key)
|
||||
:_light_set_key{ light_set_key }, _id { id } {}
|
||||
constexpr light() = default;
|
||||
constexpr light_id get_id() const { return _id; }
|
||||
constexpr u64 get_light_set_key() const { return _light_set_key; }
|
||||
constexpr bool is_valid() const { return id::is_valid(_id); }
|
||||
|
||||
/**
|
||||
* @brief 设置光源启用状态。
|
||||
* @param is_enabled true 表示启用。
|
||||
*/
|
||||
void is_enabled(bool is_enabled) const;
|
||||
/**
|
||||
* @brief 设置光照强度。
|
||||
* @param intensity 光照强度值。
|
||||
*/
|
||||
void intensity(f32 intensity) const;
|
||||
/**
|
||||
* @brief 设置光照颜色。
|
||||
* @param color RGB 颜色。
|
||||
*/
|
||||
void color(math::v3 color) const;
|
||||
|
||||
/**
|
||||
* @brief 查询光源启用状态。
|
||||
* @return 启用返回 true。
|
||||
*/
|
||||
bool is_enabled( ) const;
|
||||
/**
|
||||
* @brief 查询光照强度。
|
||||
* @return 当前强度值。
|
||||
*/
|
||||
f32 intensity( ) const;
|
||||
/**
|
||||
* @brief 查询光照颜色。
|
||||
* @return 当前 RGB 颜色。
|
||||
*/
|
||||
math::v3 color( ) const;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @brief 查询光源类型。
|
||||
* @return 光源类型枚举值。
|
||||
*/
|
||||
type light_type() const;
|
||||
/**
|
||||
* @brief 查询绑定实体 ID。
|
||||
* @return 实体 ID。
|
||||
*/
|
||||
id::id_type entity_id() const;
|
||||
|
||||
private:
|
||||
u64 _light_set_key{ 0 };
|
||||
light_id _id{ id::invalid_id };
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
51
Engine/EngineAPI/ScriptComponent.h
Normal file
51
Engine/EngineAPI/ScriptComponent.h
Normal file
@@ -0,0 +1,51 @@
|
||||
/**
|
||||
* @file ScriptComponent.h
|
||||
* @brief 脚本组件句柄定义。
|
||||
* @details
|
||||
* 该文件提供脚本组件在 ECS 中的强类型 ID 封装,用于:
|
||||
* - 标识实体绑定的脚本组件实例;
|
||||
* - 与组件系统统一的有效性检查;
|
||||
* - 在不暴露内部存储结构的前提下传递脚本组件引用。
|
||||
*/
|
||||
#pragma once
|
||||
#include "..\Components\ComponentsCommon.h"
|
||||
|
||||
|
||||
namespace XEngine::script {
|
||||
/**
|
||||
* @brief 脚本组件强类型标识符。
|
||||
*/
|
||||
DEFINE_TYPED_ID(script_id);
|
||||
|
||||
|
||||
/**
|
||||
* @brief 脚本组件句柄对象。
|
||||
*/
|
||||
class component final
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* @brief 由脚本组件 ID 构造句柄。
|
||||
* @param id 脚本组件 ID。
|
||||
*/
|
||||
constexpr explicit component(script_id id) : _id{ id } {}
|
||||
/**
|
||||
* @brief 构造无效脚本组件句柄。
|
||||
*/
|
||||
constexpr component() : _id{ id::invalid_id } {}
|
||||
/**
|
||||
* @brief 获取脚本组件 ID。
|
||||
* @return 组件 ID。
|
||||
*/
|
||||
constexpr script_id get_id() const { return _id; }
|
||||
/**
|
||||
* @brief 判断句柄是否有效。
|
||||
* @return 有效返回 true。
|
||||
*/
|
||||
constexpr bool is_valid() const { return id::is_valid(_id); }
|
||||
|
||||
private:
|
||||
script_id _id;
|
||||
|
||||
};
|
||||
}
|
||||
53
Engine/EngineAPI/TransformComponent.h
Normal file
53
Engine/EngineAPI/TransformComponent.h
Normal file
@@ -0,0 +1,53 @@
|
||||
/**
|
||||
* @file TransformComponent.h
|
||||
* @brief 变换组件句柄与只读查询接口定义。
|
||||
* @details
|
||||
* component 以轻量句柄方式访问 ECS 中的变换数据,提供:
|
||||
* - 位置(position)、朝向(orientation)、缩放(scale)查询;
|
||||
* - 四元数旋转(rotation)读取;
|
||||
* - 与 id 系统一致的有效性判断语义。
|
||||
*/
|
||||
#pragma once
|
||||
#include "..\Components\ComponentsCommon.h"
|
||||
|
||||
|
||||
namespace XEngine::transform {
|
||||
/**
|
||||
* @brief 变换组件强类型标识符。
|
||||
*/
|
||||
DEFINE_TYPED_ID(transform_id);
|
||||
|
||||
|
||||
/**
|
||||
* @brief 变换组件句柄对象。
|
||||
*/
|
||||
class component final
|
||||
{
|
||||
public:
|
||||
constexpr explicit component(transform_id id) : _id{ id } {}
|
||||
constexpr component() : _id{ id::invalid_id } {}
|
||||
constexpr transform_id get_id() const { return _id; }
|
||||
constexpr bool is_valid() const { return id::is_valid(_id); }
|
||||
|
||||
|
||||
/**
|
||||
* @brief 获取世界空间位置。
|
||||
*/
|
||||
math::v3 position() const;
|
||||
/**
|
||||
* @brief 获取朝向向量。
|
||||
*/
|
||||
math::v3 orientation() const;
|
||||
/**
|
||||
* @brief 获取缩放向量。
|
||||
*/
|
||||
math::v3 scale() const;
|
||||
/**
|
||||
* @brief 获取旋转四元数。
|
||||
*/
|
||||
math::v4 rotation() const;
|
||||
private:
|
||||
transform_id _id;
|
||||
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user