def redraw(self):
        if __debug__:
         if hasattr(DejaVu, 'functionName'): DejaVu.functionName()
        self.tk.call(self._w, 'makecurrent')

        GL.glDisable( GL.GL_DEPTH_TEST )
        GL.glDisable( GL.GL_LIGHTING )
        #GL.glPolygonMode(GL.GL_FRONT, GL.GL_FILL)
        GL.glBegin(GL.GL_QUADS)
        GL.glColor3f(0.,0.,0.)
        GL.glVertex2f(0., 1.); GL.glVertex2f(0., 0.)
        GL.glColor3f(float(self.rgbMax[0]),float(self.rgbMax[1]),float(self.rgbMax[2]))
        GL.glVertex2f( 1., 0.); GL.glVertex2f( 1., 1.)
        GL.glEnd()

        GL.glEnable(GL.GL_COLOR_LOGIC_OP)
        GL.glLogicOp(GL.GL_XOR)
        GL.glLineWidth(2)
        GL.glColor3f(.5,.5,.5)
        GL.glBegin(GL.GL_LINES)
        x1 = self.v-0.01
        x2 = self.v+0.01
        GL.glVertex2f(float(x1), 1.); GL.glVertex2f(float(x1), 0.)
        GL.glVertex2f(float(x2), 0.); GL.glVertex2f(float(x2), 1.)
        GL.glEnd()
        GL.glDisable(GL.GL_COLOR_LOGIC_OP)
示例#2
0
    def Draw(self):
        if __debug__:
            if hasattr(DejaVu, 'functionName'): DejaVu.functionName()

            GL.glEnable(GL.GL_AUTO_NORMAL)
            GL.glEnable(GL.GL_NORMALIZE)

            if self.theNurb is None:
                self.theNurb = GLU.gluNewNurbsRenderer()

                GLU.gluNurbsProperty(self.theNurb, GLU.GLU_SAMPLING_TOLERANCE,
                                     25.0)
                GLU.gluNurbsProperty(self.theNurb, GLU.GLU_DISPLAY_MODE,
                                     GLU.GLU_OUTLINE_POLYGON)
                GLU.gluNurbsCallback(self.theNurb, GLU.GLU_ERROR,
                                     self.nurbsError)

            GLU.gluBeginSurface(self.theNurb)
            _glulib.gluNurbsSurface(self.theNurb, 8, self.knots, 8, self.knots,
                                    4 * 3, 3, self.ctlpoints, 4, 4,
                                    GL.GL_MAP2_VERTEX_3)

            GLU.gluEndSurface(self.theNurb)

            GL.glPointSize(5.0)
            GL.glDisable(GL.GL_LIGHTING)
            GL.glColor3f(1.0, 1.0, 0.0)
            GL.glBegin(GL.GL_POINTS)
            for i in range(4):
                for j in range(4):
                    GL.glVertex3fv(self.ctlpoints[i][j])

            GL.glEnd()
            GL.glDisable(GL.GL_AUTO_NORMAL)
            GL.glDisable(GL.GL_NORMALIZE)
示例#3
0
    def redraw(self):
        if __debug__:
            if hasattr(DejaVu, 'functionName'): DejaVu.functionName()
        self.tk.call(self._w, 'makecurrent')

        GL.glDisable(GL.GL_DEPTH_TEST)
        GL.glDisable(GL.GL_LIGHTING)
        #GL.glPolygonMode(GL.GL_FRONT, GL.GL_FILL)
        GL.glBegin(GL.GL_QUADS)
        GL.glColor3f(0., 0., 0.)
        GL.glVertex2f(0., 1.)
        GL.glVertex2f(0., 0.)
        GL.glColor3f(float(self.rgbMax[0]), float(self.rgbMax[1]),
                     float(self.rgbMax[2]))
        GL.glVertex2f(1., 0.)
        GL.glVertex2f(1., 1.)
        GL.glEnd()

        GL.glEnable(GL.GL_COLOR_LOGIC_OP)
        GL.glLogicOp(GL.GL_XOR)
        GL.glLineWidth(2)
        GL.glColor3f(.5, .5, .5)
        GL.glBegin(GL.GL_LINES)
        x1 = self.v - 0.01
        x2 = self.v + 0.01
        GL.glVertex2f(float(x1), 1.)
        GL.glVertex2f(float(x1), 0.)
        GL.glVertex2f(float(x2), 0.)
        GL.glVertex2f(float(x2), 1.)
        GL.glEnd()
        GL.glDisable(GL.GL_COLOR_LOGIC_OP)
示例#4
0
文件: Clip.py 项目: lisarosalina/App
    def _Enable(self, side):
        """Activate the clipping plane"""

        eqnt = self.eqn * side
        #eqnt[3] = 0.0 - eqnt[3]
        GL.glClipPlane(self.clipPlaneNames[self.num], eqnt)
        GL.glEnable(self.clipPlaneNames[self.num])
示例#5
0
    def _Enable(self, side):
	"""Activate the clipping plane"""

        eqnt = self.eqn * side
            #eqnt[3] = 0.0 - eqnt[3]
        GL.glClipPlane(self.clipPlaneNames[self.num], eqnt)
        GL.glEnable(self.clipPlaneNames[self.num])
示例#6
0
 def getHistogram(self):
     GL.glHistogram(GL.GL_HISTOGRAM, 256, GL.GL_RGB, GL.GL_FALSE)
     GL.glEnable(GL.GL_HISTOGRAM)
     self.redraw()
     values = Numeric.zeros((256, 3), Numeric.UInt16)
     #seg faults
     GL.glGetHistogram(GL.GL_HISTOGRAM, GL.GL_TRUE, GL.GL_RGB,
                       GL.GL_UNSIGNED_SHORT, values)
     return values
 def getHistogram(self):
     GL.glHistogram(GL.GL_HISTOGRAM, 256, GL.GL_RGB, GL.GL_FALSE)
     GL.glEnable(GL.GL_HISTOGRAM)
     self.redraw()
     values = Numeric.zeros( (256,3), Numeric.UInt16)
     #seg faults
     GL.glGetHistogram(GL.GL_HISTOGRAM, GL.GL_TRUE, GL.GL_RGB,
                       GL.GL_UNSIGNED_SHORT, values)
     return values
示例#8
0
    def SetupGL(self):
        if __debug__:
            if hasattr(DejaVu, 'functionName'): DejaVu.functionName()
        """Setup OpenGL rendering state for this object
"""
        #print "SetupGL", self

        if not self.inheritMaterial:
            self.InitMaterial()
            self.InitColor()

        if self.GetLighting():
            #glEnable(GL_LIGHTING)
            if self.viewer is not None:
                self.viewer.enableOpenglLighting()

            shading = self.GetShading()

            if shading != GL.GL_NONE:
                if self.normals is not None:
                    if (shading==GL.GL_SMOOTH and \
                        len(self.normals)!=len(self.vertexSet)) or \
                        (shading==GL.GL_FLAT and \
                         len(self.normals)!=len(self.faceSet)):
                        self.GetNormals()
                        self.viewer.objectsNeedingRedo[self] = None

                GL.glShadeModel(shading)

        else:  # no lighting
            GL.glDisable(GL.GL_LIGHTING)

        if not self.inheritCulling:
            if self.culling in (GL.GL_BACK, GL.GL_FRONT, GL.GL_FRONT_AND_BACK):
                GL.glCullFace(self.culling)
                GL.glEnable(GL.GL_CULL_FACE)
            else:
                GL.glDisable(GL.GL_CULL_FACE)

        if not self.inheritFrontPolyMode:
            mode = self.frontPolyMode
            if self.frontPolyMode == viewerConst.OUTLINED:
                mode = GL.GL_FILL
            if self.frontAndBack:
                GL.glPolygonMode(GL.GL_FRONT_AND_BACK, mode)
            else:
                GL.glPolygonMode(GL.GL_FRONT, mode)

        if not self.inheritBackPolyMode:
            mode = self.backPolyMode
            if self.backPolyMode == viewerConst.OUTLINED:
                mode = GL.GL_FILL
            GL.glPolygonMode(GL.GL_BACK, mode)

#self._AntiAliasing()
        self._WidthAndStipple()
    def SetupGL(self):
        if __debug__:
         if hasattr(DejaVu, 'functionName'): DejaVu.functionName()
        """Setup OpenGL rendering state for this object
"""
        #print "SetupGL", self

        if not self.inheritMaterial:
            self.InitMaterial()
            self.InitColor()

        if self.GetLighting():
            #glEnable(GL_LIGHTING)
            if self.viewer is not None:
                self.viewer.enableOpenglLighting()

            shading = self.GetShading()

	    if shading != GL.GL_NONE:
                if self.normals is not None:
                    if (shading==GL.GL_SMOOTH and \
                        len(self.normals)!=len(self.vertexSet)) or \
                        (shading==GL.GL_FLAT and \
                         len(self.normals)!=len(self.faceSet)):
                        self.GetNormals()
                        self.viewer.objectsNeedingRedo[self] = None

		GL.glShadeModel(shading)

	else: # no lighting
	    GL.glDisable(GL.GL_LIGHTING)

	if not self.inheritCulling:
	    if self.culling in (GL.GL_BACK, GL.GL_FRONT, GL.GL_FRONT_AND_BACK):
		GL.glCullFace(self.culling)
		GL.glEnable(GL.GL_CULL_FACE)
	    else: GL.glDisable(GL.GL_CULL_FACE)

	if not self.inheritFrontPolyMode:
            mode =self.frontPolyMode
            if self.frontPolyMode==viewerConst.OUTLINED:
                mode = GL.GL_FILL
	    if self.frontAndBack:
		GL.glPolygonMode(GL.GL_FRONT_AND_BACK, mode)
	    else:
		GL.glPolygonMode(GL.GL_FRONT, mode)

	if not self.inheritBackPolyMode:
            mode = self.backPolyMode
            if self.backPolyMode==viewerConst.OUTLINED:
                mode = GL.GL_FILL
	    GL.glPolygonMode(GL.GL_BACK, mode)

	#self._AntiAliasing()
	self._WidthAndStipple()
示例#10
0
    def Draw(self):
        #print "Sticker.Draw", self

        GL.glMatrixMode(GL.GL_PROJECTION)
        GL.glPushMatrix()
        GL.glLoadIdentity()
        Insert2d.Draw(self)
        GL.glMatrixMode(GL.GL_MODELVIEW)
        GL.glPushMatrix()
        GL.glLoadIdentity()

        GL.glDisable(GL.GL_DEPTH_TEST)
        GL.glDisable(GL.GL_LIGHTING)

        lLabelbounds = self.calculateSize()

        width = self.size[0]
        height = self.size[1]

        fullWidth = self.viewer.currentCamera.width
        fullHeight = self.viewer.currentCamera.height

        # we want the anchor of the image to be at the given position
        posxFromLeft = self.position[0] * fullWidth - self.anchor[0] * width
        posyFrombottom = (1. - self.position[1]) * fullHeight - (
            1. - self.anchor[1]) * height
        posxFromLeft = int(floor(posxFromLeft))
        posyFrombottom = int(floor(posyFrombottom))

        if (self.position[1] == 0.):
            posyFrombottom -= 1
        #print "posxFromLeft, posyFrombottom", posxFromLeft, posyFrombottom

        # used for picking
        self.polygonContour = [(posxFromLeft, posyFrombottom),
                               (posxFromLeft + width, posyFrombottom),
                               (posxFromLeft + width, posyFrombottom + height),
                               (posxFromLeft, posyFrombottom + height)]

        GL.glTranslatef(float(posxFromLeft), float(posyFrombottom), 0)

        self.drawSticker(lLabelbounds)

        GL.glEnable(GL.GL_DEPTH_TEST)
        GL.glEnable(GL.GL_LIGHTING)

        GL.glMatrixMode(GL.GL_PROJECTION)
        GL.glPopMatrix()
        GL.glMatrixMode(GL.GL_MODELVIEW)
        GL.glPopMatrix()

        return 1
示例#11
0
    def Draw(self):
        #print "Sticker.Draw", self
        
        GL.glMatrixMode(GL.GL_PROJECTION)
        GL.glPushMatrix()
        GL.glLoadIdentity()
        Insert2d.Draw(self)
        GL.glMatrixMode(GL.GL_MODELVIEW)
        GL.glPushMatrix()
        GL.glLoadIdentity()

        GL.glDisable(GL.GL_DEPTH_TEST)
        GL.glDisable(GL.GL_LIGHTING); 

        lLabelbounds = self.calculateSize()

        width = self.size[0]
        height = self.size[1]

        fullWidth = self.viewer.currentCamera.width
        fullHeight = self.viewer.currentCamera.height

        # we want the anchor of the image to be at the given position
        posxFromLeft = self.position[0] * fullWidth - self.anchor[0] * width
        posyFrombottom = (1.-self.position[1]) * fullHeight - (1.-self.anchor[1]) * height
        posxFromLeft = int(floor( posxFromLeft ) )
        posyFrombottom = int(floor( posyFrombottom ) )
        
        if (self.position[1] == 0.):
                posyFrombottom -= 1
        #print "posxFromLeft, posyFrombottom", posxFromLeft, posyFrombottom

        # used for picking
        self.polygonContour = [ (posxFromLeft, posyFrombottom),
                                (posxFromLeft+width, posyFrombottom),
                                (posxFromLeft+width, posyFrombottom+height),
                                (posxFromLeft, posyFrombottom+height)
                              ]

        GL.glTranslatef(float(posxFromLeft), float(posyFrombottom), 0)

        self.drawSticker(lLabelbounds)

        GL.glEnable(GL.GL_DEPTH_TEST)
        GL.glEnable(GL.GL_LIGHTING); 

        GL.glMatrixMode(GL.GL_PROJECTION)
        GL.glPopMatrix()
        GL.glMatrixMode(GL.GL_MODELVIEW)
        GL.glPopMatrix()

        return 1
示例#12
0
    def redraw(self):
        """ redraw the Material editor opengl sphere that shows the effect
of the modifications
"""
        if __debug__:
            if hasattr(DejaVu, 'functionName'): DejaVu.functionName()

        self.tk.call(self._w, 'makecurrent')
        GL.glClearColor(0, 0, 0, 0)
        self.initProjection()
        self.Configure()
        GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT)
        """ draw the black background squares """
        GL.glDisable(GL.GL_LIGHTING)
        GL.glColor3f(0.1, 0.1, 0.1)
        GL.glBegin(GL.GL_QUADS)
        GL.glVertex3f(-2, 0, 2)
        GL.glVertex3f(-2, 2, 2)
        GL.glVertex3f(0, 2, 2)
        GL.glVertex3f(0, 0, 2)
        GL.glEnd()
        GL.glBegin(GL.GL_QUADS)
        GL.glVertex3f(0, -2, 2)
        GL.glVertex3f(0, 0, 2)
        GL.glVertex3f(2, 0, 2)
        GL.glVertex3f(2, -2, 2)
        GL.glEnd()
        """ draw the grey background squares """
        GL.glColor3f(0.3, 0.3, 0.3)
        GL.glBegin(GL.GL_QUADS)
        GL.glVertex3f(-2, -2, 2)
        GL.glVertex3f(-2, 0, 2)
        GL.glVertex3f(0, 0, 2)
        GL.glVertex3f(0, -2, 2)
        GL.glEnd()
        GL.glBegin(GL.GL_QUADS)
        GL.glVertex3f(0, 0, 2)
        GL.glVertex3f(0, 2, 2)
        GL.glVertex3f(2, 2, 2)
        GL.glVertex3f(2, 0, 2)
        GL.glEnd()
        """ enable the sphere transparancy """
        GL.glEnable(GL.GL_BLEND)
        GL.glDepthMask(GL.GL_FALSE)
        GL.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA)
        """ draw the sphere """
        #GL.glEnable(GL.GL_LIGHTING)
        if self.viewer is not None:
            self.viewer.enableOpenglLighting()
        self.setMaterial()
        extractedGlutSolidSphere(1.6, 30, 30, 0)
