r/C_Programming • u/NazgulResebo • 14h ago
Video Coding a fractal Kaleidoscope using C & GLSL
https://youtu.be/zHu6XoFd1FYIn this video I create a classic kaleidoscope fractal animation using Raylib, C and GLSL.
I follow these steps:
- First, I split the screen into slices (like a pizza) using polar coordinates.
- Then, I create a well-known pattern in fractal shaders: sin(x) * sin(y), inside each slice.
- Next, I add a color palette to generate some nice colors.
- Finally, I use time as a parameter in different parts of the shader to animate both the pattern and the colors.
Source code:
https://github.com/FractalCodeRicardo/zen-programming/tree/main/kaleidoscope
C part
#include <raylib.h>
#define size 800
float time = 0.0;
int main() {
InitWindow(size, size, "Kaleidoscope");
Shader shader = LoadShader(0, "kaleidoscope.glsl");
int timeLoc = GetShaderLocation(shader, "time");
while(!WindowShouldClose()) {
time = GetTime();
SetShaderValue(shader, timeLoc, &time, SHADER_UNIFORM_FLOAT);
BeginDrawing();
ClearBackground(BLACK);
BeginShaderMode(shader);
DrawRectangle(0, 0, size, size, BLACK);
EndShaderMode();
EndDrawing();
}
CloseWindow();
}GLSL part
void main() {
float pi = 3.1416;
vec2 uv = gl_FragCoord.xy / 800;
uv = uv - 0.5;
float angle = atan(uv.y, uv.x) * time * 0.3;
float radius = length(uv);
float slices = 3;
float size = pi * 2 / 3;
float internal_angle = mod(angle, size);
internal_angle = abs(internal_angle - size /2.0);
vec2 internal_point= radius *
vec2(
cos(internal_angle) * radius,
sin(internal_angle) * radius
);
float pattern =
sin(internal_point.x * time *10 * 0.5) *
sin(internal_point.y * time *10 * 0.5);
vec3 c = palette(pattern + time * 0.5);
color = vec4(c, 1);
}AI Disclaimer:
I used AI to understand the fractal then I coded my own version
3
Upvotes
•
u/AutoModerator 14h ago
Hi /u/NazgulResebo,
Your submission in r/C_Programming was filtered because it links to a git project.
You must edit the submission or respond to this comment with an explanation about how AI was involved in the creation of your project.
While AI-generated code is not disallowed, low-effort "slop" projects may be removed and it's likely that other users push back strongly on substantially AI-generated projects.
You can read more at How do I post my Project?
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.