def absorbit(self, ref, axis, amt): """ Orbit the camera around an axis passing through a reference point ref. """ # Position the reference point in the proper plane with the camera # position. ref = ref[:axis] + (self.pos[axis],) + ref[axis + 1:] # Counter-rotate the camera. self.absrotate(axis=axis, amt=-1 * amt) delta = diff3D(ref, self.pos) r = dist3D(c=delta) # Cacheable: All these handled by setattr hook if axis == X_AXIS: theta = atan2(delta[Y_AXIS], delta[Z_AXIS]) theta = theta + (self.rotateSpeed * amt * pi / 180) self.pos = add3D(ref, (0, r * sin(theta), r * cos(theta))) elif axis == Y_AXIS: theta = atan2(delta[Z_AXIS], delta[X_AXIS]) theta = theta + (self.rotateSpeed * amt * pi / 180) self.pos = add3D(ref, (r * cos(theta), 0, r * sin(theta))) elif axis == Z_AXIS: theta = atan2(delta[X_AXIS], delta[Y_AXIS]) theta = theta + (-1 * self.rotateSpeed * amt * pi / 180) self.pos = add3D(ref, (r * sin(theta), r * cos(theta), 0))
def drawEdgeSelection(self, edge, alpha=0.4): """Draw a hilight around the edge.""" if isinstance(edge, SuperEdge): for subedge in edge.edgeOrder(): self.drawEdgeSelection(subedge) for vx in edge.bends: self.drawVertexSelection(vx) return ctx = self.graph.parent # Multiply points if snapping to grid # for Netcli (has no parent) showG = False if not ctx == None: if ctx.showGrid: showG = True if showG: srcpos = utilities.mult3D(edge.source.pos, ctx.spacing) tgtpos = utilities.mult3D(edge.target.pos, ctx.spacing) else: srcpos = edge.source.pos tgtpos = edge.target.pos diff = utilities.diff3D(srcpos, tgtpos) dist = utilities.dist3D(c=diff) # Avoid dividing by zero - # don't draw anything for zero-length edges if dist <= 0: return glEnable(GL_BLEND) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) glPushMatrix() pigment = edge.color + (alpha,) glColor4f(*pigment) # Figure out radius boost factor x = max(log(40 * edge.radius, 10), 0) phi = acos(diff[Z_AXIS] / dist) * 180 / pi theta = atan2(diff[Y_AXIS], diff[X_AXIS]) * 180 / pi glTranslatef(*srcpos) glRotatef(theta, 0.0, 0.0, 1.0) glRotatef(phi, 0.0, 1.0, 0.0) gluQuadricOrientation(self.quad, GLU_OUTSIDE) gluQuadricTexture(self.quad, GL_TRUE) gluQuadricDrawStyle(self.quad, GLU_FILL) gluQuadricNormals(self.quad, GLU_SMOOTH) # glRotated(90, 1, 0, 0) gluCylinder(self.quad, edge.radius + x, edge.radius + x, dist, config.current['global:draw-edge-sides'], config.current['global:draw-edge-sides']) glPopMatrix() glDisable(GL_BLEND)
def drawLocalGrid(self, pos, size=1, spacing=1.0, snap=None): """ Draw a grid extending _size_ units of _spacing_ away from _pos_. If _snap_ is specified, the grid will be drawn relative to _pos_ - (_pos_ + 0.5 % _snap_) """ glPushMatrix() if snap != None: # print "original pos:",pos diff = [(x + snap / 2) % snap - (snap / 2) for x in pos] # print "diff:",diff pos = [x - a for x, a in zip(pos, diff)] # print "pos:",pos glTranslatef(*pos) glTranslatef(-1.0 * size * spacing, -1.0 * size * spacing, -1.0 * size * spacing) from utilities import dist3D maxDist = dist3D(c=(size, size, size)) for x in range(-1 * size, size + 1): glPushMatrix() for y in range(-1 * size, size + 1): glPushMatrix() for z in range(-1 * size, size + 1): maxAlpha = 1.0 - 0.5 * (dist3D(c=(x, y, z)) / maxDist) glPushMatrix() glScalef(0.4, 0.4, 0.4) # x axis line glBegin(GL_LINE_STRIP) glColor4f(0.75, 0.5, 0.5, 0.0) glVertex3f(-1.0, 0.0, 0.0) glColor4f(0.75, 0.5, 0.5, maxAlpha) glVertex3f(0.0, 0.0, 0.0) glColor4f(0.75, 0.5, 0.5, 0.0) glVertex3f(1.0, 0.0, 0.0) glEnd() # y axis line glBegin(GL_LINE_STRIP) glColor4f(0.5, 0.75, 0.5, 0.0) glVertex3f(0.0, -1.0, 0.0) glColor4f(0.5, 0.75, 0.5, maxAlpha) glVertex3f(0.0, 0.0, 0.0) glColor4f(0.5, 0.75, 0.5, 0.0) glVertex3f(0.0, 1.0, 0.0) glEnd() # z axis line glBegin(GL_LINE_STRIP) glColor4f(0.5, 0.5, 0.5, 0.0) glVertex3f(0.0, 0.0, -1.0) glColor4f(0.5, 0.5, 0.75, maxAlpha) glVertex3f(0.0, 0.0, 0.0) glColor4f(0.5, 0.5, 0.75, 0.0) glVertex3f(0.0, 0.0, 1.0) glEnd() glPopMatrix() glTranslatef(0.0, 0.0, spacing) glPopMatrix() glTranslatef(0.0, spacing, 0.0) glPopMatrix() glTranslatef(spacing, 0.0, 0.0) # for y in range(-1 * size, size + 1): # for z in range(-1 * size, size + 1): # # X-axis lines # glBegin(GL_LINE_STRIP) # glColor3f (1.0, 0.33, 0.33) # glVertex3f (-1 * size * spacing, y * spacing, z * spacing) # glVertex3f (size * spacing, y * spacing, z * spacing) # glEnd() # # for x in range(-1 * size, size + 1): # for z in range(-1 * size, size + 1): # # Z-axis lines # glBegin(GL_LINE_STRIP) # glColor3f (0.33, 1.0, 0.33) # glVertex3f (x * spacing, -1 * size * spacing, z * spacing) # glVertex3f (x * spacing, size * spacing, z * spacing) # glEnd() # # for x in range(-1 * size, size + 1): # for y in range(-1 * size, size + 1): # # Z-axis lines # glBegin(GL_LINE_STRIP) # glColor3f (0.33, 0.33, 1.0) # glVertex3f (x * spacing, y * spacing, -1 * size * spacing) # glVertex3f (x * spacing, y * spacing, size * spacing) # glEnd() glPopMatrix()
def drawHUD(self, camera, vertices, pos=None, status=''): from OpenGL.GLUT import glutStrokeCharacter, GLUT_STROKE_MONO_ROMAN view = glGetInteger(GL_VIEWPORT) if (view[1] != 0) or (view[3] != 0): # print "view = [", view[0], ",", view[1], ",", view[2], ",", view[3] # Switch to an orthogonal projection for drawing the HUD glDisable(GL_LIGHTING) glDisable(GL_DEPTH_TEST) glMatrixMode(GL_PROJECTION) glPushMatrix() glLoadIdentity() vheight = view[3] - view[1] vwidth = view[2] right = 100.0 top = float(vheight) / float(vwidth) * right glOrtho(0.0, right, 0.0, top, 0.0, 1.0) glMatrixMode(GL_MODELVIEW) glPushMatrix() glLoadIdentity() gluLookAt(0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0) #glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) glEnable(GL_BLEND) glEnable(GL_LINE_SMOOTH) glEnable(GL_POLYGON_SMOOTH) # # Begin draw # from math import sin, cos, pi, atan2, acos, sqrt from utilities import diff3D, dist3D def circle(): for x in range(25): theta = x * 2 * pi / 24 glVertex3f(cos(theta), sin(theta), 0.0) def halfcircle(start, end): for x in range(start, end): theta = x * 2 * pi / 24 glVertex3f(cos(theta), sin(theta), 0.0) # Move into position to draw glLineWidth(1.0) glColor4f(1.0, 1.0, 1.0, 0.35) glPushMatrix() glTranslatef(89.0, 8.0, 0.001) glPushMatrix() glPushAttrib(GL_CURRENT_BIT) # Draw a circle to represent the XZ plane (bearing) glScalef(4.0, 4.0, 1.0) glBegin(GL_TRIANGLE_FAN) glVertex3f(0.0, 0.0, 0.0) circle() glEnd() # Draw the orientation of the camera glPushMatrix() glScalef(1.01, 1.01, 1.0) glColor4f(0.9, 0.9, 0.4, 0.9) glRotatef(180.0 * atan2(-1.0 * camera.vpn[Z_AXIS], camera.vpn[X_AXIS]) / pi, 0.0, 0.0, 1.0) glBegin(GL_TRIANGLE_FAN) glVertex3f(0.0, 0.0, 0.0) halfcircle(-2, 3) glEnd() glPopMatrix() # Draw the origin marker glPushMatrix() dOrigin = diff3D((0.0, 0.0, 0.0), camera.pos) glRotatef(270 - 180.0 * atan2(camera.pos[Z_AXIS], camera.pos[X_AXIS]) / pi - 90.0, 0.0, 0.0, 1.0) glColor4f(0.9, 0.2, 0.2, 0.95) glBegin(GL_TRIANGLES) glVertex3f(0.9, 0.0, 0.0) glVertex3f(1.1, -0.1, 0.0) glVertex3f(1.1, 0.1, 0.0) glEnd() glPopMatrix() # If there are vertices selected, draw vertex markers for v in vertices: glPushMatrix() dVertex = diff3D(v.pos, camera.pos) glRotatef(270 - 180.0 * atan2(dVertex[Z_AXIS], dVertex[X_AXIS]) / pi - 90.0, 0.0, 0.0, 1.0) glColor4f(0.2, 0.7, 0.9, 0.95) glBegin(GL_TRIANGLES) glVertex3f(0.9, 0.0, 0.0) glVertex3f(1.1, -0.1, 0.0) glVertex3f(1.1, 0.1, 0.0) glEnd() glPopMatrix() # Draw the X and Z axes on it glBegin(GL_LINE_STRIP) glColor3f(1.0, 0.5, 0.5) glVertex3f(-1.0, 0.0, 0.0) glVertex3f(-0.01, 0.0, 0.0) glColor3f(1.0, 0.0, 0.0) glVertex3f(0.0, 0.0, 0.0) glColor3f(0.5, 0.0, 0.0) glVertex3f(0.01, 0.0, 0.0) glVertex3f(1.0, 0.0, 0.0) glEnd() glBegin(GL_LINE_STRIP) glColor3f(0.5, 0.5, 1.0) glVertex3f(0.0, 1.0, 0.0) glVertex3f(0.0, 0.01, 0.0) glColor3f(0.0, 0.0, 1.0) glVertex3f(0.0, 0.0, 0.0) glColor3f(0.0, 0.0, 0.5) glVertex3f(0.0, -0.01, 0.0) glVertex3f(0.0, -1.0, 0.0) glEnd() glPopAttrib() # Done drawing XZ plane # Draw a half-circle to represent elevation glPushAttrib(GL_CURRENT_BIT) glTranslatef(2.2, 0.0, 0.0) glBegin(GL_TRIANGLE_FAN) halfcircle(6, 19) glEnd() # Draw the orientation of the camera glPushMatrix() glScalef(1.01, 1.01, 1.0) glColor4f(0.9, 0.9, 0.4, 0.9) glRotatef(90.0 + 180.0 * acos(utilities.dot_product(camera.vpn, (0.0, 1.0, 0.0))) / pi, 0.0, 0.0, 1.0) glBegin(GL_TRIANGLE_FAN) glVertex3f(0.0, 0.0, 0.0) halfcircle(-2, 3) glEnd() glPopMatrix() # Draw the zero-elevation line glBegin(GL_LINES) glColor4f(0.0, 0.0, 0.0, 1.0) glVertex3f(-1.0, 0.0, 0.0) glVertex3f(0.0, 0.0, 0.0) glEnd() # Draw the origin marker glPushMatrix() dOrigin = diff3D(camera.pos, (0.0, 0.0, 0.0)) dist = dist3D(c=dOrigin) glRotatef(90.0 + 180.0 * acos(utilities.dot_product(dOrigin, (0.0, 1.0, 0.0)) / dist) / pi, 0.0, 0.0, 1.0) glColor4f(0.9, 0.2, 0.2, 0.95) glBegin(GL_TRIANGLES) glVertex3f(0.9, 0.0, 0.0) glVertex3f(1.1, -0.1, 0.0) glVertex3f(1.1, 0.1, 0.0) glEnd() glPopMatrix() # If there are vertices selected, draw vertex markers for v in vertices: glPushMatrix() dVertex = diff3D(v.pos, camera.pos) dist = dist3D(c=dVertex) glRotatef(270.0 - 180.0 * acos(utilities.dot_product(dVertex, (0.0, 1.0, 0.0)) / dist) / pi, 0.0, 0.0, 1.0) glColor4f(0.2, 0.7, 0.9, 0.95) glBegin(GL_TRIANGLES) glVertex3f(0.9, 0.0, 0.0) glVertex3f(1.1, -0.1, 0.0) glVertex3f(1.1, 0.1, 0.0) glEnd() glPopMatrix() # Clean up glPopMatrix() glPopAttrib() # Coordinates - if _pos_ is specified, draw that, # otherwise use the current position of the camera. if pos == None: pos = camera.pos glPushAttrib(GL_CURRENT_BIT) glPushMatrix() glTranslatef(-8.0, -7.0, 0.0) glScalef(0.009, 0.016, 1.0) # Draw the background box glBegin(GL_QUADS) glVertex3f(0.0, -40.0, 0.0) glVertex3f(2300.0, -40.0, 0.0) glVertex3f(2300.0, 140.0, 0.0) glVertex3f(0.0, 140.0, 0.0) glEnd() # Draw the text xS = ('%.1f' % pos[0]).rjust(6) + ' ' yS = ('%.1f' % pos[1]).rjust(6) + ' ' zS = ('%.1f' % pos[2]).rjust(6) + ' ' glLineWidth(1.7) glColor4f(0.56, 0.0, 0.0, 0.9) for c in xS: glutStrokeCharacter(GLUT_STROKE_MONO_ROMAN, ord(c)) glColor4f(0.0, 0.56, 0.0, 0.9) for c in yS: glutStrokeCharacter(GLUT_STROKE_MONO_ROMAN, ord(c)) glColor4f(0.0, 0.0, 0.56, 0.9) for c in zS: glutStrokeCharacter(GLUT_STROKE_MONO_ROMAN, ord(c)) glPopAttrib() glPopMatrix() glPopMatrix() # # end draw # # # now draw HUD status line # if status: glColor4f(1.0, 1.0, 1.0, 0.25) glPushMatrix() glTranslatef(0.0, 1.0, 0.001) glPushMatrix() glScalef(0.009, 0.016, 1.0) glBegin(GL_QUADS) glVertex3f(0.0, -40.0, 0.0) glVertex3f(5000.0, -40.0, 0.0) glVertex3f(5000.0, 140.0, 0.0) glVertex3f(0.0, 140.0, 0.0) glEnd() glLineWidth(1.4) glColor4f(0.56, 0.0, 0.0, 0.9) # Add some space before the first character glTranslatef(200.0, 0.0, 0.0) for c in status: glutStrokeCharacter(GLUT_STROKE_ROMAN, ord(c)) glPopMatrix() glPopMatrix() # # end HUD statusline draw # glPopMatrix() glMatrixMode(GL_PROJECTION) glPopMatrix() glMatrixMode(GL_MODELVIEW) glEnable(GL_LIGHTING) glEnable(GL_DEPTH_TEST)
def drawEdge(self, edge, alpha=1.0): """ draw an edge in 3D. """ # Superedge drawing is a no-op - all the parts will already have been # drawn. if isinstance(edge, SuperEdge): return if config.current['global:enable-anaglyph']: pigment = (1.0, 1.0, 1.0, alpha) else: pigment = edge.color + (alpha,) glColor4f(*pigment) # For Netcli (has no parent) ctx = self.graph.parent showG = False if not ctx == None: if ctx.showGrid: showG = True if showG: srcpos = utilities.mult3D(edge.source.pos, ctx.spacing) tgtpos = utilities.mult3D(edge.target.pos, ctx.spacing) else: srcpos = edge.source.pos tgtpos = edge.target.pos if config.current['global:draw-edge-cylinders']: diff = utilities.diff3D(srcpos, tgtpos) dist = utilities.dist3D(c=diff) # Avoid dividing by zero - # don't draw anything for zero-length edges if dist <= 0: return glPushMatrix() phi = acos(diff[Z_AXIS] / dist) * 180 / pi theta = atan2(diff[Y_AXIS], diff[X_AXIS]) * 180 / pi glTranslatef(*srcpos) glRotatef(theta, 0.0, 0.0, 1.0) glRotatef(phi, 0.0, 1.0, 0.0) gluQuadricOrientation(self.quad, GLU_OUTSIDE) gluQuadricTexture(self.quad, GL_TRUE) gluQuadricDrawStyle(self.quad, GLU_FILL) gluQuadricNormals(self.quad, GLU_SMOOTH) #glRotated(90, 1, 0, 0) gluCylinder(self.quad, edge.radius, edge.radius, dist, config.current['global:draw-edge-sides'], config.current['global:draw-edge-sides']) glPopMatrix() else: # not drawing edges as cylinders glDisable(GL_LIGHTING) glEnable(GL_LINE_SMOOTH) glLineWidth(config.current['global:draw-edge-linewidth']) glBegin(GL_LINES) glVertex3f(srcpos[0], srcpos[1], srcpos[2]) glVertex3f(tgtpos[0], tgtpos[1], tgtpos[2]) glEnd() glEnable(GL_LIGHTING)