1 """Descriptors for per-drawbuffer state.
2
3 @author: Stephan Wenger
4 @date: 2012-02-29
5 """
6
7 import itertools as _itertools
8 import numpy as _np
9
10 import glitter.raw as _gl
11 from glitter.utils import draw_buffers, bool8, GlitterError
12
15 self._context = _context
16 self._num_buffers = _context.max_draw_buffers
17 if not 0 < self._num_buffers < 512:
18 raise GlitterError("implausible number of draw buffers detected; are you sure there is a current OpenGL context?")
19
21 """Set all draw buffers.
22
23 @param obj: Ignored.
24 @type obj: any type
25 @param value: The enum of the draw buffer to bind, a number if it is a color attachment.
26 @type value: L{draw_buffers} or C{int}
27 """
28
29 _buffers = (_gl.GLenum * self._num_buffers)()
30 for i, o in _itertools.islice(_itertools.izip_longest(range(self._num_buffers), value, fillvalue=None), self._num_buffers):
31 _buffers[i] = _gl.GL_NONE if o is None else _gl.GL_COLOR_ATTACHMENT0 + o if isinstance(o, int) else draw_buffers(o)._value
32 with self._context:
33 _gl.glDrawBuffers(self._num_buffers, _buffers)
34
36 """Get the draw buffer attached to this target.
37
38 @param index: Index of the draw buffer to query.
39 @type index: C{int}
40 @return: The enum of the currently bound draw buffer enum, a number if it is a color attachment.
41 @rtype: L{draw_buffers} or C{int}
42 """
43
44 if not 0 <= index < self._num_buffers:
45 raise IndexError
46 _buffer = _gl.GLint()
47 with self._context:
48 _gl.glGetIntegerv(_gl.GL_DRAW_BUFFER0 + index, _buffer)
49 attachment = draw_buffers[_buffer.value]
50 if attachment.name.startswith("COLOR_ATTACHMENT"):
51 return int(attachment.lstrip("COLOR_ATTACHMENT", 1))
52 else:
53 return attachment
54
56 """Set a draw buffer.
57
58 @param index: Index of the draw buffer to set.
59 @type index: C{int}
60 @param value: The enum of the draw buffer to bind, a number if it is a color attachment.
61 @type value: L{draw_buffers} or C{int}
62 """
63
64 self.__set__(None, [value if i == index else self[i] for i in range(self._num_buffers)])
65
67 """Unbind a draw buffer.
68
69 @param index: Index of the draw buffer to unbind.
70 @type index: C{int}
71 """
72
73 self[index] = None
74
76 return self._num_buffers
77
79 return str(list(self))
80
83
86 self._context = _context
87 self._num_buffers = _context.max_draw_buffers
88 if not 0 < self._num_buffers < 512:
89 raise GlitterError("implausible number of draw buffers detected; are you sure there is a current OpenGL context?")
90
92 if len(value) != self._num_buffers:
93 raise ValueError("wrong size")
94 for i, v in enumerate(value):
95 self[i] = v
96
104
108
110 self[index] = (True, True, True, True)
111
113 return self._num_buffers
114
116 return str(_np.array(self))
117
120