Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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.ctx.enable(moderngl.BLEND)
self.ctx.blend_func = moderngl.ONE, moderngl.ONE
self.drops_texture.use()
self.drop_program['pos'].value = self.mouse_pos
self.sprite.render(self.drop_program)
self.ctx.disable(moderngl.BLEND)
# HACK: Just draw 100 new drops per frame (copy paste from above)
# This is pretty terrible and slow!
self.ctx.enable(moderngl.BLEND)
self.ctx.blend_func = moderngl.ONE, moderngl.ONE
self.drops_texture.use()
for i in range(10):
self.drop_program['pos'].value = random.random() * 2 - 1.0, random.random() * 2 - 1
self.sprite.render(self.drop_program)
self.ctx.disable(moderngl.BLEND)
self.fbo_1.use()
# Process the water
self.texture_2.use(location=0)
self.texture_1.use(location=1)
self.quad_fs.render(self.wave_program)
# Render the result to the screen.
# We can blit only when the texture format matches the default framebuffer
self.wnd.fbo.use()
self.texture_1.use()
self.quad_fs.render(self.texture_program)
# Swap texture and fbo
self.texture_1, self.texture_2 = self.texture_2, self.texture_1
def render(self, time, frametime):
# Render background
self.ctx.wireframe = False
if not self.with_blending:
self.ctx.enable_only(moderngl.NOTHING)
self.quad_fs.render(self.prog_background)
# Handle blend mode toggle
if self.with_blending:
self.ctx.enable_only(moderngl.BLEND)
self.ctx.blend_func = moderngl.ONE, moderngl.ONE
else:
self.ctx.enable_only(moderngl.DEPTH_TEST | moderngl.CULL_FACE)
# Render tetrahedral mesh
translate = Matrix44.from_translation((0.0, 2.5, -15.0), dtype='f4')
rotate = Matrix44.from_eulers((np.radians(180), 0, 0), dtype='f4')
scale = Matrix44.from_scale((400, 400, 400), dtype='f4')
mat = self.camera.matrix * translate * rotate * scale
# All render calls inside this context are timed
with self.query:
self.alive_texture.use(location=0)
self.prog_gen_tetra['alive_texture'].value = 0
self.prog_gen_tetra['threshold'].value = self.threshold
self.prog_gen_tetra['color'].value = self.mesh_color
vec3 linear = ambient + diffuse + specular;
f_color = vec4(linear, trans);
} else {
f_color = vec4(vec3(1.0, 1.0, 1.0) * dif + amb_int, 1.0);
}
} else if (mode == 1) {
f_color = vec4(texture(Texture, v_text).rgba);
} else {
f_color = vec4(box_rgb, 1.0);
}
}
""",
)
self.CTX.enable(moderngl.DEPTH_TEST)
self.CTX.enable(moderngl.BLEND)
self.PROG["mode"].value = 0
self.PROG["use_texture"].value = True
self.PROG["has_image"].value = False
self.PROG["x"].value = 0
self.PROG["y"].value = 0
self.PROG["z"].value = 0
self.PROG["DirLight"].value = (0, 1, 0)
self.PROG["dif_int"].value = 0.7
self.PROG["amb_int"].value = 0.5
self.PROG["cam_pos"].value = tuple(EYE)
self.angle_of_view = 16.426
self.TAN_ANGLE = np.tan(self.angle_of_view / 2 * np.pi / 180.0)
perspective = Matrix44.perspective_projection(
self.angle_of_view, RATIO, 0.1, 1000.0
)
self.PROG["VP"].write((perspective * LOOK_AT).astype("f4").tobytes())
def render(self, time, frametime):
self.ctx.clear(1.0, 1.0, 1.0)
self.ctx.enable(moderngl.BLEND)
self.rotation.value = time
self.vao.render(instances=10)
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)
self.scale.value = (0.5, self.aspect_ratio * 0.5)
self.rotation.value = time
self.vao.render(instances=10)
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
# gravity_force = 2.0
def __init__(
self, obj_f, mtl_f, background_f=None, camera_distance=2.0, angle_of_view=16.426
):
# Initialize OpenGL context.
self.ctx = moderngl.create_standalone_context()
# Render depth appropriately.
self.ctx.enable(moderngl.DEPTH_TEST)
# Setting for rendering transparent objects.
# See: https://learnopengl.com/Advanced-OpenGL/Blending
# and: https://github.com/cprogrammer1994/ModernGL/blob/master/moderngl/context.py#L129.
self.ctx.enable(moderngl.BLEND)
# Define OpenGL program.
prog = self.ctx.program(
vertex_shader="""
#version 330
uniform float x;
uniform float y;
uniform float z;
uniform mat3 R_obj;
uniform mat3 R_light;
uniform vec3 DirLight;
uniform mat4 VP;
uniform int mode;
def render(self, time, frame_time):
# randomize color
self.drop_program['color'].value = random.random(), random.random(), random.random()
self.fbo_2.use()
# Render drop (with additive blending) when mouse is pressed
if self.wnd.mouse_states.any:
self.ctx.enable(moderngl.BLEND)
self.ctx.blend_func = moderngl.ONE, moderngl.ONE
self.drops_texture.use()
self.drop_program['pos'].value = self.mouse_pos
self.sprite.render(self.drop_program)
self.ctx.disable(moderngl.BLEND)
# HACK: Just draw 100 new drops per frame (copy paste from above)
# This is pretty terrible and slow!
self.ctx.enable(moderngl.BLEND)
self.ctx.blend_func = moderngl.ONE, moderngl.ONE
self.drops_texture.use()
for i in range(10):
self.drop_program['pos'].value = random.random() * 2 - 1.0, random.random() * 2 - 1
self.sprite.render(self.drop_program)
self.ctx.disable(moderngl.BLEND)
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)