コード例 #1
0
ファイル: sphere.py プロジェクト: pyplex/pyplex
    def on_start(self, ctx: px.gl.GL43):
        self.ctx = ctx

        self.program = px.Program(
            self.ctx,
            px.VertexShader(
                self.ctx,
                os.path.join(os.path.dirname(__file__), 'sphere_vertex.glsl')),
            px.FragmentShader(
                self.ctx,
                os.path.join(os.path.dirname(__file__),
                             'sphere_fragment.glsl')))

        sphere = px.Sphere(64, 32)

        self.vertices = sphere.vertices
        self.program['position'] = px.ArrayBuffer(ctx, self.vertices)
        self.program['normal'] = px.ArrayBuffer(ctx, self.vertices)
        self.elements = px.ElementArrayBuffer(ctx, sphere.elements)

        self.program['albedo'] = np.random.uniform(size=3).astype(np.float32)

        # self.ctx.enable(px.gl.Enableable.CULL_FACE)
        # self.ctx.cull_face(px.gl.CullFace.BACK)
        self.ctx.enable(px.gl.Enableable.DEPTH_TEST)
        self.ctx.clear_color(0.1, 0.2, 0.4, 1)

        self.ctx.polygon_mode(px.gl.CullFace.FRONT_AND_BACK,
                              px.gl.PolygonMode.LINE)
コード例 #2
0
ファイル: model.py プロジェクト: pyplex/pyplex
    def on_start(self, ctx: gl.GL_ANY):
        self.ctx = ctx

        self.asset = px.AssetImporter(ctx, self.MODEL_PATH)
        self.mesh, = self.asset.meshes

        self.transform = px.transform.identity()

        self.albedo = px.vec3(1, 0.75, 0.5)
        self.specular = px.float32(0.5)
        self.shininess = px.float32(32)

        self._MOUSE_DRAG_SPEED = 4
        self._FRICTION = 0.95
        self._mouse_x, self._mouse_y = -1, -1
        self._delta_x, self._delta_y = 0, 0
        self._theta, self._phi = 0, 0
        self._r = -200

        self.fov = 60

        self.camera = px.PerspectiveCamera(ctx, self.fov, 1, 0.1, 100, px.look_at(px.vec3(0, 0, -self._r / self.fov ), px.vec3(0, 0, 0), px.vec3(0, 1, 0)))

        self.ambient = px.vec3(0.1, 0.1, 0.1)
        self.key_light = px.DirectionalLight(ctx, px.vec3(-1, 0, 1), px.vec3(1, 1, 1), px.float32(1))
        self.fill_light = px.DirectionalLight(ctx, px.vec3(1, 0, 0), px.vec3(0.5, 0.5, 1), px.float32(0.5))
        self.rim_light = px.DirectionalLight(ctx, px.vec3(-1, 1, -2), px.vec3(1, 1, 1), px.float32(4))

        self.point_light = px.PointLight(ctx, px.vec3(0, 0, 10), px.vec3(1, 0, 0), px.float32(1), px.float32(0.1))

        # self.program = px.Program(ctx,
        #     px.VertexShader(ctx, '../../pyplex/glsl/lambert/lambert.vs'),
        #     px.FragmentShader(ctx, '../../pyplex/glsl/lambert/lambert.fs'),
        #     vao=self.mesh.vao)

        self.program = px.Program(ctx,
            px.VertexShader(ctx, '../../pyplex/glsl/blinn_phong/blinn_phong.vs'),
            px.FragmentShader(ctx, '../../pyplex/glsl/blinn_phong/blinn_phong.fs'),
            vao=self.mesh.vao)

        self.program['albedo'] = self.albedo
        self.program['specular'] = self.specular
        self.program['shininess'] = self.shininess

        self.program['Camera'] = self.camera.buffer

        self.program['ambient'] = self.ambient

        self.program['DirectionalLightCount'] = px.int32(3)
        self.program['DirectionalLight[0]'] = self.key_light.buffer
        self.program['DirectionalLight[1]'] = self.fill_light.buffer
        self.program['DirectionalLight[2]'] = self.rim_light.buffer

        self.program['PointLightCount'] = px.int32(1)
        self.program['PointLight[0]'] = self.point_light.buffer

        self.ctx.clear_color(0.1, 0.2, 0.4, 1.0)

        self.ctx.enable(px.gl.Enableable.DEPTH_TEST)
