Skip to content

Commit

Permalink
update and move some examples
Browse files Browse the repository at this point in the history
  • Loading branch information
szabolcsdombi committed Feb 19, 2024
1 parent f0bb29b commit f3d1b9a
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 135 deletions.
65 changes: 42 additions & 23 deletions examples/advanced/envmap.py → examples/basic/envmap.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,50 @@
import math
import os
import struct
import sys
import zipfile

import pygame
import zengl
from objloader import Obj
from PIL import Image
from meshtools import obj
from zengl_extras import assets

import assets
from window import Window
os.environ['SDL_WINDOWS_DPI_AWARENESS'] = 'permonitorv2'

pack = zipfile.ZipFile(assets.get('forest-panorama.zip'))
pygame.init()
pygame.display.set_mode((720, 720), flags=pygame.OPENGL | pygame.DOUBLEBUF, vsync=True)

window = Window()
ctx = zengl.context()

image = ctx.image(window.size, 'rgba8unorm', samples=4)
depth = ctx.image(window.size, 'depth24plus', samples=4)
image.clear_value = (0.2, 0.2, 0.2, 1.0)

img = Image.open(pack.open('forest.jpg')).convert('RGBA') # https://polyhaven.com/a/phalzer_forest_01
texture = ctx.image(img.size, 'rgba8unorm', img.tobytes())
def load_texture(buf):
img = pygame.image.load(buf)
pixels = pygame.image.tobytes(img, 'RGBA')
return ctx.image(img.get_size(), 'rgba8unorm', pixels)


def load_model(name):
with open(assets.get(name)) as f:
model = obj.parse_obj(f.read(), 'vn')
return ctx.buffer(model)

model = Obj.open(assets.get('blob.obj')).pack('vx vy vz nx ny nz')
vertex_buffer = ctx.buffer(model)

size = pygame.display.get_window_size()

image = ctx.image(size, 'rgba8unorm', samples=4)
depth = ctx.image(size, 'depth24plus', samples=4)

pack = zipfile.ZipFile(assets.get('forest-panorama.zip'))

texture = load_texture(pack.open('forest.jpg'))

vertex_buffer = load_model('blob.obj')

uniform_buffer = ctx.buffer(size=80)

