Skip to content

Commit

Permalink
[#190]: Make gizmo scale with camera zoom
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobDomagala committed Feb 6, 2024
1 parent 6ca2d86 commit 9de0938
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 33 deletions.
6 changes: 2 additions & 4 deletions assets/shaders/default.vert
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
layout(set = 0, binding = 0) uniform UniformBufferObject
{
mat4 u_projectionMat;
mat4 u_projectionNoZoomMat;
mat4 u_viewMat;
vec4 u_cameraPos;
}
Expand Down Expand Up @@ -39,7 +38,6 @@ vs_out;

layout (push_constant) uniform PushConstants {
float selectedIdx;
float meshType;
} pushConstants;

void
Expand All @@ -49,13 +47,13 @@ main(void)
BufferData curInstanceData = Transforms[int(drawID)];

vs_out.fTexCoord = a_texCoordDrawID.xy;
vs_out.fColor = pushConstants.selectedIdx != drawID ? curInstanceData.color : vec4(0.4f, 0.1f, 0.2f, 1.0f);
vs_out.fColor = curInstanceData.color;

vs_out.fDiffSampl = int(curInstanceData.texSamples.x);
vs_out.fExtraSampl = int(curInstanceData.texSamples.y);

mat4 modelMat = curInstanceData.modelMat;
vec3 position = a_position;

gl_Position = (pushConstants.meshType != 1.0f ? ubo.u_projectionMat : ubo.u_projectionNoZoomMat) * ubo.u_viewMat * modelMat * vec4(position.xyz, 1.0f);
gl_Position = ubo.u_projectionMat * ubo.u_viewMat * modelMat * vec4(position.xyz, 1.0f);
}
1 change: 0 additions & 1 deletion assets/shaders/line.vert
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ layout(location = 0) in vec3 a_position;
layout(set = 0, binding = 0) uniform UniformBufferObject
{
mat4 u_projectionMat;
mat4 u_projectionNoZoomMat;
mat4 u_viewMat;
vec4 u_cameraPos;
}
Expand Down
Binary file modified assets/shaders/line.vert.spv
Binary file not shown.
Binary file modified assets/shaders/vert.spv
Binary file not shown.
11 changes: 2 additions & 9 deletions editor/editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ Editor::MouseScrollCallback(MouseScrollEvent& event)
if (!playGame_ && !EditorGUI::IsBlockingEvents() && levelLoaded_)
{
camera_.Zoom(static_cast< float >(event.xOffset_ + event.yOffset_));
gizmo_.Zoom(static_cast< int32_t >(-event.yOffset_));

event.handled_ = true;
}
}
Expand Down Expand Up @@ -912,7 +914,6 @@ Editor::Render(VkCommandBuffer cmdBuffer)
nullptr);
renderer::QuadShader::PushConstants pushConstants = {};
pushConstants.selectedIdx = -1.0f;
pushConstants.meshType = 0.0f;

