示例#1
0
    def test_image2d_failed(self):
        importer = trade.ImporterManager().load_and_instantiate(
            'StbImageImporter')
        importer.open_data(b'bla')

        with self.assertRaisesRegex(RuntimeError, "import failed"):
            image = importer.image2d(0)
示例#2
0
    def test(self):
        # The only way to get a mesh instance is through a manager
        importer = trade.ImporterManager().load_and_instantiate('TinyGltfImporter')
        importer.open_file(os.path.join(os.path.dirname(__file__), 'mesh.glb'))

        mesh = importer.mesh(0)
        self.assertEqual(mesh.primitive, MeshPrimitive.TRIANGLES)
示例#3
0
    def test_index_oob(self):
        importer = trade.ImporterManager().load_and_instantiate(
            'StbImageImporter')
        importer.open_file(os.path.join(os.path.dirname(__file__), 'rgb.png'))

        with self.assertRaises(IndexError):
            importer.mesh_level_count(0)
        with self.assertRaises(IndexError):
            importer.mesh_name(0)
        with self.assertRaises(IndexError):
            importer.mesh(0)

        with self.assertRaises(IndexError):
            importer.image1d_level_count(0)
        with self.assertRaises(IndexError):
            importer.image2d_level_count(1)
        with self.assertRaises(IndexError):
            importer.image3d_level_count(0)

        with self.assertRaises(IndexError):
            importer.image1d_name(0)
        with self.assertRaises(IndexError):
            importer.image2d_name(1)
        with self.assertRaises(IndexError):
            importer.image3d_name(0)

        with self.assertRaises(IndexError):
            importer.image1d(0)
        with self.assertRaises(IndexError):
            importer.image2d(0, 1)
        with self.assertRaises(IndexError):
            importer.image2d(1)
        with self.assertRaises(IndexError):
            importer.image3d(0)
示例#4
0
    def __init__(self):
        configuration = self.Configuration()
        configuration.title = "Magnum Python Textured Triangle Example"
        Application.__init__(self, configuration)

        buffer = gl.Buffer()
        buffer.set_data(array.array('f', [
            -0.5, -0.5, 0.0, 0.0,
             0.5, -0.5, 1.0, 0.0,
             0.0,  0.5, 0.5, 1.0
        ]))

        self._mesh = gl.Mesh()
        self._mesh.count = 3
        self._mesh.add_vertex_buffer(buffer, 0, 4*4,
            TexturedTriangleShader.POSITION)
        self._mesh.add_vertex_buffer(buffer, 2*4, 4*4,
            TexturedTriangleShader.TEXTURE_COORDINATES)

        importer = trade.ImporterManager().load_and_instantiate('TgaImporter')
        importer.open_file(os.path.join(os.path.dirname(__file__),
                                        '../textured-triangle/stone.tga'))
        image = importer.image2d(0)

        self._texture = gl.Texture2D()
        self._texture.wrapping = gl.SamplerWrapping.CLAMP_TO_EDGE
        self._texture.minification_filter = gl.SamplerFilter.LINEAR
        self._texture.magnification_filter = gl.SamplerFilter.LINEAR
        self._texture.set_storage(1, gl.TextureFormat.RGB8, image.size)
        self._texture.set_sub_image(0, Vector2i(), image)

        # or self._shader = shaders.Flat2D(shaders.Flat2D.Flags.TEXTURED)
        self._shader = TexturedTriangleShader()
示例#5
0
    def test_mesh_index_oob(self):
        # importer refcounting tested in image2d
        importer = trade.ImporterManager().load_and_instantiate('TinyGltfImporter')
        importer.open_file(os.path.join(os.path.dirname(__file__), 'mesh.glb'))

        with self.assertRaises(IndexError):
            importer.mesh(0, 1)
示例#6
0
    def test_open_failed(self):
        importer = trade.ImporterManager().load_and_instantiate('StbImageImporter')

        with self.assertRaisesRegex(RuntimeError, "opening nonexistent.png failed"):
            importer.open_file('nonexistent.png')
        with self.assertRaisesRegex(RuntimeError, "opening data failed"):
            importer.open_data(b'')
示例#7
0
    def test_convert_view(self):
        # The only way to get an image instance is through a manager
        importer = trade.ImporterManager().load_and_instantiate('StbImageImporter')
        importer.open_file(os.path.join(os.path.dirname(__file__), "rgb.png"))
        image = importer.image2d(0)

        view = ImageView2D(image)
        mutable_view = MutableImageView2D(image)
示例#8
0
    def test_image2d_data(self):
        importer = trade.ImporterManager().load_and_instantiate('StbImageImporter')

        with open(os.path.join(os.path.dirname(__file__), "rgb.png"), 'rb') as f:
            importer.open_data(f.read())

        image = importer.image2d(0)
        self.assertEqual(image.size, Vector2i(3, 2))
