feat: Add Camera component / Add cooked model and texture loading

This commit is contained in:
2026-10-13 00:29:17 +02:00
parent fa98bf08e8
commit ce55f75ac3
26 changed files with 930 additions and 194 deletions
@@ -28,6 +28,10 @@ public:
private:
AssetManager() = default;
[[nodiscard]] ModelDoc::ModelAsset LoadCookedOrImport(
const std::string& reference,
const ModelDoc::LoadOptions& options) const;
};
#endif // DESTRUM_ASSETMANAGER_H
@@ -3,6 +3,7 @@
#include <cstdint>
#include <filesystem>
#include <optional>
#include <span>
#include <string_view>
#include <vector>
@@ -13,11 +14,18 @@ namespace ModelFile {
inline constexpr std::uint32_t Version = 1;
inline constexpr std::string_view Extension = ".dmodel";
inline constexpr ModelDoc::LoadOptions CookOptions{};
[[nodiscard]] bool SupportsLoadOptions(const ModelDoc::ModelAsset& model, const ModelDoc::LoadOptions& options);
void ApplyLoadOptions(ModelDoc::ModelAsset& model, const ModelDoc::LoadOptions& options);
[[nodiscard]] std::vector<std::uint8_t> Write(const ModelDoc::ModelAsset& model);
[[nodiscard]] ModelDoc::ModelAsset Read(std::span<const std::uint8_t> bytes);
void WriteToFile(const ModelDoc::ModelAsset& model, const std::filesystem::path& path);
[[nodiscard]] ModelDoc::ModelAsset ReadFromFile(const std::filesystem::path& path);
[[nodiscard]] std::optional<std::uint32_t> ReadVersionFromFile(const std::filesystem::path& path);
}
#endif // DESTRUM_MODELFILE_H
@@ -0,0 +1,38 @@
#ifndef DESTRUM_TEXTUREFILE_H
#define DESTRUM_TEXTUREFILE_H
#include <cstdint>
#include <filesystem>
#include <optional>
#include <span>
#include <string_view>
#include <vector>
namespace TextureFile {
inline constexpr std::uint32_t Version = 1;
inline constexpr std::string_view Extension = ".dtex";
enum class PixelFormat : std::uint32_t {
RGBA16Float,
};
struct Texture {
std::uint32_t width{0};
std::uint32_t height{0};
PixelFormat format{PixelFormat::RGBA16Float};
std::vector<std::uint8_t> pixels;
};
[[nodiscard]] std::size_t BytesPerPixel(PixelFormat format);
[[nodiscard]] Texture FromRGBA32Float(std::uint32_t width, std::uint32_t height, std::span<const float> rgba);
[[nodiscard]] std::vector<std::uint8_t> Write(const Texture& texture);
[[nodiscard]] Texture Read(std::span<const std::uint8_t> bytes);
void WriteToFile(const Texture& texture, const std::filesystem::path& path);
[[nodiscard]] Texture ReadFromFile(const std::filesystem::path& path);
[[nodiscard]] std::optional<std::uint32_t> ReadVersionFromFile(const std::filesystem::path& path);
}
#endif // DESTRUM_TEXTUREFILE_H
@@ -0,0 +1,44 @@
#ifndef CAMERACOMPONENT_H
#define CAMERACOMPONENT_H
#include <destrum/ObjectModel/Component.h>
#include <destrum/Graphics/Camera.h>
#include <glm/glm.hpp>
class CameraComponent final : public Component {
public:
explicit CameraComponent(GameObject& parent);
CameraComponent(const CameraComponent& other) = delete;
CameraComponent(CameraComponent&& other) noexcept = delete;
CameraComponent& operator=(const CameraComponent& other) = delete;
CameraComponent& operator=(CameraComponent&& other) noexcept = delete;
std::string GetTypeName() const override { return "CameraComponent"; }
nlohmann::json Serialize() const override;
void Deserialize(const nlohmann::json& data) override;
void ImGuiInspector() override;
// Places the camera at this object's world transform, looking down its local -Z.
[[nodiscard]] Camera& GetCamera(float aspectRatio);
void SetFieldOfView(float fovDegrees);
[[nodiscard]] float GetFieldOfView() const { return m_FieldOfView; }
void SetClipPlanes(float zNear, float zFar);
[[nodiscard]] float GetZNear() const { return m_ZNear; }
[[nodiscard]] float GetZFar() const { return m_ZFar; }
void MakeCurrent();
[[nodiscard]] bool IsCurrent() const;
private:
Camera m_Camera{glm::vec3{0.0f}, glm::vec3{0.0f, 1.0f, 0.0f}};
float m_FieldOfView{90.0f};
float m_ZNear{0.01f};
float m_ZFar{1000.0f};
};
#endif // CAMERACOMPONENT_H
@@ -101,6 +101,7 @@ public:
float GetZFar() const { return m_zFar; }
void SetZFar(float zFar) { m_zFar = zFar; }
float GetFOVY() const { return fovAngle; }
void SetFOVY(float fovDegrees) { fovAngle = fovDegrees; }
float GetAspectRatio() const { return m_aspectRatio; }
glm::vec3 m_position{2.f, 0, 0};
@@ -127,6 +128,7 @@ public:
void SetRotation(float yawRadians, float pitchRadians);
void SetRotation(const glm::vec2& yawPitchRadians);
void SetOrientation(const glm::vec3& forward, const glm::vec3& up);
float GetYaw() const { return m_yaw; }
float GetPitch() const { return m_pitch; }
@@ -3,6 +3,7 @@
#include <filesystem>
#include <cstddef>
#include <vector>
#include <vulkan/vulkan.h>
#include "destrum/Graphics/TextureIntent.h"
@@ -19,11 +20,12 @@ struct ImageData {
if (this == &o) return *this;
// free current contents first
this->~ImageData();
freePixels();
// steal
pixels = o.pixels;
hdrPixels = o.hdrPixels;
cookedPixels = std::move(o.cookedPixels);
hdr = o.hdr;
width = o.width;
height = o.height;
@@ -50,8 +52,11 @@ struct ImageData {
ImageData(const ImageData&) = delete;
ImageData& operator=(const ImageData&) = delete;
void freePixels();
unsigned char* pixels{nullptr};
float* hdrPixels{nullptr};
std::vector<unsigned char> cookedPixels; // owns `pixels` for cooked textures
bool hdr{false};
int width{0};
+7
View File
@@ -12,6 +12,7 @@
#include "destrum/Physics/JoltPhysicsWorld.h"
#include "destrum/Physics/PhysicsSceneBridge.h"
class CameraComponent;
class GameObject;
class SceneSerializer;
class PhysicsWorld;
@@ -105,6 +106,10 @@ public:
Event<> OnSceneLoaded;
PhysicsSceneBridge& GetPhysics() { return m_Physics; }
[[nodiscard]] CameraComponent* GetCurrentCamera() const;
[[nodiscard]] std::vector<CameraComponent*> GetCameras() const;
void SetCurrentCamera(const CameraComponent* camera);
private:
explicit Scene(const std::string& name);
explicit Scene(const std::string& name, std::unique_ptr<PhysicsWorld> physicsWorld);
@@ -127,6 +132,8 @@ private:
bool m_ShowDemoWindow{false};
ObjectId m_SelectedObjectId{InvalidObjectId};
ObjectId m_CurrentCameraId{InvalidObjectId};
std::function<void()> m_registerBindings;
std::function<void()> m_unregisterBindings;
bool m_bindingsUnloaded{false};
@@ -2,6 +2,7 @@
#define DESTRUM_ENGINECOMPONENTLIST_H
#include <destrum/Components/Animator.h>
#include <destrum/Components/CameraComponent.h>
#include <destrum/Components/LightComponent.h>
#include <destrum/Components/MeshRendererComponent.h>
#include <destrum/Components/OrbitAndSpin.h>
@@ -15,6 +16,7 @@
#define DESTRUM_ENGINE_COMPONENTS(X) \
X(MeshRendererComponent) \
X(LightComponent) \
X(CameraComponent) \
X(Rotator) \
X(Spinner) \
X(OrbitAndSpin) \
@@ -1,6 +1,7 @@
#ifndef DESTRUM_IMGUIUTILS_H
#define DESTRUM_IMGUIUTILS_H
#include <functional>
#include <memory>
#include <string_view>
#include <vector>
@@ -22,6 +23,7 @@ namespace ImGuiUtils {
const std::vector<std::shared_ptr<GameObject>>& objects,
const std::vector<std::shared_ptr<GameObject>>& pendingAdditions,
ObjectId& selectedObjectId,
const std::function<GameObject*()>& createGameObject,
bool* open = nullptr);
}