def _init_blending(self): GL.glEnable(GL.GL_BLEND) GL.glBlendColor(1.0, 1.0, 1.0, 1.0) GL.glBlendFunc(GL.GL_ONE, GL.GL_ONE) GL.glBlendEquation(GL.GL_MAX)
def draw(self): gl.glEnable( gl.GL_TEXTURE_2D ) gl.glDisable( gl.GL_DEPTH_TEST ) gl.glEnableClientState(gl.GL_VERTEX_ARRAY) gl.glEnableClientState(gl.GL_COLOR_ARRAY) gl.glEnableClientState(gl.GL_TEXTURE_COORD_ARRAY) gl.glEnableClientState(gl.GL_VERTEX_ARRAY) gl.glVertexPointer(3, gl.GL_FLOAT, 0, self.vertices) gl.glColorPointer(4, gl.GL_FLOAT, 0, self.colors) gl.glTexCoordPointer(2, gl.GL_FLOAT, 0, self.texcoords) r,g,b = 0,0,0 gl.glColor( 1, 1, 1, 1 ) gl.glEnable( gl.GL_BLEND ) #gl.glBlendFunc( gl.GL_CONSTANT_COLOR_EXT, gl.GL_ONE_MINUS_SRC_COLOR ) #gl.glBlendColor(r,g,b,1) gl.glBlendFunc( gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA ) gl.glBlendColor( 1, 1, 1, 1 ) gl.glEnableVertexAttribArray( 1 ); gl.glVertexAttribPointer( 1, 1, gl.GL_FLOAT, gl.GL_FALSE, 0, self.attrib) shader.bind() shader.uniformi('texture', 0) shader.uniformf('pixel', 1.0/512, 1.0/512) gl.glDrawElements(gl.GL_TRIANGLES, len(self.indices), gl.GL_UNSIGNED_INT, self.indices) shader.unbind() gl.glDisableVertexAttribArray( 1 ); gl.glDisableClientState(gl.GL_VERTEX_ARRAY) gl.glDisableClientState(gl.GL_COLOR_ARRAY) gl.glDisableClientState(gl.GL_TEXTURE_COORD_ARRAY) gl.glDisable( gl.GL_TEXTURE_2D ) gl.glDisable( gl.GL_BLEND )
def draw(self): gl.glEnable(gl.GL_TEXTURE_2D) gl.glDisable(gl.GL_DEPTH_TEST) gl.glEnableClientState(gl.GL_VERTEX_ARRAY) gl.glEnableClientState(gl.GL_COLOR_ARRAY) gl.glEnableClientState(gl.GL_TEXTURE_COORD_ARRAY) gl.glEnableClientState(gl.GL_VERTEX_ARRAY) gl.glVertexPointer(3, gl.GL_FLOAT, 0, self.vertices) gl.glColorPointer(4, gl.GL_FLOAT, 0, self.colors) gl.glTexCoordPointer(2, gl.GL_FLOAT, 0, self.texcoords) alpha = 1 gl.glEnable(gl.GL_COLOR_MATERIAL) gl.glBlendFunc(gl.GL_CONSTANT_COLOR_EXT, gl.GL_ONE_MINUS_SRC_COLOR) gl.glEnable(gl.GL_BLEND) gl.glColor3f(alpha, alpha, alpha) gl.glBlendColor(1 - alpha, 1 - alpha, 1 - alpha, 1) gl.glEnableVertexAttribArray(1) gl.glVertexAttribPointer(1, 1, gl.GL_FLOAT, gl.GL_FALSE, 0, self.attrib) shader.bind() shader.uniformi('texture', 0) shader.uniformf('pixel', 1.0 / 512, 1.0 / 512) gl.glDrawElements(gl.GL_TRIANGLES, len(self.indices), gl.GL_UNSIGNED_INT, self.indices) shader.unbind() gl.glDisableVertexAttribArray(1) gl.glDisableClientState(gl.GL_VERTEX_ARRAY) gl.glDisableClientState(gl.GL_COLOR_ARRAY) gl.glDisableClientState(gl.GL_TEXTURE_COORD_ARRAY) gl.glDisable(gl.GL_TEXTURE_2D) gl.glDisable(gl.GL_BLEND)
def _DrawLines(self): # set stipple style if not self.ls in lineStyles: stipple = False else: stipple = lineStyles[self.ls] # if stipple and self.lw: gl.glEnable(gl.GL_LINE_STIPPLE) gl.glLineStipple(int(round(self.lw)), stipple) else: gl.glDisable(gl.GL_LINE_STIPPLE) # init vertex array gl.glEnableClientState(gl.GL_VERTEX_ARRAY) gl.glVertexPointerf(self._points.data) # linepieces drawn on top of other should draw just fine. See issue #95 gl.glDepthFunc(gl.GL_LEQUAL) # init blending. Only use constant blendfactor when alpha<1 gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) if self._alpha1 < 1: #if len(self._points) < 1000: if getOpenGlCapable('1.4', 'transparant points and lines'): gl.glBlendFunc(gl.GL_CONSTANT_ALPHA, gl.GL_ONE_MINUS_CONSTANT_ALPHA) gl.glBlendColor(0.0, 0.0, 0.0, self._alpha1) gl.glDisable(gl.GL_DEPTH_TEST) # get color clr = getColor(self.lc) if clr and self._alpha1 > 0: # set width and color gl.glLineWidth(self.lw) gl.glColor3f(clr[0], clr[1], clr[2]) # draw method = gl.GL_LINE_STRIP if self.ls == '+': method = gl.GL_LINES gl.glDrawArrays(method, 0, len(self._points)) # flush! gl.glFlush() # clean up gl.glDisable(gl.GL_LINE_STIPPLE) gl.glLineStipple(int(round(self.lw)), int('1111111111111111', 2)) gl.glEnable(gl.GL_DEPTH_TEST) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) gl.glDisableClientState(gl.GL_VERTEX_ARRAY) gl.glDepthFunc(gl.GL_LESS)
def OnDraw(self): # Get coords and prepare for drawing pp = self._GetCords() gl.glEnableClientState(gl.GL_VERTEX_ARRAY) gl.glVertexPointerf(pp.data) # Draw outline clr = self._clr1 gl.glColor(clr[0], clr[1], clr[2]) gl.glLineWidth(1) ind = np.array([0, 1, 2, 3, 0], dtype=np.uint8) gl.glDrawElements(gl.GL_LINE_STRIP, len(ind), gl.GL_UNSIGNED_BYTE, ind) # Enable transparancy # (note that this does not work on all OpenGl versions) alpha = 1.0 if vv.misc.getOpenGlCapable('1.4'): alpha = 0.3 gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(gl.GL_CONSTANT_ALPHA, gl.GL_ONE_MINUS_CONSTANT_ALPHA) gl.glBlendColor(0.0, 0.0, 0.0, alpha) # Get which bars to draw in what color tmp = [None, self._bar1, self._bar2, self._bar3, self._bar4] normalbars = [] dragbars = [] for i in range(1, 5): if i in self._refBars: dragbars.extend(tmp[i]) else: normalbars.extend(tmp[i]) # Draw normal bars if normalbars: clr = self._clr2 gl.glColor(clr[0], clr[1], clr[2]) ind = np.array(normalbars, dtype=np.uint8) gl.glDrawElements(gl.GL_QUADS, len(ind), gl.GL_UNSIGNED_BYTE, ind) # Draw bars being moved if dragbars: clr = self._clr1 gl.glColor(clr[0], clr[1], clr[2]) ind = np.array(dragbars, dtype=np.uint8) gl.glDrawElements(gl.GL_QUADS, len(ind), gl.GL_UNSIGNED_BYTE, ind) # Done gl.glFlush() gl.glDisableClientState(gl.GL_VERTEX_ARRAY) if alpha < 1.0: gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA)
def OnDraw(self): # Get coords and prepare for drawing pp = self._GetCords() gl.glEnableClientState(gl.GL_VERTEX_ARRAY) gl.glVertexPointerf(pp.data) # Draw outline clr = self._clr1 gl.glColor( clr[0], clr[1], clr[2]) gl.glLineWidth(1) ind = np.array([0,1,2,3,0],dtype=np.uint8) gl.glDrawElements(gl.GL_LINE_STRIP, len(ind), gl.GL_UNSIGNED_BYTE, ind) # Enable transparancy # (note that this does not work on all OpenGl versions) alpha = 1.0 if vv.misc.getOpenGlCapable('1.4'): alpha = 0.3 gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(gl.GL_CONSTANT_ALPHA, gl.GL_ONE_MINUS_CONSTANT_ALPHA) gl.glBlendColor(0.0,0.0,0.0, alpha) # Get which bars to draw in what color tmp = [None, self._bar1, self._bar2, self._bar3, self._bar4] normalbars = [] dragbars = [] for i in range(1,5): if i in self._refBars: dragbars.extend(tmp[i]) else: normalbars.extend(tmp[i]) # Draw normal bars if normalbars: clr = self._clr2 gl.glColor( clr[0], clr[1], clr[2]) ind = np.array(normalbars,dtype=np.uint8) gl.glDrawElements(gl.GL_QUADS, len(ind), gl.GL_UNSIGNED_BYTE, ind) # Draw bars being moved if dragbars: clr = self._clr1 gl.glColor( clr[0], clr[1], clr[2]) ind = np.array(dragbars,dtype=np.uint8) gl.glDrawElements(gl.GL_QUADS, len(ind), gl.GL_UNSIGNED_BYTE, ind) # Done gl.glFlush() gl.glDisableClientState(gl.GL_VERTEX_ARRAY) if alpha<1.0: gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA)
def _DrawLines(self): # set stipple style if not self.ls in lineStyles: stipple = False else: stipple = lineStyles[self.ls] # if stipple and self.lw: gl.glEnable(gl.GL_LINE_STIPPLE) gl.glLineStipple(int(round(self.lw)), stipple) else: gl.glDisable(gl.GL_LINE_STIPPLE) # init vertex array gl.glEnableClientState(gl.GL_VERTEX_ARRAY) gl.glVertexPointerf(self._points.data) # init blending. Only use constant blendfactor when alpha<1 gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) if self._alpha1<1: if getOpenGlCapable('1.4','transparant points and lines'): gl.glBlendFunc(gl.GL_CONSTANT_ALPHA, gl.GL_ONE_MINUS_CONSTANT_ALPHA) gl.glBlendColor(0.0,0.0,0.0, self._alpha1) gl.glDisable(gl.GL_DEPTH_TEST) # get color clr = getColor( self.lc ) if clr and self._alpha1>0: # set width and color gl.glLineWidth(self.lw) gl.glColor3f(clr[0], clr[1], clr[2]) # draw method = gl.GL_LINE_STRIP if self.ls == '+': method = gl.GL_LINES gl.glDrawArrays(method, 0, len(self._points)) # flush! gl.glFlush() # clean up gl.glDisable(gl.GL_LINE_STIPPLE) gl.glLineStipple(int(round(self.lw)), int('1111111111111111',2)) gl.glEnable(gl.GL_DEPTH_TEST) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) gl.glDisableClientState(gl.GL_VERTEX_ARRAY)
def render(self): with self.fbo: GL.glClearColor(0., 0., 0., 0.) GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT) self.pprogram.render() GL.glDisable(GL.GL_DEPTH_TEST) GL.glEnable(GL.GL_BLEND) GL.glBlendColor(1, 1, 1, .95) GL.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_CONSTANT_ALPHA) with self.fbo.texture: self.fsquad.draw() GL.glDisable(GL.GL_BLEND) GL.glEnable(GL.GL_DEPTH_TEST)
def _DrawPoints(self): # get colors (use color from edge or face if not present) clr1 = getColor(self.mc) clr2 = getColor(self.mec) # draw face or edge? drawFace = bool(self.mc) # if not ms or mw we would not get here drawEdge = self.mec and self.mew if not drawFace and not drawEdge: return # get figure f = self.GetFigure() if not f: return # init blending. Only use constant blendfactor when alpha<1 gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) if self._alpha1 < 1: if getOpenGlCapable('1.4', 'transparant points and lines'): gl.glBlendFunc(gl.GL_CONSTANT_ALPHA, gl.GL_ONE_MINUS_CONSTANT_ALPHA) gl.glBlendColor(0.0, 0.0, 0.0, self._alpha1) gl.glDisable(gl.GL_DEPTH_TEST) # init vertex array gl.glEnableClientState(gl.GL_VERTEX_ARRAY) gl.glVertexPointerf(self._points.data) # points drawn on top of points should draw (because we draw # the face and edge seperately) gl.glDepthFunc(gl.GL_LEQUAL) # Enable alpha test, such that fragments with 0 alpha # will not update the z-buffer. gl.glEnable(gl.GL_ALPHA_TEST) gl.glAlphaFunc(gl.GL_GREATER, 0.01) if self.ms in ['o', '.', 's'] and not drawEdge: # Use standard OpenGL points, faster and anti-aliased # Pure filled points or squares always work. # choose style if self.ms == 's': gl.glDisable(gl.GL_POINT_SMOOTH) else: gl.glEnable(gl.GL_POINT_SMOOTH) # draw faces only if drawFace: gl.glColor3f(clr1[0], clr1[1], clr1[2]) gl.glPointSize(self.mw) gl.glDrawArrays(gl.GL_POINTS, 0, len(self._points)) elif self.ms in ['o', '.', 's'] and drawFace and self.alpha == 1: # Use standard OpenGL points, faster and anti-aliased # If alpha=1 and we have a filled marker, we can draw in two steps. # choose style if self.ms == 's': gl.glDisable(gl.GL_POINT_SMOOTH) else: gl.glEnable(gl.GL_POINT_SMOOTH) # draw edges if drawEdge: gl.glColor3f(clr2[0], clr2[1], clr2[2]) gl.glPointSize(self.mw + self.mew * 2) gl.glDrawArrays(gl.GL_POINTS, 0, len(self._points)) # draw faces if drawFace: gl.glColor3f(clr1[0], clr1[1], clr1[2]) gl.glPointSize(self.mw) gl.glDrawArrays(gl.GL_POINTS, 0, len(self._points)) #elif self.alpha>0: else: # Use sprites # get sprites tmp = f._markerManager.GetSprites(self.ms, self.mw, self.mew) pSize, sprite1, sprite2 = tmp gl.glPointSize(pSize) # draw points for the edges if drawEdge: sprite2.Enable() gl.glColor3f(clr2[0], clr2[1], clr2[2]) gl.glDrawArrays(gl.GL_POINTS, 0, len(self._points)) # draw points for the faces if drawFace: sprite1.Enable() gl.glColor3f(clr1[0], clr1[1], clr1[2]) gl.glDrawArrays(gl.GL_POINTS, 0, len(self._points)) # disable sprites sprite1.Disable() # Could as well have used sprite2 # clean up gl.glDisable(gl.GL_ALPHA_TEST) gl.glEnable(gl.GL_DEPTH_TEST) gl.glDisableClientState(gl.GL_VERTEX_ARRAY) gl.glDepthFunc(gl.GL_LESS) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA)
def _DrawPoints(self): # get colors (use color from edge or face if not present) clr1 = getColor(self.mc) clr2 = getColor(self.mec) # draw face or edge? drawFace = bool(self.mc) # if not ms or mw we would not get here drawEdge = self.mec and self.mew if not drawFace and not drawEdge: return # get figure f = self.GetFigure() if not f: return # init blending. Only use constant blendfactor when alpha<1 gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) if self._alpha1<1: if getOpenGlCapable('1.4','transparant points and lines'): gl.glBlendFunc(gl.GL_CONSTANT_ALPHA, gl.GL_ONE_MINUS_CONSTANT_ALPHA) gl.glBlendColor(0.0,0.0,0.0, self._alpha1) gl.glDisable(gl.GL_DEPTH_TEST) # init vertex array gl.glEnableClientState(gl.GL_VERTEX_ARRAY) gl.glVertexPointerf(self._points.data) # points drawn on top of points should draw (because we draw # the face and edge seperately) gl.glDepthFunc(gl.GL_LEQUAL) # Enable alpha test, such that fragments with 0 alpha # will not update the z-buffer. gl.glEnable(gl.GL_ALPHA_TEST) gl.glAlphaFunc(gl.GL_GREATER, 0.01) if self.ms in ['o','.','s'] and not drawEdge: # Use standard OpenGL points, faster and anti-aliased # Pure filled points or squares always work. # choose style if self.ms == 's': gl.glDisable(gl.GL_POINT_SMOOTH) else: gl.glEnable(gl.GL_POINT_SMOOTH) # draw faces only if drawFace: gl.glColor3f(clr1[0],clr1[1],clr1[2]) gl.glPointSize(self.mw) gl.glDrawArrays(gl.GL_POINTS, 0, len(self._points)) elif self.ms in ['o','.','s'] and drawFace and self.alpha==1: # Use standard OpenGL points, faster and anti-aliased # If alpha=1 and we have a filled marker, we can draw in two steps. # choose style if self.ms == 's': gl.glDisable(gl.GL_POINT_SMOOTH) else: gl.glEnable(gl.GL_POINT_SMOOTH) # draw edges if drawEdge: gl.glColor3f(clr2[0],clr2[1],clr2[2]) gl.glPointSize(self.mw+self.mew*2) gl.glDrawArrays(gl.GL_POINTS, 0, len(self._points)) # draw faces if drawFace: gl.glColor3f(clr1[0],clr1[1],clr1[2]) gl.glPointSize(self.mw) gl.glDrawArrays(gl.GL_POINTS, 0, len(self._points)) #elif self.alpha>0: else: # Use sprites # get sprites tmp = f._markerManager.GetSprites(self.ms, self.mw, self.mew) pSize, sprite1, sprite2 = tmp gl.glPointSize(pSize) # draw points for the edges if drawEdge: sprite2.Enable() gl.glColor3f(clr2[0],clr2[1],clr2[2]) gl.glDrawArrays(gl.GL_POINTS, 0, len(self._points)) # draw points for the faces if drawFace: sprite1.Enable() gl.glColor3f(clr1[0],clr1[1],clr1[2]) gl.glDrawArrays(gl.GL_POINTS, 0, len(self._points)) # disable sprites sprite1.Disable() # Could as well have used sprite2 # clean up gl.glDisable(gl.GL_ALPHA_TEST) gl.glEnable(gl.GL_DEPTH_TEST) gl.glDisableClientState(gl.GL_VERTEX_ARRAY) gl.glDepthFunc(gl.GL_LESS) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA)
def draw(self): drawtext = self.strs + self.messages self.lines = len(drawtext) # draw head-up-display # see axis.py for more font/color configurability if ((self.showme == 0) or (self.lines == 0)): return GL.glMatrixMode(GL.GL_PROJECTION) GL.glPushMatrix() GL.glLoadIdentity() if not self.fontbase: self.fontbase = int(self.app.loadbitmapfont("9x15")) char_width, char_height = 9, 15 xmargin, ymargin = 5, 5 ypos = float(self.app.winfo_height()) GL.glOrtho(0.0, self.app.winfo_width(), 0.0, ypos, -1.0, 1.0) GL.glMatrixMode(GL.GL_MODELVIEW) GL.glPushMatrix() GL.glLoadIdentity() # draw the text box maxlen = max([len(p) for p in drawtext]) box_width = maxlen * char_width GL.glDepthFunc(GL.GL_ALWAYS) GL.glDepthMask(GL.GL_FALSE) GL.glDisable(GL.GL_LIGHTING) GL.glEnable(GL.GL_BLEND) GL.glEnable(GL.GL_NORMALIZE) GL.glBlendFunc(GL.GL_ONE, GL.GL_CONSTANT_ALPHA) GL.glColor3f(0.2, 0, 0) GL.glBlendColor(0, 0, 0, 0.5) # rgba GL.glBegin(GL.GL_QUADS) GL.glVertex3f(0, ypos, 1) # upper left GL.glVertex3f(0, ypos - 2 * ymargin - char_height * len(drawtext), 1) # lower left GL.glVertex3f(box_width + 2 * xmargin, ypos - 2 * ymargin - char_height * len(drawtext), 1) # lower right GL.glVertex3f(box_width + 2 * xmargin, ypos, 1) # upper right GL.glEnd() GL.glDisable(GL.GL_BLEND) GL.glEnable(GL.GL_LIGHTING) # fill the box with text maxlen = 0 ypos -= char_height + ymargin i = 0 GL.glDisable(GL.GL_LIGHTING) GL.glColor3f(0.9, 0.9, 0.9) for string in drawtext: maxlen = max(maxlen, len(string)) # if i < len(homed) and homed[i]: # GL.glRasterPos2i(6, ypos) # GL.glBitmap(13, 16, 0, 3, 17, 0, homeicon) GL.glRasterPos2i(xmargin, int(ypos)) for char in string: GL.glCallList(self.fontbase + ord(char)) # if i < len(homed) and limit[i]: # GL.glBitmap(13, 16, -5, 3, 17, 0, limiticon) ypos -= char_height i = i + 1 GL.glDepthFunc(GL.GL_LESS) GL.glDepthMask(GL.GL_TRUE) GL.glEnable(GL.GL_LIGHTING) GL.glPopMatrix() GL.glMatrixMode(GL.GL_PROJECTION) GL.glPopMatrix() GL.glMatrixMode(GL.GL_MODELVIEW)