23
Provides TwoDTextureTarget, which allows render to texture support.
23
Provides TextureTarget and TwoDTextureTarget, which allow
24
render to texture support.
26
27
from OpenGL.GL import glGetInteger, GL_TEXTURE_2D
27
28
from pygl3display.twodtexture import TwoDTexture, TwoDTextureCommon
28
from pygl3display.twodspriteinfrastructure import TextureAtlas2d
29
from pygl3display.twodspritesinterface import Sprite
30
29
from pygl3display.glcommoninst import GLCommon
31
30
from pygl3display.fbo import FBOCommonInst, ENABLED
33
class TwoDTextureTarget(object):
34
def __init__(self, size, compute = False):
35
"""Initialise a texture render target"""
32
class TextureTarget(object):
33
def __init__(self, number, size, compute = False):
34
"""Initialise a texture render target
36
number: Number of output textures
37
size: Size of textures
38
compute: If true, us GL_RGBA32 textures and disable blending
36
40
self.size = (size, size)
37
self.texture = TwoDTexture(size, compute)
38
self.texture.bindTexture()
41
self.textures = [TwoDTexture(size, compute) for _ in xrange(number)]
39
42
self.fbo = FBOCommonInst.getFBO(size)
40
43
self.fboid = self.fbo.getID()
41
self.fbo.addToTextureSet('color0', self.texture, self.fboid)
44
self.compute = compute
45
for x in xrange(number):
46
self.fbo.addToTextureSet('color%i' % x, self.textures[x], self.fboid)
42
47
self.fbo.bindTextureSet(self.fboid)
43
48
status = self.fbo.checkErrors()
44
49
if status is not True:
45
50
raise Exception('FBO Status %i; not usable' % status)
47
self.textureAtlas = None
49
52
def __enter__(self):
50
53
"""Ensure that the render target is bound"""
60
63
self.fbo.bindTextureSet(self.fboid)
61
64
GLCommon.target = self
62
65
GLCommon.setResolution(self.size)
67
GLCommon.enableBlend(False)
65
70
"""Unbind the texture set this render target uses"""
66
71
self.fbo.unbindTextureSet(self.fboid)
74
class TwoDTextureTarget(TextureTarget):
75
def __init__(self, size, compute = False):
76
"""A special case of a Texture Target, having only 1 texture"""
77
super(TwoDTextureTarget, self).__init__(1, size, compute)
78
self.texture = self.textures[0]
79
self.textureAtlas = None
68
81
def readPixels(self):
69
82
"""Read the pixels out of the texture"""
71
84
return self.texture.readPixels()
73
def textureAsAtlas(self):
74
"""Make a texture atlas of the texture"""
75
if self.textureAtlas is None:
76
self.textureAtlas = TextureAtlas2d(size = self.size[0],
77
texture = self.texture, rects = [])
78
return self.textureAtlas
80
def textureAsImage(self, topleft = (0, 0), bottomright = None):
81
"""Make an image of the texture"""
82
if bottomright is None:
83
bottomright = self.size
84
texatlas = self.textureAsAtlas()
86
left, top = (topleft[0] / size, topleft[1] / size)
87
right, bottom = (bottomright[0] / size, bottomright[1] / size)
88
return texatlas.getAtlasImage(top, bottom, left, right)
90
def textureAsSprite(self, topleft = (0, 0), bottomright = None):
91
"""Make a sprite of the texture; if you want to render the texture
92
to screen, this is the function to use."""
93
image = self.textureAsImage(topleft, bottomright)
94
return Sprite(image, flipx=True)