示例#13
0
    def Draw(self):

        GL.glMatrixMode(GL.GL_PROJECTION)
        GL.glPushMatrix()
        GL.glLoadIdentity()
        Insert2d.Draw(self)
        GL.glMatrixMode(GL.GL_MODELVIEW)
        GL.glPushMatrix()
        GL.glLoadIdentity()

        GL.glDisable(GL.GL_DEPTH_TEST)
        GL.glDisable(GL.GL_LIGHTING); 

        width = self.size[0]
        height = self.size[1]

        fullWidth = self.viewer.currentCamera.width
        fullHeight = self.viewer.currentCamera.height

        # we want the anchor of the image to be at the given position
        posxFromLeft = self.position[0] * fullWidth - self.anchor[0] * width
        posyFrombottom = (1.-self.position[1]) * fullHeight - (1.-self.anchor[1]) * height
        #print "posxFromLeft, posyFrombottom", posxFromLeft, posyFrombottom

        GL.glColor4fv(self.color)
        
        xo, yo = self.position
        dx, dy = self.size
        GL.glBegin(GL.GL_QUADS);
        GL.glVertex2f(xo, yo)
        GL.glVertex2f(xo+dx, yo)
        GL.glVertex2f(xo+dx, yo+dy)
        GL.glVertex2f(xo, yo+dy)
        GL.glEnd()

       # used for picking
       # self.polygonContour = [ (posxFromLeft, posyFrombottom),
       #                         (posxFromLeft+width, posyFrombottom),
       #                         (posxFromLeft+width, posyFrombottom+height),
       #                         (posxFromLeft, posyFrombottom+height)
       #                       ]

        GL.glEnable(GL.GL_DEPTH_TEST)
        GL.glEnable(GL.GL_LIGHTING); 

        GL.glMatrixMode(GL.GL_PROJECTION)
        GL.glPopMatrix()
        GL.glMatrixMode(GL.GL_MODELVIEW)
        GL.glPopMatrix()

        return 1
示例#14
0
    def Draw(self):
        centers = self.getCoords(self.vertexSet.vertices.array)
        if len(centers) == 0:
            return
            #faces = Numeric.zeros((0,3), 'i')
        else:
            faces = self.getFaces(len(self.vertexSet.vertices.array))
        if self.materials[GL.GL_FRONT] and \
           not self.inheritMaterial:
            fpProp = self.materials[GL.GL_FRONT].prop[:5]
            fpBind = self.materials[GL.GL_FRONT].binding[:5]
        else:
            fpProp = None
            fpBind = None

        if self.materials[GL.GL_BACK] and \
           not self.inheritMaterial:
            bpProp = self.materials[GL.GL_BACK].prop[:5]
            bpBind = self.materials[GL.GL_BACK].binding[:5]
        else:
            bpProp = None
            bpBind = None

        texCoords = None

        if self.lighting:
            if (self.invertNormals) and (self.normals is not None):
                norms = -self.normals
            else:
                norms = self.normals
        else:
            norms = None

        #GL.glDisable(GL.GL_DEPTH_TEST)

        if self.disableStencil is True:
            GL.glDisable(GL.GL_STENCIL_TEST)

        status = glDrawIndexedGeom(GL.GL_LINES, centers.astype('f'), faces,
                                   norms, texCoords, fpProp, bpProp, fpBind,
                                   bpBind, self.frontAndBack, 1)

        if self.disableStencil is True:
            GL.glEnable(GL.GL_STENCIL_TEST)

        #GL.glEnable(GL.GL_DEPTH_TEST)

        return status
示例#15
0
    def display(self):
        GL.glClear(GL.GL_COLOR_BUFFER_BIT)

        # select white for all lines
        GL.glColor3f(1.0, 1.0, 1.0)

        # in 1st row, 3 lines, each with a different stipple
        GL.glEnable(GL.GL_LINE_STIPPLE)

        GL.glLineStipple(1, 0x0101)  #  dotted
        self.drawOneLine(50.0, 125.0, 150.0, 125.0)
        GL.glLineStipple(1, 0x00FF)  #  dashed
        self.drawOneLine(150.0, 125.0, 250.0, 125.0)
        GL.glLineStipple(1, 0x1C47)  #  dash/dot/dash
        self.drawOneLine(250.0, 125.0, 350.0, 125.0)

        # in 2nd row, 3 wide lines, each with different stipple
        GL.glLineWidth(5.0)
        GL.glLineStipple(1, 0x0101)  #  dotted
        self.drawOneLine(50.0, 100.0, 150.0, 100.0)
        GL.glLineStipple(1, 0x00FF)  #  dashed
        self.drawOneLine(150.0, 100.0, 250.0, 100.0)
        GL.glLineStipple(1, 0x1C47)  #  dash/dot/dash
        self.drawOneLine(250.0, 100.0, 350.0, 100.0)
        GL.glLineWidth(1.0)

        # in 3rd row, 6 lines, with dash/dot/dash stipple
        # as part of a single connected line strip
        GL.glLineStipple(1, 0x1C47)  # dash/dot/dash
        GL.glBegin(GL.GL_LINE_STRIP)
        try:
            for i in range(0, 7):
                GL.glVertex2f(50.0 + (i * 50.0), 75.0)
        finally:
            GL.glEnd()

        # in 4th row, 6 independent lines with same stipple  */
        for i in range(0, 6):
            self.drawOneLine(50.0 + (i * 50.0), 50.0, 50.0 + ((i + 1) * 50.0),
                             50.0)

        # in 5th row, 1 line, with dash/dot/dash stipple
        # and a stipple repeat factor of 5
        GL.glLineStipple(5, 0x1C47)  #  dash/dot/dash
        self.drawOneLine(50.0, 25.0, 350.0, 25.0)

        GL.glDisable(GL.GL_LINE_STIPPLE)
        GL.glFlush()
    def redraw(self):
        """ redraw the Material editor opengl sphere that shows the effect
of the modifications
"""
        if __debug__:
         if hasattr(DejaVu, 'functionName'): DejaVu.functionName()

        self.tk.call(self._w, 'makecurrent')
        GL.glClearColor(0,0,0,0)
        self.initProjection()
        self.Configure()
        GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT)

        """ draw the black background squares """
        GL.glDisable( GL.GL_LIGHTING )
        GL.glColor3f( 0.1, 0.1, 0.1 )
        GL.glBegin(GL.GL_QUADS)
        GL.glVertex3f(-2, 0, 2); GL.glVertex3f(-2, 2, 2)
        GL.glVertex3f( 0, 2, 2); GL.glVertex3f( 0, 0, 2)
        GL.glEnd()
        GL.glBegin(GL.GL_QUADS)
        GL.glVertex3f( 0,-2, 2); GL.glVertex3f( 0, 0, 2)
        GL.glVertex3f( 2, 0, 2); GL.glVertex3f( 2,-2, 2)
        GL.glEnd()

        """ draw the grey background squares """
        GL.glColor3f( 0.3, 0.3, 0.3 )
        GL.glBegin(GL.GL_QUADS)
        GL.glVertex3f(-2,-2, 2); GL.glVertex3f(-2, 0, 2)
        GL.glVertex3f( 0, 0, 2); GL.glVertex3f( 0,-2, 2)
        GL.glEnd()
        GL.glBegin(GL.GL_QUADS)
        GL.glVertex3f( 0, 0, 2); GL.glVertex3f( 0, 2, 2)
        GL.glVertex3f( 2, 2, 2); GL.glVertex3f( 2, 0, 2)
        GL.glEnd()

        """ enable the sphere transparancy """
        GL.glEnable(GL.GL_BLEND)
        GL.glDepthMask(GL.GL_FALSE)
        GL.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA)

        """ draw the sphere """
        #GL.glEnable(GL.GL_LIGHTING)
        if self.viewer is not None:
            self.viewer.enableOpenglLighting()
        self.setMaterial()
        extractedGlutSolidSphere(1.6, 30, 30, 0)
示例#17
0
    def display(self):
        GL.glClear( GL.GL_COLOR_BUFFER_BIT)

        # select white for all lines
        GL.glColor3f( 1.0, 1.0, 1.0)

        # in 1st row, 3 lines, each with a different stipple
        GL.glEnable( GL.GL_LINE_STIPPLE)

        GL.glLineStipple( 1, 0x0101)  #  dotted  
        self.drawOneLine( 50.0, 125.0, 150.0, 125.0)
        GL.glLineStipple( 1, 0x00FF)  #  dashed 
        self.drawOneLine( 150.0, 125.0, 250.0, 125.0);
        GL.glLineStipple( 1, 0x1C47)  #  dash/dot/dash
        self.drawOneLine( 250.0, 125.0, 350.0, 125.0)

        # in 2nd row, 3 wide lines, each with different stipple 
        GL.glLineWidth( 5.0)
        GL.glLineStipple( 1, 0x0101)  #  dotted 
        self.drawOneLine( 50.0, 100.0, 150.0, 100.0)
        GL.glLineStipple( 1, 0x00FF)  #  dashed
        self.drawOneLine( 150.0, 100.0, 250.0, 100.0)
        GL.glLineStipple( 1, 0x1C47)  #  dash/dot/dash
        self.drawOneLine( 250.0, 100.0, 350.0, 100.0)
        GL.glLineWidth( 1.0)

        # in 3rd row, 6 lines, with dash/dot/dash stipple  
        # as part of a single connected line strip        
        GL.glLineStipple( 1, 0x1C47)  # dash/dot/dash
        GL.glBegin( GL.GL_LINE_STRIP)
        try:
            for i in range( 0, 7):
                GL.glVertex2f( 50.0 + (i * 50.0), 75.0)
        finally:
            GL.glEnd()

        # in 4th row, 6 independent lines with same stipple  */
        for i in range( 0, 6):
            self.drawOneLine( 50.0 + (i * 50.0), 50.0, 50.0 + ((i+1) * 50.0), 50.0)

        # in 5th row, 1 line, with dash/dot/dash stipple 
        # and a stipple repeat factor of 5               
        GL.glLineStipple( 5, 0x1C47)  #  dash/dot/dash 
        self.drawOneLine( 50.0, 25.0, 350.0, 25.0)

        GL.glDisable( GL.GL_LINE_STIPPLE)
        GL.glFlush()
示例#18
0
    def Draw(self):
        GL.glEnable(GL.GL_DEPTH_TEST);
        GL.glClear( GL.GL_STENCIL_BUFFER_BIT)
        GL.glDisable(GL.GL_FOG)
        GL.glPolygonMode(GL.GL_FRONT_AND_BACK, GL.GL_FILL)
        
        OpenCSG.render(self.primitives, self.algo, self.depthalgo)

        GL.glDepthFunc(GL.GL_EQUAL)

        # FIXME should only enable fog if it is on in camera
        GL.glEnable(GL.GL_FOG)
        self.SetupGL()
        for p in self.pyprimitives:
            p.render()

        GL.glDepthFunc(GL.GL_LESS);
示例#19
0
    def Draw(self):
        GL.glEnable(GL.GL_DEPTH_TEST)
        GL.glClear(GL.GL_STENCIL_BUFFER_BIT)
        GL.glDisable(GL.GL_FOG)
        #GL.glPolygonMode(GL.GL_FRONT_AND_BACK, GL.GL_FILL)

        OpenCSG.render(self.primitives, self.algo, self.depthalgo)

        GL.glDepthFunc(GL.GL_EQUAL)

        # FIXME should only enable fog if it is on in camera
        GL.glEnable(GL.GL_FOG)
        self.SetupGL()
        for p in self.pyprimitives:
            p.render()

        GL.glDepthFunc(GL.GL_LESS)
示例#20
0
    def DrawDirectionalLight(self, camera):
	"""Draw a directional light as a line"""

#	if self.antialiased is True:
#	    GL.glEnable(GL.GL_LINE_SMOOTH)
#	    GL.glEnable(GL.GL_BLEND)
#	else:
#	    GL.glDisable(GL.GL_LINE_SMOOTH)
#	    GL.glDisable(GL.GL_BLEND)

	GL.glColor4fv (self.diffuse)
	GL.glLineWidth(self.lineWidth)
	GL.glBegin (GL.GL_LINES)
	GL.glVertex3f ( float(camera.lookAt[0]),float(camera.lookAt[1]),float(camera.lookAt[2]) )
	v = camera.lookAt + self.direction[:3]
	nv = sqrt(Numeric.add.reduce(v*v))
	v = (v/nv)*self.length
	GL.glVertex3f ( float(v[0]), float(v[1]), float(v[2]) )
	GL.glEnd()

	GL.glPushMatrix()
	GL.glTranslatef(float(v[0]),float(v[1]),float(v[2]))
	GL.glPushAttrib(GL.GL_ALPHA_TEST);
#	GL.glDisable(GL.GL_LIGHTING);
	GL.glEnable(GL.GL_BLEND)
	GL.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);  
	pObj = GLU.gluNewQuadric()
	GLU.gluQuadricDrawStyle(pObj, GLU.GLU_FILL)
	GLU.gluQuadricNormals(pObj, GLU.GLU_SMOOTH)
        
	GL.glColor4ub(255,255,255,10);
	GLU.gluDisk(pObj, 0.0, 0.5, 20, 20);
        
	for light_color in range(100):               
		GL.glColor4ub(255-light_color,255-light_color,255-light_color,10)  
		GLU.gluCylinder(pObj, 0.5 - 2.*light_color/255.,0.5 - 2.*(light_color+1)/255. , light_color/255., 20, 20)
		GL.glTranslatef(0.0, 0.0, float(light_color)/255.);
	#GL.glColor4ub(255,255,255,200)  
	#GLU.gluDisk(pObj, 0,0.5 - 2.*(light_color+1)/255. , 20, 20)
	GL.glPopAttrib()
	GL.glPopMatrix()
    def redraw(self, event=None, filter=None):
        if self.imarray is None:
            return
        if filter:
            self.master.lift()
            GL.glConvolutionFilter2D(GL.GL_CONVOLUTION_2D, GL.GL_LUMINANCE,
                                     3, 3, GL.GL_LUMINANCE, GL.GL_FLOAT,
                                     filter)
