def initializeGL(self): glClearColor(0.0, 0.0, 0.0, 0.0) for visual in self.visuals: visual.init_gl() # Get AA shader fname = os.path.join(os.path.dirname(__file__), 'ddaa.glsl') DDAA_SHADER = open(fname , 'rb').read().decode('ascii', 'ignore') DDAA_SHADER0, DDAA_SHADER1, DDAA_SHADER2 = DDAA_SHADER.split('='*80) self._aaShader = Shader(None, DDAA_SHADER0)
def _pass_smaa(self, tex): # Get precomputed textures from smaa_tex import getSearchTex, getAreaTex searchTex = RenderTexture(2) searchTex.SetData(getSearchTex()) areaTex = RenderTexture(2) areaTex._interpolate = True # Enforce GL_LINEAR areaTex.SetData(getAreaTex()) # Compose shader code bigOne = self._shader_code[-1] edgeVertex = bigOne + self._shader_code[0] edgeFragment = bigOne + self._shader_code[1] blendVertex = bigOne + self._shader_code[2] blendFragment = bigOne + self._shader_code[3] neighborhoodVertex = bigOne + self._shader_code[4] neighborhoodFragment = bigOne + self._shader_code[5] if True: # Pass 1 fb = fb_edges = FrameBuffer(W, H) edgesTex = RenderTexture(2) edgesTex.create_empty_texture(W, H) # Must match depth buffer! fb.attach_texture(edgesTex) fb.add_depth_buffer() # fb.enable() gl.glClearColor(1,1,1,1); gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) self.setup_scene(0, 0, W, H); gl.glDisable(gl.GL_BLEND) # shader = Shader(edgeVertex, edgeFragment) shader.bind() tex.Enable(0) shader.uniformi('texture', 0) gl.glBegin(gl.GL_QUADS) gl.glTexCoord(0,0); gl.glVertex(0,0); gl.glTexCoord(0,1); gl.glVertex(0,1); gl.glTexCoord(1,1); gl.glVertex(1,1); gl.glTexCoord(1,0); gl.glVertex(1,0); gl.glEnd() fb.disable() tex.Disable() shader.unbind() if True: # Pass 2 fb = FrameBuffer(W, H) blendTex = RenderTexture(2) blendTex.create_empty_texture(W, H) # Must match depth buffer! fb.attach_texture(blendTex) fb.add_depth_buffer() # fb.enable() gl.glClearColor(1,1,1,1); gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) self.setup_scene(0, 0, W, H); gl.glDisable(gl.GL_BLEND) # shader = Shader(blendVertex, blendFragment) shader.bind() edgesTex.Enable(0) areaTex.Enable(1) searchTex.Enable(2) shader.uniformi('edge_tex', 0) shader.uniformi('area_tex', 1) shader.uniformi('search_tex', 2) # gl.glBegin(gl.GL_QUADS) gl.glTexCoord(0,0); gl.glVertex(0,0); gl.glTexCoord(0,1); gl.glVertex(0,1); gl.glTexCoord(1,1); gl.glVertex(1,1); gl.glTexCoord(1,0); gl.glVertex(1,0); gl.glEnd() fb.disable() edgesTex.Disable() areaTex.Disable() searchTex.Disable() shader.unbind() if True: # Pass 3 gl.glClearColor(1,1,1,1); gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) self.setup_scene(0, 0, W, H); gl.glDisable(gl.GL_BLEND) # shader = Shader(neighborhoodVertex, neighborhoodFragment) shader.bind() tex.Enable(0) blendTex.Enable(1) shader.uniformi('texture', 0) shader.uniformi('blend_tex', 1) # gl.glBegin(gl.GL_QUADS) gl.glTexCoord(0,0); gl.glVertex(0,0); gl.glTexCoord(0,1); gl.glVertex(0,1); gl.glTexCoord(1,1); gl.glVertex(1,1); gl.glTexCoord(1,0); gl.glVertex(1,0); gl.glEnd() tex.Disable() blendTex.Disable() shader.unbind()
class Window(QtOpenGL.QGLWidget): """Just a simple display window""" def __init__(self, visuals): self.zoom = 1.0 self.visuals = visuals QtOpenGL.QGLWidget.__init__(self) self.resize(800,800) self.show() def initializeGL(self): glClearColor(0.0, 0.0, 0.0, 0.0) for visual in self.visuals: visual.init_gl() # Get AA shader fname = os.path.join(os.path.dirname(__file__), 'ddaa.glsl') DDAA_SHADER = open(fname , 'rb').read().decode('ascii', 'ignore') DDAA_SHADER0, DDAA_SHADER1, DDAA_SHADER2 = DDAA_SHADER.split('='*80) self._aaShader = Shader(None, DDAA_SHADER0) def resizeGL(self, w, h): glViewport(0, 0, w, h) def paintGL(self): # self._paint() # return W, H = self.width(), self.height() # Set up FBO fb = FrameBuffer(W, H) tex = RenderTexture(2) tex.create_empty_texture(W, H) # Must match depth buffer! fb.attach_texture(tex) fb.add_depth_buffer() fb.check() # Draw scene to off-screen buffer fb.enable() gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) self._setup_scene(0,0,W,H) self._paint() fb.disable() # Draw texture to screen gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) self._setup_scene(0,0,W,H) tex.Enable(0) self._aaShader.bind() #shader.uniformi('texture', tex._texId) self._aaShader.uniformf('shape', W, H) self._aaShader.uniformi('texture', 0) gl.glBegin(gl.GL_QUADS) gl.glTexCoord(0,0); gl.glVertex(0,0); gl.glTexCoord(0,1); gl.glVertex(0,1); gl.glTexCoord(1,1); gl.glVertex(1,1); gl.glTexCoord(1,0); gl.glVertex(1,0); gl.glEnd() self._aaShader.unbind() tex.Disable() def _setup_scene(self, *args): gl.glViewport(*args); gl.glMatrixMode(gl.GL_PROJECTION); gl.glLoadIdentity(); glu.gluOrtho2D(0.0, 1.0, 0.0, 1.0); gl.glMatrixMode(gl.GL_MODELVIEW); gl.glLoadIdentity(); gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) gl.glEnable(gl.GL_BLEND) def _paint(self): transform = np.array([ [self.zoom,0,0,0], [0,self.zoom,0,0], [0,0,1,0], [0,0,0,1] ], dtype=np.float32) view_transform = np.array([ [self.width()/2.,0,0,0], [0,self.height()/2.,0,0], [0,0,1,0], [self.width()/2.,self.height()/2.,0,1] ], dtype=np.float32) for visual in self.visuals: visual.draw(transform, view_transform) def wheelEvent(self, ev): self.zoom *= 1.05 ** (ev.delta()/120.) self.update()
def _pass_ssaa(self, tex, w, h, scale): # Select Lanczos kernel based on scale. aakernel = [None, None, [0.44031130485056913, 0.29880437751590694, 0.04535643028360444, -0.06431646022479595], [0.2797564513818748, 0.2310717037833796, 0.11797652759318597, 0.01107354293249700], ][scale] # Prepare for drawing texture to screen gl.glClearColor(1,1,1,1); gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) self.setup_scene(0, 0, W, H) tex.Enable(0) shader = Shader(None, self._shader_code[0]) shader.bind() shader.uniformf('shape', w, h) # resolution of previous pass shader.uniformi('texture', 0) if aakernel: shader.uniformf('aakernel', *aakernel) # Draw texture to screen gl.glBegin(gl.GL_QUADS) gl.glTexCoord(0,0); gl.glVertex(0,0); gl.glTexCoord(0,1); gl.glVertex(0,1); gl.glTexCoord(1,1); gl.glVertex(1,1); gl.glTexCoord(1,0); gl.glVertex(1,0); gl.glEnd() shader.unbind() tex.Disable()
def _pass_aa(self, tex): if self._npasses == 1: # Prepare for drawing texture to screen gl.glClearColor(1,1,1,1); gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) self.setup_scene(0, 0, W, H) tex.Enable(0) shader = Shader(None, self._shader_code[0]) shader.bind() shader.uniformf('shape', W, H) shader.uniformi('texture', 0) # Draw texture to screen gl.glBegin(gl.GL_QUADS) gl.glTexCoord(0,0); gl.glVertex(0,0); gl.glTexCoord(0,1); gl.glVertex(0,1); gl.glTexCoord(1,1); gl.glVertex(1,1); gl.glTexCoord(1,0); gl.glVertex(1,0); gl.glEnd() shader.unbind() tex.Disable() return # We get here only if npasses != 1 for i in range(self._npasses): # Create frame buffer to render to fb = fb_edges = FrameBuffer(W, H) resultTex = RenderTexture(2) resultTex.create_empty_texture(W, H) # Must match depth buffer! fb.attach_texture(resultTex) fb.add_depth_buffer() fb.enable() # Prepare for drawing texture to screen gl.glClearColor(1,1,1,1); gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) self.setup_scene(0, 0, W, H) tex.Enable(0) shader = Shader(None, self._shader_code[0]) shader.bind() shader.uniformf('shape', W, H) shader.uniformi('texture', 0) # Draw texture to screen gl.glBegin(gl.GL_QUADS) gl.glTexCoord(0,0); gl.glVertex(0,0); gl.glTexCoord(0,1); gl.glVertex(0,1); gl.glTexCoord(1,1); gl.glVertex(1,1); gl.glTexCoord(1,0); gl.glVertex(1,0); gl.glEnd() shader.unbind() tex.Disable() fb.disable() # Prepare for next round tex = resultTex # Prepare for drawing texture to screen gl.glClearColor(1,1,1,1); gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) self.setup_scene(0, 0, W, H) tex.Enable(0) # Draw texture to screen gl.glBegin(gl.GL_QUADS) gl.glTexCoord(0,0); gl.glVertex(0,0); gl.glTexCoord(0,1); gl.glVertex(0,1); gl.glTexCoord(1,1); gl.glVertex(1,1); gl.glTexCoord(1,0); gl.glVertex(1,0); gl.glEnd() tex.Disable()
def _pass_smaa(self, tex): # Get precomputed textures from smaa_tex import getSearchTex, getAreaTex searchTex = RenderTexture(2) searchTex.SetData(getSearchTex()) areaTex = RenderTexture(2) areaTex._interpolate = True # Enforce GL_LINEAR areaTex.SetData(getAreaTex()) # Compose shader code bigOne = self._shader_code[-1] edgeVertex = bigOne + self._shader_code[0] edgeFragment = bigOne + self._shader_code[1] blendVertex = bigOne + self._shader_code[2] blendFragment = bigOne + self._shader_code[3] neighborhoodVertex = bigOne + self._shader_code[4] neighborhoodFragment = bigOne + self._shader_code[5] if True: # Pass 1 fb = fb_edges = FrameBuffer(W, H) edgesTex = RenderTexture(2) edgesTex.create_empty_texture(W, H) # Must match depth buffer! fb.attach_texture(edgesTex) fb.add_depth_buffer() # fb.enable() gl.glClearColor(1, 1, 1, 1) gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) self.setup_scene(0, 0, W, H) gl.glDisable(gl.GL_BLEND) # shader = Shader(edgeVertex, edgeFragment) shader.bind() tex.Enable(0) shader.uniformi('texture', 0) gl.glBegin(gl.GL_QUADS) gl.glTexCoord(0, 0) gl.glVertex(0, 0) gl.glTexCoord(0, 1) gl.glVertex(0, 1) gl.glTexCoord(1, 1) gl.glVertex(1, 1) gl.glTexCoord(1, 0) gl.glVertex(1, 0) gl.glEnd() fb.disable() tex.Disable() shader.unbind() if True: # Pass 2 fb = FrameBuffer(W, H) blendTex = RenderTexture(2) blendTex.create_empty_texture(W, H) # Must match depth buffer! fb.attach_texture(blendTex) fb.add_depth_buffer() # fb.enable() gl.glClearColor(1, 1, 1, 1) gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) self.setup_scene(0, 0, W, H) gl.glDisable(gl.GL_BLEND) # shader = Shader(blendVertex, blendFragment) shader.bind() edgesTex.Enable(0) areaTex.Enable(1) searchTex.Enable(2) shader.uniformi('edge_tex', 0) shader.uniformi('area_tex', 1) shader.uniformi('search_tex', 2) # gl.glBegin(gl.GL_QUADS) gl.glTexCoord(0, 0) gl.glVertex(0, 0) gl.glTexCoord(0, 1) gl.glVertex(0, 1) gl.glTexCoord(1, 1) gl.glVertex(1, 1) gl.glTexCoord(1, 0) gl.glVertex(1, 0) gl.glEnd() fb.disable() edgesTex.Disable() areaTex.Disable() searchTex.Disable() shader.unbind() if True: # Pass 3 gl.glClearColor(1, 1, 1, 1) gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) self.setup_scene(0, 0, W, H) gl.glDisable(gl.GL_BLEND) # shader = Shader(neighborhoodVertex, neighborhoodFragment) shader.bind() tex.Enable(0) blendTex.Enable(1) shader.uniformi('texture', 0) shader.uniformi('blend_tex', 1) # gl.glBegin(gl.GL_QUADS) gl.glTexCoord(0, 0) gl.glVertex(0, 0) gl.glTexCoord(0, 1) gl.glVertex(0, 1) gl.glTexCoord(1, 1) gl.glVertex(1, 1) gl.glTexCoord(1, 0) gl.glVertex(1, 0) gl.glEnd() tex.Disable() blendTex.Disable() shader.unbind()
def _pass_ssaa(self, tex, w, h, scale): # Select Lanczos kernel based on scale. aakernel = [ None, None, [ 0.44031130485056913, 0.29880437751590694, 0.04535643028360444, -0.06431646022479595 ], [ 0.2797564513818748, 0.2310717037833796, 0.11797652759318597, 0.01107354293249700 ], ][scale] # Prepare for drawing texture to screen gl.glClearColor(1, 1, 1, 1) gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) self.setup_scene(0, 0, W, H) tex.Enable(0) shader = Shader(None, self._shader_code[0]) shader.bind() shader.uniformf('shape', w, h) # resolution of previous pass shader.uniformi('texture', 0) if aakernel: shader.uniformf('aakernel', *aakernel) # Draw texture to screen gl.glBegin(gl.GL_QUADS) gl.glTexCoord(0, 0) gl.glVertex(0, 0) gl.glTexCoord(0, 1) gl.glVertex(0, 1) gl.glTexCoord(1, 1) gl.glVertex(1, 1) gl.glTexCoord(1, 0) gl.glVertex(1, 0) gl.glEnd() shader.unbind() tex.Disable()
def _pass_aa(self, tex): if self._npasses == 1: # Prepare for drawing texture to screen gl.glClearColor(1, 1, 1, 1) gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) self.setup_scene(0, 0, W, H) tex.Enable(0) shader = Shader(None, self._shader_code[0]) shader.bind() shader.uniformf('shape', W, H) shader.uniformi('texture', 0) # Draw texture to screen gl.glBegin(gl.GL_QUADS) gl.glTexCoord(0, 0) gl.glVertex(0, 0) gl.glTexCoord(0, 1) gl.glVertex(0, 1) gl.glTexCoord(1, 1) gl.glVertex(1, 1) gl.glTexCoord(1, 0) gl.glVertex(1, 0) gl.glEnd() shader.unbind() tex.Disable() return # We get here only if npasses != 1 for i in range(self._npasses): # Create frame buffer to render to fb = fb_edges = FrameBuffer(W, H) resultTex = RenderTexture(2) resultTex.create_empty_texture(W, H) # Must match depth buffer! fb.attach_texture(resultTex) fb.add_depth_buffer() fb.enable() # Prepare for drawing texture to screen gl.glClearColor(1, 1, 1, 1) gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) self.setup_scene(0, 0, W, H) tex.Enable(0) shader = Shader(None, self._shader_code[0]) shader.bind() shader.uniformf('shape', W, H) shader.uniformi('texture', 0) # Draw texture to screen gl.glBegin(gl.GL_QUADS) gl.glTexCoord(0, 0) gl.glVertex(0, 0) gl.glTexCoord(0, 1) gl.glVertex(0, 1) gl.glTexCoord(1, 1) gl.glVertex(1, 1) gl.glTexCoord(1, 0) gl.glVertex(1, 0) gl.glEnd() shader.unbind() tex.Disable() fb.disable() # Prepare for next round tex = resultTex # Prepare for drawing texture to screen gl.glClearColor(1, 1, 1, 1) gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) self.setup_scene(0, 0, W, H) tex.Enable(0) # Draw texture to screen gl.glBegin(gl.GL_QUADS) gl.glTexCoord(0, 0) gl.glVertex(0, 0) gl.glTexCoord(0, 1) gl.glVertex(0, 1) gl.glTexCoord(1, 1) gl.glVertex(1, 1) gl.glTexCoord(1, 0) gl.glVertex(1, 0) gl.glEnd() tex.Disable()