SceneCamera¶
我们新增一个透视相机
新增枚举,切换透视投影和正交投影
enum class ProjectionType { Perspective = 0, Orthographic = 1 };
ProjectionType m_ProjectionType = ProjectionType::Orthographic;
float m_PerspectiveFOV = glm::radians(45.0f);
float m_PerspectiveNear = 0.01f, m_PerspectiveFar = 1000.0f;
//perspective
float GetPerspectiveVerticalFOV() const { return m_PerspectiveFOV; }
void SetPerspectiveVerticalFOV(float verticalFov) { m_PerspectiveFOV = verticalFov; RecalculateProjection(); }
float GetPerspectiveNearClip() const { return m_PerspectiveNear; }
void SetPerspectiveNearClip(float nearClip) { m_PerspectiveNear = nearClip; RecalculateProjection(); }
float GetPerspectiveFarClip() const { return m_PerspectiveFar; }
void SetPerspectiveFarClip(float farClip) { m_PerspectiveFar = farClip; RecalculateProjection(); }
IMGUI显示¶
根据不同的相机枚举实现不同的显示逻辑,每次显示完设置一次值
if (entity.HasComponent<CameraComponent>())
{
if (ImGui::TreeNodeEx((void*)typeid(CameraComponent).hash_code(), ImGuiTreeNodeFlags_DefaultOpen, "Camera"))
{
auto& cameraComponent = entity.GetComponent<CameraComponent>();
auto& camera = cameraComponent.Camera;
ImGui::Checkbox("Primary", &cameraComponent.Primary);
const char* projectionTypeStrings[] = { "Perspective", "Orthographic" };
const char* currentProjectionTypeString = projectionTypeStrings[(int)camera.GetProjectionType()];
if (ImGui::BeginCombo("Projection", currentProjectionTypeString))
{
for (int i = 0; i < 2; i++)
{
bool isSelected = currentProjectionTypeString == projectionTypeStrings[i];
if (ImGui::Selectable(projectionTypeStrings[i], isSelected))
{
currentProjectionTypeString = projectionTypeStrings[i];
camera.SetProjectionType((SceneCamera::ProjectionType)i);
}
if (isSelected)
ImGui::SetItemDefaultFocus();
}
ImGui::EndCombo();
}
if (camera.GetProjectionType() == SceneCamera::ProjectionType::Perspective)
{
float verticalFov = glm::degrees(camera.GetPerspectiveVerticalFOV());
if (ImGui::DragFloat("Vertical FOV", &verticalFov))
camera.SetPerspectiveVerticalFOV(glm::radians(verticalFov));
float orthoNear = camera.GetPerspectiveNearClip();
if (ImGui::DragFloat("Near", &orthoNear))
camera.SetPerspectiveNearClip(orthoNear);
float orthoFar = camera.GetPerspectiveFarClip();
if (ImGui::DragFloat("Far", &orthoFar))
camera.SetPerspectiveFarClip(orthoFar);
}
if (camera.GetProjectionType() == SceneCamera::ProjectionType::Orthographic)
{
float orthoSize = camera.GetOrthographicSize();
if (ImGui::DragFloat("Size", &orthoSize))
camera.SetOrthographicSize(orthoSize);
float orthoNear = camera.GetOrthographicNearClip();
if (ImGui::DragFloat("Near", &orthoNear))
camera.SetOrthographicNearClip(orthoNear);
float orthoFar = camera.GetOrthographicFarClip();
if (ImGui::DragFloat("Far", &orthoFar))
camera.SetOrthographicFarClip(orthoFar);
ImGui::Checkbox("Fixed Aspect Ratio", &cameraComponent.FixedAspectRatio);
}
SpriteRenderer¶
if (entity.HasComponent<SpriteRendererComponent>())
{
if (ImGui::TreeNodeEx((void*)typeid(SpriteRendererComponent).hash_code(), ImGuiTreeNodeFlags_DefaultOpen, "Sprite Renderer"))
{
auto& src = entity.GetComponent<SpriteRendererComponent>();
ImGui::ColorEdit4("Color", glm::value_ptr(src.Color));
ImGui::TreePop();
}
}