if (currentSelectedGameObject_ != Object::INVALID_ID)
{
Expand All @@ -926,14 +927,6 @@ Editor::Render(VkCommandBuffer cmdBuffer)

for (int32_t layer = renderer::NUM_LAYERS - 1; layer >= 0; --layer)
{
// On-top objects
if (layer == 0)
{
pushConstants.meshType = 1.0f;
vkCmdPushConstants(cmdBuffer, renderData.pipelineLayout, VK_SHADER_STAGE_VERTEX_BIT, 0,
sizeof(renderer::QuadShader::PushConstants), &pushConstants);
}

const auto idx = static_cast< size_t >(layer);
const auto& numObjects = renderData.numMeshes.at(idx);
const auto renderThisLayer = renderLayerToDraw_ == -1 ? true : renderLayerToDraw_ == layer;
Expand Down
75 changes: 59 additions & 16 deletions editor/gizmo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ namespace looper {
void
Gizmo::Initialize()
{
gizmoCenter_.SetSpriteTextured(glm::vec3{}, {16, 16}, "centered_move.png",
renderer::SpriteType::alwaysOnTop);
gizmoCenter_.SetSpriteTextured(glm::vec3{0.0f, 0.0f, renderer::LAYER_1}, {16, 16},
"centered_move.png", renderer::SpriteType::alwaysOnTop);
gizmoCenter_.SetColor({1.0f, 1.0f, 1.0f, 1.0f});

gizmoUp_.SetSpriteTextured(glm::vec3{}, {96, 32}, "arrow.png",
gizmoUp_.SetSpriteTextured(glm::vec3{0.0f, 0.0f, renderer::LAYER_1}, {96, 32}, "arrow.png",
renderer::SpriteType::alwaysOnTop);
gizmoUp_.Rotate(90.0f, renderer::RotationType::degrees);
gizmoUp_.SetColor({0.0f, 1.0f, 0.0f, 1.0f});

gizmoSide_.SetSpriteTextured(glm::vec3{}, {96, 32}, "arrow.png",
gizmoSide_.SetSpriteTextured(glm::vec3{0.0f, 0.0f, renderer::LAYER_1}, {96, 32}, "arrow.png",
renderer::SpriteType::alwaysOnTop);
gizmoSide_.SetColor({1.0f, 0.0f, 0.0f, 1.0f});

Expand Down Expand Up @@ -61,7 +61,47 @@ Gizmo::Update(const glm::vec2& centeredPos, float rotation)
gizmoCenter_.Rotate(currentRotation_);
}

gizmoCenter_.SetInitialPosition(glm::vec3{centeredPos, renderer::LAYER_0});
gizmoCenter_.SetInitialPosition(glm::vec3{centeredPos, renderer::LAYER_1});

AdjustSize();
}

void
Gizmo::Move(const glm::vec2& moveBy)
{
gizmoCenter_.Translate(moveBy);
gizmoUp_.Translate(moveBy);
gizmoSide_.Translate(moveBy);
}

void
Gizmo::Zoom(int32_t zoomVal)
{
zoomLevel_ = glm::min(zoomLevel_ + zoomVal, 18);

AdjustSize();
}

void
Gizmo::AdjustSize()
{
float scaleFactor = 1.0f;
if (zoomLevel_ >= 0)
{
scaleFactor = 20.0f / (20.0f - zoomLevel_);
}

gizmoCenter_.Scale(
(currentState_ == GizmoState::rotate ? centerInitialSize_.second : centerInitialSize_.first)
* scaleFactor);
centerCurrentSize_ = {centerInitialSize_.first * scaleFactor,
centerInitialSize_.second * scaleFactor};

upCurrentSize_ = glm::vec2{upInitialSize_.x * scaleFactor, upInitialSize_.y * scaleFactor};
gizmoUp_.Scale(upCurrentSize_);

sideCurrentSize_ = glm::vec2{sideInitialSize_.x * scaleFactor, sideInitialSize_.y * scaleFactor};
gizmoSide_.Scale(sideCurrentSize_);

gizmoUp_.SetInitialPosition(
gizmoCenter_.GetPosition()
Expand All @@ -74,12 +114,11 @@ Gizmo::Update(const glm::vec2& centeredPos, float rotation)
0.0f});
}

void
Gizmo::Move(const glm::vec2& moveBy)

glm::vec2
Gizmo::Position() const
{
gizmoCenter_.Translate(moveBy);
gizmoUp_.Translate(moveBy);
gizmoSide_.Translate(moveBy);
return gizmoCenter_.GetPosition();
}

void
Expand All @@ -103,11 +142,11 @@ Gizmo::CheckHovered(const glm::vec3& cameraPos, const glm::vec2& globalPosition)
};

checkGizmo(gizmoCenter_,
currentState_ == GizmoState::rotate ? centerInitialSize_.second
: centerInitialSize_.first,
currentState_ == GizmoState::rotate ? centerCurrentSize_.second
: centerCurrentSize_.first,
GizmoPart::center);
checkGizmo(gizmoSide_, sideInitialSize_, GizmoPart::hotizontal);
checkGizmo(gizmoUp_, upInitialSize_, GizmoPart::vertical);
checkGizmo(gizmoSide_, sideCurrentSize_, GizmoPart::hotizontal);
checkGizmo(gizmoUp_, upCurrentSize_, GizmoPart::vertical);

mouseOnGizmo_ = gizmoTouched;
}
Expand All @@ -128,6 +167,8 @@ Gizmo::SwitchToScale()
renderer::TextureLibrary::GetTexture("scale_slider.png")->GetID());
gizmoUp_.SetTextureID(renderer::TextureType::DIFFUSE_MAP,
renderer::TextureLibrary::GetTexture("scale_slider.png")->GetID());

