Exemplo n.º 1
0
def draw_filled_rect(origin, dx, dy, color):
##    print 'draw_filled_rect',(origin, dx, dy, color) #####@@@@@
    glDisable(GL_LIGHTING) # this allows the specified color to work. Otherwise it doesn't work (I always get dark blue). Why???
     # guess: if i want lighting, i have to specify a materialcolor, not just a regular color. (and vertex normals)
    try:
        len(color)
    except:
        print "following exception in len(color) for color = %r" % (color,) # 061212 -- why didn't caller's fix_color catch it? ##k
        raise
    if len(color) == 4:
        glColor4fv(color)
        if 0 and color[3] != 1.0:
            print "color has alpha",color ####@@@@
    else:
        glColor3fv(color)
##    glRectfv(origin, origin + dx + dy) # won't work for most coords! also, ignores Z. color still not working.
    glBegin(GL_QUADS)
    glVertex3fv(origin)
    #glColor3fv(white)#
    glVertex3fv(origin + dx)
    # glColor3fv(white) # hack, see if works - yes!
    #glColor3fv(color)#
    glVertex3fv(origin + dx + dy)
    #glColor3fv(white)#
    glVertex3fv(origin + dy)
    glEnd()
    glEnable(GL_LIGHTING) # should be outside of glEnd! when inside, i got infloop! (not sure that was why; quit/reran after that)
Exemplo n.º 2
0
    def draw(self, origin, color=(0.2, 0.2, 0.2, 1.0), drawFaces=True):
        edges = (
            (0, 1),
            (0, 2),
            (0, 4),
            (1, 3),
            (1, 5),
            (7, 3),
            (7, 5),
            (7, 6),
            (6, 2),
            (6, 4),
            (3, 2),
            (5, 4)
        )
        ground = (4, 6, 2, 0)

        normal = get_plane_normal(ground, self.vertices_points_list, -self._center)
        normal_tuple = normal.get_tuple()

        glBegin(GL_QUADS)
        for vertex in ground:
            glColor4fv(color)
            glNormal3fv(normal_tuple)
            glVertex3fv(self.vertices_points_list[vertex] - origin)
        glEnd()

        glBegin(GL_LINES)
        for edge in edges:
            for vertex in edge:
                glColor4fv((0.0, 0.0, 0.0, 1.0))
                glNormal3fv((0.0, 0.0, 0.0))
                glVertex3fv(self.vertices_points_list[vertex] - origin)
        glEnd()
Exemplo n.º 3
0
def draw_filled_rect(origin, dx, dy, color):
    ##    print 'draw_filled_rect',(origin, dx, dy, color) #####@@@@@
    glDisable(
        GL_LIGHTING
    )  # this allows the specified color to work. Otherwise it doesn't work (I always get dark blue). Why???
    # guess: if i want lighting, i have to specify a materialcolor, not just a regular color. (and vertex normals)
    try:
        len(color)
    except:
        print "following exception in len(color) for color = %r" % (
            color,
        )  # 061212 -- why didn't caller's fix_color catch it? ##k
        raise
    if len(color) == 4:
        glColor4fv(color)
        if 0 and color[3] != 1.0:
            print "color has alpha", color  ####@@@@
    else:
        glColor3fv(color)
    ##    glRectfv(origin, origin + dx + dy) # won't work for most coords! also, ignores Z. color still not working.
    glBegin(GL_QUADS)
    glVertex3fv(origin)
    # glColor3fv(white)#
    glVertex3fv(origin + dx)
    # glColor3fv(white) # hack, see if works - yes!
    # glColor3fv(color)#
    glVertex3fv(origin + dx + dy)
    # glColor3fv(white)#
    glVertex3fv(origin + dy)
    glEnd()
    glEnable(
        GL_LIGHTING
    )  # should be outside of glEnd! when inside, i got infloop! (not sure that was why; quit/reran after that)
Exemplo n.º 4
0
def apply_material(color, glprefs = None):
    # todo: move into glprefs.py (see comment above for why)
    """
    In the current GL context,
    set material parameters based on the given color (length 3 or 4) and
    the material-related prefs values in glprefs (which defaults to
    drawing_globals.glprefs).
    """
    if glprefs is None:
        glprefs = drawing_globals.glprefs
        pass
    
    #bruce 051215: make sure color is a tuple, and has length exactly 4, for all
    # uses inside this function, assuming callers pass sequences of length 3 or
    # 4. Needed because glMaterial requires four-component vector and PyOpenGL
    # doesn't check. [If this is useful elsewhere, we can split it into a
    # separate function.]
    color = tuple(color)
    if len(color) == 3:
        color = color + (1.0,) # usual case
    elif len(color) != 4:
        # should never happen; if it does, this assert will always fail
        assert len(color) in [3,4], \
               "color tuples must have length 3 or 4, unlike %r" % (color,)

    glColor4fv(color)          # For drawing lines with lighting disabled.

    if not glprefs.enable_specular_highlights:
        glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color)
        # glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, (0,0,0,1))
        return

    glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color)

    whiteness = glprefs.specular_whiteness
    brightness = glprefs.specular_brightness
    if whiteness == 1.0:
        specular = (1.0, 1.0, 1.0, 1.0) # optimization
    else:
        if whiteness == 0.0:
            specular = color # optimization
        else:
            # assume color[3] (alpha) is not passed or is always 1.0
            c1 = 1.0 - whiteness
            specular = ( c1 * color[0] + whiteness,
                         c1 * color[1] + whiteness,
                         c1 * color[2] + whiteness, 1.0 )
    if brightness != 1.0:
        specular = ( specular[0] * brightness,
                     specular[1] * brightness,
                     specular[2] * brightness, 1.0 )
            #e Could optimize by merging this with above 3 cases (or, of course,
            #  by doing it in C, which we'll do eventually.)
    glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular)

    glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS,
                glprefs.specular_shininess)
    return
