Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def draw(self, time, frametime, target):
self.ctx.disable(mgl.DEPTH_TEST)
self.ctx.enable(mgl.BLEND)
self.ctx.blend_func = mgl.SRC_ALPHA, mgl.ONE_MINUS_SRC_ALPHA
m_proj = self.create_projection(90.0, 1.0, 1000.0)
# Rotate and translate
m_mv = self.create_transformation(rotation=(time * 0.0, time * 0, time * 0),
translation=(0.0, 0.0, -40.0))
# Apply the rotation and translation from the system camera
m_mv = matrix44.multiply(m_mv, self.sys_camera.view_matrix)
gravity_pos = vector3.create(math.sin(time) * 5,
math.cos(time) * 5,
math.sin(time / 3) * 5)
gravity_force = math.cos(time / 2) * 3.0 + 3.0
def render(self, time: float, frame_time: float):
self.ctx.clear(1.0, 1.0, 1.0)
self.ctx.enable(moderngl.DEPTH_TEST | moderngl.CULL_FACE)
proj = Matrix44.perspective_projection(45.0, self.aspect_ratio, 0.1, 1000.0)
lookat = Matrix44.look_at(
(47.697, -8.147, 24.498),
(0.0, 0.0, 8.0),
(0.0, 0.0, 1.0),
)
rotate = Matrix44.from_z_rotation(np.sin(time) * 0.5 + 0.2)
self.use_texture.value = False
self.light.value = (67.69, -8.14, 52.49)
self.mvp.write((proj * lookat * rotate).astype('f4').tobytes())
self.color.value = (0.67, 0.49, 0.29)
def draw_buffers(self, near, far):
"""
Draw framebuffers for debug purposes.
We need to supply near and far plane so the depth buffer can be linearized when visualizing.
:param near: Projection near value
:param far: Projection far value
"""
self.ctx.disable(moderngl.DEPTH_TEST)
texture.draw(self.gbuffer.color_attachments[0], pos=(0.0, 0.0), scale=(0.25, 0.25))
texture.draw(self.gbuffer.color_attachments[1], pos=(0.5, 0.0), scale=(0.25, 0.25))
texture.draw_depth(self.gbuffer.depth_attachment, near, far, pos=(1.0, 0.0), scale=(0.25, 0.25))
texture.draw(self.lightbuffer.color_attachments[0], pos=(1.5, 0.0), scale=(0.25, 0.25))
def draw_buffers(self, near, far):
"""
Draw framebuffers for debug purposes.
We need to supply near and far plane so the depth buffer can be linearized when visualizing.
:param near: Projection near value
:param far: Projection far value
"""
self.ctx.disable(moderngl.DEPTH_TEST)
helper.draw(self.gbuffer.color_attachments[0], pos=(0.0, 0.0), scale=(0.25, 0.25))
helper.draw(self.gbuffer.color_attachments[1], pos=(0.5, 0.0), scale=(0.25, 0.25))
helper.draw_depth(self.gbuffer.depth_attachment, near, far, pos=(1.0, 0.0), scale=(0.25, 0.25))
helper.draw(self.lightbuffer.color_attachments[0], pos=(1.5, 0.0), scale=(0.25, 0.25))
def render(self, time, frame_time):
angle = time * 0.2
self.ctx.clear(1.0, 1.0, 1.0)
self.ctx.enable(moderngl.DEPTH_TEST)
camera_pos = (np.cos(angle) * 5.0, np.sin(angle) * 5.0, 2.0)
proj = Matrix44.perspective_projection(45.0, self.aspect_ratio, 0.1, 1000.0)
lookat = Matrix44.look_at(
camera_pos,
(0.0, 0.0, 0.5),
(0.0, 0.0, 1.0),
)
self.mvp.write((proj * lookat).astype('f4').tobytes())
self.light.value = camera_pos
crate_z = np.sin(self.crate_a * time + self.crate_b) * 0.2
coordinates = np.dstack([self.crate_x, self.crate_y, crate_z])
self.point_lights = []
depth_texture = self.ctx.depth_texture(self.size)
if not self.gbuffer:
self.gbuffer = self.ctx.framebuffer(
(
self.ctx.texture(self.size, 4, dtype='f1'),
self.ctx.texture(self.size, 3, dtype='f2'),
),
depth_attachment=depth_texture,
)
self.gbuffer_scope = self.ctx.scope(
self.gbuffer,
enable_only=moderngl.DEPTH_TEST | moderngl.CULL_FACE
)
if not self.lightbuffer:
self.lightbuffer = self.ctx.framebuffer(
self.ctx.texture(self.size, 4),
)
self.lightbuffer_scope = self.ctx.scope(
self.lightbuffer,
enable_only=moderngl.BLEND | moderngl.CULL_FACE
)
# Unit cube for point lights (cube with radius 1.0)
self.unit_cube = geometry.cube(width=2, height=2, depth=2)
self.point_light_shader = self.get_program("demosys.deferred.point_light")
def draw(self, time, frametime, target):
self.ctx.enable(mgl.DEPTH_TEST)
self.ctx.enable(mgl.CULL_FACE)
mv_m = self.create_transformation(rotation=(time * 1.2, time * 2.1, time * 0.25),
translation=(0.0, 0.0, -8.0))
normal_m = self.create_normal_matrix(mv_m)
proj_m = self.create_projection(fov=60.0, aspect_ratio=1.0)
self.fbo.use()
self.cube_prog1["m_proj"].write(proj_m.astype('f4').tobytes())
self.cube_prog1["m_mv"].write(mv_m.astype('f4').tobytes())
self.cube_prog1["m_normal"].write(normal_m.astype('f4').tobytes())
self.texture1.use(location=0)
self.texture2.use(location=1)
self.cube_prog1["texture0"].value = 0
self.cube_prog1["texture1"].value = 1
# Render the scene to offscreen buffer
self.offscreen.clear()
self.offscreen.use()
# Render the scene
self.geometry_program['modelview'].write(self.modelview)
self.geometry_program['projection'].write(self.projection.matrix)
self.mesh_texture.use(location=0) # bind texture from obj file to channel 0
self.depth_sampler.use(location=0)
self.mesh.render(self.geometry_program) # render mesh
self.depth_sampler.clear(location=0)
# Activate the window as the render target
self.ctx.screen.use()
self.ctx.disable(moderngl.DEPTH_TEST)
# Render offscreen diffuse layer to screen
self.offscreen_diffuse.use(location=0)
self.quad_fs.render(self.texture_program)
# Render markers
if self.num_markers > 0:
self.ctx.point_size = 6.0 # Specify fragment size of the markers
self.marker_program['modelview'].write(self.modelview)
self.marker_program['projection'].write(self.projection.matrix)
self.marker_vao.render(self.marker_program, vertices=self.num_markers)
self.render_debug()
def render(self, time, frame_time):
self.ctx.clear(1.0, 1.0, 1.0)
self.bg_texture.use()
self.ctx.enable_only(moderngl.BLEND)
self.canvas_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)