コード例 #3
0
ファイル: wavefront.py プロジェクト: pyplex/pyplex
    def on_start(self, ctx: px.gl.GL43):
        self.ctx = ctx

        self.program = px.Program(self.ctx,
            px.VertexShader(self.ctx, 'phong_vertex.glsl'),
            px.FragmentShader(self.ctx, 'phong_fragment.glsl'))

        geometry = px.loader.wavefront(os.path.abspath('../../obj/suzanne.obj'))
        # geometry = px.Sphere(128, 64)
        # geometry = px.Cube()
        geometry.recalculate_normals()

        self.program['position'] = px.ArrayBuffer(self.ctx, geometry.vertices)
        self.program['normal'] = px.ArrayBuffer(self.ctx, geometry.normals)
        self.elements = px.ElementArrayBuffer(self.ctx, geometry.elements)

        self.program['albedo'] = np.random.uniform(size=3).astype(np.float32)

        self._light_count = 50
        self._rotation = np.random.uniform(0.001, 0.002, (self._light_count, 3)).astype(np.float32)
        self._lights = np.recarray(self._light_count, [('position', '3f4'), ('color', '3f4')])

        self._lights['position'] = np.random.uniform(-1, 1, (self._light_count, 3)).astype(np.float32)
        self._lights['position'] *= 10 / np.linalg.norm(self._lights['position'], 2, -1, True)

        self._lights['color'] = np.random.uniform(0, 1, (self._light_count, 3)).astype(np.float32)
        self._lights['color'] /= np.linalg.norm(self._lights['color'], 2, -1, True)

        self._light_buffer = px.UniformBuffer(self.ctx, self._lights, px.gl.BufferUsage.STREAM_DRAW)
        self.program['LightBlock'] = self._light_buffer
        self.program['lightCount'] = np.array(self._light_count, np.int)

        self._view_pos = np.array([0, 0, -3], np.float32)
        self._view = px.transform.translation(self._view_pos)

        self.program['view_pos'] = self._view_pos

        self.ctx.enable(px.gl.Enableable.CULL_FACE)
        self.ctx.cull_face(px.gl.CullFace.BACK)
        self.ctx.enable(px.gl.Enableable.DEPTH_TEST)
        self.ctx.clear_color(0.1, 0.2, 0.4, 1)

        self._MOUSE_DRAG_SPEED = 4
        self._FRICTION = 0.95
        self._mouse_x, self._mouse_y = -1, -1
        self._delta_x, self._delta_y = 0, 0
        self._theta, self._phi = 0, 0
        self._r = -5
