~habilain/pygl3display/1.9

« back to all changes in this revision

Viewing changes to pygl3display/rendertargets.py

  • Committer: David Griffin a.k.a. habilain
  • Date: 2012-08-23 15:39:53 UTC
  • mfrom: (48.1.191 fix2dSprites)
  • Revision ID: habilain@gmail.com-20120823153953-ivtr0fikz1yk6v3g
More work on enabling GPU compute functionality.

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
 
21
21
rendertargets.py
22
22
 
23
 
Provides TwoDTextureTarget, which allows render to texture support.
 
23
Provides TextureTarget and TwoDTextureTarget, which allow 
 
24
render to texture support.
24
25
"""
25
26
 
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
32
31
 
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
 
35
        Args:
 
36
         number: Number of output textures
 
37
         size: Size of textures
 
38
         compute: If true, us GL_RGBA32 textures and disable blending
 
39
         for the target."""
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)
46
 
            
47
 
        self.textureAtlas = None
48
 
        
 
51
 
49
52
    def __enter__(self):
50
53
        """Ensure that the render target is bound"""
51
54
        self.bind()
60
63
        self.fbo.bindTextureSet(self.fboid)
61
64
        GLCommon.target = self
62
65
        GLCommon.setResolution(self.size)
 
66
        if self.compute:
 
67
            GLCommon.enableBlend(False)
63
68
        
64
69
    def unbind(self):
65
70
        """Unbind the texture set this render target uses"""
66
71
        self.fbo.unbindTextureSet(self.fboid)
 
72
 
 
73
        
 
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
67
80
        
68
81
    def readPixels(self):
69
82
        """Read the pixels out of the texture"""
70
83
        self.unbind()
71
84
        return self.texture.readPixels()
72
85
        
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
79
 
        
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()
85
 
        size = self.size[0]
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)
89
 
        
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)
95
86
        
96
87
        
97
88