AdjustSize();
}
void
Gizmo::SwitchToRotate()
Expand All @@ -139,15 +180,17 @@ Gizmo::SwitchToRotate()

gizmoCenter_.SetTextureID(renderer::TextureType::DIFFUSE_MAP,
renderer::TextureLibrary::GetTexture("rotate.png")->GetID());
gizmoCenter_.SetSize(centerInitialSize_.second);

gizmoCenter_.Rotate(currentRotation_);

AdjustSize();
}
void
Gizmo::SwitchToTranslate()
{
currentState_ = GizmoState::translate;

gizmoCenter_.SetSize(centerInitialSize_.first);
AdjustSize();
gizmoCenter_.Rotate(0.0f);
gizmoSide_.Show();
gizmoUp_.Show();
Expand Down
14 changes: 14 additions & 0 deletions editor/gizmo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class Gizmo
Update(const glm::vec2& centeredPos, float rotation);
void
Move(const glm::vec2& moveBy);
void
Zoom(int32_t zoomVal);

void
CheckHovered(const glm::vec3& cameraPos, const glm::vec2& globalPosition);
Expand All @@ -43,6 +45,12 @@ class Gizmo
SwitchToRotate();
void
SwitchToTranslate();
glm::vec2
Position() const;

private:
void
AdjustSize();

public:
GizmoState currentState_ = GizmoState::translate;
Expand All @@ -54,10 +62,16 @@ class Gizmo
renderer::Sprite gizmoUp_ = {};
renderer::Sprite gizmoSide_ = {};

int32_t zoomLevel_ = 0;
float currentRotation_ = 0.0f;
// <default, rotate>
std::pair< glm::vec2, glm::vec2 > centerInitialSize_ = {};
glm::vec2 upInitialSize_ = {};
glm::vec2 sideInitialSize_ = {};

// <default, rotate>
std::pair< glm::vec2, glm::vec2 > centerCurrentSize_ = {};
glm::vec2 upCurrentSize_ = {};
glm::vec2 sideCurrentSize_ = {};
};
} // namespace looper
1 change: 0 additions & 1 deletion engine/renderer/renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -884,7 +884,6 @@ UpdateUniformBuffer()

tmpUBO.view = renderData.viewMat;
tmpUBO.proj = renderData.projMat;
tmpUBO.projNoZoom = renderData.projNoZoomMat;

auto& ubo = renderData.uniformBuffers.at(frame);

Expand Down
1 change: 0 additions & 1 deletion engine/renderer/shader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ struct QuadShader
struct PushConstants
{
float selectedIdx = {};
float meshType = {};
};

static void
Expand Down
1 change: 0 additions & 1 deletion engine/renderer/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ using TextureIDs = std::array< TextureID, 4 >;
struct UniformBufferObject
{
alignas(16) glm::mat4 proj = {};
alignas(16) glm::mat4 projNoZoom = {};
alignas(16) glm::mat4 view = {};
glm::vec4 cameraPos = {};
};
Expand Down

0 comments on commit 9de0938

Please sign in to comment.