3D fractal, extending the Mandelbrot set into trippy, infinite detail via power-8 iterations. Rendered in real-time with ray marching, rotation, lighting & dynamic colors.
Grok 4 Heavy one-shotted the full C code gen!
>>> prompt
You are an expert C programmer and graphics specialist with deep knowledge of OpenGL, GLSL shaders, SDL2, and fractal rendering techniques. Your task is to generate complete, working code for a standalone C application that renders a rotating 3D Mandelbulb fractal in real-time using ray marching in a GLSL fragment shader. Follow the specifications exactly, ensuring the code is efficient, well-commented, and adheres to best practices for GPU-accelerated rendering.
To generate the code, think step-by-step:
1. Understand the requirements: The application uses C with SDL2 for window/input/context management, GLEW for OpenGL extensions, and OpenGL for rendering. Render the Mandelbulb via ray marching in the fragment shader on a full-screen quad. Include automatic Y-axis rotation, simple lighting with normals, and time-based dynamic coloring.
2. Structure the code:
* In mandelbulb.c: Handle SDL2 init, OpenGL context setup, shader compilation/linking, VAO/VBO for the quad, main loop with time uniform updates, input handling, and cleanup.
* In shader.vert: Simple pass-through vertex shader for the quad.
* In shader.frag: Implement ray marching with Mandelbulb distance estimator (power-8), normal calculation for lighting, iteration-based dynamic coloring modulated by time.
3. Key implementations:
* Mandelbulb formula: Use the standard triplex power-8 iteration in spherical coordinates.
* Ray marching: Loop with a maximum steps and distance threshold; compute normals via finite differences.
* Rotation: Apply a rotation matrix to the view direction based on time.
* Lighting: Basic Phong or Lambertian with a fixed light source.
* Coloring: HSV or similar, shifting hues with time and iterations.
* Add comments explaining each part, especially in the fragment shader.
4. Efficiency: Ensure shader is optimized for real-time (e.g., limit march steps to ~256, use uniforms for resolution/time).
5. Edge cases: Handle shader compilation errors, window resizing (optional), and smooth animation via SDL_GetTicks or similar.
6. Output format: Provide exactly three code blocks for the files, plus the compilation command. Use markdown code blocks with filenames.
Core Requirements:
1. Language & Libraries:
* Use C as the primary language.
* Use the SDL2 library to create the window, handle user input (like closing the window), and manage the OpenGL context.
* Use GLEW to manage modern OpenGL extensions.
* Use OpenGL for rendering.
2. Rendering Technique:
* The Mandelbulb must be rendered using a ray marching algorithm implemented entirely within a GLSL fragment shader.
* The C application's main role is to set up a window and draw a single rectangle (two triangles) that covers the entire screen, allowing the fragment shader to run for every pixel.
3. Visuals & Animation:
* Mandelbulb: Implement the distance estimator function for a standard power-8 Mandelbulb.
* Rotation: The Mandelbulb should rotate automatically around the Y-axis. This should be controlled by passing a time-based uniform from the C code to the shaders.
* Lighting & Shading: Implement simple lighting within the fragment shader by calculating the surface normal. This will give the fractal its 3D appearance.
* Coloring: The color of the fractal should be dynamic and shift over time, similar to the "psychedelic" effect in the video. The color should be calculated based on the number of iterations and the position, modulated by a time uniform.
Output Format: Provide the following three separate code blocks:
1. mandelbulb.c: The main C source file. It should handle all SDL2/OpenGL setup, the main application loop, shader compilation, and passing uniforms (like time and resolution) to the shaders.
2. shader.vert: The GLSL vertex shader. This can be a simple pass-through shader.
3. shader.frag: The GLSL fragment shader. This is where the core logic will be, including the ray marching loop, the Mandelbulb distance function, the normal calculation for lighting, and the dynamic coloring.
4. Compilation Command: Provide the gcc command needed to compile the C code, linking against SDL2, GLEW, GL, and m.
Add comments to the code, especially in the fragment shader, to explain the ray marching process and the Mandelbulb formula. Ensure the code is complete and can be compiled directly.
In watching the clip below of Danya's last stream and hearing the pain in his voice, how deeply the baseless accusations affected him, I am left so mad at what we all allowed to happen right in front of us.
It's time to stop worrying about the backlash. I am calling on the FIDE EDC to remove Vladimir Kramnik from the FIDE record books, revoke his title, and disqualify his world championship. Chess is one of the most beloved games in the world and should be a place for kindness and inclusion. It should be unilaterally unacceptable to use a platform that Chess has given you to bully, harass, and slander a colleague. What Kramnik has done to David Navara, Hikaru, and Danya, and others can not be tolerated.
It is up to our community and the governing body to set an example that this type of behavior will have consequences. Make it known loud and clear that Chess stands against these hateful acts. If you do them intentionally and maliciously there will be no place for you in our game.
FIDE and it's EDC must establish clear policies moving forward around the safety of it's players both in person and digitally. The events leading up to this tragedy happened in broad daylight over the last 18 months. They could have been stopped. Nothing like this can ever be allowed to happen again.
The Naroditsky family shares the sad news of Daniel’s unexpected passing. Daniel was a talented chess player, educator, and beloved member of the chess community. We ask for privacy as the family grieves.