Exemplo n.º 5
0
def apply_material(color, glprefs=None):
    # todo: move into glprefs.py (see comment above for why)
    """
    In the current GL context,
    set material parameters based on the given color (length 3 or 4) and
    the material-related prefs values in glprefs (which defaults to
    drawing_globals.glprefs).
    """
    if glprefs is None:
        glprefs = drawing_globals.glprefs
        pass

    #bruce 051215: make sure color is a tuple, and has length exactly 4, for all
    # uses inside this function, assuming callers pass sequences of length 3 or
    # 4. Needed because glMaterial requires four-component vector and PyOpenGL
    # doesn't check. [If this is useful elsewhere, we can split it into a
    # separate function.]
    color = tuple(color)
    if len(color) == 3:
        color = color + (1.0, )  # usual case
    elif len(color) != 4:
        # should never happen; if it does, this assert will always fail
        assert len(color) in [3,4], \
               "color tuples must have length 3 or 4, unlike %r" % (color,)

    glColor4fv(color)  # For drawing lines with lighting disabled.

    if not glprefs.enable_specular_highlights:
        glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color)
        # glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, (0,0,0,1))
        return

    glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color)

    whiteness = glprefs.specular_whiteness
    brightness = glprefs.specular_brightness
    if whiteness == 1.0:
        specular = (1.0, 1.0, 1.0, 1.0)  # optimization
    else:
        if whiteness == 0.0:
            specular = color  # optimization
        else:
            # assume color[3] (alpha) is not passed or is always 1.0
            c1 = 1.0 - whiteness
            specular = (c1 * color[0] + whiteness, c1 * color[1] + whiteness,
                        c1 * color[2] + whiteness, 1.0)
    if brightness != 1.0:
        specular = (specular[0] * brightness, specular[1] * brightness,
                    specular[2] * brightness, 1.0)
        #e Could optimize by merging this with above 3 cases (or, of course,
        #  by doing it in C, which we'll do eventually.)
    glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular)

    glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, glprefs.specular_shininess)
    return
Exemplo n.º 6
0
 def draw(self, origin):
     edge = (0, 1)
     verticies = (
         self.source_point - origin, self.fixed_point - origin
     )
     glBegin(GL_LINES)
     for vertex in edge:
         glColor4fv((0.5, 0.5, 0.3, 1.0))
         glNormal3fv((0.0, 0.0, 0.0))
         glVertex3fv(verticies[vertex])
     glEnd()
Exemplo n.º 7
0
def color(*args):
   ''' Set the current color.

   syntax:
      color(gray)
      color(r,g,b)
      color(r,g,b,a)
      color([r,g,b])
      color([r,g,b,a])
   '''
   c = Color.helper(*args)
   glColor4fv(c.data())
Exemplo n.º 8
0
    def draw(self, origin, color=(0.45, 0.45, 0.45, 1.0), drawFaces=True):
        edges = (
            (0, 1),
            (0, 2),
            (0, 4),
            (1, 3),
            (1, 5),
            (7, 3),
            (7, 5),
            (7, 6),
            (6, 2),
            (6, 4),
            (3, 2),
            (5, 4)
        )
        surfaces = (
            (0, 2, 6, 4),
            (5, 7, 3, 1),
            (4, 6, 7, 5),
            (1, 3, 2, 0),
            (6, 2, 3, 7),
            (1, 0, 4, 5)
        )

        verticies = self.vertices_points_list(BoxVertexOrderEnum.ZYX)
        verticiesInOrigin = []

        for v in verticies:
            verticiesInOrigin.append(v - origin)

        if drawFaces:
            glBegin(GL_QUADS)
            for surface in surfaces:
                normal = get_plane_normal(surface, self.vertices_points_list, self._center)
                normal_tuple = normal.get_tuple()
                for vertex in surface:
                    glColor4fv(color)
                    glNormal3fv(normal_tuple)
                    glVertex3fv(verticiesInOrigin[vertex])
            glEnd()

        glBegin(GL_LINES)
        for edge in edges:
            for vertex in edge:
                glColor4fv((0.0, 0.0, 0.0, 1.0))
                glNormal3fv((0.0, 0.0, 0.0))
                glVertex3fv(verticiesInOrigin[vertex])
        glEnd()