##             GL.glConvolutionParameterfv(GL.GL_CONVOLUTION_2D,
##                                        GL.GL_CONVOLUTION_FILTER_SCALE,
##                                        (3., 3.,3.,3.))
##             s=  self.scale
##             GL.glPixelTransferf(GL.GL_POST_CONVOLUTION_RED_SCALE, s)
##             GL.glPixelTransferf(GL.GL_POST_CONVOLUTION_BLUE_SCALE, s)
##             GL.glPixelTransferf(GL.GL_POST_CONVOLUTION_GREEN_SCALE, s)
##             s = self.bias
##             GL.glPixelTransferf(GL.GL_POST_CONVOLUTION_RED_BIAS, s)
##             GL.glPixelTransferf(GL.GL_POST_CONVOLUTION_BLUE_BIAS, s)
##             GL.glPixelTransferf(GL.GL_POST_CONVOLUTION_GREEN_BIAS, s)
##                                        GL.GL_CONVOLUTION_FILTER_SCALE,
##                                        (3., 3.,3.,3.))
##             GL.glConvolutionParameteriv(GL.GL_CONVOLUTION_2D,
##                                        GL.GL_CONVOLUTION_FILTER_BIAS,
##                                        (1500, 1500, 1500, 1500))
            GL.glEnable(GL.GL_CONVOLUTION_2D)
            
        self.tk.call(self._w, 'makecurrent')
        GL.glClearColor(0.0, 0.0, 0.0, 0.0)
        GL.glClear(GL.GL_COLOR_BUFFER_BIT)
        GL.glRasterPos2i( 0, 0)
        GL.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1)
        if self.mode=='RGB':
            _gllib.glDrawPixels( self.width, self.height,
                                 GL.GL_RGB, GL.GL_UNSIGNED_BYTE, self.imarray)
        elif self.mode in ['L','P']:
            _gllib.glDrawPixels( self.width, self.height,
                                 GL.GL_LUMINANCE, GL.GL_UNSIGNED_BYTE,
                                 self.imarray)

        GL.glDisable(GL.GL_CONVOLUTION_2D)
示例#22
0
    def Draw(self):
        if __debug__:
         if hasattr(DejaVu, 'functionName'): DejaVu.functionName()

         GL.glEnable(GL.GL_AUTO_NORMAL)
         GL.glEnable(GL.GL_NORMALIZE)
         
         if self.theNurb is None:         
             self.theNurb = GLU.gluNewNurbsRenderer()

             GLU.gluNurbsProperty(self.theNurb, GLU.GLU_SAMPLING_TOLERANCE,
                                  25.0)
             GLU.gluNurbsProperty(self.theNurb, GLU.GLU_DISPLAY_MODE,
                                  GLU.GLU_OUTLINE_POLYGON)
             GLU.gluNurbsCallback(self.theNurb, GLU.GLU_ERROR, self.nurbsError)

         GLU.gluBeginSurface(self.theNurb)
         _glulib.gluNurbsSurface( self.theNurb,
                                  8, self.knots,
                                  8, self.knots,
                                  4*3,
                                  3,
                                  self.ctlpoints,
                                  4, 4,
                                  GL.GL_MAP2_VERTEX_3)

         GLU.gluEndSurface(self.theNurb)

         
         GL.glPointSize(5.0)
         GL.glDisable(GL.GL_LIGHTING)
         GL.glColor3f(1.0, 1.0, 0.0)
         GL.glBegin(GL.GL_POINTS)
         for i in range(4):
             for j in range(4):
                 GL.glVertex3fv(self.ctlpoints[i][j]) 

         GL.glEnd()
         GL.glDisable(GL.GL_AUTO_NORMAL)
         GL.glDisable(GL.GL_NORMALIZE)
示例#23
0
    def redraw(self, event=None, filter=None):
        if self.imarray is None:
            return
        if filter:
            self.master.lift()
            GL.glConvolutionFilter2D(GL.GL_CONVOLUTION_2D, GL.GL_LUMINANCE, 3,
                                     3, GL.GL_LUMINANCE, GL.GL_FLOAT, filter)
            ##             GL.glConvolutionParameterfv(GL.GL_CONVOLUTION_2D,
            ##                                        GL.GL_CONVOLUTION_FILTER_SCALE,
            ##                                        (3., 3.,3.,3.))
            ##             s=  self.scale
            ##             GL.glPixelTransferf(GL.GL_POST_CONVOLUTION_RED_SCALE, s)
            ##             GL.glPixelTransferf(GL.GL_POST_CONVOLUTION_BLUE_SCALE, s)
            ##             GL.glPixelTransferf(GL.GL_POST_CONVOLUTION_GREEN_SCALE, s)
            ##             s = self.bias
            ##             GL.glPixelTransferf(GL.GL_POST_CONVOLUTION_RED_BIAS, s)
            ##             GL.glPixelTransferf(GL.GL_POST_CONVOLUTION_BLUE_BIAS, s)
            ##             GL.glPixelTransferf(GL.GL_POST_CONVOLUTION_GREEN_BIAS, s)
            ##                                        GL.GL_CONVOLUTION_FILTER_SCALE,
            ##                                        (3., 3.,3.,3.))
            ##             GL.glConvolutionParameteriv(GL.GL_CONVOLUTION_2D,
            ##                                        GL.GL_CONVOLUTION_FILTER_BIAS,
            ##                                        (1500, 1500, 1500, 1500))
            GL.glEnable(GL.GL_CONVOLUTION_2D)

        self.tk.call(self._w, 'makecurrent')
        GL.glClearColor(0.0, 0.0, 0.0, 0.0)
        GL.glClear(GL.GL_COLOR_BUFFER_BIT)
        GL.glRasterPos2i(0, 0)
        GL.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1)
        if self.mode == 'RGB':
            _gllib.glDrawPixels(self.width, self.height, GL.GL_RGB,
                                GL.GL_UNSIGNED_BYTE, self.imarray)
        elif self.mode in ['L', 'P']:
            _gllib.glDrawPixels(self.width, self.height, GL.GL_LUMINANCE,
                                GL.GL_UNSIGNED_BYTE, self.imarray)

        GL.glDisable(GL.GL_CONVOLUTION_2D)
示例#24
0
    def apply(self):
        """setup this light for current OpenGL context
"""
        #print "Light.apply"

        num = self.num

        if self.ambient is not None:
            GL.glLightfv(num, GL.GL_AMBIENT, self.ambient )

        if self.diffuse is not None:
            GL.glLightfv(num, GL.GL_DIFFUSE, self.diffuse )

        if self.specular is not None:
            GL.glLightfv(num, GL.GL_SPECULAR, self.specular )

        if self.positional is False:
            GL.glLightfv(num, GL.GL_POSITION, self.direction )
        else:
            GL.glLightfv(num, GL.GL_POSITION, self.position )

        if self.spotFlag:
            GL.glLightfv(num, GL.GL_SPOT_DIRECTION,
                         self.spotDirection[:3] )
            GL.glLightfv(num, GL.GL_SPOT_EXPONENT, [self.spotExponent] )
            GL.glLightfv(num, GL.GL_SPOT_CUTOFF, [self.spotCutoff] )
            GL.glLightfv(num, GL.GL_CONSTANT_ATTENUATION,
                         [self.constantAttenuation] )
            GL.glLightfv(num, GL.GL_LINEAR_ATTENUATION,
                         [self.linearAttenuation] )
            GL.glLightfv(num, GL.GL_QUADRATIC_ATTENUATION,
                         [ self.quadraticAttenuation] )
        
        if self.enabled is True:
            GL.glEnable(num)
        else:
            GL.glDisable(num)
示例#25
0
    def setupLightModel(self):
        """
"""
        if __debug__:
            if hasattr(DejaVu, 'functionName'): DejaVu.functionName()
        # this method has to be called explicitly by the derived classes if
        # a default lighting model is wanted

        GL.glLightfv(GL.GL_LIGHT0, GL.GL_AMBIENT, [.5, .5, .5, 1.0])
        GL.glLightfv(GL.GL_LIGHT0, GL.GL_DIFFUSE, [.5, .5, .5, 1.0])
        GL.glLightfv(GL.GL_LIGHT0, GL.GL_SPECULAR, [.5, .5, .5, 1.0])
        GL.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, [1.0, 1.0, 1.0, 0.0])

        GL.glLightfv(GL.GL_LIGHT1, GL.GL_AMBIENT, [.5, .5, .5, 1.0])
        GL.glLightfv(GL.GL_LIGHT1, GL.GL_DIFFUSE, [.5, .5, .5, 1.0])
        GL.glLightfv(GL.GL_LIGHT1, GL.GL_SPECULAR, [.5, .5, .5, 1.0])
        GL.glLightfv(GL.GL_LIGHT1, GL.GL_POSITION, [-1.0, 1.0, 1.0, 0.0])

        GL.glLightModelfv(GL.GL_LIGHT_MODEL_AMBIENT, [0.2, 0.2, 0.2, 1.0])
        #GL.glEnable(GL.GL_LIGHTING)
        if self.viewer is not None:
            self.viewer.enableOpenglLighting()
        GL.glEnable(GL.GL_LIGHT0)
        GL.glEnable(GL.GL_LIGHT1)
示例#26
0
    def extrude(self):
        """Virtual Method to do the extrusion along a 3D path with a 2D shape
        using the gle extrusion. We then get the geometry information
        using the extrusion method in Feedback mode. This will then be
        used to build a triangle strip."""
        
        from gle import glec
        gle.gleSetJoinStyle ( self.joinStyle | self.normalStyle )
        glec.gleFeedBack()

        #DisplayFunction of the old GlePolyCylinder
        GL.glColorMaterial (GL.GL_FRONT_AND_BACK, GL.GL_AMBIENT)
        GL.glEnable (GL.GL_COLOR_MATERIAL)
        #glEnable(GL_LIGHTING)
        if self.viewer is not None:
            self.viewer.enableOpenglLighting()
        colors = self.materials[GL.GL_FRONT].prop[0][:,:3]
        gle.glePolyCylinder(self.trace3D, colors, self.radius)
        GL.glDisable (GL.GL_COLOR_MATERIAL)
        
        glec.gleTextureMode(0)
        v,n,s = glec.gleGetTriangleMesh()

        return v,n,s
示例#27
0
    def extrude(self):
        """Extrude a cone with radii specified at each point
        of the extrusion"""

        assert len(self.radii)==len(self.trace3D)
        from gle import glec
        gle.gleSetJoinStyle ( self.joinStyle | self.normalStyle )
        glec.gleFeedBack()

        #DisplayFunction of the old GlePolyCylinder
        GL.glColorMaterial (GL.GL_FRONT_AND_BACK, GL.GL_AMBIENT)
        GL.glEnable (GL.GL_COLOR_MATERIAL)

        if self.viewer is not None:
            self.viewer.enableOpenglLighting()

        colors = self.materials[GL.GL_FRONT].prop[0][:,:3]
        gle.glePolyCone(self.trace3D, colors, self.radii)
        GL.glDisable (GL.GL_COLOR_MATERIAL)
        
        glec.gleTextureMode(0)
        v,n,s = glec.gleGetTriangleMesh()

        return v,n,s
示例#28
0
    def _WidthAndStipple(self):
        if __debug__:
            if hasattr(DejaVu, 'functionName'): DejaVu.functionName()
        """Set the line or points width
"""
        #print "_WidthAndStipple", self.stippleLines , self.inheritStippleLines , self

        GL.glPointSize(self.getPointWidth())

        GL.glLineWidth(self.getLineWidth())

        if self.getStippleLines() in (True, 1):
            GL.glEnable(GL.GL_LINE_STIPPLE)
            ls = self.linestipple
            GL.glLineStipple(ls.factor, ls.pattern)
        else:
            GL.glDisable(GL.GL_LINE_STIPPLE)

        if self.getStipplePolygons() in (True, 1):
            GL.glEnable(GL.GL_POLYGON_STIPPLE)
            ps = self.polygonstipple
            GL.glPolygonStipple(self.polygonstipple.pattern)
        else:
            GL.glDisable(GL.GL_POLYGON_STIPPLE)
    def _WidthAndStipple(self):
        if __debug__:
         if hasattr(DejaVu, 'functionName'): DejaVu.functionName()
        """Set the line or points width
"""
        #print "_WidthAndStipple", self.stippleLines , self.inheritStippleLines , self

        GL.glPointSize(self.getPointWidth())

        GL.glLineWidth(self.getLineWidth())

        if self.getStippleLines() in (True, 1):
            GL.glEnable(GL.GL_LINE_STIPPLE)
            ls = self.linestipple
            GL.glLineStipple(ls.factor, ls.pattern)
        else:
            GL.glDisable(GL.GL_LINE_STIPPLE)

        if self.getStipplePolygons() in (True, 1):
            GL.glEnable(GL.GL_POLYGON_STIPPLE)
            ps = self.polygonstipple
            GL.glPolygonStipple(self.polygonstipple.pattern)
        else:
            GL.glDisable(GL.GL_POLYGON_STIPPLE)
    def setupLightModel(self):
        """
"""
        if __debug__:
         if hasattr(DejaVu, 'functionName'): DejaVu.functionName()
        # this method has to be called explicitly by the derived classes if
        # a default lighting model is wanted

        GL.glLightfv(GL.GL_LIGHT0, GL.GL_AMBIENT,  [.5, .5, .5, 1.0])
        GL.glLightfv(GL.GL_LIGHT0, GL.GL_DIFFUSE,  [.5, .5, .5, 1.0])
        GL.glLightfv(GL.GL_LIGHT0, GL.GL_SPECULAR, [.5, .5, .5, 1.0])
        GL.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, [1.0, 1.0, 1.0, 0.0]);   

        GL.glLightfv(GL.GL_LIGHT1, GL.GL_AMBIENT,  [.5, .5, .5, 1.0])
        GL.glLightfv(GL.GL_LIGHT1, GL.GL_DIFFUSE,  [.5, .5, .5, 1.0])
        GL.glLightfv(GL.GL_LIGHT1, GL.GL_SPECULAR, [.5, .5, .5, 1.0])
        GL.glLightfv(GL.GL_LIGHT1, GL.GL_POSITION, [-1.0, 1.0, 1.0, 0.0]);   

        GL.glLightModelfv(GL.GL_LIGHT_MODEL_AMBIENT, [0.2, 0.2, 0.2, 1.0])
        #GL.glEnable(GL.GL_LIGHTING)
        if self.viewer is not None:
            self.viewer.enableOpenglLighting()
        GL.glEnable(GL.GL_LIGHT0)
        GL.glEnable(GL.GL_LIGHT1)
示例#31
0
    def setupLightModel(self):
        # this method has to be called explicitly by the derived classes if
        # a default lighting model is wanted
        GL.glLight(GL.GL_LIGHT0, GL.GL_AMBIENT,  [.5, .5, .5, 1.0])
        GL.glLight(GL.GL_LIGHT0, GL.GL_DIFFUSE,  [.5, .5, .5, 1.0])
        GL.glLight(GL.GL_LIGHT0, GL.GL_SPECULAR, [.5, .5, .5, 1.0])
        GL.glLight(GL.GL_LIGHT0, GL.GL_POSITION, [1.0, 1.0, 1.0, 0.0]);   

        GL.glLight(GL.GL_LIGHT1, GL.GL_AMBIENT,  [.5, .5, .5, 1.0])
        GL.glLight(GL.GL_LIGHT1, GL.GL_DIFFUSE,  [.5, .5, .5, 1.0])
        GL.glLight(GL.GL_LIGHT1, GL.GL_SPECULAR, [.5, .5, .5, 1.0])
        GL.glLight(GL.GL_LIGHT1, GL.GL_POSITION, [-1.0, 1.0, 1.0, 0.0]);   

        GL.glLightModel(GL.GL_LIGHT_MODEL_AMBIENT, [0.2, 0.2, 0.2, 1.0])
        GL.glEnable(GL.GL_LIGHTING)
        GL.glEnable(GL.GL_LIGHT0)
        GL.glEnable(GL.GL_LIGHT1)