pipeline = ctx.pipeline(
vertex_shader='''
#version 300 es
precision highp float;
#version 330 core
layout (std140) uniform Common {
mat4 mvp;
Expand All @@ -49,8 +64,7 @@
}
''',
fragment_shader='''
#version 300 es
precision highp float;
#version 330 core
layout (std140) uniform Common {
mat4 mvp;
Expand Down Expand Up @@ -105,13 +119,16 @@
vertex_count=vertex_buffer.size // zengl.calcsize('3f 3f'),
)

camera = zengl.camera((3.0, 2.0, 2.0), (0.0, 0.0, 0.5), aspect=window.aspect, fov=45.0)
uniform_buffer.write(camera)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()

while window.update():
ctx.new_frame()
x, y = math.sin(window.time * 0.5) * 5.0, math.cos(window.time * 0.5) * 5.0
camera = zengl.camera((x, y, 2.0), (0.0, 0.0, 0.0), aspect=window.aspect, fov=45.0)
now = pygame.time.get_ticks() / 1000.0

x, y = math.sin(now * 0.5) * 5.0, math.cos(now * 0.5) * 5.0
camera = zengl.camera((x, y, 2.0), (0.0, 0.0, 0.0), aspect=1.0, fov=45.0)
uniform_buffer.write(camera)
uniform_buffer.write(struct.pack('3f4x', x, y, 2.0), offset=64)

Expand All @@ -120,3 +137,5 @@
pipeline.render()
image.blit()
ctx.end_frame()

pygame.display.flip()
Original file line number Diff line number Diff line change
@@ -1,131 +1,107 @@
import os
import struct
import sys

import pygame
import zengl

from window import Window

window = Window()
ctx = zengl.context()

image = ctx.image(window.size, 'rgba8unorm')
uniform_buffer = ctx.buffer(size=64)

ctx.includes['hash11'] = '''
hash_functions = '''
float hash11(float p) {
p = fract(p * 0.1031);
p *= p + 33.33;
p *= p + p;
return fract(p);
}
'''
ctx.includes['hash12'] = '''
float hash12(vec2 p) {
vec3 p3 = fract(vec3(p.xyx) * 0.1031);
p3 += dot(p3, p3.yzx + 33.33);
return fract((p3.x + p3.y) * p3.z);
}
'''
ctx.includes['hash13'] = '''
float hash13(vec3 p3) {
p3 = fract(p3 * 0.1031);
p3 += dot(p3, p3.zyx + 31.32);
return fract((p3.x + p3.y) * p3.z);
}
'''
ctx.includes['hash21'] = '''
vec2 hash21(float p) {
vec3 p3 = fract(vec3(p) * vec3(0.1031, 0.1030, 0.0973));
p3 += dot(p3, p3.yzx + 33.33);
return fract((p3.xx + p3.yz) * p3.zy);
}
'''
ctx.includes['hash22'] = '''
vec2 hash22(vec2 p) {
vec3 p3 = fract(vec3(p.xyx) * vec3(0.1031, 0.1030, 0.0973));
p3 += dot(p3, p3.yzx + 33.33);
return fract((p3.xx + p3.yz) * p3.zy);
}
'''
ctx.includes['hash23'] = '''
vec2 hash23(vec3 p3) {
p3 = fract(p3 * vec3(0.1031, 0.1030, 0.0973));
p3 += dot(p3, p3.yzx + 33.33);
return fract((p3.xx + p3.yz) * p3.zy);
}
'''
ctx.includes['hash31'] = '''
vec3 hash31(float p) {
vec3 p3 = fract(vec3(p) * vec3(0.1031, 0.1030, 0.0973));
p3 += dot(p3, p3.yzx + 33.33);
return fract((p3.xxy + p3.yzz) * p3.zyx);
}
'''

ctx.includes['hash32'] = '''
vec3 hash32(vec2 p) {
vec3 p3 = fract(vec3(p.xyx) * vec3(0.1031, 0.1030, 0.0973));
p3 += dot(p3, p3.yxz + 33.33);
return fract((p3.xxy + p3.yzz) * p3.zyx);
}
'''
ctx.includes['hash33'] = '''
vec3 hash33(vec3 p3) {
p3 = fract(p3 * vec3(0.1031, 0.1030, 0.0973));
p3 += dot(p3, p3.yxz + 33.33);
return fract((p3.xxy + p3.yxx) * p3.zyx);
}
'''
ctx.includes['hash41'] = '''
vec4 hash41(float p) {
vec4 p4 = fract(vec4(p) * vec4(0.1031, 0.1030, 0.0973, 0.1099));
p4 += dot(p4, p4.wzxy + 33.33);
return fract((p4.xxyz + p4.yzzw) * p4.zywx);
}
'''
ctx.includes['hash42'] = '''
vec4 hash42(vec2 p) {
vec4 p4 = fract(vec4(p.xyxy) * vec4(0.1031, 0.1030, 0.0973, 0.1099));
p4 += dot(p4, p4.wzxy + 33.33);
return fract((p4.xxyz + p4.yzzw) * p4.zywx);
}
'''
ctx.includes['hash43'] = '''
vec4 hash43(vec3 p) {
vec4 p4 = fract(vec4(p.xyzx) * vec4(0.1031, 0.1030, 0.0973, 0.1099));
p4 += dot(p4, p4.wzxy + 33.33);
return fract((p4.xxyz + p4.yzzw) * p4.zywx);
}
'''
ctx.includes['hash44'] = '''
vec4 hash44(vec4 p4) {
p4 = fract(p4 * vec4(0.1031, 0.1030, 0.0973, 0.1099));
p4 += dot(p4, p4.wzxy + 33.33);
return fract((p4.xxyz + p4.yzzw) * p4.zywx);
}
'''

ctx.includes['common'] = '''
layout (std140) uniform Common {
float time;
};
'''
os.environ['SDL_WINDOWS_DPI_AWARENESS'] = 'permonitorv2'

pygame.init()
pygame.display.set_mode((1280, 720), flags=pygame.OPENGL | pygame.DOUBLEBUF, vsync=True)

ctx = zengl.context()

canvas = ctx.pipeline(
size = pygame.display.get_window_size()
image = ctx.image(size, 'rgba8unorm')

pipeline = ctx.pipeline(
includes={
'hash': hash_functions,
},
vertex_shader='''
#version 300 es
precision highp float;
#version 330 core
vec2 positions[3] = vec2[](
vec2(-1.0, -1.0),
Expand All @@ -138,11 +114,11 @@
}
''',
fragment_shader='''
#version 300 es
precision highp float;
#version 330 core
#include "hash"
#include "common"
#include "hash13"
uniform float time;
layout (location = 0) out vec4 out_color;
Expand All @@ -151,52 +127,26 @@
out_color = vec4(gray, gray, gray, 1.0);
}
''',
layout=[
{
'name': 'Common',
'binding': 0,
},
],
resources=[
{
'type': 'uniform_buffer',
'binding': 0,
'buffer': uniform_buffer,
},
],
uniforms={
'time': 0.0,
},
framebuffer=[image],
topology='triangles',
vertex_count=3,
)

while window.update():
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()

now = pygame.time.get_ticks() / 1000.0

ctx.new_frame()
image.clear()
uniform_buffer.write(struct.pack('f', window.time))
canvas.render()
pipeline.uniforms['time'][:] = struct.pack('f', now)
pipeline.render()
image.blit()
ctx.end_frame()

'''
MIT License
Copyright (c) 2014 David Hoskins
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
'''
pygame.display.flip()
Loading

0 comments on commit f3d1b9a

Please sign in to comment.