Пример #1
0
    def drawGL(self,canvas=None,mode=None,color=None,**kargs):
        """Draw the geometry on the specified canvas.

        The drawing parameters not provided by the Actor itself, are
        derived from the canvas defaults.

        mode and color can be overridden for the sole purpose of allowing
        the recursive use for modes ending on 'wire' ('smoothwire' or
        'flatwire'). In these cases, two drawing operations are done:
        one with mode='wireframe' and color=black, and one with mode=mode[:-4].
        """
        from canvas import glLineStipple
        if canvas is None:
            canvas = pf.canvas
        if mode is None:
           mode = self.mode
        if mode is None:
            mode = canvas.rendermode

        if mode.endswith('wire'):
            self.drawGL(mode=mode[:-4])
            # draw the lines without lights
            canvas.glLight(False)
            self.drawGL(mode='wireframe',color=asarray(black))
            return
                            
        ############# set drawing attributes #########
        alpha = self.alpha
        if alpha is None:
            alpha = canvas.settings.alpha
        
        if color is None:
            color,colormap = self.color,self.colormap
            bkcolor, bkcolormap = self.bkcolor,self.bkcolormap
        else:
            # THIS OPTION IS ONLY MEANT FOR OVERRIDING THE COLOR
            # WITH THE EDGECOLOR IN ..wire DRAWING MODES
            # SO NO NEED TO SET bkcolor
            color,colormap = saneColor(color),None
            bkcolor, bkcolormap = None,None

        # convert color index to full colors
        if color is not None and color.dtype.kind == 'i':
            color = colormap[color]

        if bkcolor is not None and bkcolor.dtype.kind == 'i':
            bkcolor = bkcolormap[bkcolor]
        
        linewidth = self.linewidth
        if linewidth is None:
            linewidth = canvas.settings.linewidth

        if self.linewidth is not None:
            GL.glLineWidth(self.linewidth)

        if self.linestipple is not None:
            glLineStipple(*self.linestipple)

        if mode.startswith('smooth'):
            if hasattr(self,'specular'):
                fill_mode = GL.GL_FRONT
                import colors
                if color is not None:
                    spec = color * self.specular# *  pf.canvas.specular
                    spec = append(spec,1.)
                else:
                    spec = colors.GREY(self.specular)# *  pf.canvas.specular
                GL.glMaterialfv(fill_mode,GL.GL_SPECULAR,spec)
                GL.glMaterialfv(fill_mode,GL.GL_EMISSION,spec)
                GL.glMaterialfv(fill_mode,GL.GL_SHININESS,self.specular)

        ################## draw the geometry #################
        nplex = self.nplex()
        
        if nplex == 1:
            marksize = self.marksize
            if marksize is None:
                marksize = canvas.settings.pointsize
            # THIS SHOULD GO INTO drawPoints
            if self.elems is None:
                coords = self.coords
            else:
                coords = self.coords[self.elems]
            drawPoints(coords,color,alpha,marksize)

        elif nplex == 2:
            drawLines(self.coords,self.elems,color)
            
        # beware: some Formex eltypes are strings and may not
        # represent a valid Mesh elementType
        # THis is only here for Formex type.
        # We can probably remove it if we avoid eltype 'curve'
        elif nplex == 3 and self.eltype in ['curve','line3']:
            drawQuadraticCurves(self.coords,self.elems,color)

        elif self.eltype is None:
            # polygons
            if mode=='wireframe' :
                drawPolyLines(self.coords,self.elems,color)
            else:
                if bkcolor is not None:
                    GL.glEnable(GL.GL_CULL_FACE)
                    GL.glCullFace(GL.GL_BACK)
                drawPolygons(self.coords,self.elems,mode,color,alpha)
                if bkcolor is not None:
                    GL.glCullFace(GL.GL_FRONT)
                    drawPolygons(self.coords,self.elems,mode,bkcolor,alpha)
                    GL.glDisable(GL.GL_CULL_FACE)
                    
        else:
            el = elementType(self.eltype)
            
            if mode=='wireframe' or el.ndim < 2:
                for edges in el.getDrawEdges(el.name() in pf.cfg['draw/quadline']):
                    drawEdges(self.coords,self.elems,edges,edges.eltype,color)    
            else:
                for faces in el.getDrawFaces(el.name() in pf.cfg['draw/quadsurf']):
                    print faces.report()
                    if bkcolor is not None:
                        # Enable drawing front and back with different colors
                        GL.glEnable(GL.GL_CULL_FACE)
                        GL.glCullFace(GL.GL_BACK)
                    # Draw the front sides
                    drawFaces(self.coords,self.elems,faces,faces.eltype,mode,color,alpha)
                    if bkcolor is not None:
                        # Draw the back sides
                        GL.glCullFace(GL.GL_FRONT)
                        drawFaces(self.coords,self.elems,faces,faces.eltype,mode,bkcolor,alpha)
                        GL.glDisable(GL.GL_CULL_FACE)
