Debian Codegraph @

tests/interactive/window/test_window_multisample.py

raw symbols SCIP
1 import pytest
2 from tests.base.interactive import InteractiveTestCase
3
4 from pyglet.gl import *
5 from pyglet import window
6 from pyglet import clock
7 from pyglet.window import key
8 import pyglet
9
10
11 @pytest.mark.requires_user_action
12 class WINDOW_MULTISAMPLE(InteractiveTestCase):
13 """Test that a window can have multisample.
14
15 A window will be opened containing two rotating squares. Initially,
16 there will be no multisampling (the edges will look "jaggy"). Press:
17
18 * M to toggle multisampling on/off
19 * S to increase samples (2, 4, 6, 8, 10, ...)
20 * Shift+S to decrease samples
21
22 Each time sample_buffers or samples is modified, the window will be recreated.
23 Watch the console for success and failure messages. If the multisample
24 options are not supported, a "Failure" message will be printed and the window
25 will be left as-is.
26
27 Press ESC to end the test.
28 """
29 win = None
30 width = 640
31 height = 480
32
33 soft_multisample = True
34 multisample = False
35 samples = 2
36
37 # This test does not work on all hardware, unless rendered to texture.
38 texture = pyglet.image.Texture.create(width, height, GL_TEXTURE_RECTANGLE)
39
40 def set_window(self):
41 oldwindow = self.win
42 try:
43 if self.multisample:
44 print('Attempting samples=%d...' % self.samples, end=' ')
45 config = Config(sample_buffers=1,
46 samples=self.samples,
47 double_buffer=True)
48 else:
49 print('Disabling multisample...', end=' ')
50 config = Config(double_buffer=True)
51 self.win = window.Window(self.width, self.height,
52 vsync=True,
53 config=config)
54 self.win.switch_to()
55 self.win.push_handlers(self.on_key_press)
56
57 if self.multisample:
58 if self.soft_multisample:
59 glEnable(GL_MULTISAMPLE_ARB)
60 else:
61 glDisable(GL_MULTISAMPLE_ARB)
62
63 if oldwindow:
64 oldwindow.close()
65
66 print('Success.')
67 except window.NoSuchConfigException:
68 print('Failed.')
69
70 def on_key_press(self, symbol, modifiers):
71 mod = 1
72 if modifiers & key.MOD_SHIFT:
73 mod = -1
74
75 if symbol == key.M:
76 self.multisample = not self.multisample
77 self.set_window()
78 if symbol == key.S:
79 self.samples += 2 * mod
80 self.samples = max(2, self.samples)
81 self.set_window()
82
83 # Another test: try enabling/disabling GL_MULTISAMPLE_ARB...
84 # seems to have no effect if samples > 4.
85 if symbol == key.N:
86 self.soft_multisample = not self.soft_multisample
87 if self.soft_multisample:
88 print('Enabling GL_MULTISAMPLE_ARB')
89 glEnable(GL_MULTISAMPLE_ARB)
90 else:
91 print('Disabling GL_MULTISAMPLE_ARB')
92 glDisable(GL_MULTISAMPLE_ARB)
93
94 def render(self):
95 self.win.switch_to()
96 size = self.height / 4
97 glClear(GL_COLOR_BUFFER_BIT)
98 glEnable(GL_BLEND)
99 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
100
101 glLoadIdentity()
102 glTranslatef(self.width/2, self.height/2, 0)
103 glRotatef(self.angle, 0, 0, 1)
104
105 glColor3f(1, 0, 0)
106 glBegin(GL_QUADS)
107 glVertex2f(-size, -size)
108 glVertex2f(size, -size)
109 glVertex2f(size, size)
110 glVertex2f(-size, size)
111 glEnd()
112
113 glRotatef(-self.angle * 2, 0, 0, 1)
114 glColor4f(0, 1, 0, 0.5)
115 glBegin(GL_QUADS)
116 glVertex2f(-size, -size)
117 glVertex2f(size, -size)
118 glVertex2f(size, size)
119 glVertex2f(-size, size)
120 glEnd()
121
122 # Render to texture, then blit to screen:
123 buffer = pyglet.image.get_buffer_manager().get_color_buffer()
124 self.texture.blit_into(buffer, 0, 0, 0)
125 glViewport(0, 0, self.width, self.height)
126 glClearColor(0, 0, 0, 1)
127 glClear(GL_COLOR_BUFFER_BIT)
128 glLoadIdentity()
129 glColor3f(1, 1, 1)
130 self.texture.blit(0, 0, width=self.width, height=self.height)
131
132 def test_multisample(self):
133 self.set_window()
134 try:
135 self.angle = 0
136 while not self.win.has_exit:
137 dt = clock.tick()
138 self.angle += dt
139
140 self.render()
141 self.win.flip()
142 self.win.dispatch_events()
143 finally:
144 self.win.close()
145 self.user_verify('Pass test?', take_screenshot=False)
146