Exemplo n.º 9
0
    def draw(self, origin):
        self.draw_inside(origin)
        edges = (
            (0, 1),
            (0, 2),
            (0, 4),
            (1, 3),
            (1, 5),
            (7, 3),
            (7, 5),
            (7, 6),
            (6, 2),
            (6, 4),
            (3, 2),
            (5, 4)
        )
        surfaces = (
            #    Surface((0,2,6,4),(0,0,1)), #ground
            Surface((5, 7, 3, 1), (0, 0, 1)),  # ceiling
            Surface((4, 6, 7, 5), (1, 0, 0)),  # back face
            Surface((6, 2, 3, 7), (0, 1, 0)),  # left face looking from source
            Surface((1, 0, 4, 5), (0, -1, 0))  # right face looking from source

        )

        verticies = self.vertices_points_list(BoxVertexOrderEnum.ZYX)
        verticiesInOrigin = []
        for v in verticies:
            verticiesInOrigin.append(v - origin)

        glBegin(GL_QUADS)
        for surface in surfaces:
            for vertex in surface.edges:
                glColor4fv((0.4, 0.4, 0.4, 1.0))
                glNormal3fv(surface.normal)
                glVertex3fv(verticiesInOrigin[vertex])
        glEnd()

        glBegin(GL_LINES)
        for edge in edges:
            for vertex in edge:
                glColor4fv((0.0, 0.0, 0.0, 1.0))
                glNormal3fv((0.0, 0.0, 0.0))
                glVertex3fv(verticiesInOrigin[vertex])
        glEnd()
Exemplo n.º 10
0
    def draw(self, origin):
        self.draw_parable(origin)
        edges = (
            (0, 1),
            (0, 2),
            (0, 4),
            (1, 3),
            (1, 5),
            (7, 3),
            (7, 5),
            (7, 6),
            (6, 2),
            (6, 4),
            (3, 2),
            (5, 4)
        )

        #    edges = ()
        surfaces = (
            (1, 3, 2, 0),
            (1, 0, 4, 5),
            (0, 2, 6, 4),
            (1, 3, 7, 5),
            (7, 3, 2, 6)
        )

        light = (1, 3, 2, 0)

        normal = get_plane_normal(light, self.vertices_points_list, self._center)
        normal_tuple = normal.get_tuple()
        glBegin(GL_QUADS)
        for vertex in light:
            glNormal3fv(normal_tuple)
            glColor4fv((1.0, 1.0, 1.0, 1.0))
            glVertex3fv(self.vertices_points_list[vertex] - origin)
        glEnd()

        glBegin(GL_LINES)
        for edge in edges:
            for vertex in edge:
                glColor4fv((0.0, 0.0, 0.0, 1.0))
                glNormal3fv((0.0, 0.0, 0.0))
                glVertex3fv(self.vertices_points_list[vertex] - origin)
        glEnd()
Exemplo n.º 11
0
    def draw_parable(self, origin):
        number_levels = int(self.angle_ouverture / self.angle_levels)
        #    self.points_per_level = len(self.points_parable)
        glBegin(GL_QUADS)
        for j in range(number_levels - 2):
            for i in range(self.points_per_level):
                glColor4fv((0.95, 0.95, 0, 1.0))
                glNormal3fv((0.0, 0.0, 0.0))
                glVertex3fv(self.points_parable[i % self.points_per_level + (j + 1) * self.points_per_level] - origin)
                glVertex3fv(self.points_parable[i + 1 + (j + 1) * self.points_per_level] - origin)
                glVertex3fv(self.points_parable[(i + 1) % self.points_per_level + j * self.points_per_level] - origin)
                glVertex3fv(self.points_parable[i + j * self.points_per_level] - origin)
        glEnd()
        glBegin(GL_LINES)
        for j in range(number_levels):
            for i in range(self.points_per_level):
                glColor4fv((0.5, 0.5, 0.5, 1.0))
                glNormal3fv((0.0, 0.0, 0.0))
                glVertex3fv(self.points_parable[i + j * self.points_per_level] - origin)
                glVertex3fv(self.points_parable[(i + 1) % self.points_per_level + j * self.points_per_level] - origin)
        glEnd()

        glBegin(GL_LINES)
        for i in range(len(self.points_parable) - self.points_per_level):
            for j in (i, i + self.points_per_level):
                glColor4fv((0.5, 0.5, 0.5, 1.0))
                glNormal3fv((0.0, 0.0, 0.0))
                glVertex3fv(self.points_parable[j] - origin)
        glEnd()
Exemplo n.º 12
0
    def render(self):
        if self.list < 0:
            self.list = glGenLists(1)
            glNewList(self.list, GL_COMPILE)

            for n, f in enumerate(self.faces):
                glMaterialfv(GL_FRONT, GL_DIFFUSE, self.colors[n])
                glMaterialfv(GL_FRONT, GL_SPECULAR, self.colors[n])
                glMaterialf(GL_FRONT, GL_SHININESS, 50.0)
                glColor4fv(self.colors[n])

                glBegin(GL_POLYGON)
                if self.colors[n][0] > 0:
                    glNormal3fv(self.normals[n])

                for i in f:
                    if self.colors[n][1] > 0:
                        glNormal3fv(self.vnormals[i])
                    glVertex3fv(self.vertices[i])
                glEnd()
            glEndList()
        glCallList(self.list)
