add custom Ref and RefCounded to replace shared_ptr

This commit is contained in:
2025-12-05 13:24:23 +08:00
parent e730a250b7
commit e1c4cf1c37
41 changed files with 586 additions and 228 deletions

View File

@ -28,14 +28,13 @@ namespace Prism
return result;
}
void Property(const std::string& name, float& value, float min = -1.0f, float max = 1.0f,
PropertyFlag flags = PropertyFlag::None)
void Property(const std::string& name, float& value, const float min = -1.0f, const float max = 1.0f, PropertyFlag flags = PropertyFlag::None)
{
ImGui::Text(name.c_str());
ImGui::NextColumn();
ImGui::PushItemWidth(-1);
std::string id = "##" + name;
const std::string id = "##" + name;
ImGui::SliderFloat(id.c_str(), &value, min, max);
ImGui::PopItemWidth();
@ -43,8 +42,7 @@ namespace Prism
}
void Property(const std::string& name, glm::vec3& value, float min = -1.0f, float max = 1.0f,
PropertyFlag flags = PropertyFlag::None)
void Property(const std::string& name, glm::vec3& value, const float min = -1.0f, const float max = 1.0f, PropertyFlag flags = PropertyFlag::None)
{
ImGui::Text(name.c_str());
ImGui::NextColumn();
@ -66,8 +64,7 @@ namespace Prism
}
void Property(const std::string& name, glm::vec4& value, float min = -1.0f, float max = 1.0f,
PropertyFlag flags = PropertyFlag::None)
void Property(const std::string& name, glm::vec4& value, const float min = -1.0f, const float max = 1.0f, PropertyFlag flags = PropertyFlag::None)
{
ImGui::Text(name.c_str());
ImGui::NextColumn();
@ -83,26 +80,26 @@ namespace Prism
ImGui::NextColumn();
}
void Property(const std::string& name, glm::vec4& value, PropertyFlag flags)
void Property(const std::string& name, glm::vec4& value, const PropertyFlag flags)
{
Property(name, value, -1.0f, 1.0f, flags);
}
void Property(const std::string& name, glm::vec2& value, float min, float max, PropertyFlag flags)
void Property(const std::string& name, glm::vec2& value, const float min, const float max, PropertyFlag flags)
{
ImGui::Text(name.c_str());
ImGui::NextColumn();
ImGui::PushItemWidth(-1);
std::string id = "##" + name;
const std::string id = "##" + name;
ImGui::SliderFloat2(id.c_str(), glm::value_ptr(value), min, max);
ImGui::PopItemWidth();
ImGui::NextColumn();
}
void Property(const std::string& name, glm::vec2& value, PropertyFlag flags)
void Property(const std::string& name, glm::vec2& value, const PropertyFlag flags)
{
Property(name, value, -1.0f, 1.0f, flags);
}
@ -136,31 +133,31 @@ namespace Prism
// Model Scene
{
m_Scene = CreateRef<Scene>("Model Scene");
m_Scene = Ref<Scene>::Create("Model Scene");
m_Scene->SetCamera(Camera(glm::perspectiveFov(glm::radians(45.0f), 1280.0f, 720.0f, 0.1f, 10000.0f)));
m_Scene->SetEnvironment(environment);
m_MeshEntity = m_Scene->CreateEntity("test Entity");
auto mesh = CreateRef<Mesh>("assets/meshes/TestScene.fbx");
auto mesh = Ref<Mesh>::Create("assets/meshes/TestScene.fbx");
m_MeshEntity->SetMesh(mesh);
m_MeshMaterial = mesh->GetMaterial();
auto secondEntity = m_Scene->CreateEntity("Gun Entity");
secondEntity->Transform() = glm::translate(glm::mat4(1.0f), { 5, 5, 5 }) * glm::scale(glm::mat4(1.0f), {10, 10, 10});
mesh = CreateRef<Mesh>("assets/models/m1911/M1911Materials.fbx");
mesh = Ref<Mesh>::Create("assets/models/m1911/M1911Materials.fbx");
secondEntity->SetMesh(mesh);
}
// Sphere Scene
{
m_SphereScene = CreateRef<Scene>("PBR Sphere Scene");
m_SphereScene = Ref<Scene>::Create("PBR Sphere Scene");
m_SphereScene->SetCamera(Camera(glm::perspectiveFov(glm::radians(45.0f), 1280.0f, 720.0f, 0.1f, 10000.0f)));
m_SphereScene->SetEnvironment(environment);
auto sphereMesh = CreateRef<Mesh>("assets/models/Sphere1m.fbx");
auto sphereMesh = Ref<Mesh>::Create("assets/models/Sphere1m.fbx");
m_SphereBaseMaterial = sphereMesh->GetMaterial();
float x = -4.0f;
@ -169,7 +166,7 @@ namespace Prism
{
auto sphereEntity = m_SphereScene->CreateEntity();
Ref<MaterialInstance> mi = CreateRef<MaterialInstance>(m_SphereBaseMaterial);
Ref<MaterialInstance> mi = Ref<MaterialInstance>::Create(m_SphereBaseMaterial);
mi->Set("u_Metalness", 1.0f);
mi->Set("u_Roughness", roughness);
x += 1.1f;
@ -203,7 +200,7 @@ namespace Prism
m_ActiveScene = m_Scene;
m_SceneHierarchyPanel = CreateScope<SceneHierarchyPanel>(m_ActiveScene);
m_PlaneMesh.reset(new Mesh("assets/models/Plane1m.obj"));
m_PlaneMesh = Ref<Mesh>::Create("assets/models/Plane1m.obj");
// Editor
m_CheckerboardTex = Texture2D::Create("assets/editor/Checkerboard.tga");
@ -487,7 +484,7 @@ namespace Prism
std::string filename = Application::Get().OpenFile("");
if (!filename.empty())
{
auto newMesh = CreateRef<Mesh>(filename);
auto newMesh = Ref<Mesh>::Create(filename);
// m_MeshMaterial.reset(new MaterialInstance(newMesh->GetMaterial()));
// m_MeshEntity->SetMaterial(m_MeshMaterial);
m_MeshEntity->SetMesh(newMesh);
@ -757,7 +754,7 @@ namespace Prism
auto [origin, direction] = CastRay(mouseX, mouseY);
m_SelectedSubmeshes.clear();
const auto mesh = m_MeshEntity->GetMesh();
auto mesh = m_MeshEntity->GetMesh();
auto& submeshes = mesh->GetSubmeshes();
float lastT = std::numeric_limits<float>::max();
for (uint32_t i = 0; i < submeshes.size(); i++)
@ -769,7 +766,7 @@ namespace Prism
};
float t;
bool intersects = ray.IntersectsAABB(submesh.BoundingBox, t);
const bool intersects = ray.IntersectsAABB(submesh.BoundingBox, t);
if (intersects)
{
const auto& triangleCache = mesh->GetTriangleCache(i);
@ -814,9 +811,9 @@ namespace Prism
return { (mx / viewportWidth) * 2.0f - 1.0f, ((my / viewportHeight) * 2.0f - 1.0f) * -1.0f };
}
std::pair<glm::vec3, glm::vec3> EditorLayer::CastRay(const float mx, const float my) const
std::pair<glm::vec3, glm::vec3> EditorLayer::CastRay(const float mx, const float my)
{
glm::vec4 mouseClipPos = { mx, my, -1.0f, 1.0f };
const glm::vec4 mouseClipPos = { mx, my, -1.0f, 1.0f };
const auto inverseProj = glm::inverse(m_Scene->GetCamera().GetProjectionMatrix());
const auto inverseView = glm::inverse(glm::mat3(m_Scene->GetCamera().GetViewMatrix()));

View File

@ -30,7 +30,7 @@ namespace Prism
private:
std::pair<float, float> GetMouseViewportSpace() const;
std::pair<glm::vec3, glm::vec3> CastRay(float mx, float my) const;
std::pair<glm::vec3, glm::vec3> CastRay(float mx, float my);
private:
Scope<SceneHierarchyPanel> m_SceneHierarchyPanel;
@ -51,7 +51,7 @@ namespace Prism
// Imguizmo
glm::vec2 m_ViewportBounds[2];
glm::vec2 m_ViewportBounds[2] = {};
int m_GizmoType = -1; // -1 = no gizmo
float m_SnapValue = 0.5f;