Skip to content

Commit

Permalink
headless examples and fractals
Browse files Browse the repository at this point in the history
  • Loading branch information
szabolcsdombi committed Jan 30, 2024
1 parent 76a6f5c commit eff18cd
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 10 deletions.
18 changes: 13 additions & 5 deletions examples/basic/fractal.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@
layout (location = 0) out vec4 out_color;
void main() {
vec2 z = vertex * 1.75;
vec2 c = vec2(0.4, 0.1);
float mandelbrot(vec2 uv) {
vec2 z = vec2(0.0, 0.0);
vec2 c = uv;
int i;
for (i = 0; i < 100; ++i) {
vec2 v = vec2(
Expand All @@ -47,8 +47,16 @@
if (dot(v, v) > 4.0) break;
z = v;
}
float cm = fract((i == 100 ? 0.0 : float(i)) * 10.0 / 100.0);
out_color = vec4(fract(cm + 0.0 / 3.0), fract(cm + 1.0 / 3.0), fract(cm + 2.0 / 3.0), 1.0);
if (i == 100) {
i = 0;
}
return float(i) / 100.0;
}
void main() {
float cm = mandelbrot(vertex * 1.75 - vec2(0.5, 0.0));
vec3 color = vec3(0.2 + cm * 0.6, 0.4 + cm * 0.5, 1.0);
out_color = vec4(color * cm, 1.0);
}
''',
framebuffer=[image],
Expand Down
75 changes: 75 additions & 0 deletions examples/headless/headless_fractal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import os

import zengl
from PIL import Image

zengl.init(zengl.loader(headless=True))

ctx = zengl.context()

image = ctx.image((8192, 8192), 'rgba8unorm', texture=False)

pipeline = ctx.pipeline(
vertex_shader='''
#version 300 es
precision highp float;
vec2 vertices[3] = vec2[](
vec2(-1.0, -1.0),
vec2(3.0, -1.0),
vec2(-1.0, 3.0)
);
out vec2 vertex;
void main() {
gl_Position = vec4(vertices[gl_VertexID], 0.0, 1.0);
vertex = vertices[gl_VertexID];
}
''',
fragment_shader='''
#version 300 es
precision highp float;
in vec2 vertex;
layout (location = 0) out vec4 out_color;
float mandelbrot(vec2 uv) {
vec2 z = vec2(0.0, 0.0);
vec2 c = uv;
int i;
for (i = 0; i < 100; ++i) {
vec2 v = vec2(
(z.x * z.x - z.y * z.y) + c.x,
(z.y * z.x + z.x * z.y) + c.y
);
if (dot(v, v) > 4.0) break;
z = v;
}
if (i == 100) {
i = 0;
}
return float(i) / 100.0;
}
void main() {
float cm = mandelbrot(vertex * 1.75 - vec2(0.5, 0.0));
vec3 color = vec3(0.2 + cm * 0.6, 0.4 + cm * 0.5, 1.0);
out_color = vec4(color * cm, 1.0);
}
''',
framebuffer=[image],
topology='triangles',
vertex_count=3,
)

ctx.new_frame()
image.clear()
pipeline.render()
ctx.end_frame()

img = Image.frombuffer('RGBA', image.size, image.read(), 'raw', 'RGBA', 0, -1)
img.save('fractal.png')

print('Saved:', os.path.abspath('fractal.png'))
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import os

import zengl
from PIL import Image

ctx = zengl.context(zengl.loader(headless=True))
zengl.init(zengl.loader(headless=True))

ctx = zengl.context()

size = (1280, 720)
image = ctx.image(size, 'rgba8unorm', samples=4)
image.clear_value = (0.05, 0.05, 0.05, 1.0)
image = ctx.image((1280, 720), 'rgba8unorm', samples=4)

pipeline = ctx.pipeline(
vertex_shader='''
Expand Down Expand Up @@ -53,5 +55,6 @@
pipeline.render()
ctx.end_frame()

img = Image.frombuffer('RGBA', size, image.read(), 'raw', 'RGBA', 0, -1)
img = Image.frombuffer('RGBA', image.size, image.read(), 'raw', 'RGBA', 0, -1)
img.save('hello.png')
print('Created:', os.path.abspath('hello.png'))

0 comments on commit eff18cd

Please sign in to comment.