grok-3-mini-high· xAI
LMArena Human Preference 140K
Create a beautiful logically accurate black hole with shaders, no external file needed…
- User
Create a beautiful logically accurate black hole with shaders, no external file needed
Use OpenGL + python (SINGLE FILE)
- Assistant
import sys import ctypes import time as Time from OpenGL.GL import * from OpenGL.GL.shaders import compileProgram, compileShader from OpenGL.GLUT import * # Global variables width = 800 height = 600 shaderProgram = None vao = None vbo = None start_time = 0.0 # Vertex shader source shader_vs = """ #version 330 core layout (location = 0) in vec2 position; void main() { gl_Position = vec4(position, 0.0, 1.0); } """ # Fragment shader source with raymarching for black hole and accretion disk shader_fs = """ #version 330 core out vec4 fragColor; uniform vec2 resolution; uniform float time; float distSphere(vec3 p) { return length(p) - 1.0; // Black hole event horizon radius 1.0 } float distTorus(vec3 p) { mat3 rot = mat3(cos(time), 0.0, sin(time), 0.0, 1.0, 0.0, -sin(time), 0.0, cos(time)); vec3 p_local = rot * p; // Rotate p to animate the accretion disk vec2 q = vec2(length(p_local.xz) - 2.0, p_local.y); return length(q) - 0.5; // Torus with major radius 2.0 and minor radius 0.5 } void main() { vec2 fragCoord = gl_FragCoord.xy / resolution.xy; // Normalize fragment coordinates to [0,1] float aspect = resolution.x / resolution.y; vec2 uv = (fragCoord * 2.0 - 1.0) * vec2(aspect, 1.0); // Map to screen space with aspect ratio vec3 rayOrigin = vec3(0.0, 0.0, 5.0); // Camera position vec3 rayDir = normalize(vec3(uv, -2.0)); // Ray direction with focal length 2.0 float t = 0.0; const int maxSteps = 100; const float maxDist = 20.0; const float eps = 0.001; for(int i = 0; i < maxSteps; i++) { vec3 p = rayOrigin + t * rayDir; float dSphere = distSphere(p); float dTorus = distTorus(p); float dScene = min(dSphere, dTorus); if(dScene < eps) { if(dSphere < dTorus) { // Hit black hole event horizon fragColor = vec4(0.0, 0.0, 0.0, 1.0); // Render black return; } else { // Hit accretion disk (torus) float epsN = 0.01; vec3 n = vec3( distTorus(p + vec3(epsN, 0.0, 0.0)) - distTorus(p - vec3(epsN, 0.0, 0.0)), distTorus(p + vec3(0.0, epsN, 0.0)) - distTorus(p - vec3(0.0, epsN, 0.0)), distTorus(p + vec3(0.0, 0.0, epsN)) - distTorus(p - vec3(0.0, 0.0, epsN)) ); n = normalize(n); // Compute normal using numerical differentiation // Simple directional lighting for beauty vec3 lightDir = normalize(vec3(1.0, 1.0, 1.0)); float diff = max(dot(n, lightDir), 0.0); vec3 color = vec3(0.5, 0.5, 0.0) * (diff + 0.2) + vec3(0.1, 0.1, 0.0) * sin(time + length(p) * 5.0); // Yellow with animated glow for beauty fragColor = vec4(color, 1.0); return; } } t += dScene; // March along the ray using the distance estimate if(t > maxDist) { break; } } // No hit, render background (dark space with subtle star-like effect) float star = step(0.95, fract(sin(dot(fragCoord.xy, vec2(12.9898, 78.233))) * 43758.5453)); // Procedural stars vec3 bgColor = vec3(0.0, 0.0, 0.0) + vec3(star) * 0.8; fragColor = vec4(bgColor, 1.0); } """ def init(): global shaderProgram, vao, vbo, start_time # Compile shaders vs = compileShader(shader_vs, GL_VERTEX_SHADER) fs = compileShader(shader_fs, GL_FRAGMENT_SHADER) shaderProgram = compileProgram(vs, fs) # Create full-screen quad vertices (two triangles) vertices = [-1.0, -1.0, 1.0, -1.0, -1.0, 1.0, 1.0, 1.0] vertices_array = (ctypes.c_float * 8)(*vertices) size = ctypes.sizeof(vertices_array) # Generate and bind VBO vbo = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, vbo) glBufferData(GL_ARRAY_BUFFER, size, vertices_array, GL_STATIC_DRAW) # Generate and bind VAO vao = glGenVertexArrays(1) glBindVertexArray(vao) glBindBuffer(GL_ARRAY_BUFFER, vbo) glEnableVertexAttribArray(0) glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, None) glBindVertexArray(0) # Set start time for animation start_time = Time.time() def display(): glClearColor(0.0, 0.0, 0.0, 1.0) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glUseProgram(shaderProgram) # Set uniforms res_loc = glGetUniformLocation(shaderProgram, "resolution") glUniform2f(res_loc, width, height) time_loc = glGetUniformLocation(shaderProgram, "time") current_time = Time.time() - start_time glUniform1f(time_loc, current_time) # Draw the full-screen quad glBindVertexArray(vao) glDrawArrays(GL_TRIANGLE_STRIP, 0, 4) glutSwapBuffers() def reshape(w, h): global width, height width = w height = h glViewport(0, 0, w, h) def idle(): glutPostRedisplay() def main(): glutInit(sys.argv) glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH) glutInitWindowSize(width, height) glutCreateWindow(b"Black Hole Simulation") glutDisplayFunc(display) glutIdleFunc(idle) glutReshapeFunc(reshape) init() # Initialize OpenGL resources glutMainLoop() if __name__ == "__main__": main()Explanation:
- Logically Accurate Black Hole: The black hole is represented by a sphere with radius 1.0 at the origin. Rays that intersect this sphere are rendered black, simulating the event horizon where light cannot escape.
- Accretion Disk: A torus (donut-shaped) object with major radius 2.0 and minor radius 0.5 is used to represent the accretion disk. It rotates over time for visual interest.
- Shaders: The vertex shader is simple and passes through vertex positions for a full-screen quad. The fragment shader uses raymarching to render the scene procedurally. It marches rays from the camera, checks for intersections with the black hole and accretion disk, and shades the disk with simple lighting and a subtle animated glow for beauty.
- Beauty Enhancements:
- The accretion disk has a yellow color with ambient and diffuse lighting, plus a sinusoidal glow effect based on time and position for a dynamic appearance.
- The background includes a procedural starfield effect using a noise function for added visual appeal.
- OpenGL and Python: This is a single-file script using PyOpenGL and GLUT for window and event handling. No external files or libraries beyond standard Python and PyOpenGL are needed.
- Animation: The accretion disk rotates with time, and the glow effect animates, creating a visually engaging simulation.
- Run Instructions: Save this script to a file (e.g.,
black_hole.py), ensure PyOpenGL and GLUT are installed (e.g., viapip install PyOpenGL PyOpenGL_accelerate), and run it with Python. It will open a window displaying the black hole simulation.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.