示例#32
0
def drawLegendOnly(
    fullWidth,
    fullHeight,
    ramp,
    verticalLegend=True,
    roomLeftToLegend=0,
    roomBelowLegend=50,
    legendShortSide=10,
    legendLongSide=150,
    interpolate=True,
    selected=False,
    tile=None,
):

    if ramp is None or len(ramp) == 0:
        return

    if tile is not None and selected is True:
        selected = False

    # deducted values
    if verticalLegend is True:
        lRoomToLegendCloseLongSide = roomLeftToLegend
        lRoomToLegendCloseShortSide = roomBelowLegend
        if legendShortSide < 1:
            legendShortSide = 1
        if legendLongSide < 1:
            legendLongSide = 1
    else:
        lRoomToLegendCloseLongSide = roomBelowLegend
        lRoomToLegendCloseShortSide = roomLeftToLegend
        if legendShortSide < 1:
            legendShortSide = 1
        if legendLongSide < 1:
            legendLongSide = 1

    lRoomToLegendFarLongSide = lRoomToLegendCloseLongSide + legendShortSide

    GL.glMatrixMode(GL.GL_PROJECTION)
    GL.glPushMatrix()
    GL.glLoadIdentity()
    if tile is None:
        GL.glOrtho(0, float(fullWidth), 0, float(fullHeight), -1, 1)
    else:
        GL.glOrtho(float(tile[0]), float(tile[1]), float(tile[2]),
                   float(tile[3]), -1, 1)
    GL.glMatrixMode(GL.GL_MODELVIEW)
    GL.glPushMatrix()
    GL.glLoadIdentity()
    GL.glDisable(GL.GL_LIGHTING)

    GL.glPolygonMode(GL.GL_FRONT, GL.GL_FILL)
    GL.glDisable(GL.GL_DEPTH_TEST)
    GL.glDepthMask(GL.GL_FALSE)

    if len(ramp[0]) == 4:  # there are alpha values, draw checkered bg
        GL.glEnable(GL.GL_BLEND)
        GL.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA)
        #draw a bunch of quads as background
        lCheckerSquareSize = legendShortSide * .5
        if lCheckerSquareSize != 0:
            nbquads = int(legendLongSide / lCheckerSquareSize)
        else:
            nbquads = 1
        c1 = (0.1, 0.1, 0.1)
        c2 = (0.3, 0.3, 0.3)
        c = c1
        x2 = None
        for i in range(nbquads + 1):
            GL.glColor3fv(c)
            if i == nbquads and nbquads != 0:
                x1 = x2
                x2 = legendLongSide
            else:
                x1 = i * lCheckerSquareSize
                x2 = min(x1 + lCheckerSquareSize, legendLongSide)

            GL.glBegin(GL.GL_QUADS)
            if verticalLegend is True:
                GL.glVertex2f(float(lRoomToLegendCloseLongSide),
                              float(lRoomToLegendCloseShortSide + x1))
                GL.glVertex2f(
                    float(lRoomToLegendCloseLongSide + lCheckerSquareSize),
                    float(lRoomToLegendCloseShortSide + x1))
                GL.glVertex2f(
                    float(lRoomToLegendCloseLongSide + lCheckerSquareSize),
                    float(lRoomToLegendCloseShortSide + x2))
                GL.glVertex2f(float(lRoomToLegendCloseLongSide),
                              float(lRoomToLegendCloseShortSide + x2))
            else:
                GL.glVertex2f(float(lRoomToLegendCloseShortSide + x2),
                              float(lRoomToLegendCloseLongSide))
                GL.glVertex2f(
                    float(lRoomToLegendCloseShortSide + x2),
                    float(lRoomToLegendCloseLongSide + lCheckerSquareSize))
                GL.glVertex2f(
                    float(lRoomToLegendCloseShortSide + x1),
                    float(lRoomToLegendCloseLongSide + lCheckerSquareSize))
                GL.glVertex2f(float(lRoomToLegendCloseShortSide + x1),
                              float(lRoomToLegendCloseLongSide))
            GL.glEnd()

            if c == c1:
                c = c2
            else:
                c = c1

            GL.glColor3fv(c)
            GL.glBegin(GL.GL_QUADS)
            if verticalLegend is True:
                GL.glVertex2f(
                    float(lRoomToLegendCloseLongSide + lCheckerSquareSize),
                    float(lRoomToLegendCloseShortSide + x1))
                GL.glVertex2f(float(lRoomToLegendFarLongSide),
                              float(lRoomToLegendCloseShortSide + x1))
                GL.glVertex2f(float(lRoomToLegendFarLongSide),
                              float(lRoomToLegendCloseShortSide + x2))
                GL.glVertex2f(
                    float(lRoomToLegendCloseLongSide + lCheckerSquareSize),
                    float(lRoomToLegendCloseShortSide + x2))
            else:
                GL.glVertex2f(
                    float(lRoomToLegendCloseShortSide + x2),
                    float(lRoomToLegendCloseLongSide + lCheckerSquareSize))
                GL.glVertex2f(float(lRoomToLegendCloseShortSide + x2),
                              float(lRoomToLegendFarLongSide))
                GL.glVertex2f(float(lRoomToLegendCloseShortSide + x1),
                              float(lRoomToLegendFarLongSide))
                GL.glVertex2f(
                    float(lRoomToLegendCloseShortSide + x1),
                    float(lRoomToLegendCloseLongSide + lCheckerSquareSize))
            GL.glEnd()

    #interp = False
    if interpolate and (len(ramp) > 1):  # we interpolate colors
        # draw a quad strip for the color map
        lDelta = legendLongSide / float(len(ramp) - 1)
        GL.glBegin(GL.GL_QUAD_STRIP)
        for i in range(len(ramp)):
            if selected is True:
                c = deepcopy(ramp[i])
                for j in range(3):
                    c[j] = .35 + c[j] * .3
            else:
                c = ramp[i]
            if len(c) == 3:
                GL.glColor3fv(c)
            elif len(c) == 4:
                GL.glColor4fv(c)
            lStep = i * lDelta
            if verticalLegend is True:
                GL.glVertex2f(float(lRoomToLegendCloseLongSide),
                              float(lRoomToLegendCloseShortSide + lStep))
                GL.glVertex2f(float(lRoomToLegendFarLongSide),
                              float(lRoomToLegendCloseShortSide + lStep))
            else:
                GL.glVertex2f(float(lRoomToLegendCloseShortSide + lStep),
                              float(lRoomToLegendFarLongSide))
                GL.glVertex2f(float(lRoomToLegendCloseShortSide + lStep),
                              float(lRoomToLegendCloseLongSide))
        GL.glEnd()
    else:
        # we draw a quad for each color
        lDelta = legendLongSide / float(len(ramp))
        for i in range(len(ramp)):
            if selected is True:
                c = deepcopy(ramp[i])
                for j in range(3):
                    c[j] = .35 + c[j] * .3
            else:
                c = ramp[i]
            if len(c) == 3:
                GL.glColor3fv(c)
            elif len(c) == 4:
                GL.glColor4fv(c)
            x1 = i * lDelta
            x2 = x1 + lDelta
            GL.glBegin(GL.GL_QUADS)
            if verticalLegend is True:
                GL.glVertex2f(float(lRoomToLegendCloseLongSide),
                              float(lRoomToLegendCloseShortSide + x1))
                GL.glVertex2f(float(lRoomToLegendFarLongSide),
                              float(lRoomToLegendCloseShortSide + x1))
                GL.glVertex2f(float(lRoomToLegendFarLongSide),
                              float(lRoomToLegendCloseShortSide + x2))
                GL.glVertex2f(float(lRoomToLegendCloseLongSide),
                              float(lRoomToLegendCloseShortSide + x2))
            else:
                GL.glVertex2f(float(lRoomToLegendCloseShortSide + x2),
                              float(lRoomToLegendCloseLongSide))
                GL.glVertex2f(float(lRoomToLegendCloseShortSide + x2),
                              float(lRoomToLegendFarLongSide))
                GL.glVertex2f(float(lRoomToLegendCloseShortSide + x1),
                              float(lRoomToLegendFarLongSide))
                GL.glVertex2f(float(lRoomToLegendCloseShortSide + x1),
                              float(lRoomToLegendCloseLongSide))
            GL.glEnd()

    if len(ramp[0]) == 4:
        GL.glDisable(GL.GL_BLEND)

    GL.glEnable(GL.GL_DEPTH_TEST)
    GL.glDepthMask(GL.GL_TRUE)

    #    GL.glEnable(GL.GL_LIGHTING)
    GL.glPopMatrix()
    GL.glMatrixMode(GL.GL_PROJECTION)
    GL.glPopMatrix()
    GL.glMatrixMode(GL.GL_MODELVIEW)
