Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
f_color = texture(Texture, v_text);
}
''',
)
pixels = np.tile([0.0, 0.5, 1.0], 4 * 4).astype('f2')
self.tex = self.ctx.texture((4, 4), 3, pixels, dtype='f2')
self.vbo = self.ctx.buffer(np.array([-1, -1, -1, 1, 1, -1, 1, 1], dtype='f4'))
self.vao = self.ctx.simple_vertex_array(self.prog, self.vbo, 'in_vert')
self.fbo = self.ctx.simple_framebuffer((4, 4))
self.fbo.use()
self.fbo.clear()
self.tex.use()
self.vao.render(moderngl.TRIANGLE_STRIP)
res = np.frombuffer(self.fbo.read(dtype='f2'), dtype='f2')
npt.assert_almost_equal(res, pixels, decimal=2)
def test_1(self):
fbo = self.ctx.framebuffer(self.ctx.renderbuffer((16, 16)))
pixels = struct.pack('16B', 255, 0, 0, 255, 0, 255, 0, 255, 0, 0, 255, 255, 0, 0, 0, 255)
texture = self.ctx.texture((2, 2), 4, pixels)
fbo.use()
texture.use()
texture.filter = (moderngl.LINEAR, moderngl.LINEAR)
self.vao.render(moderngl.TRIANGLE_STRIP)
r, g, b = struct.unpack('3B', fbo.read((8, 8, 1, 1)))
self.assertTrue(abs(r - 49) < 8 and abs(g - 63) < 8 and abs(b - 63) < 8)
texture.filter = (moderngl.NEAREST, moderngl.NEAREST)
self.vao.render(moderngl.TRIANGLE_STRIP)
r, g, b = struct.unpack('3B', fbo.read((6, 6, 1, 1)))
self.assertTrue(abs(r - 255) < 8 and abs(g - 0) < 8 and abs(b - 0) < 8)
r, g, b = struct.unpack('3B', fbo.read((9, 6, 1, 1)))
self.assertTrue(abs(r - 0) < 8 and abs(g - 255) < 8 and abs(b - 0) < 8)
r, g, b = struct.unpack('3B', fbo.read((6, 9, 1, 1)))
self.assertTrue(abs(r - 0) < 8 and abs(g - 0) < 8 and abs(b - 255) < 8)
fbo.use()
texture.use()
texture.write(pbo1)
texture.filter = (moderngl.NEAREST, moderngl.NEAREST)
self.vao.render(moderngl.TRIANGLE_STRIP)
r, g, b = struct.unpack('3B', fbo.read((5, 7, 1, 1)))
self.assertTrue(abs(r - 255) < 8 and abs(g - 0) < 8 and abs(b - 0) < 8)
r, g, b = struct.unpack('3B', fbo.read((10, 7, 1, 1)))
self.assertTrue(abs(r - 0) < 8 and abs(g - 255) < 8 and abs(b - 0) < 8)
texture.write(pbo2)
texture.filter = (moderngl.NEAREST, moderngl.NEAREST)
self.vao.render(moderngl.TRIANGLE_STRIP)
r, g, b = struct.unpack('3B', fbo.read((5, 7, 1, 1)))
self.assertTrue(abs(r - 20) < 8 and abs(g - 30) < 8 and abs(b - 40) < 8)
r, g, b = struct.unpack('3B', fbo.read((10, 7, 1, 1)))
self.assertTrue(abs(r - 80) < 8 and abs(g - 70) < 8 and abs(b - 60) < 8)
from typing import List
import numpy
import moderngl
from demosys import context
from demosys.opengl import types
# For sanity checking draw modes when creating the VAO
DRAW_MODES = {
moderngl.TRIANGLES: 'TRIANGLES',
moderngl.TRIANGLE_FAN: 'TRIANGLE_FAN',
moderngl.TRIANGLE_STRIP: 'TRIANGLE_STRIP',
moderngl.TRIANGLES_ADJACENCY: 'TRIANGLES_ADJACENCY',
moderngl.TRIANGLE_STRIP_ADJACENCY: 'TRIANGLE_STRIP_ADJACENCY',
moderngl.POINTS: 'POINTS',
moderngl.LINES: 'LINES',
moderngl.LINE_STRIP: 'LINE_STRIP',
moderngl.LINE_LOOP: 'LINE_LOOP',
moderngl.LINES_ADJACENCY: 'LINES_ADJACENCY',
}
class BufferInfo:
"""Container for a vbo with additional information"""
def __init__(self, buffer: moderngl.Buffer, buffer_format: str, attributes=None, per_instance=False):
"""
:param buffer: The vbo object
:param format: The format of the buffer
angle = time * 0.2
self.ctx.clear(1.0, 1.0, 1.0)
self.ctx.enable(moderngl.DEPTH_TEST)
self.ctx.wireframe = True
proj = Matrix44.perspective_projection(45.0, self.aspect_ratio, 0.1, 1000.0)
lookat = Matrix44.look_at(
(np.cos(angle), np.sin(angle), 0.8),
(0.0, 0.0, 0.1),
(0.0, 0.0, 1.0),
)
self.texture.use()
self.mvp.write((proj * lookat).astype('f4').tobytes())
self.vao.render(moderngl.TRIANGLE_STRIP)
for i in range(10):
self.space.step(1 / 60 / 10)
self.prog['Camera'].value = (200, 300, width / 2, height / 2)
bodies = np.array([(b.position.x, b.position.y, b.angle, 10, 10, 1, 1, 1, 0) for b in self.bodies], dtype='f4')
self.vbo2.write(bodies.tobytes())
self.prog['Texture'].value = 0
self.vao.render(moderngl.TRIANGLE_STRIP, instances=len(self.bodies))
self.vbo2.orphan()
balls = np.array([(b.position.x, b.position.y, b.angle, 15, 15, 1, 1, 1, 0) for b in self.balls], dtype='f4')
self.vbo2.write(balls.tobytes())
self.prog['Texture'].value = 1
self.vao.render(moderngl.TRIANGLE_STRIP, instances=len(self.balls))
def render(self, time, frame_time):
width, height = self.wnd.size
self.ctx.clear(1.0, 1.0, 1.0)
self.ctx.enable(moderngl.BLEND)
for i in range(10):
self.space.step(1 / 60 / 10)
self.prog['Camera'].value = (200, 300, width / 2, height / 2)
bodies = np.array([(b.position.x, b.position.y, b.angle, 10, 10, 1, 1, 1, 0) for b in self.bodies], dtype='f4')
self.vbo2.write(bodies.tobytes())
self.prog['Texture'].value = 0
self.vao.render(moderngl.TRIANGLE_STRIP, instances=len(self.bodies))
self.vbo2.orphan()
balls = np.array([(b.position.x, b.position.y, b.angle, 15, 15, 1, 1, 1, 0) for b in self.balls], dtype='f4')
self.vbo2.write(balls.tobytes())
self.prog['Texture'].value = 1
self.vao.render(moderngl.TRIANGLE_STRIP, instances=len(self.balls))
def render(self, time, frame_time):
self.ctx.clear(1.0, 1.0, 1.0)
self.center.value = (0.5, 0.0)
self.iter.value = 100
self.scale.value = 1.5
self.ratio.value = self.aspect_ratio
self.texture.use()
self.vao.render(moderngl.TRIANGLE_STRIP)
self.ctx.enable_only(moderngl.DEPTH_TEST)
proj = Matrix44.perspective_projection(30.0, self.aspect_ratio, 1.0, 1000.0)
lookat = Matrix44.look_at(
(46.748, -280.619, 154.391),
(-23.844, 2.698, 44.493),
(0.0, 0.0, 1.0),
)
self.mvp.write((proj * lookat).astype('f4').tobytes())
self.light.value = (-143.438, -159.072, 213.268)
self.mug_texture.use()
self.mug_vao.render()
self.ctx.enable_only(moderngl.DEPTH_TEST | moderngl.BLEND)
self.sticker_texture.use()
self.sticker_vao.render(moderngl.TRIANGLE_STRIP)
float bs = (sin(b*9999*exp(-b))+1.0)/2.0;
float c = as * bs;
f_color = vec3(c, c, c);
}
""",
)
vertices = np.array([1.0, 1.0, -1.0, 1.0, -1.0, -1.0, -1.0, -1.0, 1.0, -1.0, 1.0, 1.0])
vbo = ctx.buffer(vertices.astype("f4").tobytes())
vao = ctx.simple_vertex_array(prog, vbo, "in_vert")
fbo = ctx.simple_framebuffer((image.shape[0], image.shape[1]))
fbo.use()
fbo.clear(0.0, 0.0, 0.0, 1.0)
vao.render(moderngl.TRIANGLE_STRIP)
res = numpy.frombuffer(fbo.read(), dtype=np.uint8).reshape((*fbo.size, 3)) / 255.0
image[:, :, 0] = res[:, :, 0]
image[:, :, 1] = res[:, :, 1]
image[:, :, 2] = res[:, :, 2]
return image