File: test_loaders_texture.py

package info (click to toggle)
python-moderngl-window 3.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 69,096 kB
  • sloc: python: 12,076; makefile: 21
file content (94 lines) | stat: -rw-r--r-- 3,582 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
from pathlib import Path

import moderngl
from headless import HeadlessTestCase

from moderngl_window import resources
from moderngl_window.exceptions import ImproperlyConfigured
from moderngl_window.meta import TextureDescription

resources.register_dir((Path(__file__).parent / 'fixtures' / 'resources').resolve())


class TextureLoadersTestCase(HeadlessTestCase):
    window_size = (16, 16)
    aspect_ratio = 1.0

    def test_texture_2d(self):
        """Load standard 2d texture"""
        texture = resources.textures.load(TextureDescription(path='textures/crate.png'))
        self.assertEqual(texture.size, (192, 192))
        self.assertIsInstance(texture.extra.get('meta'), TextureDescription)

    def test_texture_2d_8bit(self):
        """Test loading 8 bit texture with palette"""
        texture = resources.textures.load(TextureDescription(path='textures/8bit.png'))
        self.assertIsInstance(texture, moderngl.Texture)

    def test_texture_not_found(self):
        """Ensure ImproperlyConfigured is raised if texture is not found"""
        with self.assertRaises(ImproperlyConfigured):
            resources.textures.load(TextureDescription(path='textures/doesnotexist.png'))

    def test_texture_array(self):
        """Load texture array"""
        texture = resources.textures.load(
            TextureDescription(path='textures/array.png', layers=10, kind="array")
        )
        self.assertEqual(texture.size, (256, 256, 10))
        self.assertIsInstance(texture.extra.get('meta'), TextureDescription)

    def test_texture_array_no_layers(self):
        """Ensure error is raised when no layer is defined"""
        with self.assertRaises(ImproperlyConfigured):
            resources.textures.load(
                TextureDescription(path='textures/array.png', kind="array")
            )

    def test_cubemap(self):
        texture = resources.textures.load(TextureDescription(
            pos_x='textures/cubemap/pos_x.png',
            pos_y='textures/cubemap/pos_y.png',
            pos_z='textures/cubemap/pos_z.png',
            neg_x='textures/cubemap/neg_z.png',
            neg_y='textures/cubemap/neg_y.png',
            neg_z='textures/cubemap/neg_z.png',
            kind='cube',
        ))
        self.assertIsInstance(texture, moderngl.TextureCube)

    def test_texture_mimpamps(self):
        """Load texture with mipmapping and anisotropy"""
        desc = TextureDescription(
            path='textures/crate.png',
            mipmap_levels=(0, 2),
            anisotropy=4.0,
        )
        texture = resources.textures.load(desc)
        self.assertEqual(texture.anisotropy, 4.0)
        self.assertEqual(desc.mipmap, True)

        # Texture Array
        desc = TextureDescription(
            path='textures/array.png',
            kind="array",
            layers=10,
            mipmap_levels=(0, 5),
            anisotropy=8.0,
        )
        texture = resources.textures.load(desc)
        self.assertEqual(texture.anisotropy, 8.0)
        self.assertEqual(desc.mipmap, True)

    def test_texture_abspath(self):
        """Strip search directories and use absolute path"""
        path = (Path(__file__).parent / "fixtures/resources/textures/crate.png").resolve()
        with resources.temporary_dirs([]):
            desc = TextureDescription(
                path=path,
                mipmap_levels=(0, 2),
                anisotropy=4.0,
            )
            texture = resources.textures.load(desc)
            self.assertEqual(texture.anisotropy, 4.0)
            self.assertEqual(desc.mipmap, True)