Пример #2
0
    def drawGL(self,canvas=None,mode=None,color=None,**kargs):
        """Draw the geometry on the specified canvas.

        The drawing parameters not provided by the Actor itself, are
        derived from the canvas defaults.

        mode and color can be overridden for the sole purpose of allowing
        the recursive use for modes ending on 'wire' ('smoothwire' or
        'flatwire'). In these cases, two drawing operations are done:
        one with mode='wireframe' and color=black, and one with mode=mode[:-4].
        """
        from canvas import glLineStipple
        if canvas is None:
            canvas = pf.canvas
        #print "Calling with canvas %s" % canvas
        #print "Calling with mode %s" % mode
        if mode is None:
           mode = self.mode
        #print "self.mode %s" % mode
        if mode is None:
            mode = canvas.rendermode
        #print "DRAWING WITH MODE %s" % mode

        if mode.endswith('wire'):
            self.drawGL(mode=mode[:-4])
            # draw the lines without lights
            canvas.glLight(False)
            self.drawGL(mode='wireframe',color=asarray(black))
            return
                            
        ############# set drawing attributes #########
        alpha = self.alpha
        if alpha is None:
            alpha = canvas.settings.alpha
        
        if color is None:
            color,colormap = self.color,self.colormap
        else:
            color,colormap = saneColor(color),None

        if color is None:  # set canvas default
            color,colormap = canvas.settings.fgcolor,canvas.settings.colormap

        if color is None:
            # no color
            pass
        
        elif color.dtype.kind == 'f' and color.ndim == 1:
            # single color: set now
            GL.glColor(append(color,alpha))
            color = None

        elif color.dtype.kind == 'i':
            # color index: convert to full colors
            color = colormap[color]

        else:
            # a full color array: set later while drawing
            pass


        bkcolor, bkcolormap = self.bkcolor,self.bkcolormap
        if bkcolor is None:  # set canvas default
            bkcolor,bkcolormap = canvas.settings.bkcolor,canvas.settings.bkcolormap

        if bkcolor is not None and bkcolor.dtype.kind == 'i':
            # convert index to colors
            bkcolor = bkcolormap[bkcolor]
        
        linewidth = self.linewidth
        if linewidth is None:
            linewidth = canvas.settings.linewidth

        if self.linewidth is not None:
            GL.glLineWidth(self.linewidth)

        if self.linestipple is not None:
            glLineStipple(*self.linestipple)

        if mode.startswith('smooth'):
            if hasattr(self,'specular'):
                fill_mode = GL.GL_FRONT
                import colors
                if color is not None:
                    spec = color * self.specular# *  pf.canvas.specular
                    spec = append(spec,1.)
                else:
                    spec = colors.GREY(self.specular)# *  pf.canvas.specular
                #print self.coords.shape
                #print "SETTING SPECULAR to %s" % str(spec)
                GL.glMaterialfv(fill_mode,GL.GL_SPECULAR,spec)
                GL.glMaterialfv(fill_mode,GL.GL_EMISSION,spec)
                GL.glMaterialfv(fill_mode,GL.GL_SHININESS,self.specular)

        ################## draw the geometry #################
        nplex = self.nplex()
        #print "ELTYPE=%s" % self.eltype
        
        if nplex == 1:
            marksize = self.marksize
            if marksize is None:
                marksize = canvas.settings.pointsize
            # THIS COULD GO INTO drawPoints
            if self.elems is None:
                coords = self.coords
            else:
                coords = self.coords[self.elems]
            drawPoints(coords,color,alpha,marksize)

        elif nplex == 2:
            #save = pf.canvas.hasLight()
            #pf.canvas.glLight(False)
            drawLines(self.coords,self.elems,color)
            #pf.canvas.glLight(save)
        
        elif self.eltype == 'curve' and nplex == 3:
            pf.debug("DRAWING WITH drawQuadraticCurves")
            drawQuadraticCurves(self.coords,color,n=quadratic_curve_ndiv)
            
        elif self.eltype == 'nurbs' and (nplex == 3 or nplex == 4):
            pf.debug("DRAWING WITH drawNurbsCurves")
            drawNurbsCurves(self.coords,color)
            
        elif self.eltype is None:
            # polygons
            if mode=='wireframe' :
                drawPolyLines(self.coords,self.elems,color)
            else:
                if bkcolor is not None:
                    #print "COLOR=%s" % color
                    #print "BKCOLOR =%s" % bkcolor
                    # Draw front and back with different colors
                    #from canvas import glCulling
                    #glCulling()
                    GL.glEnable(GL.GL_CULL_FACE)
                    GL.glCullFace(GL.GL_BACK)
                    #print "DRAWING FRONT SIDES"
                drawPolygons(self.coords,self.elems,mode,color,alpha)
                if bkcolor is not None:
                    #print "DRAWING BACK SIDES"
                    GL.glCullFace(GL.GL_FRONT)
                    GL.glColor(append(bkcolor,alpha))
                    drawPolygons(self.coords,self.elems,mode,bkcolor,alpha)
                    GL.glDisable(GL.GL_CULL_FACE)
                   
        else:
            try:
                el = getattr(elements,self.eltype.capitalize())
            except:
                raise ValueError,"Invalid eltype %s" % str(self.eltype)
            if mode=='wireframe' :
                drawEdges(self.coords,self.elems,el.edges,color)    
            else:
                if hasattr(el,'drawfaces'):
                    faces = el.drawfaces
                else:
                    faces = el.faces
                if bkcolor is not None:
                    #print "COLOR=%s" % color
                    #print "BKCOLOR =%s" % bkcolor
                    # Draw front and back with different colors
                    #from canvas import glCulling
                    #glCulling()
                    GL.glEnable(GL.GL_CULL_FACE)
                    GL.glCullFace(GL.GL_BACK)
                    #print "DRAWING FRONT SIDES"
                drawFaces(self.coords,self.elems,faces,mode,color,alpha)
                if bkcolor is not None:
                    #print "DRAWING BACK SIDES"
                    GL.glCullFace(GL.GL_FRONT)
                    GL.glColor(append(bkcolor,alpha))
                    drawFaces(self.coords,self.elems,faces,mode,bkcolor,alpha)
                    GL.glDisable(GL.GL_CULL_FACE)