Exemplo n.º 13
0
def drawPlane(color, w, h, textureReady, opacity,
              SOLID=False, pickCheckOnly=False, tex_coords=None):
    """
    Draw polygon with size of <w>*<h> and with color <color>. Optionally, it
    could be texuture mapped, translucent.

    @pickCheckOnly This is used to draw the geometry only, used for OpenGL pick
      selection purpose.
    """
    vs = [[-0.5, 0.5, 0.0], [-0.5, -0.5, 0.0],
          [0.5, -0.5, 0.0], [0.5, 0.5, 0.0]]
    
    # piotr 080529: use external texture coordinates if provided
    if tex_coords is None:
        vt = [[0.0, 1.0], [0.0, 0.0], [1.0, 0.0], [1.0, 1.0]]    
    else:
        vt = tex_coords
    
    if textureReady:
        opacity = 1.0
            
    glDisable(GL_LIGHTING)
    glColor4fv(list(color) + [opacity])

    glPushMatrix()
    glScalef(w, h, 1.0)

    if SOLID:
        glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
    else:
        glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
    glDisable(GL_CULL_FACE) 
    
    if not pickCheckOnly:
        # This makes sure a translucent object will not occlude another
        # translucent object.
        glDepthMask(GL_FALSE)
        glEnable(GL_BLEND)
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)

        if textureReady:
            glEnable(GL_TEXTURE_2D)  

    glBegin(GL_QUADS)
    for ii in range(len(vs)):
        t = vt[ii]; v = vs[ii]
        if textureReady:
            glTexCoord2fv(t)
        glVertex3fv(v)
    glEnd()

    if not pickCheckOnly:
        if textureReady:
            glDisable(GL_TEXTURE_2D)

        glDisable(GL_BLEND)
        glDepthMask(GL_TRUE) 

    glEnable(GL_CULL_FACE)
    if not SOLID:
        glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)

    glPopMatrix()
    glEnable(GL_LIGHTING)
    return
Exemplo n.º 14
0
def draw_vertex_water(vertex):
    """ Draws point with water color """
    z = vertex[2]
    color = hex_to_float(WATER_COLOR, True)
    glColor4fv(color)
    glVertex3f(vertex[0], vertex[1], z * DRAW_HEIGHT_WEIGHT)
Exemplo n.º 15
0
    def draw_inside(self, origin):
        edges = (
            (0, 1),
            (0, 2),
            (0, 4),
            (1, 3),
            (1, 5),
            (7, 3),
            (7, 5),
            (7, 6),
            (6, 2),
            (6, 4),
            (3, 2),
            (5, 4),
            # windows inside
            (8, 9),
            (9, 10),
            (10, 11),
            (11, 8),
            # window outside
            (12, 13),
            (13, 14),
            (14, 15),
            (15, 12),
            # wall between windows
            (8, 12),
            (9, 13),
            (10, 14),
            (11, 15)

        )
        surfaces_inside = (
            #####inside
            Surface((5, 7, 6, 4), (-1, 0, 0)),
            Surface((5, 4, 0, 1), (0, 1, 0)),
            Surface((7, 3, 2, 6), (0, -1, 0)),
            Surface((4, 6, 2, 0), (0, 0, 1)),
            Surface((1, 3, 7, 5), (0, 0, 1))
        )
        surfaces_outside = (
            #####outside front face
            Surface((16, 17, 12, 15), (-1, 0, 0)),
            Surface((19, 13, 12, 17), (-1, 0, 0)),
            Surface((13, 19, 18, 14), (-1, 0, 0)),
            Surface((16, 15, 14, 18), (-1, 0, 0)),
            #####
            Surface((8, 11, 15, 12), (0, -1, 0)),
            Surface((12, 13, 9, 8), (0, 0, -1)),
            Surface((13, 14, 10, 9), (0, 1, 0)),
            Surface((14, 15, 11, 10), (0, 0, 1))
        )
        verticies = self.sommets_extras
        verticiesInOrigin = []
        for v in verticies:
            verticiesInOrigin.append(v - origin)
        glBegin(GL_QUADS)
        for surface in surfaces_outside:
            for vertex in surface.edges:
                glColor4fv((0.4, 0.4, 0.4, 1.0))
                glNormal3fv(surface.normal)
                glVertex3fv(verticiesInOrigin[vertex])
        for surface in surfaces_inside:
            for vertex in surface.edges:
                glColor4fv((0.6, 0.6, 0.6, 1.0))
                glNormal3fv(surface.normal)
                glVertex3fv(verticiesInOrigin[vertex])
        glEnd()
        glBegin(GL_LINES)
        for edge in edges:
            for vertex in edge:
                glColor4fv((0.0, 0.0, 0.0, 1.0))
                glNormal3fv((0.0, 0.0, 0.0))
                glVertex3fv(verticiesInOrigin[vertex])
        glEnd()