示例#33
0
def drawLegendLabelName(
    fullWidth,
    fullHeight,
    ramp,
    mini,
    maxi,
    name='',
    unit='',
    labelValues=[],
    verticalLegend=True,
    leftOrBelowLabels=False,
    roomLeftToLegend=0,
    roomBelowLegend=50,
    legendShortSide=10,
    legendLongSide=150,
    significantDigits=3,
    backgroundColor=(0, 0, 0, .8),
    labelColor=(1, 1, 1),
    interpolate=True,
    frame=True,
    selected=False,
    numOfLabels=None,
    resizeSpotRadius=5,
    fontScale=8,
    glfFontID=0,
    tile=None,
):

    if ramp is None or len(ramp) == 0:
        return

    if tile is not None and selected is True:
        selected = False

    # some glf font initialisation
    glf.glfSetCurrentFont(glfFontID)
    glf.glfStringCentering(GL.GL_FALSE)
    glf.glfStringDirection(glf.GLF_LEFT)

    # calculate name and unit size
    lNameMinAndMax = glf.glfGetStringBounds(name)
    lNameMinAndMax = (lNameMinAndMax[0] * fontScale,
                      lNameMinAndMax[1] * fontScale,
                      lNameMinAndMax[2] * fontScale,
                      lNameMinAndMax[3] * fontScale)
    lNameWidth = lNameMinAndMax[2] - lNameMinAndMax[0]
    lNameHeight = lNameMinAndMax[3] - lNameMinAndMax[1]
    if lNameWidth == 0:
        lNameWidth = fontScale
    if unit is not None and (len(unit) > 0):
        lUnitMinAndMax = glf.glfGetStringBounds(unit)
        lUnitMinAndMax = (lUnitMinAndMax[0] * fontScale,
                          lUnitMinAndMax[1] * fontScale,
                          lUnitMinAndMax[2] * fontScale,
                          lUnitMinAndMax[3] * fontScale)
        lUnitWidth = lUnitMinAndMax[2] - lUnitMinAndMax[0]
        lUnitHeight = lUnitMinAndMax[3] - lUnitMinAndMax[1]
    else:
        lUnitWidth = fontScale
        lUnitHeight = 0
    lMaxNameUnitHeight = max(lNameHeight, lUnitHeight)
    lMaxNameUnitWidth = max(lNameWidth, lUnitWidth)

    # deducted values
    fontScaleHalf = fontScale / 2
    if verticalLegend is True:
        lRoomToLegendCloseLongSide = roomLeftToLegend
        lRoomToLegendCloseShortSide = roomBelowLegend
        if legendShortSide < 1:
            legendShortSide = 1
        if legendLongSide < 1:
            legendLongSide = 1
    else:
        lRoomToLegendCloseLongSide = roomBelowLegend
        lRoomToLegendCloseShortSide = roomLeftToLegend
        if legendShortSide < lMaxNameUnitHeight + fontScaleHalf:
            legendShortSide = lMaxNameUnitHeight + fontScaleHalf
        if legendLongSide < 1:
            legendLongSide = 1

    lRoomToLegendFarLongSide = lRoomToLegendCloseLongSide + legendShortSide
    lRoomToLegendFarShortSide = lRoomToLegendCloseShortSide + legendLongSide

    # prepare the legend labels
    if len(labelValues) > 0:
        lOnScreenLabelValues = labelValues
    else:
        if numOfLabels == None or numOfLabels < 0:
            if verticalLegend is True:
                numOfLabels = 6
            else:
                numOfLabels = 4

        if (numOfLabels == 0) or maxi is None or mini is None:
            lOnScreenLabelValues = []
        elif numOfLabels == 1:
            lOnScreenLabelValues = [(mini + maxi) * 0.5]
        else:
            delta = (maxi - mini) / float(numOfLabels - 1)
            lOnScreenLabelValues = []
            for i in range(numOfLabels):
                lOnScreenLabelValues.append(mini + i * delta)

    lMaxLabelDigits = 0
    for v in lOnScreenLabelValues:
        lLabel = "%.*g" % (significantDigits, v)
        if lMaxLabelDigits < len(lLabel):
            lMaxLabelDigits = len(lLabel)

    # calculate labels size
    lLabelsMinAndMax = []
    lLabelsWidth = []
    lLabelsHeight = []
    lMaxLabelsWidth = 0
    lMaxLabelsHeight = 0
    for v in lOnScreenLabelValues:
        lLabel = "%.*g" % (significantDigits, v)
        lLabelsMinAndMax.append(glf.glfGetStringBounds(lLabel))
        lLabelsMinAndMax[-1] = (lLabelsMinAndMax[-1][0] * fontScale,
                                lLabelsMinAndMax[-1][1] * fontScale,
                                lLabelsMinAndMax[-1][2] * fontScale,
                                lLabelsMinAndMax[-1][3] * fontScale)
        lLabelsWidth.append(lLabelsMinAndMax[-1][2] - lLabelsMinAndMax[-1][0])
        lLabelsHeight.append(lLabelsMinAndMax[-1][3] - lLabelsMinAndMax[-1][1])
        if lLabelsWidth[-1] > lMaxLabelsWidth:
            lMaxLabelsWidth = lLabelsWidth[-1]
        if lLabelsHeight[-1] > lMaxLabelsHeight:
            lMaxLabelsHeight = lLabelsHeight[-1]

    # calculate frame size
    lMaxWidth = max(lMaxLabelsWidth + legendShortSide, lMaxNameUnitWidth)
    lMaxNameUnitHeightHalf = lMaxNameUnitHeight / 2
    legendShortSideHalf = legendShortSide / 2
    if verticalLegend is True:
        if leftOrBelowLabels is False:
            lPt1 = (lRoomToLegendCloseLongSide,
                    -fontScale + lRoomToLegendCloseShortSide -
                    lMaxLabelsHeight - lNameHeight, 0)
            lPt2 = (fontScale + lRoomToLegendCloseLongSide + lMaxWidth,
                    -fontScale + lRoomToLegendCloseShortSide -
                    lMaxLabelsHeight - lNameHeight, 0)
            lPt3 = (fontScale + lRoomToLegendCloseLongSide + lMaxWidth,
                    fontScale + lRoomToLegendFarShortSide + lMaxLabelsHeight +
                    lUnitHeight, 0)
            lPt4 = (lRoomToLegendCloseLongSide, fontScale +
                    lRoomToLegendFarShortSide + lMaxLabelsHeight + lUnitHeight,
                    0)
        else:
            lPt1 = (-fontScale + lRoomToLegendFarLongSide - lMaxWidth,
                    -fontScale + lRoomToLegendCloseShortSide -
                    lMaxLabelsHeight - lNameHeight, 0)
            lPt2 = (lRoomToLegendCloseLongSide + legendShortSide,
                    -fontScale + lRoomToLegendCloseShortSide -
                    lMaxLabelsHeight - lNameHeight, 0)
            lPt3 = (lRoomToLegendCloseLongSide + legendShortSide, fontScale +
                    lRoomToLegendFarShortSide + lMaxLabelsHeight + lUnitHeight,
                    0)
            lPt4 = (-fontScale + lRoomToLegendFarLongSide - lMaxWidth,
                    fontScale + lRoomToLegendFarShortSide + lMaxLabelsHeight +
                    lUnitHeight, 0)
    else:
        if leftOrBelowLabels is False:
            lPt1 = (-fontScale + lRoomToLegendCloseShortSide - lNameWidth,
                    lRoomToLegendCloseLongSide, 0)
            lPt2 = (fontScale + lRoomToLegendFarShortSide + lUnitWidth,
                    lRoomToLegendCloseLongSide, 0)
            lPt3 = (fontScale+lRoomToLegendFarShortSide+lUnitWidth,
                    fontScale+lRoomToLegendCloseLongSide \
                    + legendShortSide \
                    + lMaxLabelsHeight,
                    0)
            lPt4 = (-fontScale+lRoomToLegendCloseShortSide-lNameWidth,
                    fontScale+lRoomToLegendCloseLongSide \
                    + legendShortSide \
                    + lMaxLabelsHeight,
                    0)
        else:
            lPt1 = (-fontScale + lRoomToLegendCloseShortSide - lNameWidth,
                    -fontScale + lRoomToLegendCloseLongSide - lMaxLabelsHeight,
                    0)
            lPt2 = (fontScale + lRoomToLegendFarShortSide + lUnitWidth,
                    -fontScale + lRoomToLegendCloseLongSide - lMaxLabelsHeight,
                    0)
            lPt3 = (fontScale + lRoomToLegendFarShortSide + lUnitWidth,
                    lRoomToLegendFarLongSide, 0)
            lPt4 = (-fontScale + lRoomToLegendCloseShortSide - lNameWidth,
                    lRoomToLegendFarLongSide, 0)

    if selected is True:
        labelColor2 = (.5, .5, .5)
    else:
        labelColor2 = labelColor

    GL.glMatrixMode(GL.GL_PROJECTION)
    GL.glPushMatrix()
    GL.glLoadIdentity()
    if tile is None:
        GL.glOrtho(0, float(fullWidth), 0, float(fullHeight), -1, 1)
    else:
        GL.glOrtho(float(tile[0]), float(tile[1]), float(tile[2]),
                   float(tile[3]), -1, 1)
    GL.glMatrixMode(GL.GL_MODELVIEW)
    GL.glPushMatrix()
    GL.glLoadIdentity()
    GL.glDisable(GL.GL_LIGHTING)

    GL.glPolygonMode(GL.GL_FRONT, GL.GL_FILL)

    if len(backgroundColor) == 4:
        GL.glEnable(GL.GL_BLEND)
        GL.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA)

    #because of an unexplained bug on michel's laptop,
    #we don't draw the background when there is no frame
    #(so michel has a way to manage the legend)
    if frame is True:
        #draw transparent background
        GL.glDepthMask(GL.GL_FALSE)
        if len(backgroundColor) == 3:
            GL.glColor3fv(backgroundColor)
        else:
            GL.glColor4fv(backgroundColor)
        GL.glBegin(GL.GL_QUADS)
        GL.glVertex3fv(lPt1)
        GL.glVertex3fv(lPt2)
        GL.glVertex3fv(lPt3)
        GL.glVertex3fv(lPt4)
        GL.glEnd()
        GL.glDepthMask(GL.GL_TRUE)

    #draw frame
    if frame is True:
        GL.glPolygonMode(GL.GL_FRONT, GL.GL_LINE)
        GL.glLineWidth(1)
        GL.glColor3fv(labelColor2)
        GL.glBegin(GL.GL_QUADS)
        GL.glVertex3fv(lPt1)
        GL.glVertex3fv(lPt2)
        GL.glVertex3fv(lPt3)
        GL.glVertex3fv(lPt4)
        GL.glEnd()
        GL.glPolygonMode(GL.GL_FRONT, GL.GL_FILL)

    if mini is not None and maxi is not None and maxi > mini:
        lUnitStep = legendLongSide / float(maxi - mini)
    else:
        lUnitStep = legendLongSide

    GL.glDisable(GL.GL_LIGHTING)

    if verticalLegend is True:
        if leftOrBelowLabels is True:
            lRoomLeftToLabel = -fontScaleHalf + lRoomToLegendCloseLongSide
            lRoomLeftToName = -fontScaleHalf + lRoomToLegendFarLongSide - lNameWidth
            lRoomLeftToUnit = -fontScaleHalf + lRoomToLegendFarLongSide - lUnitWidth
            lRoomBelowName = -fontScaleHalf + roomBelowLegend - lMaxLabelsHeight - lNameHeight
            lRoomBelowUnit = fontScaleHalf + roomBelowLegend + legendLongSide + lMaxLabelsHeight
        else:
            lRoomLeftToLabel = fontScaleHalf + lRoomToLegendFarLongSide
            lRoomLeftToName = fontScaleHalf + lRoomToLegendCloseLongSide
            lRoomLeftToUnit = lRoomLeftToName
            lRoomBelowName = -fontScaleHalf + roomBelowLegend - lMaxLabelsHeight - lNameHeight
            lRoomBelowUnit = fontScaleHalf + roomBelowLegend + legendLongSide + lMaxLabelsHeight
    else:
        if leftOrBelowLabels is True:
            lRoomBelowLabel = -fontScaleHalf + lRoomToLegendFarLongSide \
                              - legendShortSide - lMaxLabelsHeight
            lRoomBelowName = lRoomToLegendCloseLongSide + legendShortSideHalf \
                             - lMaxNameUnitHeightHalf
        else:
            lRoomBelowLabel = fontScaleHalf + lRoomToLegendCloseLongSide \
                              + legendShortSide
            lRoomBelowName = lRoomToLegendCloseLongSide + legendShortSideHalf \
                             - lMaxNameUnitHeightHalf
        lRoomBelowUnit = lRoomBelowName
        lRoomLeftToName = -fontScaleHalf + lRoomToLegendCloseShortSide - lNameWidth
        lRoomLeftToUnit = fontScaleHalf + lRoomToLegendFarShortSide

    # set the color of the text
    GL.glColor3fv(labelColor2)

    # print the legend name
    GL.glPushMatrix()
    GL.glTranslatef(float(lRoomLeftToName + fontScale),
                    float(lRoomBelowName - lNameMinAndMax[1]), 0)
    GL.glScalef(float(fontScale), float(fontScale), 0)
    glf.glfDrawSolidString(name)
    GL.glPopMatrix()
    if unit is not None and (len(unit) > 0):
        GL.glPushMatrix()
        GL.glTranslatef(float(lRoomLeftToUnit + fontScale),
                        float(lRoomBelowUnit - lUnitMinAndMax[1]), 0)
        GL.glScalef(float(fontScale), float(fontScale), 1)
        glf.glfDrawSolidString(unit)
        GL.glPopMatrix()

    if mini is not None and maxi is not None:
        i = 0
        for v in lOnScreenLabelValues:
            #calculate label position
            lLabel = "%.*g" % (significantDigits, v)
            lStep = (v - mini) * lUnitStep
            GL.glPushMatrix()
            if verticalLegend:
                if leftOrBelowLabels:
                    GL.glTranslatef(
                        float(lRoomLeftToLabel + fontScale - lLabelsWidth[i]),
                        float(roomBelowLegend -
                              (lLabelsHeight[i] / 2 + lLabelsMinAndMax[i][1]) +
                              lStep), 0)
                else:
                    GL.glTranslatef(
                        float(lRoomLeftToLabel + fontScale),
                        float(roomBelowLegend -
                              (lLabelsHeight[i] / 2 + lLabelsMinAndMax[i][1]) +
                              lStep), 0)
            else:
                GL.glTranslatef(
                    float(roomLeftToLegend - (lLabelsWidth[i] / 2) +
                          fontScale + lStep),
                    float(lRoomBelowLabel - lLabelsMinAndMax[i][1]), 0)
            GL.glScalef(float(fontScale), float(fontScale), 1)
            glf.glfDrawSolidString("%s" % lLabel)
            GL.glPopMatrix()
            i += 1

    if len(backgroundColor) == 4:
        GL.glDisable(GL.GL_BLEND)


#    GL.glEnable(GL.GL_LIGHTING)
    GL.glPopMatrix()
    GL.glMatrixMode(GL.GL_PROJECTION)
    GL.glPopMatrix()
    GL.glMatrixMode(GL.GL_MODELVIEW)

    drawLegendOnly(
        fullWidth=fullWidth,
        fullHeight=fullHeight,
        ramp=ramp,
        verticalLegend=verticalLegend,
        roomLeftToLegend=roomLeftToLegend,
        roomBelowLegend=roomBelowLegend,
        legendShortSide=legendShortSide,
        legendLongSide=legendLongSide,
        interpolate=interpolate,
        selected=selected,
        tile=tile,
    )

    if selected is True:
        GL.glMatrixMode(GL.GL_PROJECTION)
        GL.glPushMatrix()
        GL.glLoadIdentity()
        GL.glOrtho(0, float(fullWidth), 0, float(fullHeight), -1, 1)
        GL.glMatrixMode(GL.GL_MODELVIEW)
        GL.glPushMatrix()
        GL.glLoadIdentity()
        GL.glDisable(GL.GL_LIGHTING)
        GL.glPolygonMode(GL.GL_FRONT, GL.GL_FILL)
        GL.glDisable(GL.GL_DEPTH_TEST)
        GL.glDepthMask(GL.GL_FALSE)
        GL.glDisable(GL.GL_LIGHTING)

        resizeSpot = []
        if verticalLegend:
            resizeSpot = [
                roomLeftToLegend + legendShortSide,
                roomBelowLegend + legendLongSide
            ]
        else:
            resizeSpot = [
                roomLeftToLegend + legendLongSide,
                roomBelowLegend + legendShortSide
            ]

        GL.glColor3fv(labelColor)
        GL.glBegin(GL.GL_QUADS)
        GL.glVertex2f(float(resizeSpot[0] + resizeSpotRadius),
                      float(resizeSpot[1] - resizeSpotRadius))
        GL.glVertex2f(float(resizeSpot[0] + resizeSpotRadius),
                      float(resizeSpot[1] + resizeSpotRadius))
        GL.glVertex2f(float(resizeSpot[0] - resizeSpotRadius),
                      float(resizeSpot[1] + resizeSpotRadius))
        GL.glVertex2f(float(resizeSpot[0] - resizeSpotRadius),
                      float(resizeSpot[1] - resizeSpotRadius))
        GL.glEnd()

        GL.glEnable(GL.GL_DEPTH_TEST)
        GL.glDepthMask(GL.GL_TRUE)
        #        GL.glEnable(GL.GL_LIGHTING)
        GL.glPopMatrix()
        GL.glMatrixMode(GL.GL_PROJECTION)
        GL.glPopMatrix()
        GL.glMatrixMode(GL.GL_MODELVIEW)
    else:
        resizeSpot = None

    return [(lPt1[0], lPt1[1]), (lPt2[0], lPt2[1]), (lPt3[0], lPt3[1]),
            (lPt4[0], lPt4[1])], resizeSpot, verticalLegend
