Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the zooming in the Julia set shader example. #3466

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions examples/shaders/resources/shaders/glsl100/julia_set.fs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ uniform float zoom; // Zoom of the scale.

// NOTE: Maximum number of shader for-loop iterations depend on GPU,
// for example, on RasperryPi for this examply only supports up to 60
const int MAX_ITERATIONS = 48; // Max iterations to do
const int MAX_ITERATIONS = 48; // Max iterations to do.

const float COLOR_CYCLES = 1; // Number of times the palette repeats.

// Square a complex number
vec2 ComplexSquare(vec2 z)
Expand Down Expand Up @@ -79,5 +81,5 @@ void main()

// If in set, color black. 0.999 allows for some float accuracy error.
if (norm > 0.999) gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
else gl_FragColor = vec4(Hsv2rgb(vec3(norm, 1.0, 1.0)), 1.0);
else gl_FragColor = vec4(Hsv2rgb(vec3(norm * COLOR_CYCLES, 1.0, 1.0)), 1.0);
}
5 changes: 3 additions & 2 deletions examples/shaders/resources/shaders/glsl330/julia_set.fs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ uniform vec2 c; // c.x = real, c.y = imaginary component. Equati
uniform vec2 offset; // Offset of the scale.
uniform float zoom; // Zoom of the scale.

const int MAX_ITERATIONS = 255; // Max iterations to do.
const int MAX_ITERATIONS = 255; // Max iterations to do.
const float COLOR_CYCLES = 2; // Number of times the palette repeats. Can show higher detail for higher iteration numbers.

// Square a complex number
vec2 ComplexSquare(vec2 z)
Expand Down Expand Up @@ -77,5 +78,5 @@ void main()

// If in set, color black. 0.999 allows for some float accuracy error.
if (norm > 0.999) finalColor = vec4(0.0, 0.0, 0.0, 1.0);
else finalColor = vec4(Hsv2rgb(vec3(norm, 1.0, 1.0)), 1.0);
else finalColor = vec4(Hsv2rgb(vec3(norm * COLOR_CYCLES, 1.0, 1.0)), 1.0);
}
60 changes: 26 additions & 34 deletions examples/shaders/shaders_julia_set.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
*
* Example originally created with raylib 2.5, last time updated with raylib 4.0
*
* Example contributed by eggmund (@eggmund) and reviewed by Ramon Santamaria (@raysan5)
* Example contributed by Josh Colclough (@joshcol9232) and reviewed by Ramon Santamaria (@raysan5)
*
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
* BSD-like license that allows static linking with closed source software
*
* Copyright (c) 2019-2023 eggmund (@eggmund) and Ramon Santamaria (@raysan5)
* Copyright (c) 2019-2023 Josh Colclough (@joshcol9232) and Ramon Santamaria (@raysan5)
*
********************************************************************************************/

Expand Down Expand Up @@ -46,8 +46,8 @@ int main(void)
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
const float zoomSpeed = 1.005f;

//SetConfigFlags(FLAG_WINDOW_HIGHDPI);
InitWindow(screenWidth, screenHeight, "raylib [shaders] example - julia sets");

// Load julia set shader
Expand All @@ -64,8 +64,6 @@ int main(void)
float offset[2] = { -(float)GetScreenWidth()/2, -(float)GetScreenHeight()/2 };
float zoom = 1.0f;

Vector2 offsetSpeed = { 0.0f, 0.0f };