Exemplo n.º 16
0
    def draw(self):
        """
        Draws the rulers.
        """
        
        width = self.glpane.width
        height = self.glpane.height
        
        # These 3 attrs (scale, aspect, and ruler_position) are checked to 
        # determine if they've changed. If any of them have, 
        # getRulerDrawingParameters() must be called to get new drawing parms.
        if (self.scale  != self.glpane.scale) or \
           (self.zoomFactor  != self.glpane.zoomFactor) or \
           (self.aspect != self.glpane.aspect) or \
           (self.ruler_position != env.prefs[rulerPosition_prefs_key]):
            
            self.scale = self.glpane.scale
            self.zoomFactor = self.glpane.zoomFactor
            self.aspect = self.glpane.aspect
            self.ruler_position = env.prefs[rulerPosition_prefs_key]
    
            self.ruler_drawing_params = \
                getRulerDrawingParameters(width, height, self.aspect,
                                          self.scale, self.zoomFactor, 
                                          self.ruler_position)
            
        (draw_ticks_and_text,
         units_text, 
         units_format,
         units_scale,
         unit_label_inc,
         long_tickmark_inc,
         medium_tickmark_inc,
         num_vert_ticks,
         num_horz_ticks,
         tickmark_spacing_multiplier,
         ruler_origin, ruler_start_pt, 
         units_text_origin, origin_square_pt1, origin_square_pt2, 
         vr_thickness, vr_tickmark_spacing,
         vr_long_tick_len, vr_medium_tick_len, vr_short_tick_len,
         vr_rect_pt1, vr_rect_pt2,
         vr_line_pt1, vr_line_pt2,
         vr_units_x_offset, vr_units_y_offset,
         hr_thickness, hr_tickmark_spacing,
         hr_long_tick_len, hr_medium_tick_len, hr_short_tick_len,
         hr_rect_pt1, hr_rect_pt2,
         hr_line_pt1, hr_line_pt2,
         hr_units_x_offset, hr_units_y_offset) = self.ruler_drawing_params
            
        ruler_color = env.prefs[rulerColor_prefs_key]
        ruler_opacity = env.prefs[rulerOpacity_prefs_key]
        
        # These may become user preferences in the future.
        tickmark_color = darkgray
        text_color = black
        
        # Set up 2D (window) coordinate system.
        # Bruce - please review this section.
        glMatrixMode(GL_MODELVIEW)
        glPushMatrix()
        glLoadIdentity()
        glMatrixMode(GL_PROJECTION)
        glPushMatrix()
        glLoadIdentity() # needed!
        gluOrtho2D(0.0, float(width), 0.0, float(height))
        glMatrixMode(GL_MODELVIEW)
        # About this glMatrixMode(GL_MODELVIEW) call, Bruce wrote in a review:
        # The only reason this is desirable (it's not really needed) is if, 
        # when someone is editing the large body of drawing code after this, 
        # they inadvertently do something which is only correct if the matrix 
        # mode is GL_MODELVIEW (e.g. if they use glTranslate to shift a ruler
        # or tickmark position). Most of our drawing code assumes it can do 
        # this, so a typical NE1 OpenGL programmer may naturally assume this,
        # which is why it's good to leave the matrix mode as GL_MODELVIEW when
        # entering into a large hunk of ordinary drawing code (especially if
        # it might call other drawing functions, now or in the future).
        # Mark 2008-03-03
        
        glDisable(GL_LIGHTING)
        glDisable(GL_DEPTH_TEST)
        
        # Suppress writing into the depth buffer so anything behind the ruler
        # can still be highlighted/selected.
        glDepthMask(GL_FALSE) 
        
        # Draw v/h ruler rectangles in the user defined color and opacity. #####
    
        glColor4fv(list(ruler_color) + [ruler_opacity])
        glEnable(GL_BLEND)
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
        glRectf(origin_square_pt1[0], origin_square_pt1[1], 
                origin_square_pt2[0], origin_square_pt2[1]) # Origin square
        if env.prefs[displayVertRuler_prefs_key]:
            glRectf(vr_rect_pt1[0], vr_rect_pt1[1], 
                    vr_rect_pt2[0], vr_rect_pt2[1]) # Vertical ruler
        if env.prefs[displayHorzRuler_prefs_key]:
            glRectf(hr_rect_pt1[0], hr_rect_pt1[1], 
                    hr_rect_pt2[0], hr_rect_pt2[1]) # Horizontal ruler

        glDisable(GL_BLEND)
        
        # Set color of ruler lines, tick marks and text.
        glColor3fv(tickmark_color)
        self.glpane.qglColor(RGBf_to_QColor(text_color))
        
        # Draw unit of measurement in corner (A or nm).
        # piotr 080326: replaced drawText with drawCenteredText
        self.drawCenteredText(units_text, units_text_origin)
        
        # Kludge alert. Finish drawing ruler edge(s) if we will not be
        # drawing the ruler tick marks and text (only happens when the user
        # is zoomed out to an "absured scale factor".
        if not draw_ticks_and_text:
            if env.prefs[displayVertRuler_prefs_key]:
                self.drawLine(vr_line_pt1, vr_line_pt2)
            if env.prefs[displayHorzRuler_prefs_key]:
                self.drawLine(hr_line_pt1, hr_line_pt2)
            
        # Draw vertical ruler line(s) and tick marks ##########################
        
        if env.prefs[displayVertRuler_prefs_key] and draw_ticks_and_text:
        
            # Draw vertical line along right/left edge of ruler.
            self.drawLine(vr_line_pt1, vr_line_pt2)
            
            # Initialize pt1 and pt2, the tick mark endpoints. The first tick 
            # mark will span the entire width of the ruler,  which serves as a 
            # divider b/w the unit of measure text (i.e. A or nm) and the rest
            # of the ruler.
            pt1 = ruler_start_pt
            pt2 = ruler_start_pt + V(-vr_thickness, 0.0, 0.0)
            
            # Draw vertical ruler tickmarks, including numeric unit labels
            for tick_num in range(num_horz_ticks + 1):
                
                # pt1 and pt2 are modified by each iteration of the loop.
                self.drawLine(pt1, pt2)
                
                # Draw units number beside long tickmarks.
                if not tick_num % unit_label_inc:
                    units_num_origin = pt1 \
                                     + V(vr_units_x_offset, 
                                         vr_units_y_offset, 
                                         0.0)
                    units_num = units_format % (tick_num * units_scale)
                    self.drawText(units_num, units_num_origin)
                    
                # Update tickmark endpoints for next tickmark.
                pt1 = ruler_start_pt + \
                    V(0.0, 
                      vr_tickmark_spacing * tickmark_spacing_multiplier
                      * (tick_num + 1),
                      0.0)
                
                if not (tick_num + 1) % long_tickmark_inc:
                    pt2 = pt1 + V(vr_long_tick_len, 0.0, 0.0)
                elif not (tick_num + 1) % medium_tickmark_inc:
                    pt2 = pt1 + V(vr_medium_tick_len, 0.0, 0.0)
                else:
                    pt2 = pt1 + V(vr_short_tick_len, 0.0, 0.0)
        
            # End vertical ruler
        
        # Draw horizontal ruler line(s) and tick marks #########################
        
        if env.prefs[displayHorzRuler_prefs_key] and draw_ticks_and_text:
            # Draw horizontal line along top/bottom edge of ruler.
            self.drawLine(hr_line_pt1, hr_line_pt2)
            
            # Initialize pt1 and pt2, the tick mark endpoints. The first tick
            # mark will span the entire width of the ruler,  which serves as a 
            # divider b/w the unit of measure text (i.e. A or nm) and the rest
            # of the ruler.
            pt1 = ruler_start_pt
            pt2 = ruler_start_pt + V(0.0, -hr_thickness, 0.0) 
        
            # Draw horizontal ruler (with vertical) tickmarks, including its
            # numeric unit labels
            for tick_num in range(num_vert_ticks + 1):
                
                # pt1 and pt2 are modified by each iteration of the loop.
                self.drawLine(pt1, pt2)
                
                # Draw units number beside long tickmarks.
                if not tick_num % unit_label_inc:
                    units_num_origin = pt1 \
                                     + V(hr_units_x_offset, 
                                         hr_units_y_offset, 
                                         0.0)
                    units_num = units_format % (tick_num * units_scale)
                    self.drawText(units_num, units_num_origin)
                    
                # Update tickmark endpoints for next tickmark.
                pt1 = \
                    ruler_start_pt + \
                    V(hr_tickmark_spacing * tickmark_spacing_multiplier
                      * (tick_num + 1),
                      0.0,
                      0.0)
                
                if not (tick_num + 1) % long_tickmark_inc:
                    pt2 = pt1 + V(0.0, hr_long_tick_len, 0.0)
                elif not (tick_num + 1) % medium_tickmark_inc:
                    pt2 = pt1 + V(0.0, hr_medium_tick_len, 0.0)
                else:
                    pt2 = pt1 + V(0.0, hr_short_tick_len, 0.0)
            
            # End horizontal ruler
        
        # Restore OpenGL state.
        glDepthMask(GL_TRUE) 
        glEnable(GL_DEPTH_TEST)
        glEnable(GL_LIGHTING)
        glDepthMask(GL_TRUE)
        glMatrixMode(GL_PROJECTION)
        glPopMatrix()
        glMatrixMode(GL_MODELVIEW)
        glPopMatrix()
        return # from drawRulers