示例#34
0
def drawLegendOnly(
                    fullWidth,
                    fullHeight,
                    ramp,
                    verticalLegend=True,
                    roomLeftToLegend=0,
                    roomBelowLegend=50,
                    legendShortSide=10,
                    legendLongSide=150,
                    interpolate=True,
                    selected=False,
                    tile=None,
                    ):

    if ramp is None or len(ramp) == 0:
        return

    if tile is not None and selected is True:
        selected = False

    # deducted values
    if verticalLegend is True:
        lRoomToLegendCloseLongSide = roomLeftToLegend
        lRoomToLegendCloseShortSide = roomBelowLegend
        if legendShortSide < 1:
            legendShortSide = 1
        if legendLongSide < 1:
            legendLongSide = 1
    else:
        lRoomToLegendCloseLongSide = roomBelowLegend 
        lRoomToLegendCloseShortSide = roomLeftToLegend 
        if legendShortSide < 1:
            legendShortSide = 1
        if legendLongSide < 1:
            legendLongSide = 1

    lRoomToLegendFarLongSide = lRoomToLegendCloseLongSide + legendShortSide 

    GL.glMatrixMode(GL.GL_PROJECTION)
    GL.glPushMatrix()
    GL.glLoadIdentity()
    if tile is None:
        GL.glOrtho(0, float(fullWidth), 0, float(fullHeight), -1, 1)
    else:
        GL.glOrtho(float(tile[0]), float(tile[1]), float(tile[2]), float(tile[3]), -1, 1)
    GL.glMatrixMode(GL.GL_MODELVIEW)
    GL.glPushMatrix()
    GL.glLoadIdentity()
    GL.glDisable( GL.GL_LIGHTING )

    GL.glPolygonMode(GL.GL_FRONT, GL.GL_FILL)
    GL.glDisable(GL.GL_DEPTH_TEST)
    GL.glDepthMask(GL.GL_FALSE)
    
    if len(ramp[0]) == 4: # there are alpha values, draw checkered bg
        GL.glEnable(GL.GL_BLEND)
        GL.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA)
        #draw a bunch of quads as background
        lCheckerSquareSize = legendShortSide * .5
        if lCheckerSquareSize != 0:
            nbquads = int(legendLongSide / lCheckerSquareSize)
        else:
            nbquads = 1
        c1 = ( 0.1, 0.1, 0.1 )
        c2 = ( 0.3, 0.3, 0.3 )
        c = c1
        x2 = None
        for i in range(nbquads+1):
            GL.glColor3fv(c)
            if i==nbquads and nbquads != 0:
                x1=x2
                x2=legendLongSide
            else:
                x1 = i*lCheckerSquareSize
                x2 = min (x1+lCheckerSquareSize, legendLongSide)

            GL.glBegin(GL.GL_QUADS)
            if verticalLegend is True:
                GL.glVertex2f(float(lRoomToLegendCloseLongSide),
                              float(lRoomToLegendCloseShortSide + x1))
                GL.glVertex2f(float(lRoomToLegendCloseLongSide+lCheckerSquareSize), 
                              float(lRoomToLegendCloseShortSide + x1))
                GL.glVertex2f(float(lRoomToLegendCloseLongSide+lCheckerSquareSize), 
                              float(lRoomToLegendCloseShortSide + x2))
                GL.glVertex2f(float(lRoomToLegendCloseLongSide), 
                              float(lRoomToLegendCloseShortSide + x2))
            else:
                GL.glVertex2f(float(lRoomToLegendCloseShortSide + x2),
                              float(lRoomToLegendCloseLongSide))
                GL.glVertex2f(float(lRoomToLegendCloseShortSide + x2),
                              float(lRoomToLegendCloseLongSide+lCheckerSquareSize))
                GL.glVertex2f(float(lRoomToLegendCloseShortSide + x1),
                              float(lRoomToLegendCloseLongSide+lCheckerSquareSize))
                GL.glVertex2f(float(lRoomToLegendCloseShortSide + x1),
                              float(lRoomToLegendCloseLongSide))
            GL.glEnd()
    
            if c==c1:
                c=c2
            else:
                c=c1
    
            GL.glColor3fv(c)
            GL.glBegin(GL.GL_QUADS)
            if verticalLegend is True:
                GL.glVertex2f(float(lRoomToLegendCloseLongSide+lCheckerSquareSize), 
                              float(lRoomToLegendCloseShortSide + x1))
                GL.glVertex2f(float(lRoomToLegendFarLongSide), 
                              float(lRoomToLegendCloseShortSide + x1))
                GL.glVertex2f(float(lRoomToLegendFarLongSide), 
                              float(lRoomToLegendCloseShortSide + x2))
                GL.glVertex2f(float(lRoomToLegendCloseLongSide+lCheckerSquareSize), 
                              float(lRoomToLegendCloseShortSide + x2))
            else:
                GL.glVertex2f(float(lRoomToLegendCloseShortSide + x2),
                              float(lRoomToLegendCloseLongSide+lCheckerSquareSize))
                GL.glVertex2f(float(lRoomToLegendCloseShortSide + x2),
                              float(lRoomToLegendFarLongSide))
                GL.glVertex2f(float(lRoomToLegendCloseShortSide + x1),
                              float(lRoomToLegendFarLongSide))
                GL.glVertex2f(float(lRoomToLegendCloseShortSide + x1),
                              float(lRoomToLegendCloseLongSide+lCheckerSquareSize))
            GL.glEnd()
        
    #interp = False
    if interpolate and (len(ramp) > 1): # we interpolate colors
        # draw a quad strip for the color map
        lDelta = legendLongSide/float(len(ramp)-1)
        GL.glBegin(GL.GL_QUAD_STRIP)
        for i in range(len(ramp)):
            if selected is True:
                c = deepcopy(ramp[i])
                for j in range(3):
                     c[j] = .35 + c[j]*.3
            else:
                c = ramp[i]
            if len(c)==3:
                GL.glColor3fv(c)
            elif len(c)==4:
                GL.glColor4fv(c)
            lStep = i*lDelta
            if verticalLegend is True:
                GL.glVertex2f(float(lRoomToLegendCloseLongSide), 
                              float(lRoomToLegendCloseShortSide + lStep))
                GL.glVertex2f(float(lRoomToLegendFarLongSide), 
                              float(lRoomToLegendCloseShortSide + lStep))
            else:
                GL.glVertex2f(float(lRoomToLegendCloseShortSide + lStep),
                              float(lRoomToLegendFarLongSide))
                GL.glVertex2f(float(lRoomToLegendCloseShortSide + lStep),
                              float(lRoomToLegendCloseLongSide))
        GL.glEnd()
    else: 
        # we draw a quad for each color
        lDelta = legendLongSide/float(len(ramp))
        for i in range(len(ramp)):
            if selected is True:
                c = deepcopy(ramp[i])
                for j in range(3):
                     c[j] = .35 + c[j]*.3
            else:
                c = ramp[i]
            if len(c)==3:
                GL.glColor3fv(c)
            elif len(c)==4:
                GL.glColor4fv(c)
            x1 = i*lDelta
            x2 = x1+lDelta
            GL.glBegin(GL.GL_QUADS)
            if verticalLegend is True:
                GL.glVertex2f(float(lRoomToLegendCloseLongSide),
                              float(lRoomToLegendCloseShortSide + x1))
                GL.glVertex2f(float(lRoomToLegendFarLongSide), 
                              float(lRoomToLegendCloseShortSide + x1))
                GL.glVertex2f(float(lRoomToLegendFarLongSide), 
                              float(lRoomToLegendCloseShortSide + x2))
                GL.glVertex2f(float(lRoomToLegendCloseLongSide), 
                              float(lRoomToLegendCloseShortSide + x2))
            else:
                GL.glVertex2f(float(lRoomToLegendCloseShortSide + x2),
                              float(lRoomToLegendCloseLongSide))
                GL.glVertex2f(float(lRoomToLegendCloseShortSide + x2),
                              float(lRoomToLegendFarLongSide))
                GL.glVertex2f(float(lRoomToLegendCloseShortSide + x1),
                              float(lRoomToLegendFarLongSide))
                GL.glVertex2f(float(lRoomToLegendCloseShortSide + x1),
                              float(lRoomToLegendCloseLongSide))
            GL.glEnd()
    
    if len(ramp[0])==4:
        GL.glDisable(GL.GL_BLEND)

    GL.glEnable(GL.GL_DEPTH_TEST)
    GL.glDepthMask(GL.GL_TRUE) 

#    GL.glEnable(GL.GL_LIGHTING)
    GL.glPopMatrix()
    GL.glMatrixMode(GL.GL_PROJECTION)
    GL.glPopMatrix()
    GL.glMatrixMode(GL.GL_MODELVIEW)
示例#35
0
def drawLegendLabelName(
                    fullWidth,
                    fullHeight,
                    ramp,
                    mini,
                    maxi,
                    name='',
                    unit='',
                    labelValues=[],
                    verticalLegend=True,
                    leftOrBelowLabels=False,
                    roomLeftToLegend=0,
                    roomBelowLegend=50,
                    legendShortSide=10,
                    legendLongSide=150,
                    significantDigits=3,
                    backgroundColor=(0,0,0,.8),
                    labelColor=(1,1,1),
                    interpolate=True,
                    frame=True,
                    selected=False,
                    numOfLabels=None,
                    resizeSpotRadius=5,
                    fontScale=8,
                    glfFontID=0,
                    tile=None,
                    ):
    
    if ramp is None or len(ramp) == 0:
        return

    if tile is not None and selected is True:
        selected = False

    # some glf font initialisation 
    glf.glfSetCurrentFont(glfFontID)
    glf.glfStringCentering(GL.GL_FALSE)
    glf.glfStringDirection(glf.GLF_LEFT)

    # calculate name and unit size
    lNameMinAndMax = glf.glfGetStringBounds(name)
    lNameMinAndMax = ( lNameMinAndMax[0]*fontScale,
                       lNameMinAndMax[1]*fontScale,
                       lNameMinAndMax[2]*fontScale,
                       lNameMinAndMax[3]*fontScale )
    lNameWidth = lNameMinAndMax[2]-lNameMinAndMax[0]
    lNameHeight = lNameMinAndMax[3]-lNameMinAndMax[1]
    if lNameWidth == 0:
        lNameWidth = fontScale
    if unit is not None and (len(unit) > 0):
        lUnitMinAndMax = glf.glfGetStringBounds(unit)
        lUnitMinAndMax = ( lUnitMinAndMax[0]*fontScale,
                           lUnitMinAndMax[1]*fontScale,
                           lUnitMinAndMax[2]*fontScale,
                           lUnitMinAndMax[3]*fontScale )
        lUnitWidth = lUnitMinAndMax[2]-lUnitMinAndMax[0]
        lUnitHeight = lUnitMinAndMax[3]-lUnitMinAndMax[1]
    else:
        lUnitWidth = fontScale
        lUnitHeight = 0
    lMaxNameUnitHeight = max(lNameHeight, lUnitHeight)
    lMaxNameUnitWidth = max(lNameWidth, lUnitWidth)

    # deducted values
    fontScaleHalf = fontScale/2
    if verticalLegend is True:
        lRoomToLegendCloseLongSide = roomLeftToLegend
        lRoomToLegendCloseShortSide = roomBelowLegend
        if legendShortSide < 1:
            legendShortSide = 1
        if legendLongSide < 1:
            legendLongSide = 1
    else:
        lRoomToLegendCloseLongSide = roomBelowLegend 
        lRoomToLegendCloseShortSide = roomLeftToLegend 
        if legendShortSide < lMaxNameUnitHeight + fontScaleHalf:
            legendShortSide = lMaxNameUnitHeight + fontScaleHalf
        if legendLongSide < 1:
            legendLongSide = 1

    lRoomToLegendFarLongSide = lRoomToLegendCloseLongSide + legendShortSide 
    lRoomToLegendFarShortSide = lRoomToLegendCloseShortSide + legendLongSide 

    # prepare the legend labels
    if len(labelValues) > 0:
        lOnScreenLabelValues = labelValues
    else:        
        if numOfLabels == None or numOfLabels < 0:
            if verticalLegend is True:
                numOfLabels = 6
            else:
                numOfLabels = 4
        
        if ( numOfLabels == 0 ) or maxi is None or mini is None:
            lOnScreenLabelValues = []
        elif numOfLabels == 1:
            lOnScreenLabelValues = [(mini+maxi)*0.5]
        else: 
            delta = (maxi - mini) / float(numOfLabels-1)
            lOnScreenLabelValues = []
            for i in range(numOfLabels):
                lOnScreenLabelValues.append(mini+i*delta)

    lMaxLabelDigits = 0
    for v in lOnScreenLabelValues:
        lLabel = "%.*g" % (significantDigits,v)
        if lMaxLabelDigits < len(lLabel):
            lMaxLabelDigits = len(lLabel)

    # calculate labels size
    lLabelsMinAndMax = []
    lLabelsWidth = []
    lLabelsHeight = []
    lMaxLabelsWidth = 0
    lMaxLabelsHeight = 0
    for v in lOnScreenLabelValues:
        lLabel = "%.*g" % (significantDigits,v)
        lLabelsMinAndMax.append( glf.glfGetStringBounds(lLabel) )
        lLabelsMinAndMax[-1] = ( lLabelsMinAndMax[-1][0]*fontScale,
                           lLabelsMinAndMax[-1][1]*fontScale,
                           lLabelsMinAndMax[-1][2]*fontScale,
                           lLabelsMinAndMax[-1][3]*fontScale )
        lLabelsWidth.append( lLabelsMinAndMax[-1][2]-lLabelsMinAndMax[-1][0] )
        lLabelsHeight.append( lLabelsMinAndMax[-1][3]-lLabelsMinAndMax[-1][1] )
        if lLabelsWidth[-1] > lMaxLabelsWidth:
            lMaxLabelsWidth = lLabelsWidth[-1]
        if lLabelsHeight[-1] > lMaxLabelsHeight:
            lMaxLabelsHeight = lLabelsHeight[-1]

    # calculate frame size
    lMaxWidth = max(lMaxLabelsWidth + legendShortSide, lMaxNameUnitWidth)
    lMaxNameUnitHeightHalf = lMaxNameUnitHeight/2
    legendShortSideHalf = legendShortSide/2
    if verticalLegend is True:
        if leftOrBelowLabels is False:
            lPt1 = (lRoomToLegendCloseLongSide,
                    -fontScale+lRoomToLegendCloseShortSide-lMaxLabelsHeight-lNameHeight,0)
            lPt2 = (fontScale+lRoomToLegendCloseLongSide+lMaxWidth, 
                    -fontScale+lRoomToLegendCloseShortSide-lMaxLabelsHeight-lNameHeight,0)
            lPt3 = (fontScale+lRoomToLegendCloseLongSide+lMaxWidth, 
                    fontScale+lRoomToLegendFarShortSide+lMaxLabelsHeight+lUnitHeight,0)
            lPt4 = (lRoomToLegendCloseLongSide, 
                    fontScale+lRoomToLegendFarShortSide+lMaxLabelsHeight+lUnitHeight,0)
        else:
            lPt1 = (-fontScale+lRoomToLegendFarLongSide-lMaxWidth,
                    -fontScale+lRoomToLegendCloseShortSide-lMaxLabelsHeight-lNameHeight,0)
            lPt2 = (lRoomToLegendCloseLongSide+legendShortSide, 
                    -fontScale+lRoomToLegendCloseShortSide-lMaxLabelsHeight-lNameHeight,0)
            lPt3 = (lRoomToLegendCloseLongSide+legendShortSide, 
                    fontScale+lRoomToLegendFarShortSide+lMaxLabelsHeight+lUnitHeight,0)
            lPt4 = (-fontScale+lRoomToLegendFarLongSide-lMaxWidth, 
                    fontScale+lRoomToLegendFarShortSide+lMaxLabelsHeight+lUnitHeight,0)
    else:
        if leftOrBelowLabels is False:
            lPt1 = (-fontScale+lRoomToLegendCloseShortSide-lNameWidth,
                    lRoomToLegendCloseLongSide,0)
            lPt2 = (fontScale+lRoomToLegendFarShortSide+lUnitWidth,
                    lRoomToLegendCloseLongSide,0)
            lPt3 = (fontScale+lRoomToLegendFarShortSide+lUnitWidth, 
                    fontScale+lRoomToLegendCloseLongSide \
                    + legendShortSide \
                    + lMaxLabelsHeight,
                    0)
            lPt4 = (-fontScale+lRoomToLegendCloseShortSide-lNameWidth, 
                    fontScale+lRoomToLegendCloseLongSide \
                    + legendShortSide \
                    + lMaxLabelsHeight,
                    0)
        else:
            lPt1 = (-fontScale+lRoomToLegendCloseShortSide-lNameWidth,
                    -fontScale+lRoomToLegendCloseLongSide-lMaxLabelsHeight,
                    0)
            lPt2 = (fontScale+lRoomToLegendFarShortSide+lUnitWidth,
                    -fontScale+lRoomToLegendCloseLongSide-lMaxLabelsHeight,
                    0)
            lPt3 = (fontScale+lRoomToLegendFarShortSide+lUnitWidth,
                    lRoomToLegendFarLongSide,0)
            lPt4 = (-fontScale+lRoomToLegendCloseShortSide-lNameWidth,
                    lRoomToLegendFarLongSide,0)

    if selected is True:
        labelColor2 = (.5, .5, .5)
    else:
        labelColor2 = labelColor

    GL.glMatrixMode(GL.GL_PROJECTION)
    GL.glPushMatrix()
    GL.glLoadIdentity()
    if tile is None:
        GL.glOrtho(0, float(fullWidth), 0, float(fullHeight), -1, 1)
    else:
        GL.glOrtho(float(tile[0]), float(tile[1]), float(tile[2]), float(tile[3]), -1, 1)
    GL.glMatrixMode(GL.GL_MODELVIEW)
    GL.glPushMatrix()
    GL.glLoadIdentity()
    GL.glDisable( GL.GL_LIGHTING )

    GL.glPolygonMode(GL.GL_FRONT, GL.GL_FILL )
    
    if len(backgroundColor) == 4:
        GL.glEnable(GL.GL_BLEND)
        GL.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA)

    #because of an unexplained bug on michel's laptop,
    #we don't draw the background when there is no frame 
    #(so michel has a way to manage the legend)
    if frame is True:
        #draw transparent background
        GL.glDepthMask(GL.GL_FALSE) 
        if len(backgroundColor) == 3:
            GL.glColor3fv(backgroundColor)
        else:
            GL.glColor4fv(backgroundColor)           
        GL.glBegin(GL.GL_QUADS)
        GL.glVertex3fv(lPt1)
        GL.glVertex3fv(lPt2)
        GL.glVertex3fv(lPt3)
        GL.glVertex3fv(lPt4)
        GL.glEnd()
        GL.glDepthMask(GL.GL_TRUE) 

    #draw frame
    if frame is True:
        GL.glPolygonMode(GL.GL_FRONT, GL.GL_LINE )
        GL.glLineWidth(1)
        GL.glColor3fv(labelColor2)    
        GL.glBegin(GL.GL_QUADS)
        GL.glVertex3fv(lPt1)
        GL.glVertex3fv(lPt2)
        GL.glVertex3fv(lPt3)
        GL.glVertex3fv(lPt4)
        GL.glEnd()
        GL.glPolygonMode(GL.GL_FRONT, GL.GL_FILL)
    
    if mini is not None and maxi is not None and maxi > mini:
        lUnitStep = legendLongSide/float(maxi-mini)
    else:
        lUnitStep = legendLongSide
    
    GL.glDisable( GL.GL_LIGHTING )

    if verticalLegend is True:
        if leftOrBelowLabels is True:
            lRoomLeftToLabel = -fontScaleHalf + lRoomToLegendCloseLongSide
            lRoomLeftToName = -fontScaleHalf + lRoomToLegendFarLongSide - lNameWidth
            lRoomLeftToUnit = -fontScaleHalf + lRoomToLegendFarLongSide - lUnitWidth
            lRoomBelowName = -fontScaleHalf + roomBelowLegend - lMaxLabelsHeight - lNameHeight
            lRoomBelowUnit = fontScaleHalf + roomBelowLegend + legendLongSide + lMaxLabelsHeight
        else:
            lRoomLeftToLabel = fontScaleHalf + lRoomToLegendFarLongSide
            lRoomLeftToName = fontScaleHalf + lRoomToLegendCloseLongSide    
            lRoomLeftToUnit = lRoomLeftToName
            lRoomBelowName = -fontScaleHalf + roomBelowLegend - lMaxLabelsHeight - lNameHeight
            lRoomBelowUnit = fontScaleHalf + roomBelowLegend + legendLongSide + lMaxLabelsHeight
    else:
        if leftOrBelowLabels is True:
            lRoomBelowLabel = -fontScaleHalf + lRoomToLegendFarLongSide \
                              - legendShortSide - lMaxLabelsHeight
            lRoomBelowName = lRoomToLegendCloseLongSide + legendShortSideHalf \
                             - lMaxNameUnitHeightHalf
        else:
            lRoomBelowLabel = fontScaleHalf + lRoomToLegendCloseLongSide \
                              + legendShortSide
            lRoomBelowName = lRoomToLegendCloseLongSide + legendShortSideHalf \
                             - lMaxNameUnitHeightHalf
        lRoomBelowUnit = lRoomBelowName
        lRoomLeftToName = -fontScaleHalf + lRoomToLegendCloseShortSide - lNameWidth
        lRoomLeftToUnit = fontScaleHalf + lRoomToLegendFarShortSide

    # set the color of the text
    GL.glColor3fv(labelColor2)

    # print the legend name
    GL.glPushMatrix()
    GL.glTranslatef(
            float(lRoomLeftToName+fontScale),
            float(lRoomBelowName-lNameMinAndMax[1]),
            0)
    GL.glScalef(float(fontScale), float(fontScale), 0);
    glf.glfDrawSolidString(name)
    GL.glPopMatrix()
    if unit is not None and (len(unit) > 0):
        GL.glPushMatrix()
        GL.glTranslatef(
            float(lRoomLeftToUnit+fontScale),
            float(lRoomBelowUnit-lUnitMinAndMax[1]),
            0)
        GL.glScalef(float(fontScale), float(fontScale), 1);
        glf.glfDrawSolidString(unit)
        GL.glPopMatrix()
       
    if mini is not None and maxi is not None:
        i = 0
        for v in lOnScreenLabelValues:
            #calculate label position
            lLabel = "%.*g" % (significantDigits,v)
            lStep = (v - mini) * lUnitStep
            GL.glPushMatrix()
            if verticalLegend:
                if leftOrBelowLabels:
                    GL.glTranslatef(
                        float(lRoomLeftToLabel+fontScale-lLabelsWidth[i]),
                        float(roomBelowLegend-(lLabelsHeight[i]/2+lLabelsMinAndMax[i][1])+lStep),
                        0)
                else:
                    GL.glTranslatef(
                        float(lRoomLeftToLabel+fontScale),
                        float(roomBelowLegend-(lLabelsHeight[i]/2+lLabelsMinAndMax[i][1])+lStep),
                        0)
            else:
                GL.glTranslatef(
                    float(roomLeftToLegend-(lLabelsWidth[i]/2)+fontScale+lStep),
                    float(lRoomBelowLabel-lLabelsMinAndMax[i][1]),
                    0)
            GL.glScalef(float(fontScale), float(fontScale), 1);
            glf.glfDrawSolidString("%s" % lLabel)
            GL.glPopMatrix()
            i += 1

    if len(backgroundColor) == 4:
        GL.glDisable(GL.GL_BLEND)

