def OnDraw(self): if not self.init: self.InitGL() self.init = True #self.InitGL() # clear color and depth buffers gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) # use a fresh transformation matrix gl.glPushMatrix() # # position object # #gl.glTranslate(0.0, 0.0, -2.0) # gl.glRotate(30.0, 1.0, 0.0, 0.0) # gl.glRotate(30.0, 0.0, 1.0, 0.0) # gl.glTranslate(0, -1, 0) # gl.glRotate(250, 1, 0, 0) glut.glutSolidCone(0.5, 1, 30, 5) gl.glPopMatrix() for s in _spheres: # use a fresh transformation matrix gl.glPushMatrix() # position object gl.glTranslate(s.x, s.y, s.z) glut.glutSolidSphere(s.radius, 60, 60) gl.glPopMatrix() # rotate the camera gl.glRotate((self.y - self.lasty), 0.0, 0.0, 1.0); gl.glRotate((self.x - self.lastx), 1.0, 0.0, 0.0); # push into visible buffer self.SwapBuffers()
def DrawGL(self): glColor3f(1.0,0.3,0.3) GLUT.glutSolidCone(1.0,2,20,16) glRotatef(180.0,0.0,1.0,0.0) glColor3f(0.3,1.0,0.3) GLUT.glutSolidCone(1.0,1,20,16) glLoadIdentity()
def DrawGL(self): glColor3f(1.0, 0.3, 0.3) GLUT.glutSolidCone(1.0, 2, 20, 16) glRotatef(180.0, 0.0, 1.0, 0.0) glColor3f(0.3, 1.0, 0.3) GLUT.glutSolidCone(1.0, 1, 20, 16) glLoadIdentity()
def draw_scene(self): gl.glEnable( gl.GL_BLEND ) # gl.glBlendFunc( gl.GL_SRC_ALPHA, gl.GL_SRC_ALPHA) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) gl.glPushMatrix() gl.glTranslatef(*self.point_manager.avg) gl.glPointSize(self.point_manager._get_scale()) if self.points_show: self.verts.draw( gl.GL_POINTS, 'pnc' ) for c in self.camera_manager.get_cams(): gl.glPushMatrix() gl.glMultMatrixf(c.R_gl) gl.glTranslatef(*c.t) if c.cone_show: gl.glPushMatrix() # cone is constructed on z axis toward image center # rotate so that it goes through pupil position point gl.glMultMatrixf(c.R_cone_gl) gl.glPointSize(4.0) c.inside_cone_vbo.draw(gl.GL_POINTS, 'pnc') gl.glTranslatef(0,0,-c.cone_len) gl.glColor4f(1.0,1.0,1.0,0.25) glut.glutSolidCone(c.cone_r, c.cone_len , 32 , 1 ) gl.glPopMatrix() if c.pyramid_show: gl.glEnable(gl.GL_TEXTURE_2D) gl.glBlendFunc( gl.GL_SRC_ALPHA, gl.GL_SRC_ALPHA) c.img.draw(-c.img_W, -c.img_H, -c.img_Z, c.img_W*2, c.img_H*2) gl.glDisable(gl.GL_TEXTURE_2D) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) gl.glPopMatrix() if c.pyramid_show: gl.glPolygonMode( gl.GL_FRONT_AND_BACK, gl.GL_LINE ) #wireframe display mode c.pyramid.draw(gl.GL_QUADS, 'pnc') if c.eye_point is not None: # gl.glEnable(gl.GL_POINT_SMOOTH) gl.glPointSize(10.0) c.eye_point.draw(gl.GL_POINTS, 'pnc') gl.glPolygonMode( gl.GL_FRONT_AND_BACK, gl.GL_FILL ) gl.glDepthMask (gl.GL_TRUE) gl.glPopMatrix()
def draw_direction_cone(p1, p2, position=0.5, precision=12, size=0.1): # convert p1 and p2 from list/tuple to Point if not hasattr(p1, "sub"): p1 = Point(*p1) if not hasattr(p2, "sub"): p2 = Point(*p2) distance = p2.sub(p1) length = distance.norm direction = distance.normalized() if direction is None: # zero-length line return cone_length = length * size cone_radius = cone_length / 3.0 # move the cone to the middle of the line GL.glTranslatef((p1.x + p2.x) * position, (p1.y + p2.y) * position, (p1.z + p2.z) * position) # rotate the cone according to the line direction # The cross product is a good rotation axis. cross = direction.cross(Point(0, 0, -1)) if cross.norm != 0: # The line direction is not in line with the z axis. try: angle = math.asin(sqrt(direction.x ** 2 + direction.y ** 2)) except ValueError: # invalid angle - just ignore this cone return # convert from radians to degree angle = angle / math.pi * 180 if direction.z < 0: angle = 180 - angle GL.glRotatef(angle, cross.x, cross.y, cross.z) elif direction.z == -1: # The line goes down the z axis - turn it around. GL.glRotatef(180, 1, 0, 0) else: # The line goes up the z axis - nothing to be done. pass # center the cone GL.glTranslatef(0, 0, -cone_length * position) # draw the cone GLUT.glutSolidCone(cone_radius, cone_length, precision, 1)
def draw_direction_cone(p1, p2, position=0.5, precision=12, size=0.1): # convert p1 and p2 from list/tuple to Point if not hasattr(p1, "sub"): p1 = Point(*p1) if not hasattr(p2, "sub"): p2 = Point(*p2) distance = p2.sub(p1) length = distance.norm direction = distance.normalized() if direction is None: # zero-length line return cone_length = length * size cone_radius = cone_length / 3.0 # move the cone to the middle of the line GL.glTranslatef((p1.x + p2.x) * position, (p1.y + p2.y) * position, (p1.z + p2.z) * position) # rotate the cone according to the line direction # The cross product is a good rotation axis. cross = direction.cross(Point(0, 0, -1)) if cross.norm != 0: # The line direction is not in line with the z axis. try: angle = math.asin(sqrt(direction.x**2 + direction.y**2)) except ValueError: # invalid angle - just ignore this cone return # convert from radians to degree angle = angle / math.pi * 180 if direction.z < 0: angle = 180 - angle GL.glRotatef(angle, cross.x, cross.y, cross.z) elif direction.z == -1: # The line goes down the z axis - turn it around. GL.glRotatef(180, 1, 0, 0) else: # The line goes up the z axis - nothing to be done. pass # center the cone GL.glTranslatef(0, 0, -cone_length * position) # draw the cone GLUT.glutSolidCone(cone_radius, cone_length, precision, 1)
def draw(self, coordslinear, index): from OpenGL import GL,GLUT xyzlinear = self.mysys.getxyz(coordslinear) xyz = xyzlinear.reshape(xyzlinear.size/3, 3) com=np.mean(xyz, axis=0) if index == 1: color1 = [0.65, 0.0, 0.0, 1.] color2 = [0.35, 0.0, 0.0, 1.] else: color1 = [0.00, 0.65, 0., 1.] color2 = [0.00, 0.35, 0., 1.] GL.glMaterialfv(GL.GL_FRONT_AND_BACK, GL.GL_DIFFUSE, color2) for i, xx in enumerate(xyz): if i % 3 == 0: GL.glMaterialfv(GL.GL_FRONT_AND_BACK, GL.GL_DIFFUSE, color1) else: GL.glMaterialfv(GL.GL_FRONT_AND_BACK, GL.GL_DIFFUSE, color2) x=xx-com GL.glPushMatrix() GL.glTranslate(x[0],x[1],x[2]) GLUT.glutSolidSphere(0.4,30,30) GL.glPopMatrix() #now try to draw cylinders connecting atoms in a molecule nsites = len(xyz[:,0]) z = np.array([0.,0.,1.]) #default cylinder orientation for j1 in range(0,nsites,3): X1 = xyz[j1,:] - com for j2 in range(j1+1,j1+3,1): #draw a cylinder from xyz[j1] to xyz[j2] X2 = xyz[j2,:] - com p = X2-X1 #desired cylinder orientation r = np.linalg.norm(p) t = np.cross(z,p) #angle about which to rotate a = np.arccos( np.dot( z,p) / r ) #rotation angle a *= (180. / np.pi) #change units to angles GL.glPushMatrix() GL.glTranslate( X1[0], X1[1], X1[2] ) GL.glRotate( a, t[0], t[1], t[2] ) GLUT.glutSolidCone(.2,r,30,30) #I can't seem to draw a cylinder GL.glPopMatrix()
def render_arrow(self, p, q, r_base=0.01, head_width=0.015, head_len=0.01): # glDisable(GL_LIGHTING) m_quadric = self.quadric GLU.gluQuadricNormals(m_quadric, GLU.GLU_SMOOTH) p = np.array(p) q = np.array(q) u = (q - p) u /= norm(u) P = q - head_len * u z = np.array([0, 0.0, 1.0]) z /= norm(z) if norm(z - u) < 1e-8: axis = np.array([0, 0, 1]) angle = 0.0 else: axis = np.cross(z, u) axis /= norm(axis) angle = math.acos(u.dot(z)) M = R_axis_angle(axis, angle) m = [ M[0, 0], M[1, 0], M[2, 0], 0.0, M[0, 1], M[1, 1], M[2, 1], 0.0, M[0, 2], M[1, 2], M[2, 2], 0.0, P[0], P[1], P[2], 1.0 ] m2 = [ M[0, 0], M[1, 0], M[2, 0], 0.0, M[0, 1], M[1, 1], M[2, 1], 0.0, M[0, 2], M[1, 2], M[2, 2], 0.0, p[0], p[1], p[2], 1.0 ] GL.glPushMatrix() GL.glMultMatrixd(m2) arrow_len = norm(q - p) - head_len # glColor(0.9, 0.2, 0.2) GLU.gluCylinder(m_quadric, r_base, r_base, arrow_len, 10, 10) GL.glPopMatrix() if head_width > 1e-6 and head_len > 1e-6: # glColor(1.0, 0.0, 0.0) GL.glPushMatrix() GL.glMultMatrixd(m) GLUT.glutSolidCone(head_width, head_len, 10, 3) GL.glPopMatrix()
def create_cone(base=1.0, height=1.0, lat=20, lng=20, color=None): """ Creates an cone with base and height, radius 1. :param base: Cone base :param height: Cone height :param lat: Cone latitude :param lng: Cone longitude :param color: Cone color :type base: float, int :type height: float, int :type lat: int :type lng: int :type color: list :return: OpenGL list """ if lat >= 3 and lng >= 10: # noinspection PyArgumentEqualDefault circlebase = create_circle(base - 0.05, 0.1, [0.0, 0.0, -1.0], color) obj = _gl.glGenLists(1) _gl.glNewList(obj, _gl.GL_COMPILE) _gl.glPushMatrix() if color is not None: _gl.glColor4fv(color) # noinspection PyBroadException try: _glut.glutSolidCone(base, height, lat, lng) except: if not _FIGURES_ERRS[3]: _print_gl_error( 'OpenGL actual version does not support glutSolidCone function' ) _FIGURES_ERRS[3] = True _gl.glCallList(circlebase) _gl.glPopMatrix() _gl.glEndList() return obj else: raise Exception( 'Latitude and longitude of the figure must be greater than 3')
def plotArrow(self, pt1, pt2, **params): super(PlotGL, self).plotArrow(pt1, pt2, **params) params = self.setDefaultParamsArrow(**params) headSize = params.get('head_size') lineVec = pt2 - pt1 lenVec = np.linalg.norm(lineVec) gl.glPushMatrix() try: self.plotLine(pt1, pt2, **params) phi, theta = self.geo.vec2Angs(pt2 - pt1) gl.glTranslatef(pt2[0, 0], pt2[1, 0], pt2[2, 0]) gl.glRotatef(np.rad2deg(phi) + 90, 0, 0, 1) gl.glRotatef(np.rad2deg(theta), 1, 0, 0) glut.glutSolidCone(0.5 * headSize * lenVec, headSize * lenVec, 50, 10) finally: gl.glPopMatrix() if params.get('text') is not None: p2 = (pt1 + pt2) / 2 self.plotPoint(p2, **params) self.plotText(p2, params.get('text'))
def draw_direction_cone(p1, p2, position=0.5, precision=12, size=0.1): distance = psub(p2, p1) length = pnorm(distance) direction = pnormalized(distance) if direction is None: # zero-length line return cone_length = length * size cone_radius = cone_length / 3.0 # move the cone to the middle of the line GL.glTranslatef((p1[0] + p2[0]) * position, (p1[1] + p2[1]) * position, (p1[2] + p2[2]) * position) # rotate the cone according to the line direction # The cross product is a good rotation axis. cross = pcross(direction, (0, 0, -1)) if pnorm(cross) != 0: # The line direction is not in line with the z axis. try: angle = math.asin(sqrt(direction[0] ** 2 + direction[1] ** 2)) except ValueError: # invalid angle - just ignore this cone return # convert from radians to degree angle = angle / math.pi * 180 if direction[2] < 0: angle = 180 - angle GL.glRotatef(angle, cross[0], cross[1], cross[2]) elif direction[2] == -1: # The line goes down the z axis - turn it around. GL.glRotatef(180, 1, 0, 0) else: # The line goes up the z axis - nothing to be done. pass # center the cone GL.glTranslatef(0, 0, -cone_length * position) # draw the cone GLUT.glutSolidCone(cone_radius, cone_length, precision, 1)
def render_arrow(self, p, q, r_base=0.01, head_width=0.015, head_len=0.01): # glDisable(GL_LIGHTING) m_quadric = self.quadric GLU.gluQuadricNormals(m_quadric, GLU.GLU_SMOOTH) p = np.array(p) q = np.array(q) u = (q - p) u /= norm(u) P = q - head_len * u z = np.array([0, 0.0, 1.0]) z /= norm(z) if norm(z - u) < 1e-8: axis = np.array([0, 0, 1]) angle = 0.0 else: axis = np.cross(z, u) axis /= norm(axis) angle = math.acos(u.dot(z)) M = R_axis_angle(axis, angle) m = [M[0, 0], M[1, 0], M[2, 0], 0.0, M[0, 1], M[1, 1], M[2, 1], 0.0, M[0, 2], M[1, 2], M[2, 2], 0.0, P[0], P[1], P[2], 1.0] m2 = [M[0, 0], M[1, 0], M[2, 0], 0.0, M[0, 1], M[1, 1], M[2, 1], 0.0, M[0, 2], M[1, 2], M[2, 2], 0.0, p[0], p[1], p[2], 1.0] GL.glPushMatrix() GL.glMultMatrixd(m2) arrow_len = norm(q - p) - head_len # glColor(0.9, 0.2, 0.2) GLU.gluCylinder(m_quadric, r_base, r_base, arrow_len, 10, 10) GL.glPopMatrix() if head_width > 1e-6 and head_len > 1e-6: # glColor(1.0, 0.0, 0.0) GL.glPushMatrix() GL.glMultMatrixd(m) GLUT.glutSolidCone(head_width, head_len, 10, 3) GL.glPopMatrix()
def draw_direction_cone(p1, p2, position=0.5, precision=12, size=0.1): distance = psub(p2, p1) length = pnorm(distance) direction = pnormalized(distance) if direction is None: # zero-length line return cone_length = length * size cone_radius = cone_length / 3.0 # move the cone to the middle of the line GL.glTranslatef((p1[0] + p2[0]) * position, (p1[1] + p2[1]) * position, (p1[2] + p2[2]) * position) # rotate the cone according to the line direction # The cross product is a good rotation axis. cross = pcross(direction, (0, 0, -1)) if pnorm(cross) != 0: # The line direction is not in line with the z axis. try: angle = math.asin(sqrt(direction[0]**2 + direction[1]**2)) except ValueError: # invalid angle - just ignore this cone return # convert from radians to degree angle = angle / math.pi * 180 if direction[2] < 0: angle = 180 - angle GL.glRotatef(angle, cross[0], cross[1], cross[2]) elif direction[2] == -1: # The line goes down the z axis - turn it around. GL.glRotatef(180, 1, 0, 0) else: # The line goes up the z axis - nothing to be done. pass # center the cone GL.glTranslatef(0, 0, -cone_length * position) # draw the cone GLUT.glutSolidCone(cone_radius, cone_length, precision, 1)
def draw_direction_cone(p1, p2): distance = p2.sub(p1) length = distance.norm direction = distance.normalized() if direction is None: # zero-length line return cone_radius = length / 30 cone_length = length / 10 # move the cone to the middle of the line GL.glTranslatef((p1.x + p2.x) / 2, (p1.y + p2.y) / 2, (p1.z + p2.z) / 2) # rotate the cone according to the line direction # The cross product is a good rotation axis. cross = direction.cross(Point(0, 0, -1)) if cross.norm != 0: # The line direction is not in line with the z axis. try: angle = math.asin(sqrt(direction.x ** 2 + direction.y ** 2)) except ValueError: # invalid angle - just ignore this cone return # convert from radians to degree angle = angle / math.pi * 180 if direction.z < 0: angle = 180 - angle GL.glRotatef(angle, cross.x, cross.y, cross.z) elif direction.z == -1: # The line goes down the z axis - turn it around. GL.glRotatef(180, 1, 0, 0) else: # The line goes up the z axis - nothing to be done. pass # center the cone GL.glTranslatef(0, 0, -cone_length / 2) # draw the cone GLUT.glutSolidCone(cone_radius, cone_length, 12, 1)
def drawCoodinateSystem(self): ''' 绘制坐标轴 ''' length = 0.8 GL.glBegin(GL.GL_LINES) # x GL.glColor3f(1, 0, 0) GL.glVertex3f(0, 0, 0) GL.glVertex3f(length, 0, 0) # y GL.glColor3f(1, 0, 1) GL.glVertex3f(0, 0, 0) GL.glVertex3f(0, length, 0) # z GL.glColor3f(0, 0, 1) GL.glVertex3f(0, 0, 0) GL.glVertex3f(0, 0, length) GL.glEnd() # x-arrow GL.glPushMatrix() GL.glColor3f(1, 0, 0) GL.glTranslatef(length, 0, 0) GL.glRotatef(90, 0, 1, 0) GLUT.glutSolidCone(length / 38, length / 18, 10, 10) GL.glTranslatef(-length, 0, 0) GL.glRotatef(-90, 0, 1, 0) GL.glPopMatrix() # y-arrow GL.glPushMatrix() GL.glColor3f(1, 0, 1) GL.glTranslatef(0, length, 0) GL.glRotatef(-90, 1, 0, 0) GLUT.glutSolidCone(length / 38, length / 18, 10, 10) GL.glTranslatef(0, -length, 0) GL.glRotatef(90, 1, 0, 0) GL.glPopMatrix() # z-arrow GL.glPushMatrix() GL.glColor3f(0, 0, 1) GL.glTranslatef(0, 0, length) GLUT.glutSolidCone(length / 38, length / 18, 10, 10) GL.glTranslatef(0, 0, -length) GL.glPopMatrix()
def draw_scene(self): gl.glEnable(gl.GL_BLEND) # gl.glBlendFunc( gl.GL_SRC_ALPHA, gl.GL_SRC_ALPHA) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) gl.glPushMatrix() gl.glTranslatef(*self.point_manager.avg) gl.glPointSize(self.point_manager._get_scale()) if self.points_show: self.verts.draw(gl.GL_POINTS, 'pnc') for c in self.camera_manager.get_cams(): gl.glPushMatrix() gl.glMultMatrixf(c.R_gl) gl.glTranslatef(*c.t) if c.cone_show: gl.glPushMatrix() # cone is constructed on z axis toward image center # rotate so that it goes through pupil position point gl.glMultMatrixf(c.R_cone_gl) gl.glPointSize(4.0) c.inside_cone_vbo.draw(gl.GL_POINTS, 'pnc') gl.glTranslatef(0, 0, -c.cone_len) gl.glColor4f(1.0, 1.0, 1.0, 0.25) glut.glutSolidCone(c.cone_r, c.cone_len, 32, 1) gl.glPopMatrix() if c.pyramid_show: gl.glEnable(gl.GL_TEXTURE_2D) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_SRC_ALPHA) c.img.draw(-c.img_W, -c.img_H, -c.img_Z, c.img_W * 2, c.img_H * 2) gl.glDisable(gl.GL_TEXTURE_2D) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) gl.glPopMatrix() if c.pyramid_show: gl.glPolygonMode(gl.GL_FRONT_AND_BACK, gl.GL_LINE) #wireframe display mode c.pyramid.draw(gl.GL_QUADS, 'pnc') if c.eye_point is not None: # gl.glEnable(gl.GL_POINT_SMOOTH) gl.glPointSize(10.0) c.eye_point.draw(gl.GL_POINTS, 'pnc') gl.glPolygonMode(gl.GL_FRONT_AND_BACK, gl.GL_FILL) gl.glDepthMask(gl.GL_TRUE) gl.glPopMatrix()