コード例 #1
0
ファイル: fbo.py プロジェクト: daniel-/python-gl-engine
 def __del__(self):
     if self.glFBO!=-1:
         if bool(glFBO.glDeleteFramebuffers):
             glFBO.glDeleteFramebuffers(1, [self.glFBO])
         else:
             # gl module might already uninitialled
             pass
コード例 #2
0
ファイル: glutils.py プロジェクト: PavelHejny/mcedit
    def __init__(self, width, height, drawFunc):
        tex = GL.glGenTextures(1)
        GL.glBindTexture(GL.GL_TEXTURE_2D, tex)
        GL.glTexParameter(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_NEAREST)
        GL.glTexParameter(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_NEAREST)
        GL.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGBA8, width, height, 0, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, None)
        self.enabled = False
        self._texID = tex
        if bool(FBO.glGenFramebuffers) and "Intel" not in GL.glGetString(GL.GL_VENDOR):
            buf = FBO.glGenFramebuffers(1)
            depthbuffer = FBO.glGenRenderbuffers(1)

            FBO.glBindFramebuffer(FBO.GL_FRAMEBUFFER, buf)

            FBO.glBindRenderbuffer(FBO.GL_RENDERBUFFER, depthbuffer)
            FBO.glRenderbufferStorage(FBO.GL_RENDERBUFFER, GL.GL_DEPTH_COMPONENT, width, height)

            FBO.glFramebufferRenderbuffer(FBO.GL_FRAMEBUFFER, FBO.GL_DEPTH_ATTACHMENT, FBO.GL_RENDERBUFFER, depthbuffer)
            FBO.glFramebufferTexture2D(FBO.GL_FRAMEBUFFER, FBO.GL_COLOR_ATTACHMENT0, GL.GL_TEXTURE_2D, tex, 0)

            status = FBO.glCheckFramebufferStatus(FBO.GL_FRAMEBUFFER)
            if status != FBO.GL_FRAMEBUFFER_COMPLETE:
                print "glCheckFramebufferStatus", status
                self.enabled = False
                return

            FBO.glBindFramebuffer(FBO.GL_FRAMEBUFFER, buf)

            with gl.glPushAttrib(GL.GL_VIEWPORT_BIT):
                GL.glViewport(0, 0, width, height)
                drawFunc()

            FBO.glBindFramebuffer(FBO.GL_FRAMEBUFFER, 0)
            FBO.glDeleteFramebuffers(1, [buf])
            FBO.glDeleteRenderbuffers(1, [depthbuffer])
            self.enabled = True
        else:
            GL.glReadBuffer(GL.GL_BACK)
            if bool(window_pos.glWindowPos2dARB):
                pixels = GL.glReadPixels(0, 0, width, height, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE)

            GL.glPushAttrib(GL.GL_VIEWPORT_BIT | GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT | GL.GL_STENCIL_TEST | GL.GL_STENCIL_BUFFER_BIT)
            GL.glDisable(GL.GL_STENCIL_TEST)

            GL.glViewport(0, 0, width, height)
            GL.glScissor(0, 0, width, height)
            with gl.glEnable(GL.GL_SCISSOR_TEST):
                drawFunc()

            GL.glBindTexture(GL.GL_TEXTURE_2D, tex)
            GL.glReadBuffer(GL.GL_BACK)
            GL.glCopyTexSubImage2D(GL.GL_TEXTURE_2D, 0, 0, 0, 0, 0, width, height)

            if bool(window_pos.glWindowPos2dARB):
                window_pos.glWindowPos2dARB(0,0)
                GL.glDrawPixels(width, height, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, pixels)

            GL.glPopAttrib()
コード例 #3
0
ファイル: GLViewWidget.py プロジェクト: grg2rsr/ILTIS
 def renderToArray(self, size, format=GL_BGRA, type=GL_UNSIGNED_BYTE, textureSize=1024, padding=256):
     w,h = map(int, size)
     
     self.makeCurrent()
     tex = None
     fb = None
     try:
         output = np.empty((w, h, 4), dtype=np.ubyte)
         fb = glfbo.glGenFramebuffers(1)
         glfbo.glBindFramebuffer(glfbo.GL_FRAMEBUFFER, fb )
         
         glEnable(GL_TEXTURE_2D)
         tex = glGenTextures(1)
         glBindTexture(GL_TEXTURE_2D, tex)
         texwidth = textureSize
         data = np.zeros((texwidth,texwidth,4), dtype=np.ubyte)
         
         ## Test texture dimensions first
         glTexImage2D(GL_PROXY_TEXTURE_2D, 0, GL_RGBA, texwidth, texwidth, 0, GL_RGBA, GL_UNSIGNED_BYTE, None)
         if glGetTexLevelParameteriv(GL_PROXY_TEXTURE_2D, 0, GL_TEXTURE_WIDTH) == 0:
             raise Exception("OpenGL failed to create 2D texture (%dx%d); too large for this hardware." % shape[:2])
         ## create teture
         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texwidth, texwidth, 0, GL_RGBA, GL_UNSIGNED_BYTE, data.transpose((1,0,2)))
         
         self.opts['viewport'] = (0, 0, w, h)  # viewport is the complete image; this ensures that paintGL(region=...) 
                                               # is interpreted correctly.
         p2 = 2 * padding
         for x in range(-padding, w-padding, texwidth-p2):
             for y in range(-padding, h-padding, texwidth-p2):
                 x2 = min(x+texwidth, w+padding)
                 y2 = min(y+texwidth, h+padding)
                 w2 = x2-x
                 h2 = y2-y
                 
                 ## render to texture
                 glfbo.glFramebufferTexture2D(glfbo.GL_FRAMEBUFFER, glfbo.GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex, 0)
                 
                 self.paintGL(region=(x, h-y-h2, w2, h2), viewport=(0, 0, w2, h2))  # only render sub-region
                 glBindTexture(GL_TEXTURE_2D, tex) # fixes issue #366
                 
                 ## read texture back to array
                 data = glGetTexImage(GL_TEXTURE_2D, 0, format, type)
                 data = np.fromstring(data, dtype=np.ubyte).reshape(texwidth,texwidth,4).transpose(1,0,2)[:, ::-1]
                 output[x+padding:x2-padding, y+padding:y2-padding] = data[padding:w2-padding, -(h2-padding):-padding]
                 
     finally:
         self.opts['viewport'] = None
         glfbo.glBindFramebuffer(glfbo.GL_FRAMEBUFFER, 0)
         glBindTexture(GL_TEXTURE_2D, 0)
         if tex is not None:
             glDeleteTextures([tex])
         if fb is not None:
             glfbo.glDeleteFramebuffers([fb])
         
     return output
コード例 #4
0
ファイル: _pyopengl.py プロジェクト: LiloD/vispy
def glDeleteFramebuffer(framebuffer):
    FBO.glDeleteFramebuffers(1, [framebuffer])