File: test_window_multisample.py

package info (click to toggle)
pyglet 2.0.17%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 15,560 kB
  • sloc: python: 80,579; xml: 50,988; ansic: 171; makefile: 146
file content (145 lines) | stat: -rwxr-xr-x 4,683 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import pytest
from tests.base.interactive import InteractiveTestCase

from pyglet.gl import *
from pyglet import window
from pyglet import clock
from pyglet.window import key


@pytest.mark.requires_user_action
class WINDOW_MULTISAMPLE(InteractiveTestCase):
    """Test that a window can have multisample.

    A window will be opened containing two rotating squares.  Initially,
    there will be no multisampling (the edges will look "jaggy").  Press:

        * M to toggle multisampling on/off
        * S to increase samples (2, 4, 6, 8, 10, ...)
        * Shift+S to decrease samples

    Each time sample_buffers or samples is modified, the window will be recreated.
    Watch the console for success and failure messages.  If the multisample
    options are not supported, a "Failure" message will be printed and the window
    will be left as-is.

    Press ESC to end the test.
    """
    win = None
    width = 640
    height = 480

    soft_multisample = True
    multisample = False
    samples = 2

    # This test does not work on all hardware, unless rendered to texture.
    texture = pyglet.image.Texture.create(width, height, GL_TEXTURE_RECTANGLE)

    def set_window(self):
        oldwindow = self.win
        try:
            if self.multisample:
                print('Attempting samples=%d...' % self.samples, end=' ')
                config = Config(sample_buffers=1,
                                samples=self.samples,
                                double_buffer=True)
            else:
                print('Disabling multisample...', end=' ')
                config = Config(double_buffer=True)
            self.win = window.Window(self.width, self.height,
                                     vsync=True,
                                     config=config)
            self.win.switch_to()
            self.win.push_handlers(self.on_key_press)

            if self.multisample:
                if self.soft_multisample:
                    glEnable(GL_MULTISAMPLE_ARB)
                else:
                    glDisable(GL_MULTISAMPLE_ARB)

            if oldwindow:
                oldwindow.close()

            print('Success.')
        except window.NoSuchConfigException:
            print('Failed.')

    def on_key_press(self, symbol, modifiers):
        mod = 1
        if modifiers & key.MOD_SHIFT:
            mod = -1

        if symbol == key.M:
            self.multisample = not self.multisample
            self.set_window()
        if symbol == key.S:
            self.samples += 2 * mod
            self.samples = max(2, self.samples)
            self.set_window()

        # Another test: try enabling/disabling GL_MULTISAMPLE_ARB...
        # seems to have no effect if samples > 4.
        if symbol == key.N:
            self.soft_multisample = not self.soft_multisample
            if self.soft_multisample:
                print('Enabling GL_MULTISAMPLE_ARB')
                glEnable(GL_MULTISAMPLE_ARB)
            else:
                print('Disabling GL_MULTISAMPLE_ARB')
                glDisable(GL_MULTISAMPLE_ARB)

    def render(self):
        self.win.switch_to()
        size = self.height / 4
        glClear(GL_COLOR_BUFFER_BIT)
        glEnable(GL_BLEND)
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)

        glLoadIdentity()
        glTranslatef(self.width/2, self.height/2, 0)
        glRotatef(self.angle, 0, 0, 1)

        glColor3f(1, 0, 0)
        glBegin(GL_QUADS)
        glVertex2f(-size, -size)
        glVertex2f(size, -size)
        glVertex2f(size, size)
        glVertex2f(-size, size)
        glEnd()

        glRotatef(-self.angle * 2, 0, 0, 1)
        glColor4f(0, 1, 0, 0.5)
        glBegin(GL_QUADS)
        glVertex2f(-size, -size)
        glVertex2f(size, -size)
        glVertex2f(size, size)
        glVertex2f(-size, size)
        glEnd()

        # Render to texture, then blit to screen:
        buffer = pyglet.image.get_buffer_manager().get_color_buffer()
        self.texture.blit_into(buffer, 0, 0, 0)
        glViewport(0, 0, self.width, self.height)
        glClearColor(0, 0, 0, 1)
        glClear(GL_COLOR_BUFFER_BIT)
        glLoadIdentity()
        glColor3f(1, 1, 1)
        self.texture.blit(0, 0, width=self.width, height=self.height)

    def test_multisample(self):
        self.set_window()
        try:
            self.angle = 0
            while not self.win.has_exit:
                dt = clock.tick()
                self.angle += dt

                self.render()
                self.win.flip()
                self.win.dispatch_events()
        finally:
            self.win.close()
        self.user_verify('Pass test?', take_screenshot=False)