コード例 #4
0
    def on_start(self, ctx: gl.GL_ANY):
        self.ctx = ctx

        self.program = px.Program(ctx,
            px.VertexShader(ctx, '../../pyplex/glsl/blinn_phong/blinn_phong.vs'),
            px.FragmentShader(ctx, '../../pyplex/glsl/blinn_phong/blinn_phong.fs'))

        self.asset = px.AssetImporter(ctx, Sponza.PATH)
        self.meshes = self.asset.meshes

        self.albedos = np.random.uniform(size=(len(self.meshes), 3)).astype(np.float32)

        self.transform = px.identity()

        self.camera = px.TrackBallCamera(self.ctx, 60, 1, 1, 5000, px.vec3(0, 100, 0), px.vec3(0, 1, 0), np.pi/2, 0, 500)

        self.point_lights = [
            px.PointLight(self.ctx,
                          px.vec3(np.random.uniform(-1000, 1000),
                                  np.random.uniform(100, 500),
                                  np.random.uniform(-500, 500)),
                          px.normalize(np.random.uniform(0, 1, 3).astype(np.float32)),
                          px.float32(20), px.float32(0.001)) for i in range(12)]

        self.program['Camera'] = self.camera.buffer
        self.program['ambient'] = px.vec3(0.1, 0.1, 0.1)

        self.program['Ns'] = px.float32(8)
        self.program['Ka'] = px.vec3(1, 1, 1)
        self.program['Kd'] = px.vec3(0.5, 0.5, 0.5)
        self.program['Ks'] = px.vec3(0.5, 0.5, 0.5)

        self.program['model'] = self.transform
        self.program['model_1T'] = np.ascontiguousarray(np.linalg.inv(self.transform).T[:3,:3])

        self.program['PointLightCount'] = px.int32(len(self.point_lights))
        for i, light in enumerate(self.point_lights):
            self.program['PointLight[{}]'.format(i)] = light.buffer

        self.ctx.clear_color(0.25, 0.5, 1.0, 1.0)

        self.ctx.enable(gl.DEPTH_TEST)
        self.ctx.enable(px.gl.Enableable.CULL_FACE)
        self.ctx.cull_face(px.gl.CullFace.BACK)
        # self.ctx.polygon_mode(px.gl.CullFace.FRONT_AND_BACK, px.gl.PolygonMode.LINE)

        self.theta = self.phi = 0.0
        self.x_prev = self.y_prev = 0.0
コード例 #5
0
    def on_start(self, ctx: gl.GL_ANY):
        self.ctx = ctx

        plane = px.Plane()
        sphere = px.Sphere(64, 32)

        self.meshes = [
            px.Mesh(self.ctx, plane.elements, plane.vertices, plane.uvs, plane.normals),
            px.Mesh(self.ctx, sphere.elements, sphere.vertices, normals=sphere.normals)
        ]
        self.transform = px.identity()

        self.program = px.Program(ctx,
            px.VertexShader(ctx, '../pyplex/glsl/blinn_phong/blinn_phong.vs'),
            px.FragmentShader(ctx, '../pyplex/glsl/blinn_phong/blinn_phong.fs'))

        self.camera = px.TrackBallCamera(self.ctx, 60, 1, 0.01, 10,
                                         px.vec3(0, 0, 0), px.vec3(0, 1, 0), np.pi/5, np.pi/4, 2)

        self.point_lights = [
            px.PointLight(self.ctx, px.vec3(0, 2, 0), px.vec3(1, 1, 1), px.float32(1), px.float32(0.1))
        ]

        self.program['Camera'] = self.camera.buffer
        self.program['ambient'] = px.vec3(0.025, 0.05, 0.1)

        self.program['Ns'] = px.float32(32)
        self.program['Ka'] = px.vec3(1, 1, 1)
        self.program['Kd'] = px.vec3(0.5, 0.5, 0.5)
        self.program['Ks'] = px.vec3(0.5, 0.5, 0.5)

        self.program['model'] = self.transform
        self.program['model_1T'] = np.ascontiguousarray(np.linalg.inv(self.transform).T[:3, :3])

        self.program['PointLightCount'] = px.int32(len(self.point_lights))
        for i, light in enumerate(self.point_lights):
            self.program['PointLight[{}]'.format(i)] = light.buffer

        self.ctx.clear_color(0.25, 0.5, 1.0, 1.0)

        self.ctx.enable(gl.DEPTH_TEST)
        self.ctx.enable(px.gl.Enableable.CULL_FACE)
        self.ctx.cull_face(px.gl.CullFace.BACK)

        self.theta = self.phi = 0.0
        self.x_prev = self.y_prev = 0.0