// Get variable (uniform) locations on the shader to connect with the program
// NOTE: If uniform variable could not be found in the shader, function returns -1
int cLoc = GetShaderLocation(shader, "c");
Expand Down Expand Up @@ -93,54 +91,48 @@ int main(void)
// Update
//----------------------------------------------------------------------------------
// Press [1 - 6] to reset c to a point of interest
if (IsKeyPressed(KEY_ONE) ||
IsKeyPressed(KEY_TWO) ||
IsKeyPressed(KEY_THREE) ||
IsKeyPressed(KEY_FOUR) ||
IsKeyPressed(KEY_FIVE) ||
IsKeyPressed(KEY_SIX))
for (KeyboardKey k = KEY_ONE; k <= KEY_SIX; ++k)
{
if (IsKeyPressed(KEY_ONE)) c[0] = pointsOfInterest[0][0], c[1] = pointsOfInterest[0][1];
else if (IsKeyPressed(KEY_TWO)) c[0] = pointsOfInterest[1][0], c[1] = pointsOfInterest[1][1];
else if (IsKeyPressed(KEY_THREE)) c[0] = pointsOfInterest[2][0], c[1] = pointsOfInterest[2][1];
else if (IsKeyPressed(KEY_FOUR)) c[0] = pointsOfInterest[3][0], c[1] = pointsOfInterest[3][1];
else if (IsKeyPressed(KEY_FIVE)) c[0] = pointsOfInterest[4][0], c[1] = pointsOfInterest[4][1];
else if (IsKeyPressed(KEY_SIX)) c[0] = pointsOfInterest[5][0], c[1] = pointsOfInterest[5][1];

SetShaderValue(shader, cLoc, c, SHADER_UNIFORM_VEC2);
if (IsKeyPressed(k))
{
// If this key is pressed, set the point of interest of the corresponding key.
c[0] = pointsOfInterest[k - KEY_ONE][0];
c[1] = pointsOfInterest[k - KEY_ONE][1];
SetShaderValue(shader, cLoc, c, SHADER_UNIFORM_VEC2);
break;
}
}

if (IsKeyPressed(KEY_SPACE)) pause = !pause; // Pause animation (c change)
if (IsKeyPressed(KEY_SPACE)) pause = !pause; // Pause animation (c change)
if (IsKeyPressed(KEY_F1)) showControls = !showControls; // Toggle whether or not to show controls

if (!pause)
{
if (IsKeyPressed(KEY_RIGHT)) incrementSpeed++;
else if (IsKeyPressed(KEY_LEFT)) incrementSpeed--;

// TODO: The idea is to zoom and move around with mouse
// Probably offset movement should be proportional to zoom level
if (IsMouseButtonDown(MOUSE_BUTTON_LEFT) || IsMouseButtonDown(MOUSE_BUTTON_RIGHT))
// If either left or right button is pressed, zoom in/out.
const bool leftMouseButtonDown = IsMouseButtonDown(MOUSE_BUTTON_LEFT);
if (leftMouseButtonDown ^ IsMouseButtonDown(MOUSE_BUTTON_RIGHT))
{
if (IsMouseButtonDown(MOUSE_BUTTON_LEFT)) zoom += zoom*0.003f;
if (IsMouseButtonDown(MOUSE_BUTTON_RIGHT)) zoom -= zoom*0.003f;

Vector2 mousePos = GetMousePosition();
// Change zoom. If Mouse left -> zoom in. Mouse right -> zoom out.
zoom *= leftMouseButtonDown ? zoomSpeed : 1.0f / zoomSpeed;

offsetSpeed.x = mousePos.x -(float)screenWidth/2;
offsetSpeed.y = mousePos.y -(float)screenHeight/2;
const Vector2 mousePos = GetMousePosition();
Vector2 offsetVelocity;
offsetVelocity.x = zoom * (mousePos.x - (float)screenWidth / 2.0);
offsetVelocity.y = zoom * (mousePos.y - (float)screenHeight / 2.0);

// Slowly move camera to targetOffset
offset[0] += GetFrameTime()*offsetSpeed.x*0.8f;
offset[1] += GetFrameTime()*offsetSpeed.y*0.8f;
// Apply move velocity to camera
offset[0] += GetFrameTime() * offsetVelocity.x;
offset[1] += GetFrameTime() * offsetVelocity.y;
}
else offsetSpeed = (Vector2){ 0.0f, 0.0f };

SetShaderValue(shader, zoomLoc, &zoom, SHADER_UNIFORM_FLOAT);
SetShaderValue(shader, offsetLoc, offset, SHADER_UNIFORM_VEC2);

// Increment c value with time
float amount = GetFrameTime()*incrementSpeed*0.0005f;
float amount = GetFrameTime() * incrementSpeed * 0.0005f;
c[0] += amount;
c[1] += amount;

Expand Down