示例#9
0
    def test_mesh3d(self):
        # importer refcounting tested in image2d
        importer = trade.ImporterManager().load_and_instantiate(
            'TinyGltfImporter')
        importer.open_file(os.path.join(os.path.dirname(__file__), 'mesh.glb'))
        self.assertEqual(importer.mesh3d_count, 3)
        self.assertEqual(importer.mesh3d_name(0), 'Non-indexed mesh')
        self.assertEqual(importer.mesh3d_for_name('Non-indexed mesh'), 0)

        mesh = importer.mesh3d(0)
        self.assertEqual(mesh.primitive, MeshPrimitive.TRIANGLES)
示例#10
0
    def test_no_file_opened(self):
        importer = trade.ImporterManager().load_and_instantiate(
            'StbImageImporter')
        self.assertFalse(importer.is_opened)

        with self.assertRaisesRegex(RuntimeError, "no file opened"):
            importer.mesh_count
        with self.assertRaisesRegex(RuntimeError, "no file opened"):
            importer.mesh_level_count(0)

        with self.assertRaisesRegex(RuntimeError, "no file opened"):
            importer.mesh_for_name('')

        with self.assertRaisesRegex(RuntimeError, "no file opened"):
            importer.mesh_name(0)

        with self.assertRaisesRegex(RuntimeError, "no file opened"):
            importer.mesh(0)

        with self.assertRaisesRegex(RuntimeError, "no file opened"):
            importer.image1d_count
        with self.assertRaisesRegex(RuntimeError, "no file opened"):
            importer.image2d_count
        with self.assertRaisesRegex(RuntimeError, "no file opened"):
            importer.image3d_count

        with self.assertRaisesRegex(RuntimeError, "no file opened"):
            importer.image1d_level_count(0)
        with self.assertRaisesRegex(RuntimeError, "no file opened"):
            importer.image2d_level_count(0)
        with self.assertRaisesRegex(RuntimeError, "no file opened"):
            importer.image3d_level_count(0)

        with self.assertRaisesRegex(RuntimeError, "no file opened"):
            importer.image1d_for_name('')
        with self.assertRaisesRegex(RuntimeError, "no file opened"):
            importer.image2d_for_name('')
        with self.assertRaisesRegex(RuntimeError, "no file opened"):
            importer.image3d_for_name('')

        with self.assertRaisesRegex(RuntimeError, "no file opened"):
            importer.image1d_name(0)
        with self.assertRaisesRegex(RuntimeError, "no file opened"):
            importer.image2d_name(0)
        with self.assertRaisesRegex(RuntimeError, "no file opened"):
            importer.image3d_name(0)

        with self.assertRaisesRegex(RuntimeError, "no file opened"):
            importer.image1d(0)
        with self.assertRaisesRegex(RuntimeError, "no file opened"):
            importer.image2d(0)
        with self.assertRaisesRegex(RuntimeError, "no file opened"):
            importer.image3d(0)
示例#11
0
    def test_convert_view_compressed(self):
        # The only way to get an image instance is through a manager
        importer = trade.ImporterManager().load_and_instantiate('DdsImporter')
        importer.open_file(os.path.join(os.path.dirname(__file__), "rgba_dxt1.dds"))
        image = importer.image2d(0)

        # No compressed-image-related APIs exposed ATM, so just verifying the
        # uncompressed ones fail properly
        with self.assertRaisesRegex(RuntimeError, "image is compressed"):
            view = ImageView2D(image)
        with self.assertRaisesRegex(RuntimeError, "image is compressed"):
            mutable_view = MutableImageView2D(image)
示例#12
0
    def test(self):
        manager = trade.ImporterManager()
        self.assertIn('StbImageImporter', manager.alias_list)
        self.assertEqual(manager.load_state('StbImageImporter'), pluginmanager.LoadState.NOT_LOADED)

        self.assertTrue(manager.load('StbImageImporter') & pluginmanager.LoadState.LOADED)
        self.assertEqual(manager.unload('StbImageImporter'), pluginmanager.LoadState.NOT_LOADED)

        with self.assertRaisesRegex(RuntimeError, "can't load plugin"):
            manager.load('NonexistentImporter')
        with self.assertRaisesRegex(RuntimeError, "can't unload plugin"):
            manager.unload('NonexistentImporter')
