Spaces:
Runtime error
Runtime error
File size: 7,702 Bytes
d82cf6a |
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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
from pyglet.gl import *
def get_max_color_attachments():
"""Get the maximum allow Framebuffer Color attachements"""
number = GLint()
glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS, number)
return number.value
class Renderbuffer:
"""OpenGL Renderbuffer Object"""
def __init__(self, width, height, internal_format, samples=1):
"""Create an instance of a Renderbuffer object."""
self._id = GLuint()
self._width = width
self._height = height
self._internal_format = internal_format
glGenRenderbuffers(1, self._id)
glBindRenderbuffer(GL_RENDERBUFFER, self._id)
if samples > 1:
glRenderbufferStorageMultisample(GL_RENDERBUFFER, samples, internal_format, width, height)
else:
glRenderbufferStorage(GL_RENDERBUFFER, internal_format, width, height)
glBindRenderbuffer(GL_RENDERBUFFER, 0)
@property
def id(self):
return self._id.value
@property
def width(self):
return self._width
@property
def height(self):
return self._height
def bind(self):
glBindRenderbuffer(GL_RENDERBUFFER, self._id)
@staticmethod
def unbind():
glBindRenderbuffer(GL_RENDERBUFFER, 0)
def delete(self):
glDeleteRenderbuffers(1, self._id)
def __del__(self):
try:
glDeleteRenderbuffers(1, self._id)
# Python interpreter is shutting down:
except Exception:
pass
def __repr__(self):
return "{}(id={})".format(self.__class__.__name__, self._id.value)
class Framebuffer:
"""OpenGL Framebuffer Object"""
def __init__(self, target=GL_FRAMEBUFFER):
"""Create an OpenGL Framebuffer object.
:rtype: :py:class:`~pyglet.image.Framebuffer`
.. versionadded:: 2.0
"""
self._id = GLuint()
glGenFramebuffers(1, self._id)
self._attachment_types = 0
self._width = 0
self._height = 0
self.target = target
@property
def id(self):
return self._id.value
@property
def width(self):
"""The width of the widest attachment."""
return self._width
@property
def height(self):
"""The width of the widest attachment."""
return self._height
def bind(self):
glBindFramebuffer(self.target, self._id)
def unbind(self):
glBindFramebuffer(self.target, 0)
def clear(self):
if self._attachment_types:
self.bind()
glClear(self._attachment_types)
self.unbind()
def delete(self):
try:
glDeleteFramebuffers(1, self._id)
except Exception:
pass
@property
def is_complete(self):
return glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE
@staticmethod
def get_status():
states = {GL_FRAMEBUFFER_UNSUPPORTED: "Framebuffer unsupported. Try another format.",
GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT: "Framebuffer incomplete attachment.",
GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT: "Framebuffer missing attachment.",
GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT: "Framebuffer unsupported dimension.",
GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT: "Framebuffer incomplete formats.",
GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER: "Framebuffer incomplete draw buffer.",
GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER: "Framebuffer incomplete read buffer.",
GL_FRAMEBUFFER_COMPLETE: "Framebuffer is complete."}
gl_status = glCheckFramebufferStatus(GL_FRAMEBUFFER)
return states.get(gl_status, "Unknown error")
def attach_texture(self, texture, target=GL_FRAMEBUFFER, attachment=GL_COLOR_ATTACHMENT0):
"""Attach a Texture to the Framebuffer
:Parameters:
`texture` : pyglet.image.Texture
Specifies the texture object to attach to the framebuffer attachment
point named by attachment.
`target` : int
Specifies the framebuffer target. target must be GL_DRAW_FRAMEBUFFER,
GL_READ_FRAMEBUFFER, or GL_FRAMEBUFFER. GL_FRAMEBUFFER is equivalent
to GL_DRAW_FRAMEBUFFER.
`attachment` : int
Specifies the attachment point of the framebuffer. attachment must be
GL_COLOR_ATTACHMENTi, GL_DEPTH_ATTACHMENT, GL_STENCIL_ATTACHMENT or
GL_DEPTH_STENCIL_ATTACHMENT.
"""
self.bind()
glFramebufferTexture(target, attachment, texture.id, texture.level)
# glFramebufferTexture2D(target, attachment, texture.target, texture.id, texture.level)
self._attachment_types |= attachment
self._width = max(texture.width, self._width)
self._height = max(texture.height, self._height)
self.unbind()
def attach_texture_layer(self, texture, layer, level, target=GL_FRAMEBUFFER, attachment=GL_COLOR_ATTACHMENT0):
"""Attach a Texture layer to the Framebuffer
:Parameters:
`texture` : pyglet.image.TextureArray
Specifies the texture object to attach to the framebuffer attachment
point named by attachment.
`layer` : int
Specifies the layer of texture to attach.
`level` : int
Specifies the mipmap level of texture to attach.
`target` : int
Specifies the framebuffer target. target must be GL_DRAW_FRAMEBUFFER,
GL_READ_FRAMEBUFFER, or GL_FRAMEBUFFER. GL_FRAMEBUFFER is equivalent
to GL_DRAW_FRAMEBUFFER.
`attachment` : int
Specifies the attachment point of the framebuffer. attachment must be
GL_COLOR_ATTACHMENTi, GL_DEPTH_ATTACHMENT, GL_STENCIL_ATTACHMENT or
GL_DEPTH_STENCIL_ATTACHMENT.
"""
self.bind()
glFramebufferTextureLayer(target, attachment, texture.id, level, layer)
self._attachment_types |= attachment
self._width = max(texture.width, self._width)
self._height = max(texture.height, self._height)
self.unbind()
def attach_renderbuffer(self, renderbuffer, target=GL_FRAMEBUFFER, attachment=GL_COLOR_ATTACHMENT0):
""""Attach a Renderbuffer to the Framebuffer
:Parameters:
`renderbuffer` : pyglet.image.Renderbuffer
Specifies the Renderbuffer to attach to the framebuffer attachment
point named by attachment.
`target` : int
Specifies the framebuffer target. target must be GL_DRAW_FRAMEBUFFER,
GL_READ_FRAMEBUFFER, or GL_FRAMEBUFFER. GL_FRAMEBUFFER is equivalent
to GL_DRAW_FRAMEBUFFER.
`attachment` : int
Specifies the attachment point of the framebuffer. attachment must be
GL_COLOR_ATTACHMENTi, GL_DEPTH_ATTACHMENT, GL_STENCIL_ATTACHMENT or
GL_DEPTH_STENCIL_ATTACHMENT.
"""
self.bind()
glFramebufferRenderbuffer(target, attachment, GL_RENDERBUFFER, renderbuffer.id)
self._attachment_types |= attachment
self._width = max(renderbuffer.width, self._width)
self._height = max(renderbuffer.height, self._height)
self.unbind()
def __del__(self):
try:
glDeleteFramebuffers(1, self._id)
# Python interpreter is shutting down:
except Exception:
pass
def __repr__(self):
return "{}(id={})".format(self.__class__.__name__, self._id.value)
|