Exemplo n.º 17
0
    def draw(self):
        """
        Draws the rulers.
        """

        width = self.glpane.width
        height = self.glpane.height

        # These 3 attrs (scale, aspect, and ruler_position) are checked to
        # determine if they've changed. If any of them have,
        # getRulerDrawingParameters() must be called to get new drawing parms.
        if (
            (self.scale != self.glpane.scale)
            or (self.zoomFactor != self.glpane.zoomFactor)
            or (self.aspect != self.glpane.aspect)
            or (self.ruler_position != env.prefs[rulerPosition_prefs_key])
        ):

            self.scale = self.glpane.scale
            self.zoomFactor = self.glpane.zoomFactor
            self.aspect = self.glpane.aspect
            self.ruler_position = env.prefs[rulerPosition_prefs_key]

            self.ruler_drawing_params = getRulerDrawingParameters(
                width, height, self.aspect, self.scale, self.zoomFactor, self.ruler_position
            )

        (
            draw_ticks_and_text,
            units_text,
            units_format,
            units_scale,
            unit_label_inc,
            long_tickmark_inc,
            medium_tickmark_inc,
            num_vert_ticks,
            num_horz_ticks,
            tickmark_spacing_multiplier,
            ruler_origin,
            ruler_start_pt,
            units_text_origin,
            origin_square_pt1,
            origin_square_pt2,
            vr_thickness,
            vr_tickmark_spacing,
            vr_long_tick_len,
            vr_medium_tick_len,
            vr_short_tick_len,
            vr_rect_pt1,
            vr_rect_pt2,
            vr_line_pt1,
            vr_line_pt2,
            vr_units_x_offset,
            vr_units_y_offset,
            hr_thickness,
            hr_tickmark_spacing,
            hr_long_tick_len,
            hr_medium_tick_len,
            hr_short_tick_len,
            hr_rect_pt1,
            hr_rect_pt2,
            hr_line_pt1,
            hr_line_pt2,
            hr_units_x_offset,
            hr_units_y_offset,
        ) = self.ruler_drawing_params

        ruler_color = env.prefs[rulerColor_prefs_key]
        ruler_opacity = env.prefs[rulerOpacity_prefs_key]

        # These may become user preferences in the future.
        tickmark_color = darkgray
        text_color = black

        # Set up 2D (window) coordinate system.
        # Bruce - please review this section.
        glMatrixMode(GL_MODELVIEW)
        glPushMatrix()
        glLoadIdentity()
        glMatrixMode(GL_PROJECTION)
        glPushMatrix()
        glLoadIdentity()  # needed!
        gluOrtho2D(0.0, float(width), 0.0, float(height))
        glMatrixMode(GL_MODELVIEW)
        # About this glMatrixMode(GL_MODELVIEW) call, Bruce wrote in a review:
        # The only reason this is desirable (it's not really needed) is if,
        # when someone is editing the large body of drawing code after this,
        # they inadvertently do something which is only correct if the matrix
        # mode is GL_MODELVIEW (e.g. if they use glTranslate to shift a ruler
        # or tickmark position). Most of our drawing code assumes it can do
        # this, so a typical NE1 OpenGL programmer may naturally assume this,
        # which is why it's good to leave the matrix mode as GL_MODELVIEW when
        # entering into a large hunk of ordinary drawing code (especially if
        # it might call other drawing functions, now or in the future).
        # Mark 2008-03-03

        glDisable(GL_LIGHTING)
        glDisable(GL_DEPTH_TEST)
        # Note: disabling GL_DEPTH_TEST is not honored by the 3d version of
        # glpane.renderText -- ruler text can still be obscured by
        # previously drawn less-deep model objects.
        # But the 2d call of renderText in part.py does honor this.
        # The Qt doc says why, if I guess how to interpret it:
        #   http://doc.trolltech.com/4.3/qglwidget.html#renderText
        # says, for the 3d version of renderText only (passing three model
        # coords as opposed to two window coords):
        #   Note that this function only works properly if GL_DEPTH_TEST
        #   is enabled, and you have a properly initialized depth buffer.
        # Possible fixes:
        # - revise ruler_origin to be very close to the screen,
        #   and hope that this disable is merely ignored, not a messup;
        # - or, use the 2d version of the function.
        # I did the latter fix (2d renderText, below) and it seems to work.
        # [bruce 081204 comment]

        # Suppress writing into the depth buffer so anything behind the ruler
        # can still be highlighted/selected.
        glDepthMask(GL_FALSE)

        # == Draw v/h ruler rectangles in the user defined color and opacity.

        glColor4fv(list(ruler_color) + [ruler_opacity])
        glEnable(GL_BLEND)
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
        glRectf(origin_square_pt1[0], origin_square_pt1[1], origin_square_pt2[0], origin_square_pt2[1])  # Origin square
        if env.prefs[displayVertRuler_prefs_key]:
            glRectf(vr_rect_pt1[0], vr_rect_pt1[1], vr_rect_pt2[0], vr_rect_pt2[1])  # Vertical ruler
        if env.prefs[displayHorzRuler_prefs_key]:
            glRectf(hr_rect_pt1[0], hr_rect_pt1[1], hr_rect_pt2[0], hr_rect_pt2[1])  # Horizontal ruler

        glDisable(GL_BLEND)

        # Set color of ruler lines, tick marks and text.
        glColor3fv(tickmark_color)
        self.glpane.qglColor(RGBf_to_QColor(text_color))

        # Draw unit of measurement in corner (A or nm).
        # piotr 080326: replaced drawText with drawCenteredText
        self.drawCenteredText(units_text, units_text_origin)

        # Kludge alert. Finish drawing ruler edge(s) if we will not be
        # drawing the ruler tick marks and text (only happens when the user
        # is zoomed out to an "absurd scale factor").
        if not draw_ticks_and_text:
            if env.prefs[displayVertRuler_prefs_key]:
                self.drawLine(vr_line_pt1, vr_line_pt2)
            if env.prefs[displayHorzRuler_prefs_key]:
                self.drawLine(hr_line_pt1, hr_line_pt2)

        # == Draw vertical ruler line(s) and tick marks

        if env.prefs[displayVertRuler_prefs_key] and draw_ticks_and_text:

            # Draw vertical line along right/left edge of ruler.
            self.drawLine(vr_line_pt1, vr_line_pt2)

            # Initialize pt1 and pt2, the tick mark endpoints. The first tick
            # mark will span the entire width of the ruler,  which serves as a
            # divider b/w the unit of measure text (i.e. A or nm) and the rest
            # of the ruler.
            pt1 = ruler_start_pt
            pt2 = ruler_start_pt + V(-vr_thickness, 0.0, 0.0)

            # Draw vertical ruler tickmarks, including numeric unit labels
            for tick_num in range(num_horz_ticks + 1):

                # pt1 and pt2 are modified by each iteration of the loop.
                self.drawLine(pt1, pt2)

                # Draw units number beside long tickmarks.
                if not tick_num % unit_label_inc:
                    units_num_origin = pt1 + V(vr_units_x_offset, vr_units_y_offset, 0.0)
                    units_num = units_format % (tick_num * units_scale)
                    self.drawText(units_num, units_num_origin)

                # Update tickmark endpoints for next tickmark.
                pt1 = ruler_start_pt + V(0.0, vr_tickmark_spacing * tickmark_spacing_multiplier * (tick_num + 1), 0.0)

                if not (tick_num + 1) % long_tickmark_inc:
                    pt2 = pt1 + V(vr_long_tick_len, 0.0, 0.0)
                elif not (tick_num + 1) % medium_tickmark_inc:
                    pt2 = pt1 + V(vr_medium_tick_len, 0.0, 0.0)
                else:
                    pt2 = pt1 + V(vr_short_tick_len, 0.0, 0.0)

            # End vertical ruler

        # == Draw horizontal ruler line(s) and tick marks

        if env.prefs[displayHorzRuler_prefs_key] and draw_ticks_and_text:
            # Draw horizontal line along top/bottom edge of ruler.
            self.drawLine(hr_line_pt1, hr_line_pt2)

            # Initialize pt1 and pt2, the tick mark endpoints. The first tick
            # mark will span the entire width of the ruler,  which serves as a
            # divider b/w the unit of measure text (i.e. A or nm) and the rest
            # of the ruler.
            pt1 = ruler_start_pt
            pt2 = ruler_start_pt + V(0.0, -hr_thickness, 0.0)

            # Draw horizontal ruler (with vertical) tickmarks, including its
            # numeric unit labels
            for tick_num in range(num_vert_ticks + 1):

                # pt1 and pt2 are modified by each iteration of the loop.
                self.drawLine(pt1, pt2)

                # Draw units number beside long tickmarks.
                if not tick_num % unit_label_inc:
                    units_num_origin = pt1 + V(hr_units_x_offset, hr_units_y_offset, 0.0)
                    units_num = units_format % (tick_num * units_scale)
                    self.drawText(units_num, units_num_origin)

                # Update tickmark endpoints for next tickmark.
                pt1 = ruler_start_pt + V(hr_tickmark_spacing * tickmark_spacing_multiplier * (tick_num + 1), 0.0, 0.0)

                if not (tick_num + 1) % long_tickmark_inc:
                    pt2 = pt1 + V(0.0, hr_long_tick_len, 0.0)
                elif not (tick_num + 1) % medium_tickmark_inc:
                    pt2 = pt1 + V(0.0, hr_medium_tick_len, 0.0)
                else:
                    pt2 = pt1 + V(0.0, hr_short_tick_len, 0.0)

            # End horizontal ruler

        # Restore OpenGL state.
        glDepthMask(GL_TRUE)
        glEnable(GL_DEPTH_TEST)
        glEnable(GL_LIGHTING)
        glDepthMask(GL_TRUE)
        glMatrixMode(GL_PROJECTION)
        glPopMatrix()
        glMatrixMode(GL_MODELVIEW)
        glPopMatrix()
        return  # from drawRulers