示例#13
0
 def test(self):
     # The only way to get an image instance is through a manager
     importer = trade.ImporterManager().load_and_instantiate('StbImageImporter')
     importer.open_file(os.path.join(os.path.dirname(__file__), "rgb.png"))
     image = importer.image2d(0)
     self.assertFalse(image.is_compressed)
     self.assertEqual(image.storage.alignment, 1) # libPNG has 4 tho
     self.assertEqual(image.format, PixelFormat.RGB8_UNORM)
     self.assertEqual(image.pixel_size, 3)
     self.assertEqual(image.size, Vector2i(3, 2))
     # TODO: ugh, report as bytes, not chars
     self.assertEqual(ord(image.pixels[1, 2, 2]), 181)
     self.assertEqual(ord(image.data[9 + 6 + 2]), 181) # libPNG has 12 +
示例#14
0
    def __init__(self):
        configuration = self.Configuration()
        configuration.title = "Magnum Python Viewer Example"
        Application.__init__(self, configuration)

        gl.Renderer.enable(gl.Renderer.Feature.DEPTH_TEST)
        gl.Renderer.enable(gl.Renderer.Feature.FACE_CULLING)

        # Scene and drawables
        self._scene = Scene3D()
        self._drawables = scenegraph.DrawableGroup3D()

        # Every scene needs a camera
        camera_object = Object3D(parent=self._scene)
        camera_object.translate(Vector3.z_axis(5.0))
        self._camera = scenegraph.Camera3D(camera_object)
        self._camera.aspect_ratio_policy = scenegraph.AspectRatioPolicy.EXTEND
        self._camera.projection_matrix = Matrix4.perspective_projection(
            fov=Deg(35.0), aspect_ratio=1.0, near=0.01, far=100.0)
        self._camera.viewport = self.framebuffer_size

        # Base object, parent of all (for easy manipulation)
        self._manipulator = Object3D(parent=self._scene)

        # Setup renderer and shader defaults
        gl.Renderer.enable(gl.Renderer.Feature.DEPTH_TEST)
        gl.Renderer.enable(gl.Renderer.Feature.FACE_CULLING)
        colored_shader = shaders.Phong()
        colored_shader.ambient_color = Color3(0.06667)
        colored_shader.shininess = 80.0

        # Import Suzanne head and eyes (yes, sorry, it's all hardcoded here)
        importer = trade.ImporterManager().load_and_instantiate(
            'TinyGltfImporter')
        importer.open_file(
            os.path.join(os.path.dirname(__file__), '../viewer/scene.glb'))
        suzanne_object = Object3D(parent=self._manipulator)
        suzanne_mesh = meshtools.compile(
            importer.mesh(importer.mesh_for_name('Suzanne')))
        suzanne_eyes_mesh = meshtools.compile(
            importer.mesh(importer.mesh_for_name('Eyes')))
        self._suzanne = ColoredDrawable(suzanne_object, self._drawables,
                                        suzanne_mesh, colored_shader,
                                        Color3(0.15, 0.49, 1.0))
        self._suzanne_eyes = ColoredDrawable(suzanne_object, self._drawables,
                                             suzanne_eyes_mesh, colored_shader,
                                             Color3(0.95))

        self._previous_mouse_position = Vector2i()
示例#15
0
    def test_compressed(self):
        # The only way to get an image instance is through a manager
        importer = trade.ImporterManager().load_and_instantiate('DdsImporter')
        importer.open_file(os.path.join(os.path.dirname(__file__), "rgba_dxt1.dds"))
        image = importer.image2d(0)
        self.assertEqual(len(image.data), 8)
        self.assertTrue(image.is_compressed)
        # TODO: compressed properties

        # No compressed-image-related APIs exposed ATM, so just verifying the
        # uncompressed ones fail properly
        with self.assertRaisesRegex(AttributeError, "image is compressed"):
            image.storage
        with self.assertRaisesRegex(AttributeError, "image is compressed"):
            image.format
        with self.assertRaisesRegex(AttributeError, "image is compressed"):
            image.pixel_size
        with self.assertRaisesRegex(AttributeError, "image is compressed"):
            image.pixels
示例#16
0
    def test_image2d(self):
        manager = trade.ImporterManager()
        manager_refcount = sys.getrefcount(manager)

        # Importer references the manager to ensure it doesn't get GC'd before
        # the plugin instances
        importer = manager.load_and_instantiate('StbImageImporter')
        self.assertIs(importer.manager, manager)
        self.assertEqual(sys.getrefcount(manager), manager_refcount + 1)

        importer.open_file(os.path.join(os.path.dirname(__file__), 'rgb.png'))
        self.assertEqual(importer.image2d_count, 1)
        self.assertEqual(importer.image2d_name(0), '')
        self.assertEqual(importer.image2d_for_name(''), -1)

        image = importer.image2d(0)
        self.assertEqual(image.size, Vector2i(3, 2))

        # Deleting the importer should decrease manager refcount again
        del importer
        self.assertEqual(sys.getrefcount(manager), manager_refcount)