def test_sphere(self): """Create a spwhere""" mesh = geometry.sphere(radius=2.0, sectors=32, rings=16, name="test_sphere") self.assertEqual(mesh.name, "test_sphere") self.assertIsInstance(mesh.get_buffer_by_name(AttributeNames.POSITION), BufferInfo) self.assertIsInstance(mesh.get_buffer_by_name(AttributeNames.NORMAL), BufferInfo) self.assertIsInstance( mesh.get_buffer_by_name(AttributeNames.TEXCOORD_0), BufferInfo) # Use custom buffer/attribute names mesh = geometry.sphere(radius=2.0, sectors=32, rings=16, name="test_sphere", attr_names=self.custom_attrs) self.assertIsInstance( mesh.get_buffer_by_name(self.custom_attrs.POSITION), BufferInfo) self.assertIsInstance( mesh.get_buffer_by_name(self.custom_attrs.NORMAL), BufferInfo) self.assertIsInstance( mesh.get_buffer_by_name(self.custom_attrs.TEXCOORD_0), BufferInfo)
def __init__(self, **kwargs): super().__init__(**kwargs) self.camera.projection.update(near=1, far=200) self.wnd.mouse_exclusivity = True # Offscreen buffer offscreen_size = 1024, 1024 self.offscreen_depth = self.ctx.depth_texture(offscreen_size) self.offscreen_depth.compare_func = '' self.offscreen_depth.repeat_x = False self.offscreen_depth.repeat_y = False self.offscreen_color = self.ctx.texture(offscreen_size, 4) self.offscreen = self.ctx.framebuffer( color_attachments=[self.offscreen_color], depth_attachment=self.offscreen_depth, ) # Scene geometry self.floor = geometry.cube(size=(25.0, 1.0, 25.0)) self.wall = geometry.cube(size=(1.0, 5, 25), center=(-12.5, 2, 0)) self.another_blob = geometry.cube(size=(5, 5, 5), center=(-7, -10, 0)) self.sphere = geometry.sphere(radius=5.0, sectors=64, rings=32) self.sun = geometry.sphere(radius=1.0) # Programs self.basic_light = self.load_program( 'programs/shadow_mapping/directional_light.glsl') self.basic_light['shadowMap'].value = 0 self.basic_light['color'].value = 1.0, 1.0, 1.0, 1.0 self.shadowmap_program = self.load_program( 'programs/shadow_mapping/shadowmap.glsl') self.sun_prog = self.load_program('programs/cube_simple.glsl') self.sun_prog['color'].value = 1, 1, 0, 1 self.lightpos = 0, 0, 0
def __init__(self, **kwargs) -> None: super().__init__(**kwargs) # load programs self.compute_shader = self.load_compute_shader("compute_shader.glsl", self.consts) self.render_program = self.load_program("points.glsl") self.render_program = self.load_program("balls.glsl") self.box_program = self.load_program("box.glsl") # set projection matrices projection_matrix = perspective(60, self.wnd.aspect_ratio, 0.1, 1000).astype("f4") self.camera_matrix = translation( (0, 0, -4 * self.BOX_SIZE)).astype("f4") self.render_program["m_projection"].write(projection_matrix.tobytes()) self.render_program["m_camera"].write(self.camera_matrix.tobytes()) # generate random positions and velocities pos_vel = self.generate_data() # generate N hsv colors _rgb_colors = np.array( (np.arange(self.N) / self.N, np.full(self.N, 0.7), np.full(self.N, 0.5))).T # convert to grb colors = hsv_to_rgb(_rgb_colors) # reshape into vec4; [h, s, v] -> [h, s, v, 0.] colors = np.c_[colors, np.ones(colors.shape[0])] # create two buffers to switch between self.buffer1 = self.ctx.buffer(pos_vel) self.buffer2 = self.ctx.buffer(reserve=pos_vel.nbytes) self.colors = self.ctx.buffer(colors.astype("f4")) # create a VAO with buffer 1 bound to it to render the balls # some number between 4 and 16 rings_sectors_count = max(4, int(16 - 0.0005 * self.N)) self.ball = sphere( radius=self.BALL_SIZE, rings=rings_sectors_count, sectors=rings_sectors_count, ) self.ball.buffer(self.buffer1, "3f 3f/i", ["ball_position", "ball_velocity"]) self.ball.buffer(self.colors, "4f/i", ["ball_color"]) # bind the buffers to 1 and 0 respectively self._toggle = False self.buffer1.bind_to_storage_buffer(self._toggle) self.buffer2.bind_to_storage_buffer(not self._toggle) # box vao self.box = bbox(size=(self.BOX_SIZE * 2, self.BOX_SIZE * 2, self.BOX_SIZE * 2)) self.box_program["m_projection"].write(projection_matrix.tobytes()) self.box_program["m_camera"].write(self.camera_matrix.tobytes())
def __init__(self, config): super().__init__(config) self.track_intensity = self.get_track('intensity') self.prog = self.config.load_program('programs/milkyway.glsl') self.sphere = geometry.sphere(radius=200) # self.texture = self.config.load_texture_2d('textures/MilkyWayPanorama4K.jpg') self.texture = self.config.load_texture_2d( 'textures/8k_stars_milky_way.jpg') self.texture.build_mipmaps()
def __init__(self, **kwargs): super().__init__(**kwargs) self.camera.projection.update(near=1, far=200) self.wnd.mouse_exclusivity = True # Offscreen buffer offscreen_size = 1024, 1024 self.offscreen_depth = self.ctx.depth_texture(offscreen_size) self.offscreen_depth.compare_func = '' self.offscreen_depth.repeat_x = False self.offscreen_depth.repeat_y = False # Less ugly by default with linear. May need to be NEAREST for some techniques self.offscreen_depth.filter = moderngl.LINEAR, moderngl.LINEAR self.offscreen = self.ctx.framebuffer( depth_attachment=self.offscreen_depth, ) # Scene geometry self.floor = geometry.cube(size=(25.0, 1.0, 25.0)) self.wall = geometry.cube(size=(1.0, 5, 25), center=(-12.5, 2, 0)) self.sphere = geometry.sphere(radius=5.0, sectors=64, rings=32) self.sun = geometry.sphere(radius=1.0) # Debug geometry self.offscreen_quad = geometry.quad_2d(size=(0.5, 0.5), pos=(0.75, 0.75)) self.offscreen_quad2 = geometry.quad_2d(size=(0.5, 0.5), pos=(0.25, 0.75)) # Programs self.raw_depth_prog = self.load_program('programs/shadow_mapping/raw_depth.glsl') self.basic_light = self.load_program('programs/shadow_mapping/directional_light.glsl') self.basic_light['shadowMap'].value = 0 self.basic_light['color'].value = 1.0, 1.0, 1.0, 1.0 self.shadowmap_program = self.load_program('programs/shadow_mapping/shadowmap.glsl') self.texture_prog = self.load_program('programs/texture.glsl') self.texture_prog['texture0'].value = 0 self.sun_prog = self.load_program('programs/cube_simple.glsl') self.sun_prog['color'].value = 1, 1, 0, 1 self.lightpos = 0, 0, 0
def __init__(self, config): super().__init__(config) self.sphere = geometry.sphere(radius=100, sectors=256, rings=128) self.program = self.config.load_program("programs/blueball.glsl")