Exemplo n.º 18
0
def drawPlane(color,
              w,
              h,
              textureReady,
              opacity,
              SOLID=False,
              pickCheckOnly=False,
              tex_coords=None):
    """
    Draw polygon with size of <w>*<h> and with color <color>. Optionally, it
    could be texuture mapped, translucent.

    @pickCheckOnly This is used to draw the geometry only, used for OpenGL pick
      selection purpose.

    @param tex_coords: texture coordinates to be explicitly provided (for
    simple image transformation purposes)
    """
    vs = [[-0.5, 0.5, 0.0], [-0.5, -0.5, 0.0], [0.5, -0.5, 0.0],
          [0.5, 0.5, 0.0]]

    # piotr 080529: use external texture coordinates if provided
    if tex_coords is None:
        vt = [[0.0, 1.0], [0.0, 0.0], [1.0, 0.0], [1.0, 1.0]]
    else:
        vt = tex_coords

    if textureReady:
        opacity = 1.0

    glDisable(GL_LIGHTING)
    glColor4fv(list(color) + [opacity])

    glPushMatrix()
    glScalef(w, h, 1.0)

    if SOLID:
        glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
    else:
        glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
    glDisable(GL_CULL_FACE)

    if not pickCheckOnly:
        # This makes sure a translucent object will not occlude another
        # translucent object.
        glDepthMask(GL_FALSE)
        glEnable(GL_BLEND)
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)

        if textureReady:
            glEnable(GL_TEXTURE_2D)

    glBegin(GL_QUADS)
    for ii in range(len(vs)):
        t = vt[ii]
        v = vs[ii]
        if textureReady:
            glTexCoord2fv(t)
        glVertex3fv(v)
    glEnd()

    if not pickCheckOnly:
        if textureReady:
            glDisable(GL_TEXTURE_2D)

        glDisable(GL_BLEND)
        glDepthMask(GL_TRUE)

    glEnable(GL_CULL_FACE)
    if not SOLID:
        glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)

    glPopMatrix()
    glEnable(GL_LIGHTING)
    return