#    GL.glEnable(GL.GL_LIGHTING)
    GL.glPopMatrix()
    GL.glMatrixMode(GL.GL_PROJECTION)
    GL.glPopMatrix()
    GL.glMatrixMode(GL.GL_MODELVIEW)

    drawLegendOnly(
        fullWidth=fullWidth,
        fullHeight=fullHeight,
        ramp=ramp,
        verticalLegend=verticalLegend,
        roomLeftToLegend=roomLeftToLegend,
        roomBelowLegend=roomBelowLegend,
        legendShortSide=legendShortSide,
        legendLongSide=legendLongSide,
        interpolate=interpolate,
        selected=selected,
        tile=tile,
        )

    if selected is True:
        GL.glMatrixMode(GL.GL_PROJECTION)
        GL.glPushMatrix()
        GL.glLoadIdentity()
        GL.glOrtho(0, float(fullWidth), 0, float(fullHeight), -1, 1)
        GL.glMatrixMode(GL.GL_MODELVIEW)
        GL.glPushMatrix()
        GL.glLoadIdentity()
        GL.glDisable( GL.GL_LIGHTING )
        GL.glPolygonMode(GL.GL_FRONT, GL.GL_FILL )
        GL.glDisable(GL.GL_DEPTH_TEST)
        GL.glDepthMask(GL.GL_FALSE)
        GL.glDisable(GL.GL_LIGHTING)

        resizeSpot = []
        if verticalLegend:
            resizeSpot= [ roomLeftToLegend + legendShortSide,
                          roomBelowLegend + legendLongSide ]
        else:
            resizeSpot = [ roomLeftToLegend + legendLongSide,
                           roomBelowLegend + legendShortSide ]

        GL.glColor3fv(labelColor)
        GL.glBegin(GL.GL_QUADS)
        GL.glVertex2f(float(resizeSpot[0]+resizeSpotRadius),float(resizeSpot[1]-resizeSpotRadius))
        GL.glVertex2f(float(resizeSpot[0]+resizeSpotRadius),float(resizeSpot[1]+resizeSpotRadius))
        GL.glVertex2f(float(resizeSpot[0]-resizeSpotRadius),float(resizeSpot[1]+resizeSpotRadius))
        GL.glVertex2f(float(resizeSpot[0]-resizeSpotRadius),float(resizeSpot[1]-resizeSpotRadius))
        GL.glEnd()

        GL.glEnable(GL.GL_DEPTH_TEST)
        GL.glDepthMask(GL.GL_TRUE) 
#        GL.glEnable(GL.GL_LIGHTING)
        GL.glPopMatrix()
        GL.glMatrixMode(GL.GL_PROJECTION)
        GL.glPopMatrix()
        GL.glMatrixMode(GL.GL_MODELVIEW)
    else:
        resizeSpot = None

    return [(lPt1[0], lPt1[1]), 
            (lPt2[0], lPt2[1]), 
            (lPt3[0], lPt3[1]), 
            (lPt4[0], lPt4[1]) ] , resizeSpot , verticalLegend
示例#36
0
lightOneColor = [0.99, 0.99, 0.99, 1.0] 

lightTwoPosition = [-40.0, 40, 100.0, 0.0]
lightTwoColor = [0.99, 0.99, 0.99, 1.0] 

###    initialize glut 
GLUT.glutInit (sys.argv)
GLUT.glutInitDisplayMode (GLUT.GLUT_DOUBLE | GLUT.GLUT_RGB | GLUT.GLUT_DEPTH)
GLUT.glutCreateWindow ("join styles")
GLUT.glutDisplayFunc (DrawStuff)
#GLUT.glutMotionFunc (MouseMotion)


#    initialize GL 
GL.glClearDepth (1.0)
GL.glEnable (GL.GL_DEPTH_TEST)
GL.glClearColor (0.0, 0.0, 0.0, 0.0)
GL.glShadeModel (GL.GL_SMOOTH)

GL.glMatrixMode (GL.GL_PROJECTION)
# roughly, measured in centimeters 
GL.glFrustum (-9.0, 9.0, -9.0, 9.0, 50.0, 150.0)
GL.glMatrixMode(GL.GL_MODELVIEW)

# initialize lighting 
GL.glLightfv (GL.GL_LIGHT0, GL.GL_POSITION, lightOnePosition)
GL.glLightfv (GL.GL_LIGHT0, GL.GL_DIFFUSE, lightOneColor)
GL.glEnable (GL.GL_LIGHT0)
GL.glLightfv (GL.GL_LIGHT1, GL.GL_POSITION, lightTwoPosition)
GL.glLightfv (GL.GL_LIGHT1, GL.GL_DIFFUSE, lightTwoColor)
GL.glEnable (GL.GL_LIGHT1)
示例#37
0
    def Set(self, **kw):
        """ set light values.  For direction, position, and spot direction,
vals are given in absolute coordinates (independent of camera or
object).  For these three values, the flag is set to 1 when they are
changed.
"""
        #print "Light.Set"
        self.hasBeenCurrent = True # remember the light has been changed

        tagModified = True
        val = getkw(kw, 'tagModified')
        if val is not None:
            tagModified = val
        assert tagModified in [True, False]
        self._modified = tagModified

        self.viewer.currentCamera.Activate()
        
	val = getkw(kw, 'ambient')
	if not val is None:
	    #self.ambient = OneColor( val )
	    #GL.glLightfv(self.num, GL.GL_AMBIENT, self.ambient )
	    if len(val)==3 or len(val)==4: 
	        self.ambient = OneColor( val )
                GL.glLightModelfv(GL.GL_LIGHT_MODEL_AMBIENT, self.ambient)
                GL.glLightfv(self.num, GL.GL_AMBIENT, self.ambient) # needed for mesa 
	    else: 
	        raise ValueError('length of new color must be 3 or 4') 

	val = getkw(kw, 'diffuse')
	if not val is None:
	    self.diffuse = OneColor( val )
	    GL.glLightfv(self.num, GL.GL_DIFFUSE, self.diffuse )

	val = getkw(kw, 'specular')
	if not val is None:
	    self.specular = OneColor( val )
	    GL.glLightfv(self.num, GL.GL_SPECULAR, self.specular )

	val = getkw(kw, 'direction')
	if not val is None:
            val = list(val)
            if len(val)==3: val += [0.]
            assert len(val)==4
	    self.direction = val
	    self.direction[3] = 0.0
            self.dirFlag = 1  # tell the camera to redraw this light
            self.positional = False

	val = getkw(kw, 'position')
	if not val is None:
            val = list(val)
            if len(val)==3: val += [1.]
            assert len(val)==4
	    self.position = val
	    self.position[3] = 1.0
            self.posFlag = 1  # tell the camera to redraw this light
            self.positional = True

	val = getkw(kw, 'spotDirection')
	if not val is None:
            val = list(val)
            if len(val)==3: val += [0.]
            assert len(val)==4
	    self.spotDirection = val
	    self.spotDirection[3] = 0.0
            self.spotFlag = 1  # tell the camera to redraw this light

	val = getkw(kw, 'spotExponent')
	if not val is None:
	    self.spotExponent = float(val)
	    GL.glLightfv(self.num, GL.GL_SPOT_EXPONENT, [self.spotExponent])
            
	val = getkw(kw, 'spotCutoff')
	if not val is None:
            if val > 180.:
                raise ValueError("spotCutoff must be in [0., 90.] or 180.")
	    self.spotCutoff = float( val )
	    GL.glLightfv(self.num, GL.GL_SPOT_CUTOFF, [self.spotCutoff] )

	val = getkw(kw, 'constantAttenuation')
	if not val is None:
	    self.constantAttenuation = float( val )
            if self.constantAttenuation < 0.0:
                raise ValueError("constantAttenuation must be >= 0.0")
	    GL.glLightfv(self.num, GL.GL_CONSTANT_ATTENUATION,
                         [self.constantAttenuation] )

	val = getkw(kw, 'linearAttenuation')
	if not val is None:
	    self.linearAttenuation = float( val )
            if self.linearAttenuation < 0.0:
                raise ValueError("linearAttenuation must be >= 0.0")
	    GL.glLightfv(self.num, GL.GL_LINEAR_ATTENUATION,
                         [self.linearAttenuation] )

	val = getkw(kw, 'quadraticAttenuation')
	if not val is None:
	    self.quadraticAttenuation = float( val )
            if self.quadraticAttenuation < 0.0:
                raise ValueError("quadraticAttenuation must be >= 0.0")
	    GL.glLightfv(self.num, GL.GL_QUADRATIC_ATTENUATION,
                         [self.quadraticAttenuation] )

	val = getkw(kw, 'positional')
	if not val is None:
	    if val is True: self.position[3] = 1.0
	    elif val is False: self.position[3] = 0.0
	    else: raise AttributeError('positional can only be True or False')
	    self.positional = val

	val = getkw(kw, 'enabled')
	if not val is None:
	    if val in (True, 1): GL.glEnable(self.num)
	    elif val in (False, 0): GL.glDisable(self.num)
	    else: raise AttributeError('enabled can only be True or False')
            self.enabled = val
            
	val = getkw(kw, 'visible')
	if not val is None:
	    if val in (True, False): self.visible = val
	    else: raise AttributeError('visible can only be True or False')

	val = getkw(kw, 'lineWidth')
	if not val is None:
	    if val >= 1:
                self.lineWidth = int(val)
	    else: raise AttributeError('lineWidth has to be >= 1')

	val = getkw(kw, 'length')
	if not val is None:
	    if val > 0.0: self.length = float ( val )
	    else: raise AttributeError('length has to be > 0.0')

