Implement zoom and panning (scale and translation) via arrow keys, and plus/minus. Currently a little janky, but working on proper scale factor and translation offset.

This commit is contained in:
2025-04-23 01:33:51 -05:00
parent dd5c68342b
commit 9126593e51
2 changed files with 61 additions and 3 deletions

View File

@@ -137,6 +137,8 @@ public:
JGL::Shader shader;
JGL::RenderTarget *canvas;
float u_time;
float u_scale = 2.f;
Vector2 u_translation {0.4f, 0.f};
Color4 u_rgb_1 = Colors::Greens::Chartreuse;
Color4 u_rgb_2 = Colors::Reds::LightCoral;
std::string frag_name;
@@ -321,6 +323,31 @@ public:
void Update(float elapsed) {
scene->Update(elapsed);
if (IsKeyDown(Keys::F6)) {
u_scale = 2.f;
u_translation = {.4, 0};
}
// TODO: Vary the rate appropriately.
float rate = elapsed * 0.125f;
if (IsKeyDown(Keys::Minus))
u_scale += rate;
if (IsKeyDown(Keys::Equals))
u_scale -= rate;
if (IsKeyDown(Keys::DownArrow))
u_translation.y += rate;
if (IsKeyDown(Keys::UpArrow))
u_translation.y -= rate;
if (IsKeyDown(Keys::LeftArrow))
u_translation.x += rate;
if (IsKeyDown(Keys::RightArrow))
u_translation.x -= rate;
u_time += elapsed;
Vector2 u_res = Vector2(GetSize().x, GetSize().y);
@@ -333,6 +360,8 @@ public:
shader.SetVector2("u_mouse", u_mouse);
shader.SetVector3("u_rgb_1", Vector3(u_rgb_1.RN(), u_rgb_1.GN(), u_rgb_1.BN()));
shader.SetVector3("u_rgb_2", Vector3(u_rgb_2.RN(), u_rgb_2.GN(), u_rgb_2.BN()));
shader.SetFloat("u_scale", u_scale);
shader.SetVector2("u_translation", u_translation);
}
void Draw() {

View File

@@ -1,7 +1,7 @@
#version 120
#ifdef GL_ES
precision mediump float;
precision highp float;
#endif
#define PI 3.14159265359
@@ -11,7 +11,7 @@ uniform vec2 u_mouse;
uniform float u_time;
uniform vec2 u_translation;
uniform float u_zoom;
uniform float u_scale;
uniform vec3 u_rgb_1;
uniform vec3 u_rgb_2;
@@ -24,6 +24,28 @@ vec3 pal(in float t, in vec3 a, in vec3 b, in vec3 c, in vec3 d) {
return a + b*cos(6.28318*(c*t+d));
}
// The mandelbrot set is a set of complex numbers c for which the function:
// f(z) = z*z + c
// stays bounded between a certain range of values when iterated from z = 0.
#define MAX_ITERATIONS 100.
vec2 square_imaginary(vec2 number) {
return vec2(
pow(number.x, 2) - pow(number.y, 2),
2*number.x*number.y
);
}
float iterate_mandelbrot(vec2 coord) {
vec2 z = vec2(0,0);
for (int i = 0; i < MAX_ITERATIONS; i++) {
z = square_imaginary(z) + coord;
if (length(z) > 2) return i / MAX_ITERATIONS;
}
return MAX_ITERATIONS;
}
float iterate(vec2 p) {
vec2 z = vec2(0), c = p;
float i;
@@ -41,7 +63,14 @@ float iterate(vec2 p) {
void main() {
vec2 R = u_resolution.xy;
vec2 uv = 1.2 * (2. * gl_FragCoord.xy - R - 1.) / R.y - vec2(.4, 0.);
//float translate_h = .4;
//float translate_v = 0.0;
//float scale = 2.;
//vec2 translation = vec2(translate_h, translate_v);
vec2 uv = (u_scale * gl_FragCoord.xy - R - 1.) / R.y - u_translation;
//vec2 z = vec2(0), c = uv;
//float i;