File: cubes.py

package info (click to toggle)
python-moderngl-window 2.4.2-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 69,260 kB
  • sloc: python: 11,225; makefile: 20
file content (66 lines) | stat: -rw-r--r-- 2,223 bytes parent folder | download | duplicates (2)
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
"""
Cubes of different vertex formats.
These formats are unique for wavefront obj files.
"""
from pathlib import Path

import moderngl
import moderngl_window

from pyrr import Matrix44


class Cubes(moderngl_window.WindowConfig):
    title = "Cubes"
    resizable = True
    aspect_ratio = None
    resource_dir = Path(__file__).parent.resolve() / 'resources'

    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        # Load the 6 different boxes with different vertex formats
        self.box_v3 = self.load_scene('scenes/box/box-V3F.obj')
        self.box_c3_v3 = self.load_scene('scenes/box/box-C3F_V3F.obj')
        self.box_n3_v3 = self.load_scene('scenes/box/box-N3F_V3F.obj')
        self.box_t2_v3 = self.load_scene('scenes/box/box-T2F_V3F.obj')
        self.box_t2_c3_v3 = self.load_scene('scenes/box/box-T2F_C3F_V3F.obj')
        self.box_t2_n3_v3 = self.load_scene('scenes/box/box-T2F_N3F_V3F.obj')

        self.resize(*self.wnd.size)

    def render(self, time, frame_time):
        self.ctx.enable_only(moderngl.DEPTH_TEST | moderngl.CULL_FACE)
        rot = Matrix44.from_eulers((time, time/2, time/3))

        # Box 1
        view = Matrix44.from_translation((-5, 2, -10), dtype='f4')
        self.box_v3.draw(self.projection, view * rot)

        # Box 2
        view = Matrix44.from_translation((0, 2, -10), dtype='f4')        
        self.box_c3_v3.draw(self.projection, view * rot)

        # Box 3
        view = Matrix44.from_translation((5, 2, -10), dtype='f4')
        self.box_n3_v3.draw(self.projection, view * rot)

        # Box 4
        view = Matrix44.from_translation((-5, -2, -10), dtype='f4')
        self.box_t2_v3.draw(self.projection, view * rot)

        # Box 5
        view = Matrix44.from_translation((0, -2, -10), dtype='f4')
        self.box_t2_c3_v3.draw(self.projection, view * rot)

        # Box 6
        view = Matrix44.from_translation((5, -2, -10), dtype='f4')
        self.box_t2_n3_v3.draw(self.projection, view * rot)

    def resize(self, width, height):
        self.ctx.viewport = 0, 0, width, height
        self.projection = Matrix44.perspective_projection(45, width / height, 1, 50, dtype='f4')


if __name__ == '__main__':
    Cubes.run()