#	val = getkw(kw, 'antialiased')
#	if not val is None:
#	    if val in (True, False):
#		self.antialiased = val
#	    else: raise ValueError ('antialiased can only be True or False')

	val = getkw(kw, 'rotation')
	if not val is None:
            mat = Numeric.reshape(Numeric.array(val), (16,)).astype('f')
            self.rotation = mat

	val = getkw(kw, 'translation')
	if not val is None:
            mat = Numeric.reshape(Numeric.array(val), (3,)).astype('f')
            self.translation = mat

	val = getkw(kw, 'scale')
	if not val is None:
            mat = Numeric.reshape(Numeric.array(val), (3,)).astype('f')
            self.scale = mat

	val = getkw(kw, 'pivot')
	if not val is None:
            mat = Numeric.reshape(Numeric.array(val), (3,)).astype('f')
            self.pivot = mat

	if len(kw):
	    print 'WARNING9: Keyword(s) %s not used' % kw.keys()

        #guillaume vareille 9/29/2005 : 
        # was c = self.viewer.cameras[0]
        # was generating alternativly good and wrong rendering when 2 cameras 
        c = self.viewer.currentCamera 
        
        # force light to be update in viewer
        c.Redraw()

        # brodcast to other application that want to know about that light
        # using aftere does not seem to make it better
        #c.after_idle(self.broadcast)
        self.broadcast()
示例#38
0
    def Setup(self):
	"""Setup texture mapping"""
	
	t = self
	if not t.enabled:
	    GL.glDisable(t.dim)
	    return

	t.MakeMatrix()

	GL.glTexEnvf(GL.GL_TEXTURE_ENV,
		     GL.GL_TEXTURE_ENV_MODE, t.envMode)
	if t.envMode==GL.GL_BLEND:
	    GL.glTexEnvf(GL.GL_TEXTURE_ENV,
			 GL.GL_TEXTURE_ENV_COLOR, t.envColor)

	GL.glTexParameterf(t.dim, GL.GL_TEXTURE_WRAP_S, t.wrap[0])
	GL.glTexParameterf(t.dim, GL.GL_TEXTURE_MAG_FILTER, 
			   t.magFilter)
	GL.glTexParameterf(t.dim, GL.GL_TEXTURE_MIN_FILTER, 
			   t.minFilter)
	if t.auto:
 	    GL.glTexGeni(GL.GL_S, GL.GL_TEXTURE_GEN_MODE,
 			 t.genMod[0])
 	    GL.glTexGenfv(GL.GL_S, t.genPlane[0], t.plane[0])
	    GL.glEnable(GL.GL_TEXTURE_GEN_S)
        else:
	    GL.glDisable(GL.GL_TEXTURE_GEN_S)

	if t.dim==GL.GL_TEXTURE_1D:
            from opengltk.extent import _gllib
	    _gllib.glTexImage1D(t.dim, t.level, t.format,
			    t.width, 0, t.format,
			    GL.GL_UNSIGNED_BYTE, t.image)

	elif t.dim==GL.GL_TEXTURE_2D:
	    GL.glTexParameterf(t.dim, GL.GL_TEXTURE_WRAP_T, t.wrap[1])
            from opengltk.extent import _gllib
            # directly call the C function to not have to transform the
            # t.image into a bufarray
            
            #print "t.width, t.height", t.width, t.height
            
	    _gllib.glTexImage2D(t.dim, t.level, t.format,
			    t.width, t.height, t.border!=0, t.format,
			    GL.GL_UNSIGNED_BYTE, t.image)
##             import bufarray
##             bimage = bufarray.Bufarray(im, bufarray.ubyteCtype)
## 	    GL.glTexImage2D(t.dim, t.level,GL.GL_UNSIGNED_BYTE,
##                             t.width, t.height, t.border!=0, t.format,
##                             bimage)
            
## 	    GL.glTexImage2D(t.dim, t.level, t.format,
## 			    t.width, t.height, t.border!=0, t.format,
## 			    GL.GL_UNSIGNED_BYTE, t.image)
            
	    if t.auto:
 		GL.glTexGeni(GL.GL_T, GL.GL_TEXTURE_GEN_MODE,
 			     t.genMod[1])
 		GL.glTexGenfv(GL.GL_T, t.genPlane[1], t.plane[1])
		GL.glEnable(GL.GL_TEXTURE_GEN_T)
            else:
                GL.glDisable(GL.GL_TEXTURE_GEN_T)

 	GL.glEnable(t.dim)
    def DisplayFunction(self):
        if __debug__:
         if hasattr(DejaVu, 'functionName'): DejaVu.functionName()
        """display a set of indexed geometric primitives"""
        
        if self.dpyList:

#            print "DisplayFunction", self.dpyList, self.fullName

            lDrawOutline = (self.getDrawOutlineMode('front'), self.getDrawOutlineMode('back'))
            if (lDrawOutline[0] or lDrawOutline[1]) and self.viewer.hasOffsetExt:

                outl = self.outline

                if   self.GetPolyMode('front') == GL.GL_FILL \
                  or self.GetPolyMode('back') == GL.GL_FILL:

                    mode = GL.GL_POLYGON_OFFSET_FILL

                    GL.glEnable(mode)
                    self.viewer.polyOffset( outl.factor, outl.unit)
                    Geom.DisplayFunction(self)
                    GL.glDisable(mode)

                    GL.glPolygonMode(GL.GL_FRONT_AND_BACK, GL.GL_LINE)
                    if not outl.colorAsMaterial:
                        if outl.lighting:
                            GL.glMaterialfv( GL.GL_FRONT_AND_BACK,
                                             GL.GL_EMISSION,
                                             outl.color )
                        else:
                            GL.glDisable(GL.GL_LIGHTING)
                            GL.glColor4fv (outl.color)

                    GL.glLineWidth(outl.lineWidth)

                    if lDrawOutline[0] is False or lDrawOutline[1] is False:
                        GL.glEnable(GL.GL_CULL_FACE)
                        if lDrawOutline[0]:
                            GL.glCullFace(GL.GL_BACK)
                        elif lDrawOutline[1]:
                            GL.glCullFace(GL.GL_FRONT)
                    else:
                        GL.glDisable(GL.GL_CULL_FACE)

                    if outl.dpyList:
                        currentcontext = self.viewer.currentCamera.tk.call(self.viewer.currentCamera._w, 'contexttag')
                        if currentcontext != outl.dpyList[1]:
                            warnings.warn("""DisplayFunction failed because the current context is the wrong one""")
                            #print "currentcontext != outl.dpyList[1]", currentcontext, outl.dpyList[1]
                        else:
                            #print '#%d'%outl.dpyList[0], currentcontext, "glCallList IndexedGeom"
                            GL.glCallList(outl.dpyList[0])

                    GL.glEnable(GL.GL_CULL_FACE)
                    GL.glEnable(GL.GL_LIGHTING)

                else:
                    Geom.DisplayFunction(self)
            else:
                Geom.DisplayFunction(self)
    def Draw(self):
        if __debug__:
         if hasattr(DejaVu, 'functionName'): DejaVu.functionName()
        """ draw geom
"""
        #print "IndexedGeom.Draw", self.name
        if self.vertexArrayFlag is True \
          and DejaVu.enableVertexArray is True:
            if not (hasattr(self.vertexSet, "texCoords") \
              and self.vertexSet.texCoords.status >= viewerConst.COMPUTED):
                return self.drawVertexArray()

        if len(self.faceSet) and len(self.vertexSet):
            if self.materials[GL.GL_FRONT] and \
                   not self.inheritMaterial:
                mat = self.materials[GL.GL_FRONT]
                fpProp = []
                fpBind = []
                for propInd in range(4):
                    b, p = mat.GetProperty(propInd)
                    fpProp.append(p)
                    fpBind.append(b)
                fpProp.append(mat.prop[4])
                fpBind.append(mat.binding[4])
            else:
                fpProp = None
                fpBind = None

            if self.materials[GL.GL_BACK] and \
               not self.inheritMaterial:
                mat = self.materials[GL.GL_BACK]
                bpProp = []
                bpBind = []
                for propInd in range(4):
                    b, p = mat.GetProperty(propInd)
                    bpProp.append(p)
                    bpBind.append(b)
                bpProp.append(mat.prop[4])
                bpBind.append(mat.binding[4])

            else:
                bpProp = None
                bpBind = None

            texCoords = None
            if hasattr(self.vertexSet, "texCoords"):
                if self.vertexSet.texCoords.status >= viewerConst.COMPUTED:
                    texCoords = self.vertexSet.texCoords.array

            if self.lighting and self.normals is not None:
                if self.invertNormals:
                    norms = - self.normals
                else:
                    norms = self.normals
            else:
                norms = None

            from DejaVu import preventIntelBug_BlackTriangles
            if preventIntelBug_BlackTriangles:
                preventIntelBug = 1
            else:
                preventIntelBug = 0

            lsharpColorBoundaries = self.getSharpColorBoundaries()

            if self.disableStencil is True:
                GL.glDisable(GL.GL_STENCIL_TEST)

            status = glDrawIndexedGeom(
                self.primitiveType,
                self.vertexSet.vertices.array,
                self.faceSet.faces.array,
                norms,
                texCoords,
                fpProp, bpProp, fpBind, bpBind,
                self.frontAndBack, 1,
                lsharpColorBoundaries,
                preventIntelBug,
                highlight=self.highlight,
                )

            if self.disableStencil is True:
                GL.glEnable(GL.GL_STENCIL_TEST)

            return status
    def drawVertexArray(self):
        if __debug__:
         if hasattr(DejaVu, 'functionName'): DejaVu.functionName()
        """ drawVertexArray
"""
        #print "drawVertexArray", self.name
        if hasattr(self, 'faceSet') and len(self.faceSet.faces.array) > 0:
            # vertices
            GL.glEnableClientState(GL.GL_VERTEX_ARRAY)
            
            # normals
            if len(self.vertexSet.normals.array) > 0:
                GL.glEnableClientState(GL.GL_NORMAL_ARRAY)
        
            # colors
            if hasattr(self, 'colorPointerIsOn') and self.colorPointerIsOn is True:
                GL.glEnableClientState(GL.GL_COLOR_ARRAY)
                from DejaVu import preventIntelBug_WhiteTriangles
                if preventIntelBug_WhiteTriangles:
                    GL.glColorMaterial(GL.GL_FRONT_AND_BACK, GL.GL_DIFFUSE)
                else:
                    GL.glColorMaterial(GL.GL_FRONT_AND_BACK, GL.GL_AMBIENT_AND_DIFFUSE)
                GL.glEnable( GL.GL_COLOR_MATERIAL )
                
            if DejaVu.enableVBO is True:
                from opengltk.extent import _glextlib
                # vertices
                _glextlib.glBindBufferARB(_glextlib.GL_ARRAY_BUFFER_ARB,
                                 int(self.vertexArrayFlagBufferList[0]))
                _gllib.glVertexPointer(len(self.vertexSet.vertices.array[0]),
                                              GL.GL_FLOAT, 0, 0)
                # normals
                if len(self.vertexSet.normals.array) > 0:
                    _glextlib.glBindBufferARB(_glextlib.GL_ARRAY_BUFFER_ARB,
                                              int(self.vertexArrayFlagBufferList[1]))
                    _gllib.glNormalPointer(GL.GL_FLOAT, 0, 0)
                # colors
                if hasattr(self, 'colorPointerIsOn') and self.colorPointerIsOn is True:
                    _glextlib.glBindBufferARB(_glextlib.GL_ARRAY_BUFFER_ARB,
                                              int(self.vertexArrayFlagBufferList[3]))
                    _gllib.glColorPointer(4, GL.GL_FLOAT, 0, 0)
            else:
                # vertices
                _gllib.glVertexPointer(len(self.vertexSet.vertices.array[0]),
                                   GL.GL_FLOAT, 0, self.vertexSet.vertices.array)
                # normals
                if len(self.vertexSet.normals.array) > 0:
                    _gllib.glNormalPointer(GL.GL_FLOAT, 0, self.vertexSet.normals.array)
                # colors
                if hasattr(self, 'colorPointerIsOn') and self.colorPointerIsOn is True:
                    _gllib.glColorPointer(4, GL.GL_FLOAT, 0, self.colorArray)

            # Draw faces
            if self.primitiveType == GL.GL_LINE_STRIP:
                lPrimitiveType = GL.GL_LINES
            elif self.primitiveType == GL.GL_TRIANGLES:
                #print "triangles' length:", len(self.faceSet.faces.array[0])
                lPrimitiveType = GL.GL_TRIANGLES
            elif self.primitiveType == GL.GL_QUADS:
                #print "quads' length:", len(self.faceSet.faces.array[0])
                lPrimitiveType = GL.GL_QUADS
            else:
                #print "what's that ?" , self.primitiveType
                lPrimitiveType = self.primitiveType

            lNumOfNonHighlightedIndices = len(self.faceSet.faces.array) - self.numOfHighlightedIndex
            if DejaVu.enableVBO is True:
                _glextlib.glBindBufferARB(_glextlib.GL_ELEMENT_ARRAY_BUFFER, 
                                      int(self.vertexArrayFlagBufferList[2])) #this protect from unexplained segfault
                if self.disableStencil is False and self.numOfHighlightedIndex > 0:
                    # highlighted
                    GL.glStencilFunc(GL.GL_ALWAYS, 1, 1)
                    _gllib.glDrawElements(lPrimitiveType,
                                  self.numOfHighlightedIndex*len(self.faceSet.faces.array[0]),
                                  GL.GL_UNSIGNED_INT,
                                  0 
                                 )
                    GL.glStencilFunc(GL.GL_ALWAYS, 0, 1)

                    # non highlighted
                    _gllib.glDrawElements(lPrimitiveType,
                                  lNumOfNonHighlightedIndices * len(self.faceSet.faces.array[0]),
                                  GL.GL_UNSIGNED_INT,
                                  self.numOfHighlightedIndex * len(self.faceSet.faces.array[0]) * 4
                                 )
                else:
                    _gllib.glDrawElements(lPrimitiveType,
                                  len(self.faceSet.faces.array)*len(self.faceSet.faces.array[0]),
                                  GL.GL_UNSIGNED_INT,
                                  0 
                                 )
                _glextlib.glBindBufferARB(_glextlib.GL_ELEMENT_ARRAY_BUFFER, 0 ) #this protect from unexplained segfault
            else:
                if self.disableStencil is False and self.numOfHighlightedIndex > 0:
                    # highlighted
                    GL.glStencilFunc(GL.GL_ALWAYS, 1, 1)
                    _gllib.glDrawElements(lPrimitiveType,
                                  self.numOfHighlightedIndex*len(self.faceSet.faces.array[0]),
                                  GL.GL_UNSIGNED_INT,
                                  self.faceSet.faces.array 
                                 )
                    GL.glStencilFunc(GL.GL_ALWAYS, 0, 1)

                    # non highlighted
                    _gllib.glDrawElements(lPrimitiveType,
                                  lNumOfNonHighlightedIndices * len(self.faceSet.faces.array[0]),
                                  GL.GL_UNSIGNED_INT,
                                  self.faceSet.faces.array[self.numOfHighlightedIndex: ]
                                 )
                else:
                    _gllib.glDrawElements(lPrimitiveType,
                                  len(self.faceSet.faces.array)*len(self.faceSet.faces.array[0]),
                                  GL.GL_UNSIGNED_INT,
                                  self.faceSet.faces.array
                                 )
            if hasattr(self, 'colorPointerIsOn') and self.colorPointerIsOn is True:
                GL.glDisable( GL.GL_COLOR_MATERIAL )
                GL.glDisableClientState(GL.GL_COLOR_ARRAY)
            if len(self.vertexSet.normals.array) > 0:
                GL.glDisableClientState(GL.GL_NORMAL_ARRAY)
            GL.glDisableClientState(GL.GL_VERTEX_ARRAY)

        return 1