Skip to content

Commit

Permalink
Added camera component UI to editor
Browse files Browse the repository at this point in the history
and extended scene camera class
  • Loading branch information
L4ZZA committed Sep 24, 2020
1 parent 4a88cf7 commit 48e7d13
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 31 deletions.
14 changes: 7 additions & 7 deletions ember/imgui.ini
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ Size=1280,720
Collapsed=0

[Window][Viewport]
Pos=285,19
Size=577,701
Pos=315,19
Size=547,701
Collapsed=0
DockId=0x00000003,0

Expand All @@ -26,22 +26,22 @@ Collapsed=0

[Window][Scene hierarchy panel]
Pos=0,19
Size=283,316
Size=313,316
Collapsed=0
DockId=0x00000005,0

[Window][Properties]
Pos=0,337
Size=283,383
Size=313,383
Collapsed=0
DockId=0x00000006,0

[Docking][Data]
DockSpace ID=0x582DCB9B Window=0xCB5DF48C Pos=164,206 Size=1280,701 Split=X Selected=0x995B0CF8
DockSpace ID=0x582DCB9B Window=0xCB5DF48C Pos=216,258 Size=1280,701 Split=X Selected=0x995B0CF8
DockNode ID=0x00000002 Parent=0x582DCB9B SizeRef=862,701 Split=X
DockNode ID=0x00000001 Parent=0x00000002 SizeRef=283,701 Split=Y Selected=0x556289C7
DockNode ID=0x00000001 Parent=0x00000002 SizeRef=313,701 Split=Y Selected=0x556289C7
DockNode ID=0x00000005 Parent=0x00000001 SizeRef=283,316 Selected=0x556289C7
DockNode ID=0x00000006 Parent=0x00000001 SizeRef=283,383 Selected=0xC89E3217
DockNode ID=0x00000003 Parent=0x00000002 SizeRef=577,701 CentralNode=1 HiddenTabBar=1 Selected=0x995B0CF8
DockNode ID=0x00000003 Parent=0x00000002 SizeRef=547,701 CentralNode=1 HiddenTabBar=1 Selected=0x995B0CF8
DockNode ID=0x00000004 Parent=0x582DCB9B SizeRef=416,701 Selected=0x1C33C293

22 changes: 8 additions & 14 deletions ember/src/editor_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,17 @@ namespace pyro
#else
m_active_scene = make_ref<scene>();

m_square_entity = m_active_scene->create_entity("Green Square");
m_square_entity.add_component<sprite_renderer_component>(glm::vec4{ 0.f,1.f,0.f,1.f });
auto green_square = m_active_scene->create_entity("Green Square");
green_square.add_component<sprite_renderer_component>(glm::vec4{ 0.f,1.f,0.f,1.f });

auto red_square = m_active_scene->create_entity("Red Square");
red_square.add_component<sprite_renderer_component>(glm::vec4{ 1.f,0.f,0.f,1.f });

m_camera_entity = m_active_scene->create_entity("Camera Entity");
m_camera_entity.add_component<camera_component>();

m_square_entity = green_square;

m_second_camera = m_active_scene->create_entity("Clip-space Camera");
auto &sc = m_second_camera.add_component<camera_component>();
sc.primary = false;
Expand Down Expand Up @@ -263,22 +268,11 @@ namespace pyro
}
}


ImGui::DragFloat3("Camera Transform",
glm::value_ptr(m_camera_entity.get_component<transform_component>().transform[3]));

if(ImGui::Checkbox("Camera A", &m_is_primary_camera))
if(ImGui::Checkbox("Toggle Primary cameras", &m_is_primary_camera))
{
m_camera_entity.get_component<camera_component>().primary = m_is_primary_camera;
m_second_camera.get_component<camera_component>().primary = !m_is_primary_camera;
}

{
auto &camera = m_second_camera.get_component<camera_component>().camera;
float orthoSize = camera.orthographic_size();
if(ImGui::DragFloat("Second Camera Ortho Size", &orthoSize))
camera.orthographic_size(orthoSize);
}
#endif

for(auto &result : m_profile_results)
Expand Down
82 changes: 82 additions & 0 deletions ember/src/panels/scene_hierarchy_panel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,88 @@ namespace pyro
ImGui::TreePop();
}
}

