跳转至

编辑时有一个编辑摄像机呈现画面给开发人员,运行时有一个主摄像机呈现游戏画面给玩家。

Camera结构

主要还是绑定几个事件 位置由焦点+偏移计算

#pragma once
#include "Camera.h"
#include "CryDust/Core/Timestep.h"
#include "CryDust/Events/Event.h"
#include "CryDust/Events/MouseEvent.h"
#include <glm/glm.hpp>
namespace CryDust {
    class EditorCamera : public Camera
    {
    public:
        EditorCamera() = default;
        EditorCamera(float fov, float aspectRatio, float nearClip, float farClip);
        void OnUpdate(Timestep ts);
        void OnEvent(Event& e);
        inline float GetDistance() const { return m_Distance; }
        inline void SetDistance(float distance) { m_Distance = distance; }
        inline void SetViewportSize(float width, float height) { m_ViewportWidth = width; m_ViewportHeight = height; UpdateProjection(); }
        const glm::mat4& GetViewMatrix() const { return m_ViewMatrix; }
        glm::mat4 GetViewProjection() const { return m_Projection * m_ViewMatrix; }
        glm::vec3 GetUpDirection() const;
        glm::vec3 GetRightDirection() const;
        glm::vec3 GetForwardDirection() const;
        const glm::vec3& GetPosition() const { return m_Position; }
        glm::quat GetOrientation() const;
        float GetPitch() const { return m_Pitch; }
        float GetYaw() const { return m_Yaw; }
    private:
        void UpdateProjection();
        void UpdateView();
        bool OnMouseScroll(MouseScrolledEvent& e);
        void MousePan(const glm::vec2& delta);
        void MouseRotate(const glm::vec2& delta);
        void MouseZoom(float delta);
        glm::vec3 CalculatePosition() const;
        std::pair<float, float> PanSpeed() const;
        float RotationSpeed() const;
        float ZoomSpeed() const;
    private:
        float m_FOV = 45.0f, m_AspectRatio = 1.778f, m_NearClip = 0.1f, m_FarClip = 1000.0f;
        glm::mat4 m_ViewMatrix;
        glm::vec3 m_Position = { 0.0f, 0.0f, 0.0f };
        glm::vec3 m_FocalPoint = { 0.0f, 0.0f, 0.0f };
        glm::vec2 m_InitialMousePosition = { 0.0f, 0.0f };
        float m_Distance = 10.0f;
        float m_Pitch = 0.0f, m_Yaw = 0.0f;
        float m_ViewportWidth = 1280, m_ViewportHeight = 720;
    };
}

Scene

设置编辑时和运行时(原来的update)

        void OnUpdateRuntime(Timestep ts);
        void OnUpdateEditor(Timestep ts, EditorCamera& camera);