Add renderer phase A textured scene path

This commit is contained in:
2026-03-26 20:43:17 +08:00
parent 0921f2a459
commit a78593e7e1
32 changed files with 2455 additions and 9 deletions

View File

@@ -8,6 +8,7 @@ set(COMPONENTS_TEST_SOURCES
test_transform_component.cpp
test_game_object.cpp
test_camera_light_component.cpp
test_mesh_render_components.cpp
)
add_executable(components_tests ${COMPONENTS_TEST_SOURCES})

View File

@@ -6,6 +6,8 @@
#include <XCEngine/Components/ComponentFactoryRegistry.h>
#include <XCEngine/Components/GameObject.h>
#include <XCEngine/Components/LightComponent.h>
#include <XCEngine/Components/MeshFilterComponent.h>
#include <XCEngine/Components/MeshRendererComponent.h>
using namespace XCEngine::Components;
@@ -18,6 +20,8 @@ TEST(ComponentFactoryRegistry_Test, BuiltInTypesAreRegistered) {
EXPECT_TRUE(registry.IsRegistered("Light"));
EXPECT_TRUE(registry.IsRegistered("AudioSource"));
EXPECT_TRUE(registry.IsRegistered("AudioListener"));
EXPECT_TRUE(registry.IsRegistered("MeshFilter"));
EXPECT_TRUE(registry.IsRegistered("MeshRenderer"));
EXPECT_FALSE(registry.IsRegistered("Transform"));
EXPECT_FALSE(registry.IsRegistered("MissingComponent"));
}
@@ -30,6 +34,8 @@ TEST(ComponentFactoryRegistry_Test, CreateBuiltInComponentsByTypeName) {
EXPECT_NE(dynamic_cast<LightComponent*>(registry.CreateComponent(&gameObject, "Light")), nullptr);
EXPECT_NE(dynamic_cast<AudioSourceComponent*>(registry.CreateComponent(&gameObject, "AudioSource")), nullptr);
EXPECT_NE(dynamic_cast<AudioListenerComponent*>(registry.CreateComponent(&gameObject, "AudioListener")), nullptr);
EXPECT_NE(dynamic_cast<MeshFilterComponent*>(registry.CreateComponent(&gameObject, "MeshFilter")), nullptr);
EXPECT_NE(dynamic_cast<MeshRendererComponent*>(registry.CreateComponent(&gameObject, "MeshRenderer")), nullptr);
EXPECT_EQ(registry.CreateComponent(&gameObject, "MissingComponent"), nullptr);
}

View File

@@ -0,0 +1,125 @@
#include <gtest/gtest.h>
#include <XCEngine/Components/GameObject.h>
#include <XCEngine/Components/MeshFilterComponent.h>
#include <XCEngine/Components/MeshRendererComponent.h>
#include <XCEngine/Core/Asset/IResource.h>
#include <XCEngine/Resources/Material/Material.h>
#include <XCEngine/Resources/Mesh/Mesh.h>
#include <sstream>
using namespace XCEngine::Components;
using namespace XCEngine::Resources;
namespace {
Mesh* CreateTestMesh(const char* name, const char* path) {
auto* mesh = new Mesh();
IResource::ConstructParams params = {};
params.name = name;
params.path = path;
params.guid = ResourceGUID::Generate(path);
mesh->Initialize(params);
return mesh;
}
Material* CreateTestMaterial(const char* name, const char* path) {
auto* material = new Material();
IResource::ConstructParams params = {};
params.name = name;
params.path = path;
params.guid = ResourceGUID::Generate(path);
material->Initialize(params);
return material;
}
TEST(MeshFilterComponent_Test, SetMeshCachesResourceAndPath) {
GameObject gameObject("MeshHolder");
auto* component = gameObject.AddComponent<MeshFilterComponent>();
Mesh* mesh = CreateTestMesh("Quad", "Meshes/quad.mesh");
component->SetMesh(mesh);
EXPECT_EQ(component->GetMesh(), mesh);
EXPECT_EQ(component->GetMeshPath(), "Meshes/quad.mesh");
component->ClearMesh();
delete mesh;
}
TEST(MeshFilterComponent_Test, SerializeAndDeserializePreservesPath) {
MeshFilterComponent source;
Mesh* mesh = CreateTestMesh("Quad", "Meshes/serialized.mesh");
source.SetMesh(mesh);
std::stringstream stream;
source.Serialize(stream);
MeshFilterComponent target;
target.Deserialize(stream);
EXPECT_EQ(target.GetMeshPath(), "Meshes/serialized.mesh");
EXPECT_EQ(target.GetMesh(), nullptr);
source.ClearMesh();
delete mesh;
}
TEST(MeshRendererComponent_Test, SetMaterialsKeepsSlotsAndFlags) {
GameObject gameObject("RendererHolder");
auto* component = gameObject.AddComponent<MeshRendererComponent>();
Material* material0 = CreateTestMaterial("M0", "Materials/m0.mat");
Material* material1 = CreateTestMaterial("M1", "Materials/m1.mat");
component->SetMaterial(0, material0);
component->SetMaterial(1, material1);
component->SetCastShadows(false);
component->SetReceiveShadows(false);
component->SetRenderLayer(7);
ASSERT_EQ(component->GetMaterialCount(), 2u);
EXPECT_EQ(component->GetMaterial(0), material0);
EXPECT_EQ(component->GetMaterial(1), material1);
EXPECT_EQ(component->GetMaterialPaths()[0], "Materials/m0.mat");
EXPECT_EQ(component->GetMaterialPaths()[1], "Materials/m1.mat");
EXPECT_FALSE(component->GetCastShadows());
EXPECT_FALSE(component->GetReceiveShadows());
EXPECT_EQ(component->GetRenderLayer(), 7u);
component->ClearMaterials();
delete material0;
delete material1;
}
TEST(MeshRendererComponent_Test, SerializeAndDeserializePreservesMaterialPathsAndSettings) {
MeshRendererComponent source;
Material* material0 = CreateTestMaterial("M0", "Materials/serialized0.mat");
Material* material1 = CreateTestMaterial("M1", "Materials/serialized1.mat");
source.SetMaterial(0, material0);
source.SetMaterial(1, material1);
source.SetCastShadows(false);
source.SetReceiveShadows(true);
source.SetRenderLayer(3);
std::stringstream stream;
source.Serialize(stream);
MeshRendererComponent target;
target.Deserialize(stream);
ASSERT_EQ(target.GetMaterialCount(), 2u);
EXPECT_EQ(target.GetMaterial(0), nullptr);
EXPECT_EQ(target.GetMaterial(1), nullptr);
EXPECT_EQ(target.GetMaterialPaths()[0], "Materials/serialized0.mat");
EXPECT_EQ(target.GetMaterialPaths()[1], "Materials/serialized1.mat");
EXPECT_FALSE(target.GetCastShadows());
EXPECT_TRUE(target.GetReceiveShadows());
EXPECT_EQ(target.GetRenderLayer(), 3u);
source.ClearMaterials();
delete material0;
delete material1;
}
} // namespace