if(e.has_component<camera_component>())
{
if(ImGui::TreeNodeEx(
reinterpret_cast<void *>(typeid(camera_component).hash_code()),
ImGuiTreeNodeFlags_DefaultOpen, "Camera"))
{
auto &camera_comp = e.get_component<camera_component>();
auto &camera = camera_comp.camera;

const char *projection_type_strings[]{ "Perspective", "Otrhographic" };
const char *current_projection_type_string =
projection_type_strings[static_cast<int>(camera.projection_type())];

if(ImGui::BeginCombo("Projection", current_projection_type_string))
{
for(int i = 0; i < 2; i++)
{
bool is_selected =
current_projection_type_string == projection_type_strings[i];
if(ImGui::Selectable(projection_type_strings[i], is_selected))
{
current_projection_type_string = projection_type_strings[i];
camera.projection_type(static_cast<scene_camera::e_projection_type>(i));
}

if(is_selected)
ImGui::SetItemDefaultFocus();
}

ImGui::EndCombo();
}

ImGui::Checkbox("Primary", &camera_comp.primary);

if(camera.projection_type() == scene_camera::e_projection_type::perspective)
{
float perspective_vertical_fov = glm::degrees(camera.perspective_vertical_fov());
if(ImGui::DragFloat("Size", &perspective_vertical_fov))
{
camera.perspective_vertical_fov(glm::radians(perspective_vertical_fov));
}

float perspective_near = camera.perspective_near();
if(ImGui::DragFloat("Near", &perspective_near))
{
camera.perspective_near(perspective_near);
}

float perspective_far = camera.perspective_far();
if(ImGui::DragFloat("Far", &perspective_far))
{
camera.perspective_far(perspective_far);
}
}

if(camera.projection_type() == scene_camera::e_projection_type::orthographic)
{
ImGui::Checkbox("Fixed Aspect ratio", &camera_comp.fixed_aspect_ratio);

float ortho_size = camera.orthographic_size();
if(ImGui::DragFloat("Size", &ortho_size))
{
camera.orthographic_size(ortho_size);
}

float ortho_near = camera.orthographic_near();
if(ImGui::DragFloat("Near", &ortho_near))
{
camera.orthographic_near(ortho_near);
}

float ortho_far = camera.orthographic_far();
if(ImGui::DragFloat("Far", &ortho_far))
{
camera.orthographic_far(ortho_far);
}
}

ImGui::TreePop();
}
}
}
void scene_hierarchy_panel::context(ref<scene> scene_context)
{
Expand Down
36 changes: 27 additions & 9 deletions pyro/src/pyro/scene/scene_camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,18 @@ pyro::scene_camera::scene_camera()
recalculate_projection();
}

void pyro::scene_camera::make_perspective(float vertical_fov, float near_clip, float far_clip)
{
m_projection_type = e_projection_type::perspective;
m_perspective_vertical_fov = vertical_fov;
m_perspective_near = near_clip;
m_perspective_far = far_clip;
recalculate_projection();
}

void pyro::scene_camera::make_orthographic(float size, float near_clip, float far_clip)
{
m_projection_type = e_projection_type::orthographic;
m_orthographic_size = size;
m_orthographic_near = near_clip;
m_orthographic_far = far_clip;
Expand All @@ -25,13 +35,21 @@ void pyro::scene_camera::viewport_size(uint32_t width, uint32_t height)

void pyro::scene_camera::recalculate_projection()
{
float ortho_left = -m_orthographic_size * m_aspect_ratio * 0.5f;
float ortho_right = m_orthographic_size * m_aspect_ratio * 0.5f;
float ortho_bottom = -m_orthographic_size * 0.5f;
float ortho_top = m_orthographic_size * 0.5f;

m_projection = glm::ortho(
ortho_left, ortho_right,
ortho_bottom, ortho_top,
m_orthographic_near, m_orthographic_far);
if(m_projection_type == e_projection_type::perspective)
{
m_projection = glm::perspective(m_perspective_vertical_fov,
m_aspect_ratio, m_perspective_near, m_perspective_far);
}
else
{
float ortho_left = -m_orthographic_size * m_aspect_ratio * 0.5f;
float ortho_right = m_orthographic_size * m_aspect_ratio * 0.5f;
float ortho_bottom = -m_orthographic_size * 0.5f;
float ortho_top = m_orthographic_size * 0.5f;

m_projection = glm::ortho(
ortho_left, ortho_right,
ortho_bottom, ortho_top,
m_orthographic_near, m_orthographic_far);
}
}
35 changes: 34 additions & 1 deletion pyro/src/pyro/scene/scene_camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,56 @@

namespace pyro
{
// It represents all the real-time cameras present in the game.
class scene_camera : public camera
{
public:
enum class e_projection_type { perspective = 0, orthographic = 1 };

public:
scene_camera();
virtual ~scene_camera() = default;

void make_orthographic(float size, float nearClip, float farClip);
void make_perspective(float vertical_fov, float near_clip, float far_clip);

void viewport_size(uint32_t width, uint32_t height);

float perspective_vertical_fov() const { return m_perspective_vertical_fov; }
void perspective_vertical_fov(float vertical_radians) { m_perspective_vertical_fov = vertical_radians; recalculate_projection(); }

float perspective_near() const { return m_perspective_near; }
void perspective_near(float near_clip) { m_perspective_near = near_clip; recalculate_projection(); }

float perspective_far() const { return m_perspective_far; }
void perspective_far(float far_clip) { m_perspective_far = far_clip; recalculate_projection(); }


float orthographic_size() const { return m_orthographic_size; }
void orthographic_size(float size) { m_orthographic_size = size; recalculate_projection(); }

float orthographic_near() const { return m_orthographic_near; }
void orthographic_near(float near_clip) { m_orthographic_near = near_clip; recalculate_projection(); }

float orthographic_far() const { return m_orthographic_far; }
void orthographic_far(float far_clip) { m_orthographic_far = far_clip; recalculate_projection(); }

e_projection_type projection_type() const { return m_projection_type; }
void projection_type(e_projection_type const &t) { m_projection_type = t; }

private:
void recalculate_projection();
private:
e_projection_type m_projection_type{ e_projection_type::orthographic };
// Field of view angle stored in radians.
float m_perspective_vertical_fov = glm::radians(45.f);
float m_perspective_near = 0.1f;
float m_perspective_far = 1000.f;

// Full width of the orthographic camera fov.
float m_orthographic_size = 10.0f;
float m_orthographic_near = -1.0f, m_orthographic_far = 1.0f;
float m_orthographic_near = -1.0f;
float m_orthographic_far = 1.0f;

float m_aspect_ratio = 0.0f;
};
Expand Down

0 comments on commit 48e7d13

Please sign in to comment.