Package glitter :: Package contexts :: Module drawbuffers
[hide private]
[frames] | no frames]

Source Code for Module glitter.contexts.drawbuffers

  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   
13 -class DrawBufferList(object):
14 - def __init__(self, _context):
15 self._context = _context 16 self._num_buffers = _context.max_draw_buffers 17 if not 0 < self._num_buffers < 512: # sanity check 18 raise GlitterError("implausible number of draw buffers detected; are you sure there is a current OpenGL context?")
19
20 - def __set__(self, obj, value):
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
35 - def __getitem__(self, index):
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
55 - def __setitem__(self, index, value):
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
66 - def __delitem__(self, index):
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
75 - def __len__(self):
76 return self._num_buffers
77
78 - def __str__(self):
79 return str(list(self))
80
81 - def __repr__(self):
82 return str(self)
83
84 -class ColorWritemaskList(object):
85 - def __init__(self, _context):
86 self._context = _context 87 self._num_buffers = _context.max_draw_buffers 88 if not 0 < self._num_buffers < 512: # sanity check 89 raise GlitterError("implausible number of draw buffers detected; are you sure there is a current OpenGL context?")
90
91 - def __set__(self, obj, value):
92 if len(value) != self._num_buffers: 93 raise ValueError("wrong size") 94 for i, v in enumerate(value): 95 self[i] = v
96
97 - def __getitem__(self, index):
98 if not 0 <= index < self._num_buffers: 99 raise IndexError 100 mask = _np.empty(4, bool8.as_numpy()) 101 with self._context: 102 _gl.glGetBooleani_v(_gl.GL_COLOR_WRITEMASK, index, _gl.cast(mask.ctypes, _gl.glGetBooleani_v.argtypes[-1])) 103 return mask
104
105 - def __setitem__(self, index, value):
106 with self._context: 107 _gl.glColorMaski(index, *value)
108
109 - def __delitem__(self, index):
110 self[index] = (True, True, True, True)
111
112 - def __len__(self):
113 return self._num_buffers
114
115 - def __str__(self):
116 return str(_np.array(self))
117
118 - def __repr__(self):
119 return str(self)
120