コード例 #6
0
    def on_start(self, ctx: gl.GL43):
        self.ctx = ctx

        # Camera Definition #
        self.camera_fov = 30
        self.camera_near = 0.1
        self.camera_far = 10

        self.camera_pos = np.array([1, 2, 2], np.float32)
        self.camera_center = np.array([0, 0.10, 0], np.float32)
        self.camera_up = np.array([0, 1, 0], np.float32)
        self.view = px.transform.look_at(self.camera_pos, self.camera_center, self.camera_up)

        # Light Definition #
        self.light_pos = np.array([-10, 10, 10], np.float32)

        # Plane Definition #
        plane = px.Plane()
        self.plane_transform = px.transform.identity()
        self.plane_program = px.Program(self.ctx,
                                        px.VertexShader(self.ctx, 'lambert_vertex.glsl'),
                                        px.FragmentShader(self.ctx, 'lambert_fragment.glsl'))

        self.plane_elements = px.ElementArrayBuffer(self.ctx, plane.elements)
        self.plane_vertices = px.ArrayBuffer(self.ctx, plane.vertices)
        self.plane_program['position'] = self.plane_vertices
        self.plane_program['normal'] = px.ArrayBuffer(self.ctx, plane.normals)
        self.plane_program['albedo'] = np.array([1, 0.75, 0.5], np.float32)


        # Object Definition #
        obj = px.loader.wavefront('../../obj/suzanne.obj')
        obj.recalculate_normals()
        self.cube_transform = px.transform.scale(0.2) * px.transform.translation([0, 0.1, 0]) * px.transform.rotation_x(-np.pi / 4)
        self.cube_program = px.Program(self.ctx,
                                        px.VertexShader(self.ctx, 'lambert_vertex.glsl'),
                                        px.FragmentShader(self.ctx, 'lambert_fragment.glsl'))
        self.cube_elements = px.ElementArrayBuffer(self.ctx, obj.elements)
        self.cube_vertices = px.ArrayBuffer(self.ctx, obj.vertices)
        self.cube_program['position'] = self.cube_vertices
        self.cube_program['normal'] = px.ArrayBuffer(self.ctx, obj.normals)
        self.cube_program['albedo'] = np.array([0.5, 0.75, 1.0], np.float32)

        # Shadow Definition #
        self.shadow_program = px.Program(self.ctx,
            px.VertexShader(self.ctx, 'depth_vertex.glsl'),
            px.FragmentShader(self.ctx, 'depth_fragment.glsl'))

        self.shadow_resolution = 1024
        self.shadow_area = 1
        self.shadow_near = 1
        self.shadow_far = 20
        self.shadow_bias = 0.001
        self.depth_map = px.DepthTexture2D(self.ctx, self.shadow_resolution, self.shadow_resolution, px.TextureFilter.nearest())
        self.depth_map_fbo = px.FrameBuffer(self.ctx, depth=self.depth_map)
        self.light_projection = px.transform.orthographic(-self.shadow_area, self.shadow_area,
                                                          -self.shadow_area, self.shadow_area,
                                                          self.shadow_near, self.shadow_far)

        self.light_view = px.transform.look_at(self.light_pos, self.camera_center, self.camera_up)
        self.light_space = self.light_view * self.light_projection
        self.shadow_program['light_space'] = self.light_space
        self.cube_program['light_space'] = self.light_space
        self.plane_program['light_space'] = self.light_space

        self.shadow_program['model'] = self.cube_transform
        self.shadow_program['position'] = self.cube_vertices

        self.cube_program['shadow_map'] = self.depth_map
        self.plane_program['shadow_map'] = self.depth_map

        # Texture Display Program #
        self.texture_program = px.Program(self.ctx,
            px.VertexShader(self.ctx, 'quad_vertex.glsl'),
            px.FragmentShader(self.ctx, 'quad_fragment.glsl'))

        self.texture_program['position'] = px.ArrayBuffer(self.ctx, px.Quad().vertices)
        self.texture_program['uv'] = px.ArrayBuffer(self.ctx, px.Quad().uvs)
        self.texture_program['tex'] = self.depth_map
        self.texture_program_elements = px.ElementArrayBuffer(self.ctx, px.Quad().elements)

        self._MOUSE_DRAG_SPEED = 4
        self._FRICTION = 0.95
        self._mouse_x, self._mouse_y = -1, -1
        self._delta_x, self._delta_y = 0, 0
        self._theta, self._phi = np.pi/4, np.pi/4
        self.r = 3

        self.ctx.enable(px.gl.Enableable.CULL_FACE)
        self.ctx.clear_color(0.1, 0.2, 0.4, 1)