def draw_light_source(self): glColor3f(1.0, 1.0, 1.0) # 15 is radius of the sphere that light source around self.light_source_position = [ 15 * sin(self.theta_light_angle * pi / 180) * sin(self.phi_light_angle * pi / 180), 15 * cos(self.theta_light_angle * pi / 180), 15 * sin(self.theta_light_angle * pi / 180) * cos(self.phi_light_angle * pi / 180) ] glTranslate(self.light_source_position[0], self.light_source_position[1], self.light_source_position[2]) # set light source position glLightfv(GL_LIGHT0, GL_POSITION, [*self.light_source_position, 1]) # set light color glLightfv(GL_LIGHT0, GL_DIFFUSE, self.light_color) # set light intensity glLightfv(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 0.7) glLightfv(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0.0001) # turning the rays of light so that the back of the ball is shaded gl_q = gluNewQuadric() gluQuadricOrientation(gl_q, GLU_INSIDE) # draw visual envelope (ball) gluSphere(gl_q, 1, 20, 20) glLoadIdentity()
def draw(self): '''Draw the svg on screen''' with gx_matrix: glTranslate(self.x, self.y, 0) glScale(self._scale_x, self._scale_y, 1) glTranslate(self.anchor_x, self.anchor_y, 0) self.svg_object.draw()
def paint(self): glPushMatrix() position = self.player.getPosition() glTranslate(position.x, position.y, position.z) glRotate(self.player.orientation.y, 0.0, 1.0, 0.0) # Slide back because the pyramid below is centered at 0.5, 0, 0.5 # instead of at the origin. Without this it rotates around its corner # instead of around its center. glTranslate(-0.5, 0, -0.5) glColor(1.0, 1.0, 1.0) glBegin(GL_TRIANGLES) glVertex3f(0.5, 0.0, 0.5) glVertex3f(0.0, 1.0, 0.0) glVertex3f(1.0, 1.0, 0.0) glVertex3f(0.5, 0.0, 0.5) glVertex3f(1.0, 1.0, 0.0) glVertex3f(1.0, 1.0, 1.0) glVertex3f(0.5, 0.0, 0.5) glVertex3f(1.0, 1.0, 1.0) glVertex3f(0.0, 1.0, 1.0) glVertex3f(0.5, 0.0, 0.5) glVertex3f(0.0, 1.0, 1.0) glVertex3f(0.0, 1.0, 0.0) glEnd() glPopMatrix()
def paint(self): if self.quad is None: self.quad = gluNewQuadric() glPushMatrix() glColor(self.color.red, self.color.green, self.color.blue) glTranslate(self.center.x, self.center.y, self.center.z) gluSphere(self.quad, self.radius, 25, 25) glPopMatrix()
def render(self): glMatrixMode(GL_MODELVIEW) glLoadIdentity() glRotate(self.pitch, 1, 0, 0) glRotate(self.yaw, 0, 1, 0) x, y, z = self.position glTranslate(-x, -y, -z)
def draw(self): super(MTTextArea, self).draw_background() glPushMatrix() glTranslate(self.x, self.y+self.height,0) for line_num in xrange(len(self.lines)): self.line_labels[line_num].draw() if self.edit_line == line_num and self.is_active_input: self.draw_cursor() glTranslate(0,-(self.line_height+self.line_spacing),0) glPopMatrix()
def translate(*args): ''' Move in (x,y,z) space. syntax: translate(x,y,z) translate([x,y,z]) ''' tx, ty, tz = Vector.helper(*args).data() glTranslate(tx, ty, tz)
def render_me(self): glColor3f(1, 1, 1) glLoadIdentity() glTranslate(self.position[0], self.position[1], self.position[2]) glutSolidCube(self.side_size) glLoadIdentity()
def paint(self): v = self.player.getPosition() o = self.player.orientation glRotate(o.x, 1.0, 0.0, 0.0) glRotate(o.y, 0.0, 1.0, 0.0) glRotate(o.z, 0.0, 0.0, 1.0) # XXX Put the camera somewhere in the middle-ish of the player model. # This is a wild guess for now, camera position data should be available # from the model at some later point. glTranslate(-v.x - 0.5, -v.y - 2, -v.z - 0.5)
def teleport_player(self, add_height, speed): if self.steps == 1: height = add_height * self.degree / 90 glTranslate(0, 0, height) if self.degree + speed >= 90: self.degree = 90 self.steps = 0 return True else: self.degree += speed return False
def render(self): glColor(0, 1, 1) glPushMatrix() glTranslate(*self.p) if self.skel.m < 10.0: glutSolidSphere(0.02, 4, 2) else: glutSolidSphere(0.06, 4, 2) glPopMatrix()
def render(self): x, y, z = (self.boxHalfExtents.x, self.boxHalfExtents.y, self.boxHalfExtents.z) o = self.motion.getWorldTransform().getOrigin() glColor(0, 0, 255) glTranslate(o.x, o.y, o.z) glBegin(GL_TRIANGLE_STRIP) glVertex(-x, y, -z) glVertex(x, y, -z) glVertex(-x, y, z) glVertex(x, y, z) glEnd()
def drawLinearArrows(longScale): glCallList(drawing_globals.linearArrowList) newPos = drawing_globals.halfHeight*longScale glPushMatrix() glTranslate(0.0, 0.0, -newPos) glCallList(drawing_globals.linearArrowList) glPopMatrix() glPushMatrix() glTranslate(0.0, 0.0, newPos -2.0*drawing_globals.halfEdge) glCallList(drawing_globals.linearArrowList) glPopMatrix() return
def render(self): o = self.motion.getWorldTransform().getOrigin() glColor(0, 0, 255) glTranslate(o.x, o.y, o.z) glBegin(GL_TRIANGLES) for i in range(len(self.groundIndices)): base = self.groundIndices[i] * 3 x = self.groundVertices[base] y = self.groundVertices[base + 1] z = self.groundVertices[base + 2] glVertex(x, y, z) glEnd()
def gl_apply_inverse(transformation): # OpenGL uses an algebra with row vectors instead of column vectors. if not isinstance(transformation, Rotation): glTranslate(*(-transformation.t)) else: a = numpy.zeros((4,4), float) if isinstance(transformation, Translation): a[3,:3] = numpy.dot(-transformation.t, transformation.r) if isinstance(transformation, Rotation): a[:3,:3] = transformation.r a[3,3] = 1 glMultMatrixf(a)
def gl_apply_inverse(transformation): # OpenGL uses an algebra with row vectors instead of column vectors. if not isinstance(transformation, Rotation): glTranslate(*(-transformation.t)) else: a = numpy.zeros((4, 4), float) if isinstance(transformation, Translation): a[3, :3] = numpy.dot(-transformation.t, transformation.r) if isinstance(transformation, Rotation): a[:3, :3] = transformation.r a[3, 3] = 1 glMultMatrixf(a)
def drawLinearArrows(longScale): glCallList(drawing_globals.linearArrowList) newPos = drawing_globals.halfHeight * longScale glPushMatrix() glTranslate(0.0, 0.0, -newPos) glCallList(drawing_globals.linearArrowList) glPopMatrix() glPushMatrix() glTranslate(0.0, 0.0, newPos - 2.0 * drawing_globals.halfEdge) glCallList(drawing_globals.linearArrowList) glPopMatrix() return
def applyTransformation(): global position """ This function is used to apply the camera transformation to the game. it must be used only before calling every object in the game. it only do transformation and rotation. so scaling. """ glTranslate(-position.x, -position.y, -position.z) glRotatef(rotation.x, 1, 0, 0) glRotatef(rotation.y, 0, 1, 0) glRotatef(rotation.z, 0, 0, 1)
def drawaxes(scale, point, color=black, coloraxes=False, dashEnabled=False): """ Draw axes. @note: used for both origin axes and point of view axes. @see: drawOriginAsSmallAxis (related code) """ n = scale glPushMatrix() glTranslate(point[0], point[1], point[2]) glDisable(GL_LIGHTING) if dashEnabled: #ninad060921 Note that we will only support dotted origin axis #(hidden lines) but not POV axis. (as it could be annoying) glLineStipple(5, 0xAAAA) glEnable(GL_LINE_STIPPLE) glDisable(GL_DEPTH_TEST) glColor3fv(color) if coloraxes: glColor3fv(red) glBegin(GL_LINES) glVertex(n, 0, 0) glVertex(-n, 0, 0) if coloraxes: glColor3fv(darkgreen) glVertex(0, n, 0) glVertex(0, -n, 0) if coloraxes: glColor3fv(blue) glVertex(0, 0, n) glVertex(0, 0, -n) glEnd() if dashEnabled: glDisable(GL_LINE_STIPPLE) glEnable(GL_DEPTH_TEST) glEnable(GL_LIGHTING) glPopMatrix() return
def rotate_before_swap(self): x_center, y_center = self.previous[0] x_diff, y_diff = 0, 0 if self.move_direction == 'up': y_diff = -2 elif self.move_direction == 'down': y_center += 2 y_diff = 2 elif self.move_direction == 'left': x_diff = -2 elif self.move_direction == 'right': x_center += 2 x_diff = 2 if self.steps == 1: glTranslate(x_center, -y_center, 0) glRotate(90, y_diff, x_diff, 0) glTranslate(-x_center, y_center, 0) return False if self.degree + rotating_speed >= 90: self.degree = 90 else: self.degree += rotating_speed glTranslate(x_center, -y_center, 0) glRotate(self.degree, y_diff, x_diff, 0) glTranslate(-x_center, y_center, 0) if self.degree == 90: return True return False
def drawaxes(scale, point, color = black, coloraxes = False, dashEnabled = False): """ Draw axes. @note: used for both origin axes and point of view axes. @see: drawOriginAsSmallAxis (related code) """ n = scale glPushMatrix() glTranslate(point[0], point[1], point[2]) glDisable(GL_LIGHTING) if dashEnabled: #ninad060921 Note that we will only support dotted origin axis #(hidden lines) but not POV axis. (as it could be annoying) glLineStipple(5, 0xAAAA) glEnable(GL_LINE_STIPPLE) glDisable(GL_DEPTH_TEST) glColor3fv(color) if coloraxes: glColor3fv(red) glBegin(GL_LINES) glVertex( n, 0, 0) glVertex(-n, 0, 0) if coloraxes: glColor3fv(darkgreen) glVertex(0, n, 0) glVertex(0, -n, 0) if coloraxes: glColor3fv(blue) glVertex(0, 0, n) glVertex(0, 0, -n) glEnd() if dashEnabled: glDisable(GL_LINE_STIPPLE) glEnable(GL_DEPTH_TEST) glEnable(GL_LIGHTING) glPopMatrix() return
def applyTransformation(screenWidthRatio, screenHeightRatio): """ This function is used to apply the camera transformation to the game. it must be used only before calling every object in the game. it only do transformation and rotation. so scaling. """ global position, __swr, __shr __swr, __shr = screenWidthRatio, screenHeightRatio glTranslate(-position.x, -position.y, -position.z) glRotatef(rotation.x, 1, 0, 0) glRotatef(rotation.y, 0, 1, 0) glRotatef(rotation.z, 0, 0, 1)
def draw_x_switch(position: Tuple[int, int], color: Tuple[float, float, float]): glPushMatrix() glTranslate(0.5 + position[0], -0.5 - position[1], 0) height = 0.15 # glColor(Cube.colors['light_blue']) if len(Draw.x_switch_pt) == 0: width = 0.15 size = 0.25 Draw.x_switch_pt = [(-width, 0), (-(width + size), -size), (-(width + size), -(width + size)), (-size, -(width + size)), (0, -width), (size, -(width + size)), (width + size, -(width + size)), (width + size, -size), (width, 0), (width + size, size), (width + size, width + size), (size, width + size), (0, width), (-size, width + size), (-(width + size), width + size), (-(width + size), size)] glColor(color) glBegin(GL_QUAD_STRIP) for x, y in Draw.x_switch_pt: glVertex3f(x, y, height) glVertex3f(x, y, 0) glVertex3f(Draw.x_switch_pt[0][0], Draw.x_switch_pt[0][1], height) glVertex3f(Draw.x_switch_pt[0][0], Draw.x_switch_pt[0][1], 0) glEnd() glLineWidth(3) glColor(Draw.colors['gray']) # -0.2, 0 -> -0.8, 0.6 glBegin(GL_POLYGON) for x, y in Draw.x_switch_pt: glVertex3f(x, y, height) glEnd() glLineWidth(1) glPopMatrix()
def rotate_player(self): current = self.player [x_diff, y_diff] = current[0] - self.previous[0] x_center = self.previous[0, 0] + x_diff if x_diff > 0 else self.previous[0, 0] y_center = self.previous[0, 1] + y_diff if y_diff > 0 else self.previous[0, 1] if self.degree + rotating_speed >= 90: self.degree = 90 if self.check_merge(self.player): self.player[[0, 1], :] = self.player[[1, 0], :] else: self.degree += rotating_speed if (current - self.previous).tolist() != [[0, 0], [0, 0]]: glTranslate(x_center, -y_center, 0) glRotate(self.degree, y_diff, x_diff, 0) glTranslate(-x_center, y_center, 0) if self.degree == 90: return True return False
def Draw_other(mode, glpane, superclass): # called by testmode.Draw_other glPushMatrix() try: drawtest0( glpane ) # this does all our special drawing, and sometimes puts some of it into a display list # [no point in putting the main model drawing into it, for now -- but when we can know when to inval, there might be] except: print_compact_traceback("exc ignored: ") glPopMatrix( ) # it turns out this is needed, if drawtest0 does glTranslate, or our coords are messed up when glselect code # [not sure what this next comment is about:] # makes us draw twice! noticed on g4, should happen on g5 too, did it happen but less?? if env.prefs.get("A9 devel/testdraw/super.Draw last?", False): # revised prefs key and default, 070404 # NOTE: after 090310 refactoring of Draw API, this is happening at the wrong time # (in Draw_other rather than in Draw_model) which may make it slower and/or have bugs. # [bruce 090310 comment] glPushMatrix() if 1: superclass.Draw_model( mode) # needed for region selection's separate xor-drawing; # I suspect this is slower than the other case. Does it draw more than once (for glselect) or something like that? ####@@@@ else: # region selection's drawing [later: xor mode, i guess] won't work in this case, though its selection op itself will work glpane.part.draw(glpane) # highlighting works fine on this copy... if 0 and 'draw copy2 of model': glTranslate(5, 0, 0) glpane.part.draw( glpane ) # but not on this one - i see why it doesn't draw it there, but why not complain about not finding it? glPopMatrix() # draw invisible stuff #e (we'll want to split this into stages for more than one kind of such stuff; # later, to go through a "rendering pass widget expr") for func in glpane._testmode_stuff_2: # colors, for translucent stuff func() for func in glpane._testmode_stuff_3: # depths, for translucent stuff (matters if they are highlightable) func() for func in glpane._testmode_stuff: # depths, for invisible stuff func() return
def draw_tool_chain(self): font_scale = 0.00015 glMatrixMode(GL_MODELVIEW) glColor(0, 0, 0) glLineWidth(5) glBegin(GL_LINE_STRIP) for point in self.points: glVertex3f(point[0], point[1], 0.0) glEnd() glColor(1, 1, 1) glLineWidth(1) glBegin(GL_LINE_STRIP) for point in self.points: glVertex3f(point[0], point[1], 0.0) glEnd() if len(self.model_objects) == 4 and \ self.model_objects[0] == self.model_objects[3]: num = 3 else: num = 4 glColor(0, 0, 0) glLineWidth(9) for index, point in enumerate(self.points[:num]): glPushMatrix() glTranslate(point[0], point[1], 0.0) glScaled(font_scale, font_scale, 1) glRectf(-10, -20, 114, 130) glPopMatrix() glColor(1, 1, 1) glLineWidth(1) for index, point in enumerate(self.points[:num]): glPushMatrix() glTranslate(point[0], point[1], 0.0) glScaled(font_scale, font_scale, 1) glutStrokeCharacter(GLUT_STROKE_MONO_ROMAN, ord(str(index+1))) glPopMatrix()
def Draw(mode, glpane, super): # called by testmode.Draw init_glpane_vars(glpane) if env.prefs.get("A9 devel/testdraw/super.Draw first?", True): #070404 glPushMatrix() #k needed?? super.Draw(mode) glPopMatrix() # glpane.part.draw(glpane) # this doesn't draw the model in any different place as when done below... [061211] glPushMatrix() try: drawtest0(glpane) # this does all our special drawing, and sometimes puts some of it into a display list # [no point in putting the main model drawing into it, for now -- but when we can know when to inval, there might be] except: print_compact_traceback("exc ignored: ") glPopMatrix() # it turns out this is needed, if drawtest0 does glTranslate, or our coords are messed up when glselect code # [not sure what this next comment is about:] # makes us draw twice! noticed on g4, should happen on g5 too, did it happen but less?? if env.prefs.get("A9 devel/testdraw/super.Draw last?", False): # revised prefs key and default, 070404 glPushMatrix() if 1: super.Draw(mode) # needed for region selection's separate xor-drawing; # I suspect this is slower than the other case. Does it draw more than once (for glselect) or something like that? ####@@@@ else: # region selection's drawing [later: xor mode, i guess] won't work in this case, though its selection op itself will work glpane.part.draw(glpane) # highlighting works fine on this copy... if 0 and 'draw copy2 of model': glTranslate(5,0,0) glpane.part.draw(glpane) # but not on this one - i see why it doesn't draw it there, but why not complain about not finding it? glPopMatrix() # draw invisible stuff #e (we'll want to split this into stages for more than one kind of such stuff; later, to go through a "rendering pass widget expr") for func in glpane._testmode_stuff_2: # colors, for translucent stuff func() for func in glpane._testmode_stuff_3: # depths, for translucent stuff (matters if they are highlightable) func() for func in glpane._testmode_stuff: # depths, for invisible stuff func() return
def draw_tool_chain(self): font_scale = 0.00015 glMatrixMode(GL_MODELVIEW) glColor(0, 0, 0) glLineWidth(5) glBegin(GL_LINE_STRIP) for point in self.points: glVertex3f(point[0], point[1], 0.0) glEnd() glColor(1, 1, 1) glLineWidth(1) glBegin(GL_LINE_STRIP) for point in self.points: glVertex3f(point[0], point[1], 0.0) glEnd() if len(self.model_objects) == 4 and \ self.model_objects[0] == self.model_objects[3]: num = 3 else: num = 4 glColor(0, 0, 0) glLineWidth(9) for index, point in enumerate(self.points[:num]): glPushMatrix() glTranslate(point[0], point[1], 0.0) glScaled(font_scale, font_scale, 1) glRectf(-10, -20, 114, 130) glPopMatrix() glColor(1, 1, 1) glLineWidth(1) for index, point in enumerate(self.points[:num]): glPushMatrix() glTranslate(point[0], point[1], 0.0) glScaled(font_scale, font_scale, 1) glutStrokeCharacter(GLUT_STROKE_MONO_ROMAN, ord(str(index + 1))) glPopMatrix()
def main(): pygame.init() pygame.display.set_mode((1024, 768), pygame.locals.OPENGL | pygame.locals.DOUBLEBUF) glEnable(GL_DEPTH_TEST) gluPerspective(60.0, 640.0 / 480.0, 0.5, 1000.0) glTranslate(0, -15, -60) objects = [] dynamicsWorld = DiscreteDynamicsWorld() objects.append(Ground()) objects.append(Ball(Vector3(1, 10, 0), (255, 0, 0))) objects.append(Ball(Vector3(0, 20, 1), (0, 255, 0))) objects.append(Ball(Vector3(0, 30, 1), (255, 255, 0))) objects.append(Ball(Vector3(0, 40, 1), (0, 255, 255, 0))) for o in objects: dynamicsWorld.addRigidBody(o.body) simulate(dynamicsWorld, objects)
def main(): pygame.init() pygame.display.set_mode( (1024, 768), pygame.locals.OPENGL | pygame.locals.DOUBLEBUF) glEnable(GL_DEPTH_TEST) gluPerspective(60.0, 640.0 / 480.0, 0.5, 1000.0) glTranslate(0, -15, -60) objects = [] dynamicsWorld = DiscreteDynamicsWorld() debug = DebugDraw() dynamicsWorld.setDebugDrawer(debug) b1 = Ball(Vector3(-30, 0, 0), (255, 0, 0)) b1.body.applyCentralImpulse(Vector3(30, 40, 0)) objects.append(b1) b2 = Ball(Vector3(+30, 0, 0), (0, 255, 0)) b2.body.applyCentralImpulse(Vector3(-30, 40, 0)) objects.append(b2) for o in objects: dynamicsWorld.addRigidBody(o.body) while True: step(dynamicsWorld) debug.reset() dynamicsWorld.debugDrawWorld() glBegin(GL_LINES) for line in debug.lines: glColor(*line[6:]) glVertex(*line[:3]) glVertex(*line[3:6]) if debug.contacts: print 'Contact!', debug.contacts glEnd() render(objects)
def main(): pygame.init() pygame.display.set_mode((1024, 768), pygame.locals.OPENGL | pygame.locals.DOUBLEBUF) glEnable(GL_DEPTH_TEST) gluPerspective(60.0, 640.0 / 480.0, 0.5, 1000.0) glTranslate(0, -15, -60) objects = [] dynamicsWorld = DiscreteDynamicsWorld() debug = DebugDraw() dynamicsWorld.setDebugDrawer(debug) b1 = Ball(Vector3(-30, 0, 0), (255, 0, 0)) b1.body.applyCentralImpulse(Vector3(30, 40, 0)) objects.append(b1) b2 = Ball(Vector3(+30, 0, 0), (0, 255, 0)) b2.body.applyCentralImpulse(Vector3(-30, 40, 0)) objects.append(b2) for o in objects: dynamicsWorld.addRigidBody(o.body) while True: step(dynamicsWorld) debug.reset() dynamicsWorld.debugDrawWorld() glBegin(GL_LINES) for line in debug.lines: glColor(*line[6:]) glVertex(*line[:3]) glVertex(*line[3:6]) if debug.contacts: print 'Contact!', debug.contacts glEnd() render(objects)
def drawCube(self): glPushMatrix() glLoadIdentity() glTranslate(0.0, 0.0, -50.0) glScale(20.0, 20.0, 20.0) glRotate(self.yRotDeg, 0.2, 1.0, 0.3) glTranslate(-0.5, -0.5, -0.5) glColor3f(1, 1, 1) glBegin(GL_LINES) glVertex3f(0., 0., 0.) glVertex3f(1., 0., 0.) glVertex3f(1., 0., 0.) glVertex3f(1., 1., 0.) glVertex3f(1., 1., 0.) glVertex3f(0., 1., 0.) glVertex3f(0., 1., 0.) glVertex3f(0., 0., 0.) glVertex3f(0., 0., 1.) glVertex3f(1., 0., 1.) glVertex3f(1., 0., 1.) glVertex3f(1., 1., 1.) glVertex3f(1., 1., 1.) glVertex3f(0., 1., 1.) glVertex3f(0., 1., 1.) glVertex3f(0., 0., 1.) glVertex3f(0., 0., 0.) glVertex3f(0., 0., 1.) glVertex3f(1., 0., 0.) glVertex3f(1., 0., 1.) glVertex3f(1., 1., 0.) glVertex3f(1., 1., 1.) glVertex3f(0., 1., 0.) glVertex3f(0., 1., 1.) glEnd() glPopMatrix()
def main(): pygame.init() pygame.display.set_mode( (1024, 768), pygame.locals.OPENGL | pygame.locals.DOUBLEBUF) glEnable(GL_DEPTH_TEST) gluPerspective(60.0, 640.0 / 480.0, 0.5, 1000.0) glTranslate(0, -15, -60) objects = [] dynamicsWorld = DiscreteDynamicsWorld() objects.append(Ground()) objects.append(Ball(Vector3(1, 10, 0), (255, 0, 0))) objects.append(Ball(Vector3(0, 20, 1), (0, 255, 0))) objects.append(Ball(Vector3(0, 30, 1), (255, 255, 0))) objects.append(Ball(Vector3(0, 40, 1), (0, 255, 255, 0))) for o in objects: dynamicsWorld.addRigidBody(o.body) simulate(dynamicsWorld, objects)
def draw_round_switch(position: Tuple[int, int], color: Tuple[float, float, float]): glPushMatrix() glTranslate(0.5 + position[0], -0.5 - position[1], 0) glColor(color) radius = 0.4 height = 0.15 angle = 0.0 angle_stepsize = 0.1 # glColor(Cube.colors['light_blue']) glBegin(GL_QUAD_STRIP) while angle < 2 * pi: x = radius * cos(angle) y = radius * sin(angle) glVertex3f(x, y, height - 0.01) glVertex3f(x, y, 0.0) angle += angle_stepsize glVertex3f(radius, 0.0, height - 0.01) glVertex3f(radius, 0.0, 0.0) glEnd() # glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) glLineWidth(3) glColor(Draw.colors['gray']) glBegin(GL_POLYGON) angle = 0.0 while angle < 2 * pi: x = radius * cos(angle) y = radius * sin(angle) glVertex3f(x, y, height) angle += angle_stepsize glVertex3f(radius, 0.0, height) glEnd() glLineWidth(1) glPopMatrix()
def translate(self, x, y, z): glTranslate(x, y, z)
def test_pyrex_opengl(test_type): # not tested since major refactoring try: print_compact_stack("selectMode Draw: " )### ### BUG: if import quux fails, we get into some sort of infinite ### loop of Draw calls. [bruce 070917 comment] #self.w.win_update() ## sys.path.append("./experimental/pyrex-opengl") # no longer ##needed here -- always done in drawer.py binPath = os.path.normpath(os.path.dirname( os.path.abspath(sys.argv[0])) + '/../bin') if binPath not in sys.path: sys.path.append(binPath) import quux if "experimental" in os.path.dirname(quux.__file__): print "WARNING: Using experimental version of quux module" # quux.test() quux.shapeRendererInit() quux.shapeRendererSetUseDynamicLOD(0) quux.shapeRendererStartDrawing() if test_type == 1: center = Numeric.array((Numeric.array((0, 0, 0), 'f'), Numeric.array((0, 0, 1), 'f'), Numeric.array((0, 1, 0), 'f'), Numeric.array((0, 1, 1), 'f'), Numeric.array((1, 0, 0), 'f'), Numeric.array((1, 0, 1), 'f'), Numeric.array((1, 1, 0), 'f'), Numeric.array((1, 1, 1), 'f')), 'f') radius = Numeric.array((0.2, 0.4, 0.6, 0.8, 1.2, 1.4, 1.6, 1.8), 'f') color = Numeric.array((Numeric.array((0, 0, 0, 0.5), 'f'), Numeric.array((0, 0, 1, 0.5), 'f'), Numeric.array((0, 1, 0, 0.5), 'f'), Numeric.array((0, 1, 1, 0.5), 'f'), Numeric.array((1, 0, 0, 0.5), 'f'), Numeric.array((1, 0, 1, 0.5), 'f'), Numeric.array((1, 1, 0, 0.5), 'f'), Numeric.array((1, 1, 1, 0.5), 'f')), 'f') result = quux.shapeRendererDrawSpheres(8, center, radius, color) elif test_type == 2: # grantham - I'm pretty sure the actual compilation, init, # etc happens once from bearing_data import sphereCenters, sphereRadii from bearing_data import sphereColors, cylinderPos1 from bearing_data import cylinderPos2, cylinderRadii from bearing_data import cylinderCapped, cylinderColors glPushMatrix() glTranslate(-0.001500, -0.000501, 151.873627) result = quux.shapeRendererDrawSpheres(1848, sphereCenters, sphereRadii, sphereColors) result = quux.shapeRendererDrawCylinders(5290, cylinderPos1, cylinderPos2, cylinderRadii, cylinderCapped, cylinderColors) glPopMatrix() quux.shapeRendererFinishDrawing() except ImportError: env.history.message(redmsg( "Can't import Pyrex OpenGL or maybe bearing_data.py, rebuild it")) return
def render(self): o = self.motion.getWorldTransform().getOrigin() glColor(*self.color) glTranslate(o.x, o.y, o.z) gluSphere(self.quad, self.radius, 25, 25)
def main(): pygame.init() dimensions = (800, 600) display = pygame.display.set_mode(dimensions, DOUBLEBUF | OPENGL) pygame.display.set_caption("Rubik's Cube Simulator") gluPerspective(45, (dimensions[0] / dimensions[1]), 0.1, 50.0) glTranslate(0, 0, -40) glRotate(45, 0, 1, 0) glRotate(30, 1, 0, 1) glEnable(GL_DEPTH_TEST) cube = Cube() cube.render() scramble_button = pygame.Rect(300, 300, 50, 50) while True: pygame.draw.rect(display, (0, 0, 255), scramble_button) # pygame.display.flip() cube.render() for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() quit() elif event.type == pygame.MOUSEBUTTONDOWN: # mouse_pos == event.pos if scramble_button.collidepoint(event.pos): print("hi") keys = pygame.key.get_pressed() if keys[pygame.K_LSHIFT] or keys[pygame.K_RSHIFT]: if keys[pygame.K_f]: cube.rotate(Face.front, Radians.CW) elif keys[pygame.K_b]: cube.rotate(Face.back, Radians.CCW) elif keys[pygame.K_l]: cube.rotate(Face.left, Radians.CW) elif keys[pygame.K_r]: cube.rotate(Face.right, Radians.CCW) elif keys[pygame.K_u]: cube.rotate(Face.up, Radians.CW) elif keys[pygame.K_d]: cube.rotate(Face.down, Radians.CCW) else: if keys[pygame.K_f]: cube.rotate(Face.front, Radians.CCW) elif keys[pygame.K_b]: cube.rotate(Face.back, Radians.CW) elif keys[pygame.K_l]: cube.rotate(Face.left, Radians.CCW) elif keys[pygame.K_r]: cube.rotate(Face.right, Radians.CW) elif keys[pygame.K_u]: cube.rotate(Face.up, Radians.CCW) elif keys[pygame.K_d]: cube.rotate(Face.down, Radians.CW) elif keys[pygame.K_LEFT]: cube.full_rotate("y", Radians.CW) elif keys[pygame.K_RIGHT]: cube.full_rotate("y", Radians.CCW) elif keys[pygame.K_UP]: cube.full_rotate("x", Radians.CCW) elif keys[pygame.K_DOWN]: cube.full_rotate("x", Radians.CW) elif keys[pygame.K_x]: cube.full_rotate("z", Radians.CCW) elif keys[pygame.K_z]: cube.full_rotate("z", Radians.CW) elif keys[pygame.K_s]: cube.scramble()
def drawLinearSign(color, center, axis, l, h, w): """Linear motion sign on the side of squa-linder """ depthOffset = 0.005 glPushMatrix() glColor3fv(color) glDisable(GL_LIGHTING) glTranslatef(center[0], center[1], center[2]) ##Huaicai 1/17/05: To avoid rotate around (0, 0, 0), which causes ## display problem on some platforms angle = -acos(axis[2]) * 180.0 / pi if (axis[2] * axis[2] >= 1.0): glRotate(angle, 0.0, 1.0, 0.0) else: glRotate(angle, axis[1], -axis[0], 0.0) glPushMatrix() glTranslate(h / 2.0 + depthOffset, 0.0, 0.0) glPushMatrix() glScale(1.0, 1.0, l) glCallList(drawing_globals.linearLineList) glPopMatrix() if l < 2.6: sl = l / 2.7 glScale(1.0, sl, sl) if w < 1.0: glScale(1.0, w, w) drawLinearArrows(l) glPopMatrix() glPushMatrix() glTranslate(-h / 2.0 - depthOffset, 0.0, 0.0) glRotate(180.0, 0.0, 0.0, 1.0) glPushMatrix() glScale(1.0, 1.0, l) glCallList(drawing_globals.linearLineList) glPopMatrix() if l < 2.6: glScale(1.0, sl, sl) if w < 1.0: glScale(1.0, w, w) drawLinearArrows(l) glPopMatrix() glPushMatrix() glTranslate(0.0, w / 2.0 + depthOffset, 0.0) glRotate(90.0, 0.0, 0.0, 1.0) glPushMatrix() glScale(1.0, 1.0, l) glCallList(drawing_globals.linearLineList) glPopMatrix() if l < 2.6: glScale(1.0, sl, sl) if w < 1.0: glScale(1.0, w, w) drawLinearArrows(l) glPopMatrix() glPushMatrix() glTranslate(0.0, -w / 2.0 - depthOffset, 0.0) glRotate(-90.0, 0.0, 0.0, 1.0) glPushMatrix() glScale(1.0, 1.0, l) glCallList(drawing_globals.linearLineList) glPopMatrix() if l < 2.6: glScale(1.0, sl, sl) if w < 1.0: glScale(1.0, w, w) drawLinearArrows(l) glPopMatrix() glEnable(GL_LIGHTING) glPopMatrix() return
def drawGrid(scale, center, latticeType): """ Construct the grid model and show as position references for cookies. The model is build around "pov" and has size of 2*"scale" on each of the (x, y, z) directions. @note: This should be optimized later. For "scale = 200", it takes about 1479623 loops. ---Huaicai """ glDisable(GL_LIGHTING) # bruce 041201: # Quick fix to prevent "hang" from drawing too large a cookieMode grid # with our current cubic algorithm (bug 8). The constant 120.0 is still on # the large side in terms of responsiveness -- on a 1.8GHz iMac G5 it can # take many seconds to redraw the largest grid, or to update a selection # rectangle during a drag. I also tried 200.0 but that was way too large. # Since some users have slower machines, I'll be gentle and put 90.0 here. # Someday we need to fix the alg to be quadratic by teaching this code # (and the cookie baker code too) about the eyespace clipping planes. # Once we support user prefs, this should be one of them (if the alg is # not fixed by then). MAX_GRID_SCALE = 90.0 if scale > MAX_GRID_SCALE: scale = MAX_GRID_SCALE if latticeType == 'DIAMOND': cellX = cellY = cellZ = drawing_globals.DiGridSp elif latticeType == 'LONSDALEITE': cellX = drawing_globals.XLen cellY = drawing_globals.YLen cellZ = drawing_globals.ZLen bblo = center - scale bbhi = center + scale i1 = int(floor(bblo[0]/cellX)) i2 = int(ceil(bbhi[0]/cellX)) j1 = int(floor(bblo[1]/cellY)) j2 = int(ceil(bbhi[1]/cellY)) k1 = int(floor(bblo[2]/cellZ)) k2 = int(ceil(bbhi[2]/cellZ)) glPushMatrix() glTranslate(i1*cellX, j1*cellY, k1*cellZ) for i in range(i1, i2): glPushMatrix() for j in range(j1, j2): glPushMatrix() for k in range(k1, k2): if latticeType == 'DIAMOND': glCallList(drawing_globals.diamondGridList) else: glCallList(drawing_globals.lonsGridList) glTranslate(0.0, 0.0, cellZ) glPopMatrix() glTranslate(0.0, cellY, 0.0) glPopMatrix() glTranslate(cellX, 0.0, 0.0) glPopMatrix() glEnable(GL_LIGHTING) #drawCubeCell(V(1, 0, 0)) return
def drawGrid(scale, center, latticeType): """ Construct the grid model and show as position references for cookies. The model is build around "pov" and has size of 2*"scale" on each of the (x, y, z) directions. @note: This should be optimized later. For "scale = 200", it takes about 1479623 loops. ---Huaicai """ glDisable(GL_LIGHTING) # bruce 041201: # Quick fix to prevent "hang" from drawing too large a BuildCrystal_Command grid # with our current cubic algorithm (bug 8). The constant 120.0 is still on # the large side in terms of responsiveness -- on a 1.8GHz iMac G5 it can # take many seconds to redraw the largest grid, or to update a selection # rectangle during a drag. I also tried 200.0 but that was way too large. # Since some users have slower machines, I'll be gentle and put 90.0 here. # Someday we need to fix the alg to be quadratic by teaching this code # (and the Crystal builder code too) about the eyespace clipping planes. # Once we support user prefs, this should be one of them (if the alg is # not fixed by then). MAX_GRID_SCALE = 90.0 if scale > MAX_GRID_SCALE: scale = MAX_GRID_SCALE if latticeType == 'DIAMOND': cellX = cellY = cellZ = drawing_globals.DiGridSp elif latticeType == 'LONSDALEITE': cellX = drawing_globals.XLen cellY = drawing_globals.YLen cellZ = drawing_globals.ZLen bblo = center - scale bbhi = center + scale i1 = int(floor(bblo[0] / cellX)) i2 = int(ceil(bbhi[0] / cellX)) j1 = int(floor(bblo[1] / cellY)) j2 = int(ceil(bbhi[1] / cellY)) k1 = int(floor(bblo[2] / cellZ)) k2 = int(ceil(bbhi[2] / cellZ)) glPushMatrix() glTranslate(i1 * cellX, j1 * cellY, k1 * cellZ) for i in range(i1, i2): glPushMatrix() for j in range(j1, j2): glPushMatrix() for k in range(k1, k2): if latticeType == 'DIAMOND': glCallList(drawing_globals.diamondGridList) else: glCallList(drawing_globals.lonsGridList) glTranslate(0.0, 0.0, cellZ) glPopMatrix() glTranslate(0.0, cellY, 0.0) glPopMatrix() glTranslate(cellX, 0.0, 0.0) glPopMatrix() glEnable(GL_LIGHTING) #drawCubeCell(V(1, 0, 0)) return
def on_draw(self): glBlendFunc(GL_SRC_ALPHA, GL_DST_ALPHA) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glLoadIdentity() glTranslate(self.translatex, self.translatey, self.depth) if self.autoRun: glRotate(self.rotx, 0, 1, 0) else: # Perform arcball rotation. glScale(1,-1,1) glMultMatrixf(self.arcball.transform) glScale(1,-1,1) with displayListify("cubeTranslate") as shouldLeave: if shouldLeave: raise LeaveWith # Scale the co-ordinates so that they are correct glScale(2/128, 2/128, 2/128) glColor(0.25, 0.25, 0.25, 1) glutWireCube(255) glTranslate(-128, -128, -128) with displayListify("allPoints") as shouldLeave: if shouldLeave: raise LeaveWith with SaveMatrix(): # Flip the co-ordinates and translate upwards glScale(1,-1,1) glColor(0.25, 0.25, 0.25, 0.5) glTranslate(0,-255,0) glPointSize(pointSize) for dat in reversed(vLists): glTranslate(0., 0., 1./nChunks*255) dat.draw(GL_POINTS) with displayListify("axisLabels") as shouldLeave: if shouldLeave: raise LeaveWith #if True: with SaveMatrix(): glTranslate(128,0,0) self.makeLabel("End").draw() glTranslate(0,0,255) self.makeLabel("Beginning").draw() with SaveMatrix(): glTranslate(-128,128,0) glRotate(90,0,0,1) self.makeLabel("Byte 1").draw() glTranslate(0,255,0) self.makeLabel("Byte 2").draw() with displayListify("fileName") as shouldLeave: if shouldLeave: raise LeaveWith glLoadIdentity() with SaveMatrix(): glColor(1,0,0) glTranslate(0,-2.2,-4) glScale(1/64, 1/64, 1/64) l = self.makeLabel(basename(currentInputFile)) l.color = (0, 128, 230, 255) l.draw() glTranslate(0,0,-1) if self.autoRun: if self.rotx > 360 - 45: vlist = vertex_list(4, ('v2i', (-1, -1, 1, -1, 1, 1, -1, 1))) glColor(0,0,0,(self.rotx-(360-45))/45) vlist.draw(GL_QUADS) if self.rotx < 45: vlist = vertex_list(4, ('v2i', (-1, -1, 1, -1, 1, 1, -1, 1))) glColor(0,0,0,1-(self.rotx/45)) vlist.draw(GL_QUADS)
def Draw(self): if 1: # TODO: move this test code into a specific test mode just for it, # so it doesn't clutter up or slow down this general-use mode. # # wware 060124 Embed Pyrex/OpenGL unit tests into the cad code # grantham 060207: # Set to 1 to see a small array of eight spheres. # Set to 2 to see the Large-Bearing model, but this is most effective if # the Large-Bearing has already been loaded normally into rotate mode #bruce 060209 set this from a debug_pref menu item, not a hardcoded flag TEST_PYREX_OPENGL = debug_pref("TEST_PYREX_OPENGL", Choice([0, 1, 2])) # uncomment this line to set it in the old way: ## TEST_PYREX_OPENGL = 1 if TEST_PYREX_OPENGL: try: print_compact_stack("selectMode Draw: ") ### ### BUG: if import quux fails, we get into some sort of infinite ### loop of Draw calls. [bruce 070917 comment] #self.w.win_update() ## sys.path.append("./experimental/pyrex-opengl") # no longer ##needed here -- always done in drawer.py binPath = os.path.normpath( os.path.dirname(os.path.abspath(sys.argv[0])) + '/../bin') if binPath not in sys.path: sys.path.append(binPath) import quux if "experimental" in os.path.dirname(quux.__file__): print "WARNING: Using experimental version of quux module" # quux.test() quux.shapeRendererInit() quux.shapeRendererSetUseDynamicLOD(0) quux.shapeRendererStartDrawing() if TEST_PYREX_OPENGL == 1: center = Numeric.array( (Numeric.array( (0, 0, 0), 'f'), Numeric.array( (0, 0, 1), 'f'), Numeric.array((0, 1, 0), 'f'), Numeric.array( (0, 1, 1), 'f'), Numeric.array((1, 0, 0), 'f'), Numeric.array( (1, 0, 1), 'f'), Numeric.array( (1, 1, 0), 'f'), Numeric.array( (1, 1, 1), 'f')), 'f') radius = Numeric.array( (0.2, 0.4, 0.6, 0.8, 1.2, 1.4, 1.6, 1.8), 'f') color = Numeric.array((Numeric.array( (0, 0, 0, 0.5), 'f'), Numeric.array( (0, 0, 1, 0.5), 'f'), Numeric.array((0, 1, 0, 0.5), 'f'), Numeric.array((0, 1, 1, 0.5), 'f'), Numeric.array((1, 0, 0, 0.5), 'f'), Numeric.array((1, 0, 1, 0.5), 'f'), Numeric.array((1, 1, 0, 0.5), 'f'), Numeric.array((1, 1, 1, 0.5), 'f')), 'f') result = quux.shapeRendererDrawSpheres( 8, center, radius, color) elif TEST_PYREX_OPENGL == 2: # grantham - I'm pretty sure the actual compilation, init, # etc happens once from bearing_data import sphereCenters, sphereRadii from bearing_data import sphereColors, cylinderPos1 from bearing_data import cylinderPos2, cylinderRadii from bearing_data import cylinderCapped, cylinderColors glPushMatrix() glTranslate(-0.001500, -0.000501, 151.873627) result = quux.shapeRendererDrawSpheres( 1848, sphereCenters, sphereRadii, sphereColors) result = quux.shapeRendererDrawCylinders( 5290, cylinderPos1, cylinderPos2, cylinderRadii, cylinderCapped, cylinderColors) glPopMatrix() quux.shapeRendererFinishDrawing() except ImportError: env.history.message( redmsg( "Can't import Pyrex OpenGL or maybe bearing_data.py, rebuild it" )) else: if self.bc_in_use is not None: #bruce 060414 self.bc_in_use.draw(self.o, 'fake dispdef kluge') # bruce comment 040922: code is almost identical with modifyMode.Draw; # the difference (no check for self.o.assy existing) might be a bug # in this version, or might have no effect. commonGraphicsMode.Draw(self) #self.griddraw() if self.selCurve_List: self.draw_selection_curve() self.o.assy.draw(self.o)
def test_pyrex_opengl(test_type): # not tested since major refactoring try: print_compact_stack("selectMode Draw: ") ### ### BUG: if import quux fails, we get into some sort of infinite ### loop of Draw calls. [bruce 070917 comment] #self.w.win_update() ## sys.path.append("./experimental/pyrex-opengl") # no longer ##needed here -- always done in drawer.py binPath = os.path.normpath( os.path.dirname(os.path.abspath(sys.argv[0])) + '/../bin') if binPath not in sys.path: sys.path.append(binPath) import quux if "experimental" in os.path.dirname(quux.__file__): print "WARNING: Using experimental version of quux module" # quux.test() quux.shapeRendererInit() quux.shapeRendererSetUseDynamicLOD(0) quux.shapeRendererStartDrawing() if test_type == 1: center = Numeric.array( (Numeric.array((0, 0, 0), 'f'), Numeric.array( (0, 0, 1), 'f'), Numeric.array( (0, 1, 0), 'f'), Numeric.array((0, 1, 1), 'f'), Numeric.array((1, 0, 0), 'f'), Numeric.array( (1, 0, 1), 'f'), Numeric.array( (1, 1, 0), 'f'), Numeric.array((1, 1, 1), 'f')), 'f') radius = Numeric.array((0.2, 0.4, 0.6, 0.8, 1.2, 1.4, 1.6, 1.8), 'f') color = Numeric.array( (Numeric.array( (0, 0, 0, 0.5), 'f'), Numeric.array((0, 0, 1, 0.5), 'f'), Numeric.array( (0, 1, 0, 0.5), 'f'), Numeric.array((0, 1, 1, 0.5), 'f'), Numeric.array( (1, 0, 0, 0.5), 'f'), Numeric.array((1, 0, 1, 0.5), 'f'), Numeric.array( (1, 1, 0, 0.5), 'f'), Numeric.array( (1, 1, 1, 0.5), 'f')), 'f') result = quux.shapeRendererDrawSpheres(8, center, radius, color) elif test_type == 2: # grantham - I'm pretty sure the actual compilation, init, # etc happens once from bearing_data import sphereCenters, sphereRadii from bearing_data import sphereColors, cylinderPos1 from bearing_data import cylinderPos2, cylinderRadii from bearing_data import cylinderCapped, cylinderColors glPushMatrix() glTranslate(-0.001500, -0.000501, 151.873627) result = quux.shapeRendererDrawSpheres(1848, sphereCenters, sphereRadii, sphereColors) result = quux.shapeRendererDrawCylinders(5290, cylinderPos1, cylinderPos2, cylinderRadii, cylinderCapped, cylinderColors) glPopMatrix() quux.shapeRendererFinishDrawing() except ImportError: env.history.message( redmsg( "Can't import Pyrex OpenGL or maybe bearing_data.py, rebuild it" )) return
def drawLinearSign(color, center, axis, l, h, w): """Linear motion sign on the side of squa-linder """ depthOffset = 0.005 glPushMatrix() glColor3fv(color) glDisable(GL_LIGHTING) glTranslatef(center[0], center[1], center[2]) ##Huaicai 1/17/05: To avoid rotate around (0, 0, 0), which causes ## display problem on some platforms angle = -acos(axis[2])*180.0/pi if (axis[2]*axis[2] >= 1.0): glRotate(angle, 0.0, 1.0, 0.0) else: glRotate(angle, axis[1], -axis[0], 0.0) glPushMatrix() glTranslate(h/2.0 + depthOffset, 0.0, 0.0) glPushMatrix() glScale(1.0, 1.0, l) glCallList(drawing_globals.linearLineList) glPopMatrix() if l < 2.6: sl = l/2.7 glScale(1.0, sl, sl) if w < 1.0: glScale(1.0, w, w) drawLinearArrows(l) glPopMatrix() glPushMatrix() glTranslate(-h/2.0 - depthOffset, 0.0, 0.0) glRotate(180.0, 0.0, 0.0, 1.0) glPushMatrix() glScale(1.0, 1.0, l) glCallList(drawing_globals.linearLineList) glPopMatrix() if l < 2.6: glScale(1.0, sl, sl) if w < 1.0: glScale(1.0, w, w) drawLinearArrows(l) glPopMatrix() glPushMatrix() glTranslate(0.0, w/2.0 + depthOffset, 0.0) glRotate(90.0, 0.0, 0.0, 1.0) glPushMatrix() glScale(1.0, 1.0, l) glCallList(drawing_globals.linearLineList) glPopMatrix() if l < 2.6: glScale(1.0, sl, sl) if w < 1.0: glScale(1.0, w, w) drawLinearArrows(l) glPopMatrix() glPushMatrix() glTranslate(0.0, -w/2.0 - depthOffset, 0.0 ) glRotate(-90.0, 0.0, 0.0, 1.0) glPushMatrix() glScale(1.0, 1.0, l) glCallList(drawing_globals.linearLineList) glPopMatrix() if l < 2.6: glScale(1.0, sl, sl) if w < 1.0: glScale(1.0, w, w) drawLinearArrows(l) glPopMatrix() glEnable(GL_LIGHTING) glPopMatrix() return
def drawSiCGrid(color, line_type, w, h, up, right): """ Draw SiC grid. """ if line_type == NO_LINE: return XLen = sic_uLen * 3.0 YLen = sic_yU * 2.0 hw = w / 2.0 hh = h / 2.0 i1 = int(floor(-hw / XLen)) i2 = int(ceil(hw / XLen)) j1 = int(floor(-hh / YLen)) j2 = int(ceil(hh / YLen)) glDisable(GL_LIGHTING) glColor3fv(color) if line_type > 1: glEnable(GL_LINE_STIPPLE) if line_type == DASHED_LINE: glLineStipple(1, 0x00FF) # dashed elif line_type == DOTTED_LINE: glLineStipple(1, 0x0101) # dotted else: print "drawer.drawSiCGrid(): line_type '", line_type, \ "' is not valid. Drawing dashed grid line." glLineStipple(1, 0x00FF) # dashed glClipPlane(GL_CLIP_PLANE0, (1.0, 0.0, 0.0, hw)) glClipPlane(GL_CLIP_PLANE1, (-1.0, 0.0, 0.0, hw)) glClipPlane(GL_CLIP_PLANE2, (0.0, 1.0, 0.0, hh)) glClipPlane(GL_CLIP_PLANE3, (0.0, -1.0, 0.0, hh)) glEnable(GL_CLIP_PLANE0) glEnable(GL_CLIP_PLANE1) glEnable(GL_CLIP_PLANE2) glEnable(GL_CLIP_PLANE3) glPushMatrix() glTranslate(i1 * XLen, j1 * YLen, 0.0) for i in range(i1, i2): glPushMatrix() for j in range(j1, j2): glCallList(SiCGridList) glTranslate(0.0, YLen, 0.0) glPopMatrix() glTranslate(XLen, 0.0, 0.0) glPopMatrix() glDisable(GL_CLIP_PLANE0) glDisable(GL_CLIP_PLANE1) glDisable(GL_CLIP_PLANE2) glDisable(GL_CLIP_PLANE3) if line_type > 1: glDisable(GL_LINE_STIPPLE) xpos, ypos = hw, 0.0 f3d = Font3D(xpos=xpos, ypos=ypos, right=right, up=up, rot90=False, glBegin=False) for j in range(j1, j2): yoff = j * YLen if -hh < yoff + ypos < hh: f3d.drawString("%-.4g" % yoff, color=color, yoff=yoff) xpos, ypos = 0.0, hh f3d = Font3D(xpos=xpos, ypos=ypos, right=right, up=up, rot90=True, glBegin=False) for i in range(2 * i1, 2 * i2): yoff = i * (XLen / 2) if -hw < yoff + xpos < hw: f3d.drawString("%-.4g" % yoff, color=color, yoff=yoff) glEnable(GL_LIGHTING) return
def Draw(self): if 1: # TODO: move this test code into a specific test mode just for it, # so it doesn't clutter up or slow down this general-use mode. # # wware 060124 Embed Pyrex/OpenGL unit tests into the cad code # grantham 060207: # Set to 1 to see a small array of eight spheres. # Set to 2 to see the Large-Bearing model, but this is most effective if # the Large-Bearing has already been loaded normally into rotate mode #bruce 060209 set this from a debug_pref menu item, not a hardcoded flag TEST_PYREX_OPENGL = debug_pref("TEST_PYREX_OPENGL", Choice([0,1,2])) # uncomment this line to set it in the old way: ## TEST_PYREX_OPENGL = 1 if TEST_PYREX_OPENGL: try: print_compact_stack("selectMode Draw: " )### ### BUG: if import quux fails, we get into some sort of infinite ### loop of Draw calls. [bruce 070917 comment] #self.w.win_update() ## sys.path.append("./experimental/pyrex-opengl") # no longer ##needed here -- always done in drawer.py binPath = os.path.normpath(os.path.dirname( os.path.abspath(sys.argv[0])) + '/../bin') if binPath not in sys.path: sys.path.append(binPath) import quux if "experimental" in os.path.dirname(quux.__file__): print "WARNING: Using experimental version of quux module" # quux.test() quux.shapeRendererInit() quux.shapeRendererSetUseDynamicLOD(0) quux.shapeRendererStartDrawing() if TEST_PYREX_OPENGL == 1: center = Numeric.array((Numeric.array((0, 0, 0), 'f'), Numeric.array((0, 0, 1), 'f'), Numeric.array((0, 1, 0), 'f'), Numeric.array((0, 1, 1), 'f'), Numeric.array((1, 0, 0), 'f'), Numeric.array((1, 0, 1), 'f'), Numeric.array((1, 1, 0), 'f'), Numeric.array((1, 1, 1), 'f')), 'f') radius = Numeric.array((0.2, 0.4, 0.6, 0.8, 1.2, 1.4, 1.6, 1.8), 'f') color = Numeric.array((Numeric.array((0, 0, 0, 0.5), 'f'), Numeric.array((0, 0, 1, 0.5), 'f'), Numeric.array((0, 1, 0, 0.5), 'f'), Numeric.array((0, 1, 1, 0.5), 'f'), Numeric.array((1, 0, 0, 0.5), 'f'), Numeric.array((1, 0, 1, 0.5), 'f'), Numeric.array((1, 1, 0, 0.5), 'f'), Numeric.array((1, 1, 1, 0.5), 'f')), 'f') result = quux.shapeRendererDrawSpheres(8, center, radius, color) elif TEST_PYREX_OPENGL == 2: # grantham - I'm pretty sure the actual compilation, init, # etc happens once from bearing_data import sphereCenters, sphereRadii from bearing_data import sphereColors, cylinderPos1 from bearing_data import cylinderPos2, cylinderRadii from bearing_data import cylinderCapped, cylinderColors glPushMatrix() glTranslate(-0.001500, -0.000501, 151.873627) result = quux.shapeRendererDrawSpheres(1848, sphereCenters, sphereRadii, sphereColors) result = quux.shapeRendererDrawCylinders(5290, cylinderPos1, cylinderPos2, cylinderRadii, cylinderCapped, cylinderColors) glPopMatrix() quux.shapeRendererFinishDrawing() except ImportError: env.history.message(redmsg( "Can't import Pyrex OpenGL or maybe bearing_data.py, rebuild it")) else: if self.bc_in_use is not None: #bruce 060414 self.bc_in_use.draw(self.o, 'fake dispdef kluge') # bruce comment 040922: code is almost identical with modifyMode.Draw; # the difference (no check for self.o.assy existing) might be a bug # in this version, or might have no effect. commonGraphicsMode.Draw(self) #self.griddraw() if self.selCurve_List: self.draw_selection_curve() self.o.assy.draw(self.o)
def drawOriginAsSmallAxis(scale, origin, dashEnabled=False): """ Draws a small wireframe version of the origin. It is rendered as a 3D point at (0, 0, 0) with 3 small axes extending from it in the positive X, Y, Z directions. @see: drawaxes (related code) """ #Perhaps we should split this method into smaller methods? ninad060920 #Notes: #1. drawing arrowheads implemented on 060918 #2. ninad060921 Show the origin axes as dotted if behind the mode. #3. ninad060922 The arrow heads are drawn as wireframe cones if behind the # object the arrowhead size is slightly smaller (otherwise some portion of # the the wireframe arrow shows up! #4 .Making origin non-zoomable is acheived by replacing hardcoded 'n' with # glpane's scale - ninad060922 #ninad060922 in future , the following could be user preferences. if (dashEnabled): dashShrinkage = 0.9 else: dashShrinkage = 1 x1, y1, z1 = scale * 0.01, scale * 0.01, scale * 0.01 xEnd, yEnd, zEnd = scale * 0.04, scale * 0.09, scale * 0.025 arrowBase = scale * 0.0075 * dashShrinkage arrowHeight = scale * 0.035 * dashShrinkage lineWidth = 1.0 glPushMatrix() glTranslate(origin[0], origin[1], origin[2]) glDisable(GL_LIGHTING) glLineWidth(lineWidth) gleNumSides = gleGetNumSides() #Code to show hidden lines of the origin if some model obscures it # ninad060921 if dashEnabled: glLineStipple(2, 0xAAAA) glEnable(GL_LINE_STIPPLE) glDisable(GL_DEPTH_TEST) glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) gleSetNumSides(5) else: gleSetNumSides(10) glBegin(GL_LINES) color = env.prefs[originAxisColor_prefs_key] glColor3fv(color) #start draw a point at origin . #ninad060922 is thinking about using GL_POINTS here glVertex(-x1, 0.0, 0.0) glVertex(x1, 0.0, 0.0) glVertex(0.0, -y1, 0.0) glVertex(0.0, y1, 0.0) glVertex(-x1, y1, z1) glVertex(x1, -y1, -z1) glVertex(x1, y1, z1) glVertex(-x1, -y1, -z1) glVertex(x1, y1, -z1) glVertex(-x1, -y1, z1) glVertex(-x1, y1, -z1) glVertex(x1, -y1, z1) #end draw a point at origin #start draw small origin axes glColor3fv(color) glVertex(xEnd, 0.0, 0.0) glVertex(0.0, 0.0, 0.0) glColor3fv(color) glVertex(0.0, yEnd, 0.0) glVertex(0.0, 0.0, 0.0) glColor3fv(color) glVertex(0.0, 0.0, zEnd) glVertex(0.0, 0.0, 0.0) glEnd() #end draw lines glLineWidth(1.0) # End push matrix for drawing various lines in the origin and axes. glPopMatrix() #start draw solid arrow heads for X , Y and Z axes glPushMatrix() glDisable(GL_CULL_FACE) glColor3fv(color) glTranslatef(xEnd, 0.0, 0.0) glRotatef(90, 0.0, 1.0, 0.0) glePolyCone( [[0, 0, -1], [0, 0, 0], [0, 0, arrowHeight], [0, 0, arrowHeight + 1]], None, [arrowBase, arrowBase, 0, 0]) glPopMatrix() glPushMatrix() glColor3fv(color) glTranslatef(0.0, yEnd, 0.0) glRotatef(-90, 1.0, 0.0, 0.0) glePolyCone( [[0, 0, -1], [0, 0, 0], [0, 0, arrowHeight], [0, 0, arrowHeight + 1]], None, [arrowBase, arrowBase, 0, 0]) glPopMatrix() glPushMatrix() glColor3fv(color) glTranslatef(0.0, 0.0, zEnd) glePolyCone( [[0, 0, -1], [0, 0, 0], [0, 0, arrowHeight], [0, 0, arrowHeight + 1]], None, [arrowBase, arrowBase, 0, 0]) #Disable line stipple and Enable Depth test if dashEnabled: glLineStipple(1, 0xAAAA) glDisable(GL_LINE_STIPPLE) glEnable(GL_DEPTH_TEST) glPolygonMode(GL_FRONT_AND_BACK, GL_FILL) gleSetNumSides(gleNumSides) glEnable(GL_CULL_FACE) glEnable(GL_LIGHTING) glPopMatrix() #end draw solid arrow heads for X , Y and Z axes return
def paint(self): glRotate(self.orientation.x, 1.0, 0.0, 0.0) glRotate(self.orientation.y, 0.0, 1.0, 0.0) glRotate(self.orientation.z, 0.0, 0.0, 1.0) glTranslate(-self.position.x, -self.position.y, -self.position.z)
def draw_teleport_switch(position: Tuple[int, int], color: Tuple[float, float, float]): radius = 0.4 height = 0.15 angle_stepsize = 0.1 glPushMatrix() glTranslate(0.5 + position[0], -0.5 - position[1], 0) glColor(color) # side 1 glBegin(GL_QUAD_STRIP) angle = 0.0 first = True temp_x, temp_y = 0, 0 while angle < 2 * pi: x = radius * cos(angle) y = radius * sin(angle) if y >= 0.1: if first: temp_x, temp_y = x, y first = False glVertex3f(x, y, height) glVertex3f(x, y, 0) angle += angle_stepsize while angle > 0: x = radius * cos(angle) y = radius * sin(angle) if y >= 0.1: glVertex3f(x * 0.6, y * 0.6, height) glVertex3f(x * 0.6, y * 0.6, 0) angle -= angle_stepsize glVertex3f(temp_x, temp_y, height) glVertex3f(temp_x, temp_y, 0) glEnd() # side 2 glBegin(GL_QUAD_STRIP) angle = 0.0 first = True temp_x, temp_y = 0, 0 while angle < 2 * pi: x = radius * cos(angle) y = radius * sin(angle) if y <= -0.1: if first: temp_x, temp_y = x, y first = False glVertex3f(x, y, height) glVertex3f(x, y, 0) angle += angle_stepsize while angle > 0: x = radius * cos(angle) y = radius * sin(angle) if y <= -0.1: glVertex3f(x * 0.6, y * 0.6, height) glVertex3f(x * 0.6, y * 0.6, 0) angle -= angle_stepsize glVertex3f(temp_x, temp_y, height) glVertex3f(temp_x, temp_y, 0) glEnd() # top 1 glColor(Draw.colors['gray']) glBegin(GL_QUAD_STRIP) angle = 0.0 while angle < 2 * pi: x1 = radius * cos(angle) y1 = radius * sin(angle) x2 = radius * cos(angle) * 0.6 y2 = radius * sin(angle) * 0.6 if y1 >= 0.1: glVertex3f(x1, y1, height) glVertex3f(x2, y2, height) angle += angle_stepsize glEnd() # top 2 glColor(Draw.colors['gray']) glBegin(GL_QUAD_STRIP) angle = 0.0 while angle < 2 * pi: x1 = radius * cos(angle) y1 = radius * sin(angle) x2 = radius * cos(angle) * 0.6 y2 = radius * sin(angle) * 0.6 if y1 <= -0.1: glVertex3f(x1, y1, height) glVertex3f(x2, y2, height) angle += angle_stepsize glEnd() glPopMatrix()
def drawSiCGrid(color, line_type, w, h, up, right): """ Draw SiC grid. """ if line_type == NO_LINE: return XLen = sic_uLen * 3.0 YLen = sic_yU * 2.0 hw = w/2.0; hh = h/2.0 i1 = int(floor(-hw/XLen)) i2 = int(ceil(hw/XLen)) j1 = int(floor(-hh/YLen)) j2 = int(ceil(hh/YLen)) glDisable(GL_LIGHTING) glColor3fv(color) if line_type > 1: glEnable(GL_LINE_STIPPLE) if line_type == DASHED_LINE: glLineStipple (1, 0x00FF) # dashed elif line_type == DOTTED_LINE: glLineStipple (1, 0x0101) # dotted else: print "drawer.drawSiCGrid(): line_type '", line_type, \ "' is not valid. Drawing dashed grid line." glLineStipple (1, 0x00FF) # dashed glClipPlane(GL_CLIP_PLANE0, (1.0, 0.0, 0.0, hw)) glClipPlane(GL_CLIP_PLANE1, (-1.0, 0.0, 0.0, hw)) glClipPlane(GL_CLIP_PLANE2, (0.0, 1.0, 0.0, hh)) glClipPlane(GL_CLIP_PLANE3, (0.0, -1.0, 0.0, hh)) glEnable(GL_CLIP_PLANE0) glEnable(GL_CLIP_PLANE1) glEnable(GL_CLIP_PLANE2) glEnable(GL_CLIP_PLANE3) glPushMatrix() glTranslate(i1*XLen, j1*YLen, 0.0) for i in range(i1, i2): glPushMatrix() for j in range(j1, j2): glCallList(SiCGridList) glTranslate(0.0, YLen, 0.0) glPopMatrix() glTranslate(XLen, 0.0, 0.0) glPopMatrix() glDisable(GL_CLIP_PLANE0) glDisable(GL_CLIP_PLANE1) glDisable(GL_CLIP_PLANE2) glDisable(GL_CLIP_PLANE3) if line_type > 1: glDisable (GL_LINE_STIPPLE) xpos, ypos = hw, 0.0 f3d = Font3D(xpos=xpos, ypos=ypos, right=right, up=up, rot90=False, glBegin=False) for j in range(j1, j2): yoff = j * YLen if -hh < yoff + ypos < hh: f3d.drawString("%-.4g" % yoff, color=color, yoff=yoff) xpos, ypos = 0.0, hh f3d = Font3D(xpos=xpos, ypos=ypos, right=right, up=up, rot90=True, glBegin=False) for i in range(2*i1, 2*i2): yoff = i * (XLen/2) if -hw < yoff + xpos < hw: f3d.drawString("%-.4g" % yoff, color=color, yoff=yoff) glEnable(GL_LIGHTING) return
def drawOriginAsSmallAxis(scale, origin, dashEnabled = False): """ Draws a small wireframe version of the origin. It is rendered as a 3D point at (0, 0, 0) with 3 small axes extending from it in the positive X, Y, Z directions. @see: drawaxes (related code) """ #Perhaps we should split this method into smaller methods? ninad060920 #Notes: #1. drawing arrowheads implemented on 060918 #2. ninad060921 Show the origin axes as dotted if behind the mode. #3. ninad060922 The arrow heads are drawn as wireframe cones if behind the # object the arrowhead size is slightly smaller (otherwise some portion of # the the wireframe arrow shows up! #4 .Making origin non-zoomable is acheived by replacing hardcoded 'n' with # glpane's scale - ninad060922 #ninad060922 in future , the following could be user preferences. if (dashEnabled): dashShrinkage = 0.9 else: dashShrinkage=1 x1, y1, z1 = scale * 0.01, scale * 0.01, scale * 0.01 xEnd, yEnd, zEnd = scale * 0.04, scale * 0.09, scale * 0.025 arrowBase = scale * 0.0075 * dashShrinkage arrowHeight = scale * 0.035 * dashShrinkage lineWidth = 1.0 glPushMatrix() glTranslate(origin[0], origin[1], origin[2]) glDisable(GL_LIGHTING) glLineWidth(lineWidth) gleNumSides = gleGetNumSides() #Code to show hidden lines of the origin if some model obscures it # ninad060921 if dashEnabled: glLineStipple(2, 0xAAAA) glEnable(GL_LINE_STIPPLE) glDisable(GL_DEPTH_TEST) glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) gleSetNumSides(5) else: gleSetNumSides(10) glBegin(GL_LINES) color = env.prefs[originAxisColor_prefs_key] glColor3fv(color) #start draw a point at origin . #ninad060922 is thinking about using GL_POINTS here glVertex(-x1, 0.0, 0.0) glVertex( x1, 0.0, 0.0) glVertex(0.0, -y1, 0.0) glVertex(0.0, y1, 0.0) glVertex(-x1, y1, z1) glVertex( x1, -y1, -z1) glVertex(x1, y1, z1) glVertex(-x1, -y1, -z1) glVertex(x1, y1, -z1) glVertex(-x1, -y1, z1) glVertex(-x1, y1, -z1) glVertex(x1, -y1, z1) #end draw a point at origin #start draw small origin axes glColor3fv(color) glVertex(xEnd, 0.0, 0.0) glVertex( 0.0, 0.0, 0.0) glColor3fv(color) glVertex(0.0, yEnd, 0.0) glVertex(0.0, 0.0, 0.0) glColor3fv(color) glVertex(0.0, 0.0, zEnd) glVertex(0.0, 0.0, 0.0) glEnd() #end draw lines glLineWidth(1.0) # End push matrix for drawing various lines in the origin and axes. glPopMatrix() #start draw solid arrow heads for X , Y and Z axes glPushMatrix() glDisable(GL_CULL_FACE) glColor3fv(color) glTranslatef(xEnd, 0.0, 0.0) glRotatef(90, 0.0, 1.0, 0.0) glePolyCone([[0, 0, -1], [0, 0, 0], [0, 0, arrowHeight], [0, 0, arrowHeight+1]], None, [arrowBase, arrowBase, 0, 0]) glPopMatrix() glPushMatrix() glColor3fv(color) glTranslatef(0.0, yEnd, 0.0) glRotatef(-90, 1.0, 0.0, 0.0) glePolyCone([[0, 0, -1], [0, 0, 0], [0, 0, arrowHeight], [0, 0, arrowHeight+1]], None, [arrowBase, arrowBase, 0, 0]) glPopMatrix() glPushMatrix() glColor3fv(color) glTranslatef(0.0,0.0,zEnd) glePolyCone([[0, 0, -1], [0, 0, 0], [0, 0, arrowHeight], [0, 0, arrowHeight+1]], None, [arrowBase, arrowBase, 0, 0]) #Disable line stipple and Enable Depth test if dashEnabled: glLineStipple(1, 0xAAAA) glDisable(GL_LINE_STIPPLE) glEnable(GL_DEPTH_TEST) glPolygonMode(GL_FRONT_AND_BACK, GL_FILL) gleSetNumSides(gleNumSides) glEnable(GL_CULL_FACE) glEnable(GL_LIGHTING) glPopMatrix() #end draw solid arrow heads for X , Y and Z axes return