Skip to content

Commit

Permalink
fix: handle window rescale properly
Browse files Browse the repository at this point in the history
  • Loading branch information
Astral-Sheep committed Jul 26, 2024
1 parent 90a6042 commit c1b86fd
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 29 deletions.
7 changes: 4 additions & 3 deletions res/2d/blend.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ layout(location = 0) out vec4 color;

in vec2 v_UV;
in float v_AspectRatio;
in float v_SizeFactor;

// -- Default parameters --
uniform vec2 u_CameraPos;
Expand Down Expand Up @@ -138,9 +139,9 @@ void main()
if (u_ShowMouseDistance)
{
vec2 mpos = u_MousePos;
mpos.x -= v_AspectRatio * 0.5f;
mpos.y += 0.5f;
mpos *= 4.f; // 2 (default) x 2 (UV multiplier)
mpos += vec2(-0.5f, 0.5f);
mpos.x *= v_AspectRatio;
mpos *= 4.f * v_SizeFactor; // 2 (default) x 2 (UV multiplier)

float d = abs(get_dist(u_CameraPos + mpos * zoom));
float l = length(uv * zoom - mpos * zoom);
Expand Down
7 changes: 4 additions & 3 deletions res/2d/fractals.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ layout(location = 0) out vec4 color;

in vec2 v_UV;
in float v_AspectRatio;
in float v_SizeFactor;

// -- Default parameters --
uniform vec2 u_CameraPos;
Expand Down Expand Up @@ -171,9 +172,9 @@ void main()
if (u_ShowMouseDistance)
{
vec2 mpos = u_MousePos;
mpos.x -= v_AspectRatio * 0.5f;
mpos.y += 0.5f;
mpos *= 4.f; // 2 (default) x 2 (UV multiplier)
mpos += vec2(-0.5f, 0.5f);
mpos.x *= v_AspectRatio;
mpos *= 4.f * v_SizeFactor; // 2 (default) x 2 (UV multiplier)

float d = abs(get_dist(u_CameraPos + mpos * zoom));
float l = length(uv * zoom - mpos * zoom);
Expand Down
7 changes: 4 additions & 3 deletions res/2d/repetition.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ layout(location = 0) out vec4 color;

in vec2 v_UV;
in float v_AspectRatio;
in float v_SizeFactor;

// -- Default parameters --
uniform vec2 u_CameraPos;
Expand Down Expand Up @@ -79,9 +80,9 @@ void main()
if (u_ShowMouseDistance)
{
vec2 mpos = u_MousePos;
mpos.x -= v_AspectRatio * 0.5f;
mpos.y += 0.5f;
mpos *= 4.f; // 2 (default) x 2 (UV multiplier)
mpos += vec2(-0.5f, 0.5f);
mpos.x *= v_AspectRatio;
mpos *= 4.f * v_SizeFactor; // 2 (default) x 2 (UV multiplier)

float d = abs(get_dist(u_CameraPos + mpos * zoom));
float l = length(uv * zoom - mpos * zoom);
Expand Down
7 changes: 4 additions & 3 deletions res/2d/shapes.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ layout(location = 0) out vec4 color;

in vec2 v_UV;
in float v_AspectRatio;
in float v_SizeFactor;

// -- Default parameters --
uniform vec2 u_CameraPos;
Expand Down Expand Up @@ -859,9 +860,9 @@ void main()
if (u_ShowMouseDistance)
{
vec2 mpos = u_MousePos;
mpos.x -= v_AspectRatio * 0.5f;
mpos.y += 0.5f;
mpos *= 4.f; // 2 (default) x 2 (UV multiplier)
mpos += vec2(-0.5f, 0.5f);
mpos.x *= v_AspectRatio;
mpos *= 4.f * v_SizeFactor; // 2 (default) x 2 (UV multiplier)

float d = abs(get_dist(u_CameraPos + mpos * zoom));
float l = length(uv * zoom - mpos * zoom);
Expand Down
7 changes: 4 additions & 3 deletions res/vertex.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ layout(location = 0) in vec4 position;

out vec2 v_UV;
out float v_AspectRatio;
out float v_SizeFactor;

uniform ivec2 u_ScreenSize = DEFAULT_SCREEN_SIZE;

void main()
{
vec2 sizeFactor = vec2(float(u_ScreenSize.x) / DEFAULT_SCREEN_SIZE.x, float(u_ScreenSize.y) / DEFAULT_SCREEN_SIZE.y);
v_SizeFactor = max(float(u_ScreenSize.x) / DEFAULT_SCREEN_SIZE.x, float(u_ScreenSize.y) / DEFAULT_SCREEN_SIZE.y);
gl_Position = position;
v_AspectRatio = float(u_ScreenSize.x) / u_ScreenSize.y;
v_UV = position.xy * sizeFactor;
v_AspectRatio = float(u_ScreenSize.x) / float(u_ScreenSize.y);
v_UV = position.xy * v_SizeFactor;
v_UV.x *= v_AspectRatio;
}

71 changes: 59 additions & 12 deletions src/2d/FractalRaymarcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
#include "Engine/events/KeyCodes.h"
#include "Engine/events/KeyboardEvent.hpp"
#include "imgui/imgui.h"
#include "math/Math.hpp"
#include <cmath>

using namespace GL;
using namespace Math;

namespace _2D
{
Expand All @@ -25,8 +28,23 @@ namespace _2D
/* "Koch Curve", */
};

static const int maxIterations[Max] = {
21,
15,
14,
};

static const float maxZoom[Max] = {
50.f,
57.f,
54.f,
};

const Vector2F FractalRaymarcher::CAMERA_POS = Vector2F(0.0265505f, 0.288753f);

FractalRaymarcher::FractalRaymarcher()
: Raymarcher2D(), mFractal(SierpinskiTriangle), mIterations(3), mShowDistanceField(false)
: Raymarcher2D(), mFractal(SierpinskiTriangle), mIterations(3), mShowDistanceField(false),
mAutoCamera(false), mCameraState(0.f)
{
mShader.reset(Shader::FromGLSLTextFiles("res/vertex.glsl", "res/2d/fractals.glsl"));
InitShader();
Expand All @@ -37,13 +55,27 @@ namespace _2D
if (ImGui::CollapsingHeader("Fractals"))
{
ImGui::Combo("Fractal", &mFractal, fractals, Max);
ImGui::SliderInt("Iterations", &mIterations, 0, 10);
ImGui::SliderInt("Iterations", &mIterations, 0, 100);
ImGui::Checkbox("Show distance field", &mShowDistanceField);
}

Raymarcher2D::RenderImGuiParameters();
}

void FractalRaymarcher::_Process(const float pDelta)
{
if (mAutoCamera)
{
mCameraState += pDelta;
mZoom = Math::Lerp(0.f, maxZoom[mFractal], (std::cos((mCameraState + Math::PI) / MOVEMENT_DURATION + Math::PI) + 1.f) * 0.5f);
mIterations = (int)std::round(Math::Lerp(0.f, (float)maxIterations[mFractal], mZoom / maxZoom[mFractal]));
}
else
{
Raymarcher2D::_Process(pDelta);
}
}

void FractalRaymarcher::_Render(const float pDelta)
{
mShader->SetUniform1f("u_Time", Time::GetElapsedTime());
Expand All @@ -62,19 +94,34 @@ namespace _2D
return;
}

if (pEvent.GetEventType() == EventType::KeyPressed)
if (pEvent.GetEventType() == EventType::KeyReleased)
{
KeyPressedEvent &lKPEvent = pEvent.Cast<KeyPressedEvent>();
KeyReleasedEvent &lKREvent = pEvent.Cast<KeyReleasedEvent>();

if (lKPEvent.GetKeyCode() == (int)KeyCode::Up)
switch (lKREvent.GetKeyCode())
{
mIterations = Math::Clamp(mIterations + 1, 0, 10);
lKPEvent.handled = true;
}
else if (lKPEvent.GetKeyCode() == (int)KeyCode::Down)
{
mIterations = Math::Clamp(mIterations - 1, 0, 10);
lKPEvent.handled = true;
case (int)KeyCode::Up:
mIterations = Math::Clamp(mIterations + 1, 0, 100);
lKREvent.handled = true;
break;
case (int)KeyCode::Down:
mIterations = Math::Clamp(mIterations - 1, 0, 100);
lKREvent.handled = true;
break;
case (int)KeyCode::F7:
mAutoCamera = !mAutoCamera;

if (mAutoCamera)
{
/* float lRatio = mZoom / maxZoom[mFractal]; */
/* mCameraState = Math::Abs(MOVEMENT_DURATION / Math::PI * (std::acos(lRatio * 2.f - 1.f) - Math::PI)); */
mCameraState = 0.f;
}

lKREvent.handled = true;
break;
default:
break;
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/2d/FractalRaymarcher.hpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
#pragma once

#include "2d/Raymarcher2D.hpp"
#include "math/Vector2.hpp"

namespace _2D
{
class FractalRaymarcher : public Raymarcher2D
{
private:
static constexpr float MOVEMENT_DURATION = 5.f;
static const Math::Vector2F CAMERA_POS;

int mFractal;
int mIterations;
bool mShowDistanceField;
bool mAutoCamera;
float mCameraState;

private:
void RenderImGuiParameters() override;
void _Process(const float pDelta) override;
void _Render(const float pDelta) override;
void _OnEvent(Event &pEvent) override;

Expand Down
6 changes: 4 additions & 2 deletions src/2d/Raymarcher2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "imgui/imgui.h"
#include "math/Math.hpp"
#include "math/Vector2.hpp"
#include <iostream>

using namespace GL;
using namespace Math;
Expand Down Expand Up @@ -67,7 +68,7 @@ namespace _2D
mMousePos.y = lMMEvent.GetY();
mShader->SetUniform2f(
"u_MousePos",
mMousePos.x / Application::Get().GetWindow().GetHeight(),
mMousePos.x / Application::Get().GetWindow().GetWidth(),
-mMousePos.y / Application::Get().GetWindow().GetHeight()
);

Expand All @@ -77,7 +78,8 @@ namespace _2D
case EventType::MouseScrolled:
{
MouseScrolledEvent lMSEvent = pEvent.Cast<MouseScrolledEvent>();
mZoom = Math::Clamp(mZoom + lMSEvent.GetYOffset(), -10.f, 20.f);
mZoom = std::max(mZoom + lMSEvent.GetYOffset(), -10.f);
/* mZoom = Math::Clamp(mZoom + lMSEvent.GetYOffset(), -10.f, 20.f); */
pEvent.handled = true;
break;
}
Expand Down

0 comments on commit c1b86fd

Please sign in to comment.