update asset

This commit is contained in:
ouczbs 2024-03-16 17:40:52 +08:00
parent 3ce73c2f99
commit 3e8a27fbcf
35 changed files with 517 additions and 22 deletions

View File

@ -16,6 +16,9 @@ namespace zlog {
void log(level_enum level, format_with_location& fmt, Args &&...args) {
m_logger->log(fmt.loc, level, fmt::runtime(fmt.value), std::forward<Args>(args)...);
}
void flush() {
m_logger->flush();
}
};
extern zloger zlog;
template <typename... Args>
@ -40,4 +43,5 @@ namespace zlog {
const std::string format_str = fmt::format(std::forward<Args>(args)...);
throw std::runtime_error(format_str);
};
void flush();
};

View File

@ -33,4 +33,7 @@ namespace zlog {
m_logger->flush();
spdlog::drop_all();
}
void flush(){
zlog.flush();
}
}

View File

@ -1,5 +1,9 @@
#include "asset/asset.h"
#include "asset_manager.h"
namespace engineapi
{
Asset::~Asset()
{
}
}

View File

@ -10,6 +10,7 @@ namespace engineapi
using std::map;
class Asset {
public:
friend class AssetManager;
enum :uint32_t {
ASSET_NONE_FLAG = 0,
ASSET_SHARED_FLAG = 1 << 0,
@ -24,6 +25,7 @@ namespace engineapi
string mName;
public:
Asset(string name, uint32_t flags):mName(name),mFlags(flags) {};
~Asset();
virtual void BeginLoad() {
mFlags |= ASSET_LOADING_FLAG;
};

View File

@ -1,4 +1,17 @@
#include "asset/asset_manager.h"
namespace engineapi {
void AssetManager::ClearAsset(Asset* asset)
{
auto it = mAssetMap.find(asset->mName);
if (!asset->IsShared()) {
delete asset;
}
if (it != mAssetMap.end()) {
it->second.count--;
if (it->second.count < 1) {
delete it->second.asset;
mAssetMap.erase(it);
}
}
}
}

View File

@ -13,7 +13,7 @@ namespace engineapi
struct AssetWrap {
Asset* asset;
uint32_t count;
AssetWrap(Asset* asset): asset(asset),count(0) {
AssetWrap(Asset* asset): asset(asset),count(1) {
}
};
@ -21,11 +21,14 @@ namespace engineapi
private:
map<const string, AssetWrap> mAssetMap;
public:
void ClearAsset(Asset* asset);
template<typename TAsset>
TAsset* LoadAsset(const string& name, uint32_t flags)
{
auto it = mAssetMap.find(name);
if (it != mAssetMap.end()) {
bool isFind = it != mAssetMap.end();
if (isFind) {
it->second.count++;
if (it->second.asset->IsShared()) {
return (TAsset*)(it->second.asset);
}
@ -36,7 +39,7 @@ namespace engineapi
}
}
TAsset* asset = new TAsset(name, flags);
if (asset->IsShared() || asset->IsCopyed()) {
if (!isFind && (asset->IsShared() || asset->IsCopyed())) {
mAssetMap.emplace(name, asset);
}
asset->BeginLoad();

View File

@ -1,5 +1,5 @@
#pragma once
#include <cstdint>
namespace engineapi
{
typedef uint32_t FrameBufferClearFlags;

View File

@ -1,5 +1,6 @@
#pragma once
#include <string>
using std::string;
namespace engineapi
{
class Matrix4

View File

@ -1,5 +1,7 @@
#include "camera.h"
#include "math/math.h"
#include "render/window.h"
#include "object/component/transform.h"
namespace engineapi {
Camera::Camera()
{
@ -7,4 +9,21 @@ namespace engineapi {
Camera::~Camera()
{
}
Matrix4 Camera::GetViewMatrix()
{
// Model矩阵是把顶点从模型空间转到世界空间而相机的View矩阵其实就是把场景中所有顶点从世界空间转到相机自己的模型空间
// 所以这里直接返回Model矩阵的逆矩阵即可
// 这里用glm::lookAt或者Math::GetLookToMatrix也是一样的
Matrix4 model = GetTransform()->GetModelMatrix();
return Math::Inverse(model);
}
Matrix4 Camera::GetProjectionMatrix()
{
auto window = Window::GetSingletonPtr();
uint32_t width, height;
window->GetSize(width, height);
if (mAspect == 0.0f)
return Math::Perspective(Math::Deg2Rad(mFov), static_cast<float>(width) / static_cast<float>(height), mNearClipDis, mFarClipDis);
return Math::Perspective(Math::Deg2Rad(mFov), mAspect, mNearClipDis, mFarClipDis);
}
}

View File

@ -1,9 +1,19 @@
#pragma once
#include "../component.h"
#include "math/matrix4.h"
namespace engineapi {
class Camera {
class Camera : public Component{
protected:
float mFov;
float mAspect = 0.0f;
float mNearClipDis = 0.1f;
float mFarClipDis = 200.0f;
bool mEnableAfterEffects = false;
CameraType cameraType;
public:
Camera();
~Camera();
Matrix4 GetViewMatrix();
Matrix4 GetProjectionMatrix();
};
};

View File

@ -1,5 +1,6 @@
#include "component.h"
#include "game_object.h"
#include "component/transform.h"
namespace engineapi {
Component::Component()
{
@ -7,4 +8,8 @@ namespace engineapi {
Component::~Component()
{
}
Transform* Component::GetTransform()
{
return mOwner->GetComponent<Transform>();
}
}

View File

@ -1,9 +1,16 @@
#pragma once
#include "asset/render/asset_enum.h"
namespace engineapi {
class GameObject;
class Transform;
class Component {
friend class GameObject;
protected:
GameObject* mOwner;
public:
Component();
~Component();
public:
Transform* GetTransform();
};
};

View File

@ -0,0 +1,218 @@
#include "transform.h"
#include "math/math.h"
#include "../game_object.h"
namespace engineapi {
Transform::Transform()
{
}
Transform::~Transform()
{
}
Matrix4 Transform::GetLocalPositionMatrix() const
{
return Math::TranslationMatrix(mPosition);
}
Matrix4 Transform::GetLocalRotationMatrix() const
{
return mRotation.ToMatrix();
}
Matrix4 Transform::GetLocalScaleMatrix() const
{
return Math::ScaleMatrix(mScale);
}
Matrix4 Transform::GetModelMatrix() const
{
// 把顶点从Local Space变换到World Space的矩阵
return GetPositionMatrix() * GetRotationAndScaleMatrix();
}
Matrix4 Transform::GetPositionMatrix() const
{
Vector3 position = GetPosition();
return Matrix4(
1, 0, 0, position.x,
0, 1, 0, position.y,
0, 0, 1, position.z,
0, 0, 0, 1);
}
Matrix4 Transform::GetRotationMatrix() const
{
Quaternion rotation = GetRotation();
return rotation.ToMatrix();
}
Matrix4 Transform::GetScaleMatrix() const
{
// 在Transform嵌套起来并且有旋转的情况下scale只能倒推出来很难正向计算
auto invRotationMat = Math::Inverse(GetRotation().ToMatrix());
auto rotationAndScaleMat = GetRotationAndScaleMatrix();
return invRotationMat * rotationAndScaleMat;
}
Matrix4 Transform::GetRotationAndScaleMatrix() const
{
auto mScaleMat = GetLocalScaleMatrix();
auto mRotationMat = GetLocalRotationMatrix();
auto parent = mOwner->GetParent();
if (parent == nullptr)
{
return mRotationMat * mScaleMat;
}
return parent->GetComponent<Transform>()->GetRotationAndScaleMatrix() * mRotationMat * mScaleMat;
}
Vector3 Transform::GetLocalScale() const
{
return mScale;
}
Vector3 Transform::GetLocalPosition() const
{
return mPosition;
}
Vector3 Transform::GetLocalEulerAngles() const
{
return mRotation.GetEulerAngles();
}
Quaternion Transform::GetLocalRotation() const
{
return mRotation;
}
void Transform::SetLocalScale(const Vector3& scale)
{
mScale = scale;
}
void Transform::SetLocalScale(float x, float y, float z)
{
SetLocalScale(Vector3(x, y, z));
}
void Transform::SetLocalPosition(const Vector3& position)
{
mPosition = position;
}
void Transform::SetLocalPosition(float x, float y, float z)
{
SetLocalPosition(Vector3(x, y, z));
}
void Transform::SetLocalEulerAngles(const Vector3& eulerAngles)
{
mRotation.SetEulerAngles(eulerAngles);
}
void Transform::SetLocalEulerAngles(float x, float y, float z)
{
SetLocalEulerAngles(Vector3(x, y, z));
}
void Transform::SetLocalRotation(const Quaternion& rotation)
{
mRotation = rotation;
}
Matrix3 Transform::GetScale() const
{
// 当Transform嵌套起来并且有旋转时scale无法再用简单的Vector3表达
// 因为父级缩放时子对象如果有旋转则对于子对象来说缩放不再是标准的xyz轴缩放了
// 相应的缩放也无法再用Vector3表达只能用Matrix3表达
return Matrix3(GetScaleMatrix());
}
Vector3 Transform::GetPosition() const
{
auto parent = mOwner->GetParent();
if (parent == nullptr)
{
return mPosition;
}
auto transform = parent->GetComponent<Transform>();
Vector3 offset = transform->GetRotationAndScaleMatrix() * mPosition.ToPosVec4();
return transform->GetPosition() + offset;
}
Vector3 Transform::GetEulerAngles() const
{
return GetRotation().GetEulerAngles();
}
Quaternion Transform::GetRotation() const
{
auto parent = mOwner->GetParent();
if (parent == nullptr) {
return mRotation;
}
return mRotation * parent->GetComponent<Transform>()->GetRotation();
}
void Transform::SetPosition(const Vector3& position)
{
auto parent = mOwner->GetParent();
if (parent == nullptr)
{
mPosition = position;
}
else
{
auto wPosition = GetPosition();
auto transform = parent->GetComponent<Transform>();
Vector3 offset = transform->GetRotationAndScaleMatrix() * mPosition;
mPosition = Math::Inverse(transform->GetRotationAndScaleMatrix()) * (offset + position - wPosition);
}
}
void Transform::SetPosition(float x, float y, float z)
{
SetPosition(Vector3(x, y, z));
}
void Transform::SetEulerAngles(const Vector3& eulerAngles)
{
SetRotation(Quaternion::Euler(eulerAngles));
}
void Transform::SetEulerAngles(float x, float y, float z)
{
SetEulerAngles(Vector3(x, y, z));
}
void Transform::SetRotation(const Quaternion& rotation)
{
auto parent = mOwner->GetParent();
if (parent == nullptr)
mRotation = rotation;
else
mRotation = parent->GetComponent<Transform>()->GetRotation().GetInverse() * rotation;
}
Vector3 Transform::GetUp() const
{
Quaternion rotation = GetRotation();
Vector4 up = rotation.ToMatrix() * Vector4(0, 1, 0, 0);
return Vector3(up.x, up.y, up.z);
}
Vector3 Transform::GetRight() const
{
Quaternion rotation = GetRotation();
Vector4 right = rotation.ToMatrix() * Vector4(1, 0, 0, 0);
return Vector3(right.x, right.y, right.z);
}
Vector3 Transform::GetForward() const
{
Quaternion rotation = GetRotation();
Vector4 forward = rotation.ToMatrix() * Vector4(0, 0, 1, 0);
return Vector3(forward.x, forward.y, forward.z);
}
}

View File

@ -0,0 +1,61 @@
#pragma once
#include "../component.h"
#include "math/matrix4.h"
#include "math/vector3.h"
#include "math/quaternion.h"
namespace engineapi {
class Transform : public Component{
protected:
Vector3 mPosition = Vector3();
Quaternion mRotation = Quaternion();
Vector3 mScale = Vector3(1.0f);
public:
Transform();
~Transform();
// Local Space
Matrix4 GetLocalPositionMatrix() const;
Matrix4 GetLocalRotationMatrix() const;
Matrix4 GetLocalScaleMatrix() const;
// World Space
Matrix4 GetModelMatrix() const;
Matrix4 GetPositionMatrix() const;
Matrix4 GetRotationMatrix() const;
Matrix4 GetScaleMatrix() const;
Matrix4 GetRotationAndScaleMatrix() const;
// Local Space
Vector3 GetLocalScale() const;
Vector3 GetLocalPosition() const;
Vector3 GetLocalEulerAngles() const;
Quaternion GetLocalRotation() const;
void SetLocalScale(const Vector3& scale);
void SetLocalScale(float x, float y, float z);
void SetLocalPosition(const Vector3& position);
void SetLocalPosition(float x, float y, float z);
void SetLocalEulerAngles(const Vector3& eulerAngles);
void SetLocalEulerAngles(float x, float y, float z);
void SetLocalRotation(const Quaternion& rotation);
// World Space
Matrix3 GetScale() const;
Vector3 GetPosition() const;
Vector3 GetEulerAngles() const;
Quaternion GetRotation() const;
void SetPosition(const Vector3& position);
void SetPosition(float x, float y, float z);
void SetEulerAngles(const Vector3& eulerAngles);
void SetEulerAngles(float x, float y, float z);
void SetRotation(const Quaternion& rotation);
// World Space
Vector3 GetUp() const;
Vector3 GetRight() const;
Vector3 GetForward() const;
public:
static ComponentType GetType() {
return ComponentType::Transform;
};
};
};

View File

@ -7,4 +7,9 @@ namespace engineapi {
GameObject::~GameObject()
{
}
void GameObject::AddComponent(ComponentType type, Component* component)
{
component->mOwner = this;
mComponents.insert(std::pair<ComponentType, Component*>(type, component));
}
}

View File

@ -1,10 +1,36 @@
#pragma once
#include <map>
#include "component.h"
#include "asset/asset.h"
namespace engineapi {
class GameObject {
protected:
string mName;
uint32_t mLayer = 0;
GameObject* mParent = nullptr;
vector<GameObject*> mChildren;
std::multimap<ComponentType, Component*> mComponents = {};
public:
GameObject();
~GameObject();
void AddComponent(ComponentType type, Component* component);
GameObject* GetParent() {
return mParent;
}
public:
template<class T>
T* GetComponent();
};
template<class T>
inline T* GameObject::GetComponent()
{
ComponentType type = T::GetType();
auto iter = mComponents.find(type);
if (iter != mComponents.end()) {
return static_cast<T*> (iter->second);
}
else {
return static_cast<T*> (nullptr);
}
}
};

View File

@ -1,7 +1,10 @@
#include "scene.h"
#include "object/camera/camera.h"
#include "render/renderapi.h"
namespace engineapi {
Scene::Scene()
{
mCamera = new Camera();
}
Scene::~Scene()
{
@ -11,6 +14,7 @@ namespace engineapi {
}
void Scene::Render()
{
RenderAPI::GetSingletonPtr()->Render(*mCamera);
}
void Scene::AddGameObject(GameObject* gameObject)
{

View File

@ -1,8 +1,10 @@
#pragma once
#include "../game_object.h"
namespace engineapi {
class Camera;
class Scene{
protected:
Camera* mCamera;
public:
Scene();
~Scene();

View File

@ -0,0 +1,15 @@
#include "material.h"
namespace engineapi {
Material::Material(string name, uint32_t flags)
:Asset(name, flags)
{
}
Material::~Material()
{
}
void Material::Use()
{
}
}

View File

@ -3,6 +3,14 @@
namespace engineapi {
class Material : public Asset {
protected:
public:
Material(string name, uint32_t flags);
~Material();
void Use();
};
};

View File

@ -0,0 +1,15 @@
#include "shader.h"
namespace engineapi {
Shader::Shader(string name, uint32_t flags)
:Asset(name, flags)
{
}
Shader::~Shader()
{
}
void Shader::BeginLoad()
{
}
}

View File

@ -0,0 +1,13 @@
#pragma once
#include "asset_render.h"
namespace engineapi {
class Shader : public Asset {
protected:
public:
Shader(string name, uint32_t flags);
~Shader();
void BeginLoad()override;
};
};

View File

@ -19,7 +19,7 @@ namespace engineapi {
RenderPass() {};
~RenderPass() {};
virtual void Render(Camera* camera) = 0;
virtual void Render(Camera& camera) = 0;
};
class RenderStateSetting
{

View File

@ -1,10 +1,13 @@
#include "renderpass_forward.h"
#include "../renderapi.h"
#include "../window.h"
#include "object/camera/camera.h"
namespace engineapi {
RenderPassForwardRendering::RenderPassForwardRendering()
{
skyBox = nullptr;
skyBoxMaterial = nullptr;
//skyBoxMaterial = new Material(new Shader(Resources::GetAssetFullPath("Shaders/SkyBox.zxshader", true), FrameBufferType::Normal));
skyBoxRenderState = new RenderStateSetting();
skyBoxRenderState->depthTest = false;
@ -20,11 +23,30 @@ namespace engineapi {
RenderPassForwardRendering::~RenderPassForwardRendering()
{
}
void RenderPassForwardRendering::Render(Camera* camera)
void RenderPassForwardRendering::Render(Camera& camera)
{
auto renderAPI = RenderAPI::GetSingletonPtr();
auto window = Window::GetSingletonPtr();
uint32_t width, height;
window->GetSize(width, height);
// 切换到主FBO
//FBOManager::GetInstance()->SwitchFBO("Forward");
// ViewPort设置为窗口大小
renderAPI->SetViewPort(width, height);
}
void RenderPassForwardRendering::RenderSkyBox(Camera* camera)
void RenderPassForwardRendering::RenderSkyBox(Camera& camera)
{
// 先转3x3再回4x4把相机位移信息去除
Matrix4 mat_V = Matrix4(Matrix3(camera.GetViewMatrix()));
Matrix4 mat_P = camera.GetProjectionMatrix();
//skyBoxMaterial->Use();
//skyBoxMaterial->SetMatrix("ENGINE_View", mat_V);
//skyBoxMaterial->SetMatrix("ENGINE_Projection", mat_P);
//skyBoxMaterial->SetCubeMap("_Skybox", SceneManager::GetInstance()->GetCurScene()->skyBox->GetID(), 0);
//RenderAPI::GetInstance()->Draw(skyBox->VAO);
}
void RenderPassForwardRendering::RenderBatches(const map<uint32_t, vector<MeshRenderer*>>& batchs)
{

View File

@ -1,14 +1,15 @@
#pragma once
#include "renderpass.h"
#include "object/component/mesh_render.h"
#include "object/render/mesh_render.h"
namespace engineapi {
class Camera;
class RenderPassForwardRendering : public RenderPass
{
public:
RenderPassForwardRendering();
~RenderPassForwardRendering();
virtual void Render(Camera* camera);
virtual void Render(Camera& camera);
private:
uint32_t drawCommandID = 0;
@ -18,7 +19,7 @@ namespace engineapi {
RenderStateSetting* opaqueRenderState;
RenderStateSetting* transparentRenderState;
void RenderSkyBox(Camera* camera);
void RenderSkyBox(Camera& camera);
void RenderBatches(const map<uint32_t, vector<MeshRenderer*>>& batchs);
};
}

View File

@ -25,6 +25,12 @@ namespace engineapi {
//mCurPasses.push_back(mAllPasses[ZX_RENDER_PASS_AFTER_EFFECT_RENDERING]);
//mCurPasses.push_back(mAllPasses[ZX_RENDER_PASS_UI_RENDERING]);
}
void RenderAPI::Render(Camera& camera)
{
for (auto pass : mCurPasses) {
pass->Render(camera);
}
}
RenderAPI* RenderAPI::MakeInstance()
{
#ifdef VULKAN_API

View File

@ -9,14 +9,19 @@ namespace engineapi
class RenderAPI : public Singleton<RenderAPI>
{
protected:
ViewPortInfo mViewPortInfo;
vector<RenderPass*> mCurPasses;
vector<RenderPass*> mAllPasses;
public:
RenderAPI();
virtual ~RenderAPI() {};
virtual void SetUpRenderPasses();
public:
virtual void SetViewPort(uint32_t width, uint32_t height, uint32_t xOffset = 0, uint32_t yOffset = 0) = 0;
virtual void BeginFrame() = 0;
virtual void Render(Camera& camera) = 0;
virtual void Render(Camera& camera);
virtual void EndFrame() = 0;
virtual void SetStaticMesh(Mesh* mesh) = 0;
public:

View File

@ -34,6 +34,11 @@ namespace engineapi {
HWND Ptr() {
return mPtr;
}
void GetSize(uint32_t& width, uint32_t& height) {
width = mWidth;
height = mHeight;
}
public:
static bool ProcessMessages(int& code);
static Window* MakeInstance(int frames, uint32_t width, uint32_t height, const char* title);
};

View File

@ -33,6 +33,10 @@ namespace vulkanapi {
if (mDevice) {
delete mDevice;
}
for (auto it : mWorkerMap) {
delete it.second;
}
mWorkerMap.clear();
}
void Backend::InitWorker(const string& name, VkCommandPoolCreateFlags flag)
{

View File

@ -1,4 +1,5 @@
#include "thread_worker.h"
#include "zlog.h"
namespace vulkanapi {
CommandThreadWorker::CommandThreadWorker(const string name, int buffer)
: mName(name)
@ -7,10 +8,14 @@ namespace vulkanapi {
{
mThread = std::thread(&CommandThreadWorker::workloop, this);
}
CommandThreadWorker::~CommandThreadWorker() {
zlog::info("~CommandThreadWorker");
zlog::flush();
}
void CommandThreadWorker::workloop()
{
mThread.detach();
while(true){
while (true) {
voidFn fn = mChannel.acquire();
fn();
}

View File

@ -13,6 +13,7 @@ namespace vulkanapi {
void workloop();
public:
CommandThreadWorker(const string name, int buffer);
~CommandThreadWorker();
void Invoke(voidFn fn);
void SyncInvoke(voidFn fn);

View File

@ -11,11 +11,18 @@ namespace engineapi {
{
Buffer::MakeVmaAllocator(backend);
}
void RenderVulkanAPI::BeginFrame()
RenderVulkanAPI::~RenderVulkanAPI()
{
zlog::info("~RenderVulkanAPI");
}
void RenderVulkanAPI::Render(Camera& camera)
void RenderVulkanAPI::SetViewPort(uint32_t width, uint32_t height, uint32_t xOffset, uint32_t yOffset)
{
mViewPortInfo.width = width;
mViewPortInfo.height = height;
mViewPortInfo.xOffset = xOffset;
mViewPortInfo.yOffset = yOffset;
}
void RenderVulkanAPI::BeginFrame()
{
}

View File

@ -12,9 +12,10 @@ namespace engineapi
vector<VulkanVAO*> VAOList;
public:
RenderVulkanAPI();
virtual ~RenderVulkanAPI()override;
virtual void SetViewPort(uint32_t width, uint32_t height, uint32_t xOffset = 0, uint32_t yOffset = 0)override;
virtual void BeginFrame()override;
virtual void Render(Camera& camera)override;
virtual void EndFrame()override;
virtual void SetStaticMesh(Mesh* mesh)override;
public: