def draw(self): quad = glu.gluNewQuadric() ogl.glPushMatrix() ogl.glMultMatrixd(self.frame.matrix()) glu.gluSphere(quad, 0.03, 10, 6) glu.gluCylinder(quad, 0.03, 0.0, 0.09, 10, 1) ogl.glPopMatrix()
def paintGL(self): gl.glMatrixMode(gl.GL_MODELVIEW) gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) gl.glLoadIdentity() # gl.glPushMatrix() glu.gluLookAt(self.camera[0], self.camera[1], self.camera[2], self.center[0], self.center[1], self.center[2], 0, 1, 0) self.paintAxes() normalSize = numpy.max(self.center - self.b_box[1]) / 100.0 gl.glEnable(gl.GL_BLEND) gl.glEnable(gl.GL_CULL_FACE) gl.glCullFace(gl.GL_BACK) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) gluQuadric = glu.gluNewQuadric() for cmd, (array, colors, sizes) in self.commands: for (x, y, z), (r, g, b), size in zip(array, colors, sizes): gl.glPushMatrix() gl.glTranslatef(x, y, z) gl.glColor4f(r, g, b, 0.3) glu.gluSphere(gluQuadric, normalSize * size, 10, 10) gl.glPopMatrix() glu.gluDeleteQuadric(gluQuadric)
def _draw_one_robot(posture_pos: list, highlight=None): """Permet de dessiner un robot avec ces link et ses moteurs. `posture_pos` - liste de matrice de rotation décrivant le robot dans sa posture""" global _section_color, _joint_color, _hjoint_color # Chaque section est representee par un segment noir gl.glBegin(gl.GL_LINE_STRIP) gl.glColor4f(_section_color[0], _section_color[1], _section_color[2], _section_color[3]) for section in posture_pos: gl.glVertex3f(section[0][3], section[1][3], section[2][3]) gl.glEnd() # Chaque moteur est represente par une sphere bleue for i in range(len(posture_pos)): # Si on veux mettre en valeur un moteur, on l'affiche en rouge (pour l'animation) if highlight is not None and i == highlight + 1: gl.glColor4f(_hjoint_color[0], _hjoint_color[1], _hjoint_color[2], _hjoint_color[3]) else: gl.glColor4f(_joint_color[0], _joint_color[1], _joint_color[2], _joint_color[3]) gl.glPushMatrix() gl.glTranslatef(posture_pos[i][0][3], posture_pos[i][1][3], posture_pos[i][2][3]) glu.gluSphere(glu.gluNewQuadric(), 0.003, 16, 16) gl.glPopMatrix()
def init_gl(self): """ Initialise OpenGL settings """ self.sphere = GL.glGenLists(1) GL.glNewList(self.sphere, GL.GL_COMPILE) quad_obj = GLU.gluNewQuadric() GLU.gluQuadricDrawStyle(quad_obj, GLU.GLU_FILL) GLU.gluQuadricNormals(quad_obj, GLU.GLU_SMOOTH) GLU.gluSphere(quad_obj, 1, 16, 16) GL.glEndList() GL.glShadeModel(GL.GL_SMOOTH) GL.glEnable(GL.GL_DEPTH_TEST) GL.glEnable(GL.GL_CULL_FACE) GL.glEnable(GL.GL_LIGHTING) # make sure normal vectors of scaled spheres are normalised GL.glEnable(GL.GL_NORMALIZE) GL.glEnable(GL.GL_LIGHT0) light_pos = list(_LIGHT_POSITION) + [1] GL.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, light_pos) GL.glLightfv(GL.GL_LIGHT0, GL.GL_AMBIENT, [1.0, 1.0, 1.0, 1.0]) GL.glLightfv(GL.GL_LIGHT0, GL.GL_DIFFUSE, [1.0, 1.0, 1.0, 1.0]) GL.glLightfv(GL.GL_LIGHT0, GL.GL_SPECULAR, [1.0, 1.0, 1.0, 1.0]) GL.glMaterialfv(GL.GL_FRONT, GL.GL_AMBIENT, [0.2, .2, .2, 1]) GL.glMaterialfv(GL.GL_FRONT, GL.GL_DIFFUSE, [0.7, 0.7, 0.7, 1]) GL.glMaterialfv(GL.GL_FRONT, GL.GL_SPECULAR, [0.1, 0.1, 0.1, 1]) GL.glMaterialf(GL.GL_FRONT, GL.GL_SHININESS, 20) GL.glMatrixMode(GL.GL_PROJECTION) GL.glLoadIdentity() GLU.gluPerspective(60, 1, .01, 10) GL.glMatrixMode(GL.GL_MODELVIEW)
def draw(self): if not GLPresent: raise "OpenGL module could not be loaded" GL.glPushMatrix() GL.glScalef( self.width, self.height, self.depth ) GL.glPolygonMode( GL.GL_FRONT_AND_BACK, GL.GL_LINE ) GLU.gluSphere( self.quad, 1.0, 12, 12 ) GL.glPolygonMode( GL.GL_FRONT_AND_BACK, GL.GL_FILL ) GL.glPopMatrix()
def draw(self): x, y, z, r = self.coords() # need to translate the whole thing to x,y,z GL.glPushMatrix() GL.glTranslatef(x, y, z) # the sphere starts out at the origin GLU.gluSphere(self.q, r, 32, 16) GL.glPopMatrix()
def draw_points_cloud(end_points: list, max_dist=0.3, robot=None, sphere=0, rota=False): """Dessine un nuage de point 3d. `end_points` - les coordonnees des points en (x, y, z) ou liste d'end_points""" global _cloud_point_color _init_display() # Calcul de la couleur des points avant affichage points = [] for ep in end_points: if isinstance(ep, EndPoint): pos = ep.get_pos() else: pos = ep if _cloud_point_color is None: d = 0 for p in pos: d += p**2 d = math.sqrt(d) fact = d / max_dist color = _get_rainbow_color(fact) points.append((pos, color)) else: points.append((pos, _cloud_point_color)) while _StaticVars.is_running: gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) if rota: gl.glRotatef(1, 0, 0, 1) _draw_axes() gl.glBegin(gl.GL_POINTS) for (p, c) in points: gl.glColor3f(c[0], c[1], c[2]) gl.glVertex3f(p[0], p[1], p[2]) gl.glEnd() if robot is not None: posture = robot.get_posture_pos((0, 0, 0, 0, 0, 0)) _draw_one_robot(posture) if sphere != 0: gl.glColor4f(_sphere_color[0], _sphere_color[1], _sphere_color[2], _sphere_color[3]) glu.gluSphere(glu.gluNewQuadric(), sphere, 64, 32) # _draw_fps() pg.display.flip() _event_handler() pg.time.wait(10)
def display(self): ogl.glPushMatrix() ogl.glTranslate(-1, -3.3, 1) ang = ((time.time() / 40) % 1) * 360 # 2*3.14 ogl.glRotate(ang, 0, 0, 1) quad = oglu.gluNewQuadric() oglu.gluQuadricDrawStyle(quad, oglu.GLU_LINE) oglu.gluSphere(quad, 0.7, 15, 15) ogl.glPopMatrix()
def draw(self): GL.glPushMatrix() GL.glTranslatef( self.center.x, self.center.y, self.center.z ) GL.glScalef( self.radius, self.radius, self.radius ) GL.glPolygonMode( GL.GL_FRONT_AND_BACK, GL.GL_LINE ) GL.glDisable( GL.GL_TEXTURE_2D) GLU.gluSphere( self.quadratic, 1.0, 24, 24 ) GL.glEnable( GL.GL_TEXTURE_2D) GL.glPolygonMode( GL.GL_FRONT_AND_BACK, GL.GL_FILL ) GL.glPopMatrix()
def draw(self): GL.glPushMatrix() GL.glTranslatef(self.center.x, self.center.y, self.center.z) GL.glScalef(self.radius, self.radius, self.radius) GL.glPolygonMode(GL.GL_FRONT_AND_BACK, GL.GL_LINE) GL.glDisable(GL.GL_TEXTURE_2D) GLU.gluSphere(self.quadratic, 1.0, 24, 24) GL.glEnable(GL.GL_TEXTURE_2D) GL.glPolygonMode(GL.GL_FRONT_AND_BACK, GL.GL_FILL) GL.glPopMatrix()
def paintGL(self): gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) gl.glColor3f(1.0, 2.0, 1.0) gl.glMatrixMode(gl.GL_MODELVIEW) gl.glPushMatrix() gl.glTranslatef(0, 0, 0) qobj = glu.gluNewQuadric() glu.gluQuadricOrientation(qobj, glu.GLU_OUTSIDE) glu.gluSphere(qobj, 0.67, 100, 100) gl.glPopMatrix()
def add_atoms(self): prop = self.prop mendeley = TPeriodTable() gl.glNewList(self.object, gl.GL_COMPILE) min_val = 0 max_val = 0 mean_val = 0 if (len(prop) > 0) and (prop != "charge"): min_val = self.MainModel.atoms[0].properties[prop] max_val = self.MainModel.atoms[0].properties[prop] mean_val = self.MainModel.atoms[0].properties[prop] for at in self.MainModel.atoms: val = at.properties[prop] if min_val > val: min_val = val if max_val < val: max_val = val mean_val += val mean_val /= self.MainModel.nAtoms() for at in self.MainModel.atoms: gl.glPushMatrix() gl.glTranslatef(at.x, at.y, at.z) self.QuadObjS.append(glu.gluNewQuadric()) rad = mendeley.Atoms[at.charge].radius / mendeley.Atoms[6].radius if at.isSelected() == False: if (len(prop) > 0) and (prop != "charge"): val = at.properties[prop] if val > mean_val: gl.glColor3f( 0, math.fabs((val - mean_val) / (max_val - mean_val)), 0) else: gl.glColor3f( 0, 0, math.fabs((val - mean_val) / (min_val - mean_val))) else: color = self.color_of_atoms[at.charge] if (self.SelectedFragmentMode == True) and (at.fragment1 == True): gl.glColor4f(color[0], color[1], color[2], self.SelectedFragmentAtomsTransp) else: gl.glColor3f(color[0], color[1], color[2]) glu.gluSphere(self.QuadObjS[-1], 0.3 * rad, self.quality * 70, self.quality * 70) else: gl.glColor3f(1, 0, 0) glu.gluSphere(self.QuadObjS[-1], 0.35 * rad, self.quality * 70, self.quality * 70) gl.glPopMatrix() gl.glEndList() self.active = True
def add_selected_atom(self): gl.glNewList(self.object + 7, gl.GL_COMPILE) for at in self.MainModel.atoms: if at.isSelected() == True: gl.glPushMatrix() gl.glTranslatef(at.x, at.y, at.z) self.QuadObjS.append(glu.gluNewQuadric()) gl.glColor3f(1, 0, 0) glu.gluSphere(self.QuadObjS[-1], 0.35, 70, 70) gl.glPopMatrix() gl.glEndList()
def __init__(self,radius=1.0, slices=20, stacks=20): _engine.Object_model.__init__(self) prim=_engine.Primitive() sphere_gl_list=_gl.glGenLists(1) quadric=_glu.gluNewQuadric() _gl.glNewList(sphere_gl_list,_gl.GL_COMPILE) _glu.gluSphere(quadric,radius,slices,stacks) _gl.glEndList() prim.gl_list=sphere_gl_list self.add_primitive(prim) self.scale=[1.,1.,1.]
def draw_element(self, element, position): gl.glPushMatrix() gl.glTranslate(*position) if element.color: gl.glColor(*element.color) else: gl.glColor(*self.default_element_color) glu.gluSphere(self.quad, 4 * self.bond_width, self.sphere_resolution, self.sphere_resolution) gl.glPopMatrix()
def to_OpenGL(self): if not GL_enabled: return GL.glPushMatrix() GL.glTranslate(self.center.x, self.center.y, self.center.z) if not hasattr(self, "_sphere"): self._sphere = GLU.gluNewQuadric() GLU.gluSphere(self._sphere, self.radius, 10, 10) if not hasattr(self, "_cylinder"): self._cylinder = GLU.gluNewQuadric() GLU.gluCylinder(self._cylinder, self.radius, self.radius, self.height, 10, 10) GL.glPopMatrix()
def draw_sphere(color, pos, radius=2): gl.glColor(*color) gl.glPushMatrix() x, y = pos gl.glTranslate(x, y, 3) q = glu.gluNewQuadric() glu.gluSphere(q, radius, 20, 20) glu.gluDeleteQuadric(q) gl.glPopMatrix()
def drawSolidSphere(radius=1.0, slices=16, stacks=16, Color=None): if (Color != None): gl.glEnable(gl.GL_DEPTH_TEST) gl.glHint(gl.GL_PERSPECTIVE_CORRECTION_HINT, gl.GL_NICEST) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) # Orig gl.glEnable(gl.GL_BLEND) gl.glColor4fv(Color) else: gl.glColor3f(0.0, 0.0, 0.0) glu.gluQuadricDrawStyle(QUADRIC, glu.GLU_FILL) glu.gluSphere(QUADRIC, radius, slices, stacks)
def to_opengl(self): if not GL_enabled: return GL.glPushMatrix() GL.glTranslate(self.center[0], self.center[1], self.center[2]) if not hasattr(self, "_sphere"): self._sphere = GLU.gluNewQuadric() GLU.gluSphere(self._sphere, self.radius, 10, 10) if not hasattr(self, "_cylinder"): self._cylinder = GLU.gluNewQuadric() GLU.gluCylinder(self._cylinder, self.radius, self.radius, self.height, 10, 10) GL.glPopMatrix()
def draw_nodes(pos, nodelist, node_color, node_size, node_border_size, node_border_color, scale, fancy, lights): GL.glDisable(GL.GL_DEPTH_TEST) i = 0 if fancy: vp = GL.glGetIntegerv(GL.GL_VIEWPORT) sphere_size_scale = 2./(vp[2]+vp[3]) for n in nodelist: if fancy: if lights: GL.glEnable(GL.GL_LIGHTING) GL.glPushMatrix() if len(pos[n]) ==2: position = tuple(list(pos[n]) + [0.]) else: position = pos[n] GL.glTranslate(*position) GL.glScale(scale,scale,scale) GL.glColor(*node_color[i]) sphere = GLU.gluNewQuadric() GLU.gluQuadricNormals(sphere,GLU.GLU_SMOOTH) GLU.gluQuadricTexture(sphere,GLU.GLU_TRUE) GLU.gluSphere(sphere, node_size[i]*sphere_size_scale, 32, 32) GL.glPopMatrix() if lights: GL.glDisable(GL.GL_LIGHTING) else: GL.glPointSize(node_size[i]+node_border_size[i]) GL.glBegin(GL.GL_POINTS) GL.glColor(*node_border_color[i]) GL.glVertex(*tuple(pos[n])) GL.glEnd() GL.glPointSize(node_size[i]) GL.glBegin(GL.GL_POINTS) GL.glColor(*node_color[i]) GL.glVertex(*tuple(pos[n])) GL.glEnd() i+=1 GL.glEnable(GL.GL_DEPTH_TEST)
def add_axes(self): gl.glNewList(self.object + 7, gl.GL_COMPILE) gl.glColor3f(self.color_of_axes[0], self.color_of_axes[1], self.color_of_axes[2]) size = 2 sizeCone = 0.2 letter_height = sizeCone letter_width = 0.6 * sizeCone width = 0.06 self.QuadObjS.append(glu.gluNewQuadric()) glu.gluSphere(self.QuadObjS[-1], 2 * width, 70, 70) p0 = np.array([0, 0, 0]) p1 = np.array([size, 0, 0]) p1cone = np.array([size + sizeCone, 0, 0]) p2 = np.array([0, size, 0]) p2cone = np.array([0, size + sizeCone, 0]) p3 = np.array([0, 0, size]) p3cone = np.array([0, 0, size + sizeCone]) self.add_bond(p0, p1, width) self.add_bond(p1, p1cone, 2 * width, 'conus') self.add_bond(p0, p2, width) self.add_bond(p2, p2cone, 2 * width, 'conus') self.add_bond(p0, p3, width) self.add_bond(p3, p3cone, 2 * width, 'conus') # lets drow "X" pX = p1cone + np.array([0, 1.5 * sizeCone, 0]) pX1 = pX + np.array([-0.5 * letter_width, -0.5 * letter_height, 0]) pX2 = pX + np.array([+0.5 * letter_width, +0.5 * letter_height, 0]) pX3 = pX + np.array([-0.5 * letter_width, +0.5 * letter_height, 0]) pX4 = pX + np.array([+0.5 * letter_width, -0.5 * letter_height, 0]) self.add_bond(pX1, pX2, 0.5 * width) self.add_bond(pX3, pX4, 0.5 * width) # lets drow "Y" pY = p2cone + np.array([0, 1.5 * sizeCone, 0]) pY1 = pY pY2 = pY + np.array([0, +0.5 * letter_height, +0.5 * letter_width]) pY3 = pY + np.array([0, -0.5 * letter_height, +0.5 * letter_width]) pY4 = pY + np.array([0, +0.5 * letter_height, -0.5 * letter_width]) self.add_bond(pY1, pY2, 0.5 * width) self.add_bond(pY3, pY4, 0.5 * width) # lets drow "Z" pZ = p3cone + np.array([1.5 * sizeCone, 0, 0]) pZ1 = pZ + np.array([-0.5 * letter_height, 0, -0.5 * letter_width]) pZ2 = pZ + np.array([+0.5 * letter_height, 0, +0.5 * letter_width]) pZ3 = pZ + np.array([-0.5 * letter_height, 0, +0.5 * letter_width]) pZ4 = pZ + np.array([+0.5 * letter_height, 0, -0.5 * letter_width]) self.add_bond(pZ1, pZ3, 0.5 * width) self.add_bond(pZ1, pZ2, 0.5 * width) self.add_bond(pZ2, pZ4, 0.5 * width) gl.glEndList()
def paintGL(self): glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glColor3f(1.0, 1.0, 1.0) # Default color is white # Draw the flashlight self.drawFlashLight() # Draw the sphere glMaterialfv(GL_FRONT, GL_AMBIENT, [0.3, 0.3, 0.3, 1.0]) glMaterialfv(GL_FRONT, GL_DIFFUSE, [1.0, 1.0, 1.0, 1.0]) glMaterialfv(GL_FRONT, GL_SPECULAR, [1.0, 1.0, 1.0, 1.0]) glLightfv(self.light, GL_POSITION, self.lightposition) glMaterialf(GL_FRONT, GL_SHININESS, 25.0) quad = GLU.gluNewQuadric() GLU.gluQuadricDrawStyle(quad, GLU.GLU_FILL) GLU.gluQuadricNormals(quad, GLU.GLU_SMOOTH) GLU.gluSphere(quad, 3.0, 30, 30) glFlush()
def render(self): """Drawing a sphere""" x = self.position[0] y = self.position[1] z = self.position[2] v = self.visible gl.glPushMatrix() gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) gl.glEnable(gl.GL_COLOR_MATERIAL) gl.glColorMaterial(gl.GL_FRONT_AND_BACK, gl.GL_AMBIENT_AND_DIFFUSE) self.color = (self.color[0], self.color[1], self.color[2], v) gl.glColor4f(*self.color) gl.glTranslatef(x, y, z) glu.gluSphere(self.quadric, self.radius, self.slices, self.stacks) gl.glDisable(gl.GL_BLEND) gl.glPopMatrix()
def drawPoints(self) : GL.glEnable(GL.GL_CULL_FACE) GL.glEnable(GL.GL_LIGHTING) quadric = GLU.gluNewQuadric() for e,a,d in self._points : ra, re = math.radians(a), math.radians(e) sa, se = math.sin(ra), math.sin(re) ca, ce = math.cos(ra), math.cos(re) if d<0 : GL.glColor(1., 1., 1., .5) d = -d else : GL.glColor(1., .6, .6, .5) GL.glPushMatrix() GL.glTranslate(d*ce*ca, d*ce*sa,d*se) GLU.gluSphere(quadric, .1, 10, 10) GL.glPopMatrix()
def paint_enter(self): """ Implements the GLNodeAdapter's paint_enter method for an OpenGL Render operation. """ GL.glPushAttrib(GL.GL_ENABLE_BIT | GL.GL_DEPTH_BUFFER_BIT | GL.GL_LINE_BIT | GL.GL_CURRENT_BIT) GL.glPushClientAttrib(GL.GL_CLIENT_VERTEX_ARRAY_BIT) GL.glMatrixMode(GL.GL_MODELVIEW) GL.glPushMatrix() GL.glMultMatrixf(self._node.matrix().data()) GL.glColor(self._node.color.getRgbF()) q = self.__quadric r = self._node.radius sl = self._node.slices st = self._node.stacks GLU.gluQuadricNormals(q, GLU.GLU_SMOOTH ) GLU.gluQuadricDrawStyle(q, GLU.GLU_FILL ); GLU.gluSphere(q, r, sl, st)
def init_gl(self): """ Initialise OpenGL settings """ self.sphere = GL.glGenLists(1) # creates model for planet GL.glNewList(self.sphere, GL.GL_COMPILE) quad_obj = GLU.gluNewQuadric() GLU.gluQuadricDrawStyle(quad_obj, GLU.GLU_FILL) GLU.gluQuadricNormals(quad_obj, GLU.GLU_SMOOTH) GLU.gluSphere(quad_obj, 1, 16, 16) GL.glEndList() GL.glShadeModel(GL.GL_SMOOTH) GL.glEnable(GL.GL_DEPTH_TEST) GL.glEnable(GL.GL_CULL_FACE) GL.glEnable(GL.GL_LIGHTING) # make sure normal vectors of scaled spheres are normalised GL.glEnable(GL.GL_NORMALIZE) GL.glEnable(GL.GL_LIGHT0)
def draw_discretization(grid: Discretisation, alpha_per_point=0.01, d=None): """Permet de représenter le tableau de la discretisation de l'espace. Chaque case est transparente, mais l'est de moins en moins en fonction du nombre de points dans celle-ci. Réglable avec `alpha_per_point` `d` si préciser représente une sphère. (vecteur directeur)""" _init_display() size = grid.size mins = grid.min while _StaticVars.is_running: gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) _draw_axes() for cell in grid.visited: x = cell[0] * size + mins y = cell[1] * size + mins z = cell[2] * size + mins a = grid.get_cell(cell) * alpha_per_point if a > 1: a = 1 gl.glColor4f(0, 1, 0, a) _draw_cube((x, y, z), size) #Permet d'afficher une direction pour tester l'algo frontier if d is not None: gl.glColor3f(1, 0, 0) gl.glPushMatrix() gl.glTranslatef(d[0], d[1], d[2]) glu.gluSphere(glu.gluNewQuadric(), 0.03, 16, 16) gl.glPopMatrix() _draw_fps() pg.display.flip() _event_handler() pg.time.wait(10)
def drawSphere(s,color=cyan,ndiv=8): """Draws a centered sphere with radius s in given color.""" quad = GLU.gluNewQuadric() GLU.gluQuadricNormals(quad, GLU.GLU_SMOOTH) GL.glColor(*color) GLU.gluSphere(quad,s,ndiv,ndiv)
def to_OpenGL(self, color=None, show_directions=False): if not GL_enabled: return if not color is None: GL.glColor4f(*color) GL.glBegin(GL.GL_TRIANGLES) # use normals to improve lighting (contributed by imyrek) normal_t = self.normal GL.glNormal3f(normal_t[0], normal_t[1], normal_t[2]) # The triangle's points are in clockwise order, but GL expects # counter-clockwise sorting. GL.glVertex3f(self.p1[0], self.p1[1], self.p1[2]) GL.glVertex3f(self.p3[0], self.p3[1], self.p3[2]) GL.glVertex3f(self.p2[0], self.p2[1], self.p2[2]) GL.glEnd() if show_directions: # display surface normals n = self.normal c = self.center d = 0.5 GL.glBegin(GL.GL_LINES) GL.glVertex3f(c[0], c[1], c[2]) GL.glVertex3f(c[0]+n[0]*d, c[1]+n[1]*d, c[2]+n[2]*d) GL.glEnd() if False: # display bounding sphere GL.glPushMatrix() middle = self.middle GL.glTranslate(middle[0], middle[1], middle[2]) if not hasattr(self, "_sphere"): self._sphere = GLU.gluNewQuadric() GLU.gluSphere(self._sphere, self.radius, 10, 10) GL.glPopMatrix() if pycam.Utils.log.is_debug(): # draw triangle id on triangle face GL.glPushMatrix() c = self.center GL.glTranslate(c[0], c[1], c[2]) p12 = pmul(padd(self.p1, self.p2), 0.5) p3_12 = pnormalized(psub(self.p3, p12)) p2_1 = pnormalized(psub(self.p1, self.p2)) pn = pcross(p2_1, p3_12) GL.glMultMatrixf((p2_1[0], p2_1[1], p2_1[2], 0, p3_12[0], p3_12[1], p3_12[2], 0, pn[0], pn[1], pn[2], 0, 0, 0, 0, 1)) n = pmul(self.normal, 0.01) GL.glTranslatef(n[0], n[1], n[2]) maxdim = max((self.maxx - self.minx), (self.maxy - self.miny), (self.maxz - self.minz)) factor = 0.001 GL.glScalef(factor * maxdim, factor * maxdim, factor * maxdim) w = 0 id_string = "%s." % str(self.id) for ch in id_string: w += GLUT.glutStrokeWidth(GLUT.GLUT_STROKE_ROMAN, ord(ch)) GL.glTranslate(-w/2, 0, 0) for ch in id_string: GLUT.glutStrokeCharacter(GLUT.GLUT_STROKE_ROMAN, ord(ch)) GL.glPopMatrix() if False: # draw point id on triangle face c = self.center p12 = pmul(padd(self.p1, self.p2), 0.5) p3_12 = pnormalized(psub(self.p3, p12)) p2_1 = pnormalized(psub(self.p1, self.p2)) pn = pcross(p2_1, p3_12) n = pmul(self.normal, 0.01) for p in (self.p1, self.p2, self.p3): GL.glPushMatrix() pp = psub(p, pmul(psub(p, c), 0.3)) GL.glTranslate(pp[0], pp[1], pp[2]) GL.glMultMatrixf((p2_1[0], p2_1[1], p2_1[2], 0, p3_12[0], p3_12[1], p3_12[2], 0, pn[0], pn[1], pn[2], 0, 0, 0, 0, 1)) GL.glTranslatef(n[0], n[1], n[2]) GL.glScalef(0.001, 0.001, 0.001) w = 0 for ch in str(p.id): w += GLUT.glutStrokeWidth(GLUT.GLUT_STROKE_ROMAN, ord(ch)) GL.glTranslate(-w/2, 0, 0) for ch in str(p.id): GLUT.glutStrokeCharacter(GLUT.GLUT_STROKE_ROMAN, ord(ch)) GL.glPopMatrix()
def to_opengl(self, color=None, show_directions=False): if not GL_enabled: return if color is not None: GL.glColor4f(*color) GL.glBegin(GL.GL_TRIANGLES) # use normals to improve lighting (contributed by imyrek) normal_t = self.normal GL.glNormal3f(normal_t[0], normal_t[1], normal_t[2]) # The triangle's points are in clockwise order, but GL expects # counter-clockwise sorting. GL.glVertex3f(self.p1[0], self.p1[1], self.p1[2]) GL.glVertex3f(self.p3[0], self.p3[1], self.p3[2]) GL.glVertex3f(self.p2[0], self.p2[1], self.p2[2]) GL.glEnd() if show_directions: # display surface normals n = self.normal c = self.center d = 0.5 GL.glBegin(GL.GL_LINES) GL.glVertex3f(c[0], c[1], c[2]) GL.glVertex3f(c[0] + n[0] * d, c[1] + n[1] * d, c[2] + n[2] * d) GL.glEnd() if False: # display bounding sphere GL.glPushMatrix() middle = self.middle GL.glTranslate(middle[0], middle[1], middle[2]) if not hasattr(self, "_sphere"): self._sphere = GLU.gluNewQuadric() GLU.gluSphere(self._sphere, self.radius, 10, 10) GL.glPopMatrix() if pycam.Utils.log.is_debug(): # draw triangle id on triangle face GL.glPushMatrix() c = self.center GL.glTranslate(c[0], c[1], c[2]) p12 = pmul(padd(self.p1, self.p2), 0.5) p3_12 = pnormalized(psub(self.p3, p12)) p2_1 = pnormalized(psub(self.p1, self.p2)) pn = pcross(p2_1, p3_12) GL.glMultMatrixf((p2_1[0], p2_1[1], p2_1[2], 0, p3_12[0], p3_12[1], p3_12[2], 0, pn[0], pn[1], pn[2], 0, 0, 0, 0, 1)) n = pmul(self.normal, 0.01) GL.glTranslatef(n[0], n[1], n[2]) maxdim = max((self.maxx - self.minx), (self.maxy - self.miny), (self.maxz - self.minz)) factor = 0.001 GL.glScalef(factor * maxdim, factor * maxdim, factor * maxdim) w = 0 id_string = "%s." % str(self.id) for ch in id_string: w += GLUT.glutStrokeWidth(GLUT_STROKE_ROMAN, ord(ch)) GL.glTranslate(-w / 2, 0, 0) for ch in id_string: GLUT.glutStrokeCharacter(GLUT_STROKE_ROMAN, ord(ch)) GL.glPopMatrix() if False: # draw point id on triangle face c = self.center p12 = pmul(padd(self.p1, self.p2), 0.5) p3_12 = pnormalized(psub(self.p3, p12)) p2_1 = pnormalized(psub(self.p1, self.p2)) pn = pcross(p2_1, p3_12) n = pmul(self.normal, 0.01) for p in (self.p1, self.p2, self.p3): GL.glPushMatrix() pp = psub(p, pmul(psub(p, c), 0.3)) GL.glTranslate(pp[0], pp[1], pp[2]) GL.glMultMatrixf( (p2_1[0], p2_1[1], p2_1[2], 0, p3_12[0], p3_12[1], p3_12[2], 0, pn[0], pn[1], pn[2], 0, 0, 0, 0, 1)) GL.glTranslatef(n[0], n[1], n[2]) GL.glScalef(0.001, 0.001, 0.001) w = 0 for ch in str(p.id): w += GLUT.glutStrokeWidth(GLUT_STROKE_ROMAN, ord(ch)) GL.glTranslate(-w / 2, 0, 0) for ch in str(p.id): GLUT.glutStrokeCharacter(GLUT_STROKE_ROMAN, ord(ch)) GL.glPopMatrix()
def test_quadrics(self): """Test for rendering quadric objects""" quad = GLU.gluNewQuadric() glColor3f(1, 0, 0) GLU.gluSphere(quad, 1.0, 16, 16)
def applyShape(self): Q=GLU.gluNewQuadric() GLU.gluQuadricNormals(Q, self.GL(self._normals)) GLU.gluQuadricTexture(Q, self.GL(self._texture)) GLU.gluSphere(Q, self._radius, self._subdiv, self._subdiv/2) GLU.gluDeleteQuadric( Q )
def to_OpenGL(self, color=None, show_directions=False): if not GL_enabled: return if not color is None: GL.glColor4f(*color) GL.glBegin(GL.GL_TRIANGLES) # use normals to improve lighting (contributed by imyrek) normal_t = self.normal GL.glNormal3f(normal_t.x, normal_t.y, normal_t.z) # The triangle's points are in clockwise order, but GL expects # counter-clockwise sorting. GL.glVertex3f(self.p1.x, self.p1.y, self.p1.z) GL.glVertex3f(self.p3.x, self.p3.y, self.p3.z) GL.glVertex3f(self.p2.x, self.p2.y, self.p2.z) GL.glEnd() if show_directions: # display surface normals n = self.normal c = self.center d = 0.5 GL.glBegin(GL.GL_LINES) GL.glVertex3f(c.x, c.y, c.z) GL.glVertex3f(c.x+n.x*d, c.y+n.y*d, c.z+n.z*d) GL.glEnd() if False: # display bounding sphere GL.glPushMatrix() middle = self.middle GL.glTranslate(middle.x, middle.y, middle.z) if not hasattr(self, "_sphere"): self._sphere = GLU.gluNewQuadric() GLU.gluSphere(self._sphere, self.radius, 10, 10) GL.glPopMatrix() if pycam.Utils.log.is_debug(): # draw triangle id on triangle face GL.glPushMatrix() c = self.center GL.glTranslate(c.x, c.y, c.z) p12 = self.p1.add(self.p2).mul(0.5) p3_12 = self.p3.sub(p12).normalized() p2_1 = self.p1.sub(self.p2).normalized() pn = p2_1.cross(p3_12) GL.glMultMatrixf((p2_1.x, p2_1.y, p2_1.z, 0, p3_12.x, p3_12.y, p3_12.z, 0, pn.x, pn.y, pn.z, 0, 0, 0, 0, 1)) n = self.normal.mul(0.01) GL.glTranslatef(n.x, n.y, n.z) maxdim = max((self.maxx - self.minx), (self.maxy - self.miny), (self.maxz - self.minz)) factor = 0.001 GL.glScalef(factor * maxdim, factor * maxdim, factor * maxdim) w = 0 id_string = "%s." % str(self.id) for ch in id_string: w += GLUT.glutStrokeWidth(GLUT.GLUT_STROKE_ROMAN, ord(ch)) GL.glTranslate(-w/2, 0, 0) for ch in id_string: GLUT.glutStrokeCharacter(GLUT.GLUT_STROKE_ROMAN, ord(ch)) GL.glPopMatrix() if False: # draw point id on triangle face c = self.center p12 = self.p1.add(self.p2).mul(0.5) p3_12 = self.p3.sub(p12).normalized() p2_1 = self.p1.sub(self.p2).normalized() pn = p2_1.cross(p3_12) n = self.normal.mul(0.01) for p in (self.p1, self.p2, self.p3): GL.glPushMatrix() pp = p.sub(p.sub(c).mul(0.3)) GL.glTranslate(pp.x, pp.y, pp.z) GL.glMultMatrixf((p2_1.x, p2_1.y, p2_1.z, 0, p3_12.x, p3_12.y, p3_12.z, 0, pn.x, pn.y, pn.z, 0, 0, 0, 0, 1)) GL.glTranslatef(n.x, n.y, n.z) GL.glScalef(0.001, 0.001, 0.001) w = 0 for ch in str(p.id): w += GLUT.glutStrokeWidth(GLUT.GLUT_STROKE_ROMAN, ord(ch)) GL.glTranslate(-w/2, 0, 0) for ch in str(p.id): GLUT.glutStrokeCharacter(GLUT.GLUT_STROKE_ROMAN, ord(ch)) GL.glPopMatrix()
def to_OpenGL(self, color=None, show_directions=False): if not GL_enabled: return if not color is None: GL.glColor4f(*color) GL.glBegin(GL.GL_TRIANGLES) # use normals to improve lighting (contributed by imyrek) normal_t = self.normal GL.glNormal3f(normal_t.x, normal_t.y, normal_t.z) # The triangle's points are in clockwise order, but GL expects # counter-clockwise sorting. GL.glVertex3f(self.p1.x, self.p1.y, self.p1.z) GL.glVertex3f(self.p3.x, self.p3.y, self.p3.z) GL.glVertex3f(self.p2.x, self.p2.y, self.p2.z) GL.glEnd() if show_directions: # display surface normals n = self.normal c = self.center d = 0.5 GL.glBegin(GL.GL_LINES) GL.glVertex3f(c.x, c.y, c.z) GL.glVertex3f(c.x + n.x * d, c.y + n.y * d, c.z + n.z * d) GL.glEnd() if False: # display bounding sphere GL.glPushMatrix() middle = self.middle GL.glTranslate(middle.x, middle.y, middle.z) if not hasattr(self, "_sphere"): self._sphere = GLU.gluNewQuadric() GLU.gluSphere(self._sphere, self.radius, 10, 10) GL.glPopMatrix() if pycam.Utils.log.is_debug(): # draw triangle id on triangle face GL.glPushMatrix() c = self.center GL.glTranslate(c.x, c.y, c.z) p12 = self.p1.add(self.p2).mul(0.5) p3_12 = self.p3.sub(p12).normalized() p2_1 = self.p1.sub(self.p2).normalized() pn = p2_1.cross(p3_12) GL.glMultMatrixf((p2_1.x, p2_1.y, p2_1.z, 0, p3_12.x, p3_12.y, p3_12.z, 0, pn.x, pn.y, pn.z, 0, 0, 0, 0, 1)) n = self.normal.mul(0.01) GL.glTranslatef(n.x, n.y, n.z) maxdim = max((self.maxx - self.minx), (self.maxy - self.miny), (self.maxz - self.minz)) factor = 0.001 GL.glScalef(factor * maxdim, factor * maxdim, factor * maxdim) w = 0 id_string = "%s." % str(self.id) for ch in id_string: w += GLUT.glutStrokeWidth(GLUT.GLUT_STROKE_ROMAN, ord(ch)) GL.glTranslate(-w / 2, 0, 0) for ch in id_string: GLUT.glutStrokeCharacter(GLUT.GLUT_STROKE_ROMAN, ord(ch)) GL.glPopMatrix() if False: # draw point id on triangle face c = self.center p12 = self.p1.add(self.p2).mul(0.5) p3_12 = self.p3.sub(p12).normalized() p2_1 = self.p1.sub(self.p2).normalized() pn = p2_1.cross(p3_12) n = self.normal.mul(0.01) for p in (self.p1, self.p2, self.p3): GL.glPushMatrix() pp = p.sub(p.sub(c).mul(0.3)) GL.glTranslate(pp.x, pp.y, pp.z) GL.glMultMatrixf((p2_1.x, p2_1.y, p2_1.z, 0, p3_12.x, p3_12.y, p3_12.z, 0, pn.x, pn.y, pn.z, 0, 0, 0, 0, 1)) GL.glTranslatef(n.x, n.y, n.z) GL.glScalef(0.001, 0.001, 0.001) w = 0 for ch in str(p.id): w += GLUT.glutStrokeWidth(GLUT.GLUT_STROKE_ROMAN, ord(ch)) GL.glTranslate(-w / 2, 0, 0) for ch in str(p.id): GLUT.glutStrokeCharacter(GLUT.GLUT_STROKE_ROMAN, ord(ch)) GL.glPopMatrix()
def draw(self): """Draws one frame in the OpenGL window """ blue = (.27, .388, .678) dark_grey = (.235, .243, .266) grey = (.309, .309, .309) light_grey = (.447, .435, .449) sensor_data = self.sensor_data print("\r%s" % sensor_data, end='') #FIXME: Workaround to avoid using quaternions quat = Quaternion(sensor_data.gyro.w, sensor_data.gyro.x, sensor_data.gyro.y, sensor_data.gyro.z)#.unit offset = Quaternion(self.offset.gyro.w, self.offset.gyro.x, self.offset.gyro.y, self.offset.gyro.z) if offset: #TODO: Change back to quaternions # quat = offset.inverse * sensor_data.gyro quat = quat - offset self.flex_bent = self.offset.flex - self.flex_straight + self.flex_bent self.flex_straight = self.offset.flex gyro_euler = quat_to_euler(quat) rotation = quat_to_axis_rotation(quat) flex_angle =\ self.translate_range(self.sensor_data.flex, self.flex_straight, self.flex_bent, 0.0, 90.0) \ if self.sensor_data.flex != 0 else 0 flex_angle = min(170, max(-20, flex_angle)) gl.glClearColor(.8, .8, .8, 1.0) gl.glClearDepth(1.0) gl.glEnable(gl.GL_DEPTH_TEST) gl.glEnable(gl.GL_LIGHTING) gl.glShadeModel(gl.GL_SMOOTH) gl.glDisable(gl.GL_COLOR_MATERIAL) gl.glDepthFunc(gl.GL_LEQUAL) gl.glHint(gl.GL_PERSPECTIVE_CORRECTION_HINT, gl.GL_NICEST) gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT | gl.GL_STENCIL_BUFFER_BIT) gl.glEnable(gl.GL_LIGHT0) gl.glLightfv(gl.GL_LIGHT0, gl.GL_POSITION, (1, 2, 3)) gl.glLightfv(gl.GL_LIGHT0, gl.GL_AMBIENT, (.5, .5, .5)) gl.glLightfv(gl.GL_LIGHT0, gl.GL_DIFFUSE, (.6, .6, .6)) gl.glLightfv(gl.GL_LIGHT0, gl.GL_SPECULAR, (0, 0, 0)) # gl.glLightfv(gl.GL_LIGHT0, gl.GL_SPOT_DIRECTION, (-2, -3, -3)) gl.glLightf(gl.GL_LIGHT0, gl.GL_SPOT_CUTOFF, 180) # omnidirectional gl.glLoadIdentity() gl.glTranslatef(0, 0.0, -7.0) osd_line = \ "x: {0:<7.2f}".format(quat.x) + \ "y: {0:<7.2f}".format(quat.y) + \ "z: {0:<7.2f}".format(quat.z) + \ "flex: {0:>8}".format("{0:.2f}°".format(flex_angle)) self.drawText((-2, 1.9, 2), osd_line) gl.glPushMatrix() gl.glTranslatef(0, 2.0, 0.0) gl.glNormal3f(0.0, -1.0, 0.0) #TODO: Change back to quaternions # gl.glRotatef(-gyro_euler.x, 0, 1, 0) # gl.glRotatef(-gyro_euler.y, 1, 0, 0) # gl.glRotatef(gyro_euler.z*2, 0, 0, 1) # gl.glRotatef(quat.x, 0, 1, 0) gl.glRotatef(2*quat.y, 0, 0, 1) gl.glRotatef(quat.z, 1, 0, 0) gl.glRotatef(120, .5, .5, -.5) gl.glMaterialfv(gl.GL_FRONT_AND_BACK, gl.GL_AMBIENT_AND_DIFFUSE, blue) glu.gluCylinder(self.quad, 0.2, 0.15, 2, 10, 1) gl.glTranslatef(0, 0, 2) # Flex sensor: # Pitch, rotate around x-axis gl.glRotatef(flex_angle, 1.0, 0.0, 0.0) gl.glMaterialfv(gl.GL_FRONT_AND_BACK, gl.GL_AMBIENT_AND_DIFFUSE,dark_grey) glu.gluSphere(self.quad, 0.2, 6, 6) gl.glMaterialfv(gl.GL_FRONT_AND_BACK, gl.GL_AMBIENT_AND_DIFFUSE,blue) glu.gluCylinder(self.quad, 0.15, 0.125, 1.8, 9, 1) gl.glTranslatef(0, 0, 1.8) # ------------------- # First part of foot # ------------------- if self.pose == 0: pass elif self.pose == 1: gl.glRotatef(60.0, 1.0, 0.0, 0.0) gl.glMaterialfv(gl.GL_FRONT_AND_BACK, gl.GL_AMBIENT_AND_DIFFUSE,dark_grey) glu.gluSphere(self.quad, 0.2, 6, 6) gl.glBegin(gl.GL_QUADS) gl.glMaterialfv(gl.GL_FRONT_AND_BACK, gl.GL_AMBIENT_AND_DIFFUSE,grey) # foot - back gl.glNormal3f(0, -1, 0) gl.glVertex3f(-0.2, -0.1, 0.0) gl.glVertex3f(0.2, -0.1, 0.0) gl.glVertex3f(0.2, -0.1, 0.3) gl.glVertex3f(-0.2, -0.1, 0.3) # foot - right gl.glNormal3f(-1, 0, 0) gl.glVertex3f(-0.2, -0.1, 0.3) gl.glVertex3f(-0.2, 0.8, 0.3) gl.glVertex3f(-0.2, 0.8, 0.1) gl.glVertex3f(-0.2, -0.1, 0.0) # foot - left gl.glNormal3f(1, 0, 0) gl.glVertex3f(0.2, -0.1, 0.3) gl.glVertex3f(0.2, 0.8, 0.3) gl.glVertex3f(0.2, 0.8, 0.1) gl.glVertex3f(0.2, -0.1, 0.0) # foot - top gl.glNormal3f(0, 0, -1) gl.glVertex3f(-0.2, -0.1, 0.0) gl.glVertex3f(-0.2, 0.8, 0.1) gl.glVertex3f(0.2, 0.8, 0.1) gl.glVertex3f(0.2, -0.1, 0.0) # foot - front gl.glNormal3f(0, 1, 0) gl.glVertex3f(-0.2, -0.1, 0.3) gl.glVertex3f(-0.2, 0.8, 0.3) gl.glVertex3f(0.2, 0.8, 0.3) gl.glVertex3f(0.2, -0.1, 0.3) # foot - bottom gl.glNormal3f(0, 0, 1) gl.glVertex3f(-0.2, 0.8, 0.3) gl.glVertex3f(-0.2, 0.8, 0.1) gl.glVertex3f(0.2, 0.8, 0.1) gl.glVertex3f(0.2, 0.8, 0.3) gl.glEnd() gl.glTranslatef(0, 0.8, 0.1) # ------------------- # Second part of foot # ------------------- if self.pose == 0: pass elif self.pose == 1: gl.glRotatef(-60.0, 1.0, 0.0, 0.0) gl.glMaterialfv(gl.GL_FRONT_AND_BACK, gl.GL_AMBIENT_AND_DIFFUSE,dark_grey) glu.gluSphere(self.quad, 0.1, 6, 6) gl.glMaterialfv(gl.GL_FRONT_AND_BACK, gl.GL_AMBIENT_AND_DIFFUSE,grey) gl.glBegin(gl.GL_QUADS) # foot - back gl.glNormal3f(0, -1, 0) gl.glVertex3f(-0.2, 0.02, 0.0) gl.glVertex3f(0.2, 0.02, 0.0) gl.glVertex3f(0.2, 0.02, 0.2) gl.glVertex3f(-0.2, 0.02, 0.2) # foot - right gl.glNormal3f(-1, 0, 0) gl.glVertex3f(-0.2, 0.02, 0.2) gl.glVertex3f(-0.2, 0.4, 0.2) gl.glVertex3f(-0.2, 0.4, 0.1) gl.glVertex3f(-0.2, 0.02, 0.0) # foot - left gl.glNormal3f(1, 0, 0) gl.glVertex3f(0.2, 0.02, 0.2) gl.glVertex3f(0.2, 0.4, 0.2) gl.glVertex3f(0.2, 0.4, 0.1) gl.glVertex3f(0.2, 0.02, 0.0) # foot - top gl.glNormal3f(0, 0, -1) gl.glVertex3f(-0.2, 0.02, 0.0) gl.glVertex3f(-0.2, 0.4, 0.1) gl.glVertex3f(0.2, 0.4, 0.1) gl.glVertex3f(0.2, 0.02, 0.0) # foot - front gl.glNormal3f(0, 1, 0) gl.glVertex3f(-0.2, 0.02, 0.2) gl.glVertex3f(-0.2, 0.4, 0.2) gl.glVertex3f(0.2, 0.4, 0.2) gl.glVertex3f(0.2, 0.02, 0.2) # foot - bottom gl.glNormal3f(0, 0, 1) gl.glVertex3f(-0.2, 0.4, 0.2) gl.glVertex3f(-0.2, 0.4, 0.1) gl.glVertex3f(0.2, 0.4, 0.1) gl.glVertex3f(0.2, 0.4, 0.2) gl.glEnd() gl.glPopMatrix()
def visualisation_particle(self): #print('visualisation_particle') if len(self.list_of_solar_system) == 9: for k in range(len(self.list_of_solar_system)): sphere = glu.gluNewQuadric() gl.glPushMatrix() gl.glLightModelfv( gl.GL_LIGHT_MODEL_AMBIENT, self.list_of_solar_system[k].color) #цвет задаем gl.glMaterialfv(gl.GL_FRONT_AND_BACK, gl.GL_SPECULAR, self.list_of_solar_system[k].color) gl.glTranslatef(self.list_of_solar_system[k].x * 7, self.list_of_solar_system[k].y * 7, self.list_of_solar_system[k].z * 7) glu.gluQuadricDrawStyle(sphere, glu.GLU_FILL) if k == 0: glu.gluSphere(sphere, self.list_of_solar_system[k].m / 150000.0, 16, 16) if k == 1: glu.gluSphere(sphere, self.list_of_solar_system[k].m * 20, 16, 16) if k == 2: glu.gluSphere(sphere, self.list_of_solar_system[k].m * 2, 16, 16) if k == 3: glu.gluSphere(sphere, self.list_of_solar_system[k].m * 1.5, 16, 16) if k == 4: glu.gluSphere(sphere, self.list_of_solar_system[k].m * 7, 16, 16) if k == 5: glu.gluSphere(sphere, self.list_of_solar_system[k].m / 30, 16, 16) if k == 6: glu.gluSphere(sphere, self.list_of_solar_system[k].m / 12, 16, 16) if k == 7: glu.gluSphere(sphere, self.list_of_solar_system[k].m / 2.3, 16, 16) if k == 8: glu.gluSphere(sphere, self.list_of_solar_system[k].m / 3.2, 16, 16) gl.glTranslatef(-self.list_of_solar_system[k].x * 7, -self.list_of_solar_system[k].y * 7, -self.list_of_solar_system[k].z * 7) gl.glPopMatrix() glu.gluDeleteQuadric(sphere) self.update() else: #for l in self.general_list: #for k in range(len(l)): for k in range(len(self.list_of_particles)): sphere = glu.gluNewQuadric() gl.glPushMatrix() gl.glLightModelfv( gl.GL_LIGHT_MODEL_AMBIENT, self.list_of_particles[k].color) #цвет задаем gl.glMaterialfv(gl.GL_FRONT_AND_BACK, gl.GL_SPECULAR, self.list_of_particles[k].color) gl.glTranslatef(self.list_of_particles[k].x, self.list_of_particles[k].y, self.list_of_particles[k].z) glu.gluQuadricDrawStyle(sphere, glu.GLU_FILL) glu.gluSphere(sphere, self.list_of_particles[k].m / 100.0, 16, 16) gl.glTranslatef(-self.list_of_particles[k].x, -self.list_of_particles[k].y, -self.list_of_particles[k].z) gl.glPopMatrix() glu.gluDeleteQuadric(sphere) self.update()
def drawPointAndDirs(self, matrix, color=GREEN): try: xAxis = numpy.array([matrix[0][0], matrix[1][0], matrix[2][0]], 'float') yAxis = numpy.array([matrix[0][1], matrix[1][1], matrix[2][1]], 'float') zAxis = numpy.array([matrix[0][2], matrix[1][2], matrix[2][2]], 'float') center = numpy.array([matrix[0][3], matrix[1][3], matrix[2][3]], 'float') except: print('invalid matrix!') return None qobj = GLU.gluNewQuadric() sphere_radius = 3 cyl_height = 20 cyl_radius = 0.7 z_dir = numpy.array([0., 0., 1.], dtype='float64') genList = GL.glGenLists(1) GL.glNewList(genList, GL.GL_COMPILE) self.qglColor(color) GL.glPushMatrix() GL.glTranslatef(center[0], center[1], center[2]) GLU.gluSphere(qobj, sphere_radius, 20, 20) GL.glPopMatrix() #Cylinder along z, we want it to be allong xAxis #Solution: use the appropriate rotation matrix #We have to choose the right axis, perpendicular to both, and rotate by the appropriate angle, the angle between the 2 vectors self.qglColor(RED) rotation_dir = numpy.cross(z_dir, xAxis) angle = math.acos(z_dir.dot(xAxis)) * 180 / 3.14159 GL.glPushMatrix() GL.glTranslatef(center[0], center[1], center[2]) GL.glRotatef(angle, rotation_dir[0], rotation_dir[1], rotation_dir[2]) GLU.gluCylinder(qobj, cyl_radius, cyl_radius, cyl_height, 20, 20) GL.glPopMatrix() self.qglColor(GREEN) rotation_dir = numpy.cross(z_dir, yAxis) angle = math.acos(z_dir.dot(yAxis)) * 180 / 3.14159 GL.glPushMatrix() GL.glTranslatef(center[0], center[1], center[2]) GL.glRotatef(angle, rotation_dir[0], rotation_dir[1], rotation_dir[2]) GLU.gluCylinder(qobj, cyl_radius, cyl_radius, cyl_height, 20, 20) GL.glPopMatrix() self.qglColor(BLUE) rotation_dir = numpy.cross(z_dir, zAxis) angle = math.acos(z_dir.dot(zAxis)) * 180 / 3.14159 z_axis_center = center - zAxis * 10 GL.glPushMatrix() GL.glTranslatef(z_axis_center[0], z_axis_center[1], z_axis_center[2]) GL.glRotatef(angle, rotation_dir[0], rotation_dir[1], rotation_dir[2]) GLU.gluCylinder(qobj, cyl_radius, cyl_radius, cyl_height * 2, 20, 20) GL.glPopMatrix() GL.glEndList() return genList
def draw(self): global _particle_list sphere = glu.gluNewQuadric() #Solar system drawing if self.checkBox.isChecked(): #Sun gl.glPushMatrix() gl.glLightModelfv(gl.GL_LIGHT_MODEL_AMBIENT, _particle_list[0].color) gl.glMaterialfv(gl.GL_FRONT_AND_BACK, gl.GL_SPECULAR, _particle_list[0].color) gl.glTranslatef(_particle_list[0].x, _particle_list[0].y, _particle_list[0].z) glu.gluQuadricDrawStyle(sphere, glu.GLU_FILL) glu.gluSphere(sphere, _particle_list[0].m / 120000.0, 16, 16) gl.glTranslatef(-_particle_list[0].x, -_particle_list[0].y, -_particle_list[0].z) gl.glPopMatrix() #Mercury gl.glPushMatrix() gl.glLightModelfv(gl.GL_LIGHT_MODEL_AMBIENT, _particle_list[1].color) gl.glMaterialfv(gl.GL_FRONT_AND_BACK, gl.GL_SPECULAR, _particle_list[1].color) gl.glTranslatef(_particle_list[1].x, _particle_list[1].y, _particle_list[1].z) glu.gluQuadricDrawStyle(sphere, glu.GLU_FILL) glu.gluSphere(sphere, _particle_list[1].m * 10, 16, 16) gl.glTranslatef(-_particle_list[1].x, -_particle_list[1].y, -_particle_list[1].z) gl.glPopMatrix() #Venus gl.glPushMatrix() gl.glLightModelfv(gl.GL_LIGHT_MODEL_AMBIENT, _particle_list[2].color) gl.glMaterialfv(gl.GL_FRONT_AND_BACK, gl.GL_SPECULAR, _particle_list[2].color) gl.glTranslatef(_particle_list[2].x, _particle_list[2].y, _particle_list[2].z) glu.gluQuadricDrawStyle(sphere, glu.GLU_FILL) glu.gluSphere(sphere, _particle_list[2].m, 16, 16) gl.glTranslatef(-_particle_list[2].x, -_particle_list[2].y, -_particle_list[2].z) gl.glPopMatrix() #Earth gl.glPushMatrix() gl.glLightModelfv(gl.GL_LIGHT_MODEL_AMBIENT, _particle_list[3].color) gl.glMaterialfv(gl.GL_FRONT_AND_BACK, gl.GL_SPECULAR, _particle_list[3].color) gl.glTranslatef(_particle_list[3].x, _particle_list[3].y, _particle_list[3].z) glu.gluQuadricDrawStyle(sphere, glu.GLU_FILL) glu.gluSphere(sphere, _particle_list[3].m, 16, 16) gl.glTranslatef(-_particle_list[3].x, -_particle_list[3].y, -_particle_list[3].z) gl.glPopMatrix() #Mars gl.glPushMatrix() gl.glLightModelfv(gl.GL_LIGHT_MODEL_AMBIENT, _particle_list[4].color) gl.glMaterialfv(gl.GL_FRONT_AND_BACK, gl.GL_SPECULAR, _particle_list[4].color) gl.glTranslatef(_particle_list[4].x, _particle_list[4].y, _particle_list[4].z) glu.gluQuadricDrawStyle(sphere, glu.GLU_FILL) glu.gluSphere(sphere, _particle_list[4].m * 7, 16, 16) gl.glTranslatef(-_particle_list[4].x, -_particle_list[4].y, -_particle_list[4].z) gl.glPopMatrix() #Jupiter gl.glPushMatrix() gl.glLightModelfv(gl.GL_LIGHT_MODEL_AMBIENT, _particle_list[5].color) gl.glMaterialfv(gl.GL_FRONT_AND_BACK, gl.GL_SPECULAR, _particle_list[5].color) gl.glTranslatef(_particle_list[5].x, _particle_list[5].y, _particle_list[5].z) glu.gluQuadricDrawStyle(sphere, glu.GLU_FILL) glu.gluSphere(sphere, _particle_list[5].m / 30.0, 16, 16) gl.glTranslatef(-_particle_list[5].x, -_particle_list[5].y, -_particle_list[5].z) gl.glPopMatrix() #Saturn gl.glPushMatrix() gl.glLightModelfv(gl.GL_LIGHT_MODEL_AMBIENT, _particle_list[6].color) gl.glMaterialfv(gl.GL_FRONT_AND_BACK, gl.GL_SPECULAR, _particle_list[6].color) gl.glTranslatef(_particle_list[6].x, _particle_list[6].y, _particle_list[6].z) glu.gluQuadricDrawStyle(sphere, glu.GLU_FILL) glu.gluSphere(sphere, _particle_list[6].m / 12.0, 16, 16) gl.glTranslatef(-_particle_list[6].x, -_particle_list[6].y, -_particle_list[6].z) gl.glPopMatrix() #Uran gl.glPushMatrix() gl.glLightModelfv(gl.GL_LIGHT_MODEL_AMBIENT, _particle_list[7].color) gl.glMaterialfv(gl.GL_FRONT_AND_BACK, gl.GL_SPECULAR, _particle_list[7].color) gl.glTranslatef(_particle_list[7].x, _particle_list[7].y, _particle_list[7].z) glu.gluQuadricDrawStyle(sphere, glu.GLU_FILL) glu.gluSphere(sphere, _particle_list[7].m / 2.3, 16, 16) gl.glTranslatef(-_particle_list[7].x, -_particle_list[7].y, -_particle_list[7].z) gl.glPopMatrix() #Neptune gl.glPushMatrix() gl.glLightModelfv(gl.GL_LIGHT_MODEL_AMBIENT, _particle_list[8].color) gl.glMaterialfv(gl.GL_FRONT_AND_BACK, gl.GL_SPECULAR, _particle_list[8].color) gl.glTranslatef(_particle_list[8].x, _particle_list[8].y, _particle_list[8].z) glu.gluQuadricDrawStyle(sphere, glu.GLU_FILL) glu.gluSphere(sphere, _particle_list[8].m / 3.2, 16, 16) gl.glTranslatef(-_particle_list[8].x, -_particle_list[8].y, -_particle_list[8].z) gl.glPopMatrix() #other else: _particle_list = [p for p in _particle_list if p.alive == True] for i in range(len(_particle_list)): if ((_particle_list[i].x > -2000) & (_particle_list[i].x < 2000) & (_particle_list[i].y > -2000) & (_particle_list[i].y < 2000) & (_particle_list[i].z > -2000) & (_particle_list[i].z < 2000)): gl.glPushMatrix() gl.glLightModelfv(gl.GL_LIGHT_MODEL_AMBIENT, _particle_list[i].color) gl.glMaterialfv(gl.GL_FRONT_AND_BACK, gl.GL_SPECULAR, _particle_list[i].color) gl.glTranslatef(_particle_list[i].x, _particle_list[i].y, _particle_list[i].z) glu.gluQuadricDrawStyle(sphere, glu.GLU_FILL) glu.gluSphere(sphere, _particle_list[i].m / 100.0, 16, 16) gl.glTranslatef(-_particle_list[i].x, -_particle_list[i].y, -_particle_list[i].z) gl.glPopMatrix() else: _particle_list[i].alive = False glu.gluDeleteQuadric(sphere) glu.gluDeleteQuadric(sphere) label = str(len(_particle_list)) self.label_12.setText(label)
def applyShape(self): Q = GLU.gluNewQuadric() GLU.gluQuadricNormals(Q, self.GL(self._normals)) GLU.gluQuadricTexture(Q, self.GL(self._texture)) GLU.gluSphere(Q, self._radius, self._subdiv, self._subdiv / 2) GLU.gluDeleteQuadric(Q)
def drawGLScene(): GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT) for object in sceneObjects.values(): # apply camera transform for > 3 dimensions iterateCamerasD() # apply 3D camera transform iterateCamerasD() # first we need to project geometry from > 3 dimensions to 3D projectFromHigherDimensions(object) vertexBuffer = object.getVertexData3D() edgesData = object.getEdgesData() #print "edges:", edgesData #print "vertex:", vertexBuffer if vertexBuffer != None: if renderingMode == RENDER_MODE_1: # in this rendering mode we represent edges as # cylinders and vertices as spheres if len(vertexBuffer) == 0: return # draw vertices (spheres) quadObj1 = GLU.gluNewQuadric() GLU.gluQuadricDrawStyle(quadObj1, GLU.GLU_FILL) #GLU.gluQuadricDrawStyle(quadObj1, GL.GL_WIREFRAME) for vertex in vertexBuffer.values(): GL.glLoadIdentity() iterateCameras3D() GL.glTranslatef(vertex[0], vertex[1], vertex[2]) GLU.gluSphere(quadObj1, 0.1, 15, 15) # draw faces GL.glBegin(GL.GL_TRIANGLES) GL.glEnd() # draw edges (cylinders) # initially all the cylinders must points towards the positive z # direction quadObj2 = GLU.gluNewQuadric() GLU.gluQuadricDrawStyle(quadObj2, GLU.GLU_FILL) for edge in edgesData.values(): GL.glLoadIdentity() iterateCameras3D() #modelview = GL.glGetDouble(GL.GL_MODELVIEW_MATRIX) #print "before" #dumpOGLMatrix(modelview) # get 'from' and 'to' vectors for the edge x0, y0, z0 = vertexBuffer[int(edge[0])] x1, y1, z1 = vertexBuffer[int(edge[1])] # position a cylinder GL.glTranslatef(x0, y0, z0) # calculate edge length length = math.sqrt( (x1 - x0) ** 2 + (y1 - y0) ** 2 + (z1 - z0) ** 2 ) tmp = Vector([x1 - x0, y1 - y0, z1 - z0]) # get the cross products if (negZ == tmp.inv().normalize()): axis1 = Vector([0,1.0,0]) else: axis1 = Matrix.crossProduct3D(negZ, tmp.normalize()) axis2 = Matrix.crossProduct3D(negZ, axis1) axis3 = posZ # get the angle we need to rotate to cos_angle = posZ.dotProduct(tmp.normalize()) angle = math.degrees(math.acos(cos_angle)) # calculate the transformation axis1.normalizeSelf() axis2.normalizeSelf() # we need an inverse matrix and we know that the transpose of the rotation matrix is equal to its inverse a1, a2, a3 = axis1.getComponents(), axis2.getComponents(), axis3.getComponents() v1 = [ a1[0], a2[0], a3[0], 0 ] v2 = [ a1[1], a2[1], a3[1], 0 ] v3 = [ a1[2], a2[2], a3[2], 0 ] axis1, axis2, axis3 = v1, v2, v3 # feed to openGL rotTransform = createOGLMatrixFromLists(axis1, axis2, axis3) rotTransform.extend(homogenousVec) GL.glMultMatrixf(rotTransform) # rotate a cylinder around axis1 GL.glRotatef(angle, 1.0, 0.0, 0.0) # draw a cylinder GLU.gluCylinder(quadObj2, 0.05, 0.05, length, 12, 12) GLU.gluDeleteQuadric(quadObj1) GLU.gluDeleteQuadric(quadObj2) GL.glUseProgram(shaders[0]) #renderString(100, 100, GLUT.GLUT_BITMAP_8_BY_13, "Hello World", (1.0, 0.0, 0.0)) GLUT.glutSwapBuffers()
def prepare(self): """ Build display lists """ # for efficiency, allow skipping the TRANSPARENT render if opaque self._has_transparent = (self.getstyle('colour')[3] < 1) # consts v3 = glVertex3f r = self.getgeom('stick',0.05) # feet dl = self.newpiece("footL") self.dlists["footR"] = dl # foot is same on both legs glNewList(dl,GL_COMPILE) x = self.getgeom('footX',0.1) glBegin(GL_TRIANGLES) v3(0,0,0) v3(x,-x/2.0,0) v3(x,x/2.0,0) glEnd() glEndList() # shins dl = self.newpiece("shinL") self.dlists["shinR"] = dl glNewList(dl,GL_COMPILE) z = self.getgeom('shinZ',0.25) GLU.gluCylinder(self.quadric,r,r,z,3,2) glEndList() # thighs dl = self.newpiece("thighL") self.dlists["thighR"] = dl glNewList(dl,GL_COMPILE) z = self.getgeom('thighZ',0.25) GLU.gluCylinder(self.quadric,r,r,z,4,2) glEndList() #hips dl = self.newpiece("hips") glNewList(dl,GL_COMPILE) w = self.getgeom('hipY',0.1) glBegin(GL_LINES) # simple line across hip joint v3(0,-w,0) v3(0,w,0) glEnd() glEndList() #torso dl = self.newpiece("torso") glNewList(dl,GL_COMPILE) z = self.getgeom('torsoZ',0.5) w = self.getgeom('shoulderY',0.2) GLU.gluCylinder(self.quadric,r,r,z,6,2) glBegin(GL_LINES) # line across shoulders v3(0,-w,z) v3(0,w,z) glEnd() glEndList() # upper arms dl = self.newpiece("upperarmL") self.dlists['upperarmR'] = dl z = self.getgeom('upperarmZ',0.2) glNewList(dl,GL_COMPILE) GLU.gluCylinder(self.quadric,r,r,z,4,2) glEndList() # forearms dl = self.newpiece("forearmL") self.dlists['forearmR'] = dl z = self.getgeom('forearmZ',0.2) glNewList(dl,GL_COMPILE) GLU.gluCylinder(self.quadric,r,r,z,3,2) glEndList() # hands dl = self.newpiece("handL") self.dlists["handR"] = dl # hand is same on both arms glNewList(dl,GL_COMPILE) z = self.getgeom('handZ',0.1) glBegin(GL_TRIANGLES) v3(0,0,0) v3(0,-z/2.0,-z) v3(0,z/2.0,-z) glEnd() glEndList() # head dl = self.newpiece("head") z = self.getgeom('headZ',0.2) glNewList(dl,GL_COMPILE) GLU.gluCylinder(self.quadric,r,r,z,4,1) # neck glTranslate(0,0,z) # centre of head GLU.gluSphere(self.quadric,2*r,8,6) glTranslate(2*r,0,0) # to nose glColor(1,0,0,1) # red nose for testing GLU.gluSphere(self.quadric,r*0.5,6,4) glEndList()