def drawAgent(self, b): glPushMatrix() # apply the transformation for the boid glTranslatef(b.body.location.x / self.scaleFactor, b.body.location.y / self.scaleFactor, 0.0) # a = signedAngle() glRotatef( math.degrees(math.atan2(b.body.velocity.x, b.body.velocity.y)), 0.0, 0.0, -1.0) # render the boid's velocity if False: self.render_velocity(b) # render the boid's view if self.printFustrum: if b.type == "Taxi": self.render_view(b) elif b.type == "Client": if b.onboard != 1: self.render_view(b) # render the boid itself self.render_agent(b) glPopMatrix()
def display(self, mode_2d=False): with self.lock: glPushMatrix() glTranslatef(self.offset_x, self.offset_y, 0) glEnableClientState(GL_VERTEX_ARRAY) has_vbo = isinstance(self.vertex_buffer, VertexBufferObject) if self.display_travels: self._display_travels(has_vbo) glEnable(GL_LIGHTING) glEnableClientState(GL_NORMAL_ARRAY) glEnableClientState(GL_COLOR_ARRAY) glMaterialfv(GL_FRONT, GL_SPECULAR, vec(1, 1, 1, 1)) glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, vec(0, 0, 0, 0)) glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 50) glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE) self._display_movements(has_vbo) glDisable(GL_LIGHTING) glDisableClientState(GL_COLOR_ARRAY) glDisableClientState(GL_VERTEX_ARRAY) glDisableClientState(GL_NORMAL_ARRAY) glPopMatrix()
def draw(self): self.margin.draw(GL_LINE_LOOP) scale = self.height / max(1, self.low_high.amplitude) glTranslatef(0, -self.low_high.lowest * scale, 0) glScalef(1, scale, 1) for candle in self.candles: candle.draw()
def draw_objects(self): '''called in the middle of ondraw after the buffer has been cleared''' self.create_objects() glPushMatrix() if self.orthographic: glTranslatef(0, 0, -3 * self.dist) # Move back else: glTranslatef(0, 0, -self.dist) # Move back # Rotate according to trackball glMultMatrixd(build_rotmatrix(self.basequat)) # Move origin to bottom left of platform platformx0 = -self.build_dimensions[3] - self.parent.platform.width / 2 platformy0 = -self.build_dimensions[4] - self.parent.platform.depth / 2 glTranslatef(platformx0, platformy0, 0) for obj in self.parent.objects: if not obj.model \ or not obj.model.loaded \ or not obj.model.initialized: continue glPushMatrix() glTranslatef(*(obj.offsets)) glTranslatef(*(obj.centeroffset)) glRotatef(obj.rot, 0.0, 0.0, 1.0) glScalef(*obj.scale) obj.model.display() glPopMatrix() glPopMatrix()
def flush_labels(self): gl.glClear(gl.GL_DEPTH_BUFFER_BIT) gl.glPushMatrix() gl.glTranslatef(-self.game.camera_x * defs.WINDOW_SCALE[0], -self.game.camera_y * defs.WINDOW_SCALE[1], 0) for label, x, y, scale in self.labels: if scale: gl.glPushMatrix() label.anchor_x = 'center' label.anchor_y = 'center' gl.glTranslatef(x * defs.WINDOW_SCALE[0], y * defs.WINDOW_SCALE[1], 0) gl.glScalef(*scale) label.x = label.y = 0 label.draw() gl.glPopMatrix() else: label.x = x * defs.WINDOW_SCALE[0] label.y = y * defs.WINDOW_SCALE[1] label.draw() self.labels = [] gl.glColor3f(1, 1, 1) gl.glEnable(gl.GL_DEPTH_TEST) gl.glPopMatrix() # self.fps_label.draw() self.game.score.draw()
def render(self, draw_bbox): """ Renders the object to screen """ if not self.visible: return from pyglet import gl # Draw the bounding box if draw_bbox: gl.glColor3f(1, 0, 0) gl.glBegin(gl.GL_LINE_LOOP) gl.glVertex3f(self.obj_corners.T[0, 0], 0.01, self.obj_corners.T[1, 0]) gl.glVertex3f(self.obj_corners.T[0, 1], 0.01, self.obj_corners.T[1, 1]) gl.glVertex3f(self.obj_corners.T[0, 2], 0.01, self.obj_corners.T[1, 2]) gl.glVertex3f(self.obj_corners.T[0, 3], 0.01, self.obj_corners.T[1, 3]) gl.glEnd() gl.glPushMatrix() gl.glTranslatef(*self.pos) gl.glScalef(self.scale, self.scale, self.scale) gl.glRotatef(self.y_rot, 0, 1, 0) gl.glColor3f(*self.color) self.mesh.render() gl.glPopMatrix()
def draw_all_objects(): window.clear() background.blit_tiled(0, 0, 0, window.width, window.height) if (game_over): game_over_label = pyglet.text.Label('Game Over', font_name='League Gothic', font_size=40, x=window.width / 2, y=window.height / 2, anchor_x='center') game_over_label.draw() for x_offset in (-window.width, 0, window.width): for y_offset in (-window.height, 0, window.height): # Remember the current state gl.glPushMatrix() # Move everything drawn from now on by (x_offset, y_offset, 0) gl.glTranslatef(x_offset, y_offset, 0) # Draw batch.draw() # Restore remembered state (this cancels the glTranslatef) gl.glPopMatrix()
def set_3d(self): """ Configure OpenGL to draw in 3d. """ width, height = self.get_size() gl.glEnable(gl.GL_DEPTH_TEST) gl.glViewport(0, 0, width, height) gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() gl.gluPerspective(65.0, width / float(height), 0.1, DIST) gl.glMatrixMode(gl.GL_MODELVIEW) gl.glLoadIdentity() x, y = self.rotation gl.glRotatef(x, 0, 1, 0) gl.glRotatef(-y, math.cos(math.radians(x)), 0, math.sin(math.radians(x))) x, y, z = self.position gl.glTranslatef(-x, -y, -z) gl.glEnable(gl.GL_LIGHTING) gl.glLightModelfv(gl.GL_LIGHT_MODEL_AMBIENT, GLfloat4(0.05, 0.05, 0.05, 1.0)) gl.glEnable(gl.GL_COLOR_MATERIAL) gl.glColorMaterial(gl.GL_FRONT, gl.GL_AMBIENT_AND_DIFFUSE) #gl.glLightfv(gl.GL_LIGHT1,gl.GL_SPOT_DIRECTION, GLfloat3(0,0,-1)) gl.glLightfv(gl.GL_LIGHT1, gl.GL_AMBIENT, GLfloat4(0.5, 0.5, 0.5, 1.0)) gl.glLightfv(gl.GL_LIGHT1, gl.GL_DIFFUSE, GLfloat4(1.0, 1.0, 1.0, 1.0)) gl.glLightfv(gl.GL_LIGHT1, gl.GL_POSITION, GLfloat4(0.35, 1.0, 0.65, 0.0)) #gl.glLightfv(gl.GL_LIGHT0,gl.GL_SPECULAR, GLfloat4(1,1,1,1)) gl.glDisable(gl.GL_LIGHT0) gl.glEnable(gl.GL_LIGHT1)
def pan(self): """Performs panning (translation). Suggested to use with pan during displazing the scene: 1.cam.zoomAndRotate() 2.cam.pan() 3.draw(some 3D objects) """ glTranslatef(-self.position.x, -self.position.y, -self.position.z)
def translate(self, dx, dy, dz): glPushMatrix() glLoadIdentity() glTranslatef(dx, dy, dz) glMultMatrixf(self.matrix) self.matrix = get_model_matrix() glPopMatrix()
def calculate_position(self): if self.speed == 0: return y = self.speed * math.cos(math.radians(self.car.rotation)) x = self.speed * math.sin(math.radians(self.car.rotation)) for i in range(len(self.previous_direction)): direction = self.previous_direction[i] self.car.x += (direction.x * (i + 1) / 30) self.car.y += (direction.y * (i + 1) / 30) self.car.x += (x * 0.5) self.car.y += (y * 0.5) self.previous_direction.append( dotmap.DotMap(x=x, y=y, rotation=self.car.rotation)) if len(self.previous_direction) > 5: self.previous_direction.pop(0) config.camera_position[0] -= x config.camera_position[1] -= y glTranslatef(-x, -y, 0) self.speed_label.x += x self.speed_label.y += y self.calculate_checkpoint() self.set_segments_visible() if self.calculate_collision(): self.speed = 0 self.stop = True self.replay["distance"] = self.segment self.handle_stop()
def draw_circle(xpos, ypos, size, hsv): gl.glPushMatrix() gl.glTranslatef(xpos, ypos, 0) gl.glScalef(size, size, size) gl.glColor3f(*hsv_to_rgb(*hsv)) CIRCLE_VERTS.draw(gl.GL_TRIANGLE_FAN) gl.glPopMatrix()
def draw_square(xpos, ypos, height, width, hsv): gl.glPushMatrix() gl.glTranslatef(xpos, ypos, 0) gl.glScalef(height, width, 1) gl.glColor3f(*hsv_to_rgb(*hsv)) SQUARE_VERTS.draw(gl.GL_TRIANGLE_STRIP) gl.glPopMatrix()
def draw_vector(initial_point, vector, width, arrow_head_size, color): length = np.linalg.norm(vector) # orthogonal vector used for constructing vertices that make up # arrow shape w = (vector[1]/length, -vector[0]/length) # factor scaling vector such that it only reaches to arrow head and not to tip rescale = (length - arrow_head_size)/length v_re = [rescale*x for x in vector] gl.glPushMatrix() gl.glTranslatef(initial_point[0], initial_point[1], 0.) gl.glBegin(gl.GL_TRIANGLES) gl.glColor3f(*color) # rectangle gl.glVertex2f( 0.5*w[0]*width, 0.5*w[1]*width) gl.glVertex2f( 0.5*w[0]*width + v_re[0], 0.5*w[1]*width + v_re[1]) gl.glVertex2f(-0.5*w[0]*width + v_re[0], -0.5*w[1]*width + v_re[1]) gl.glVertex2f(-0.5*w[0]*width + v_re[0], -0.5*w[1]*width + v_re[1]) gl.glVertex2f(-0.5*w[0]*width, -0.5*w[1]*width) gl.glVertex2f( 0.5*w[0]*width, 0.5*w[1]*width) # arrow head gl.glVertex2f( 0.5*w[0]*arrow_head_size + v_re[0], 0.5*w[1]*arrow_head_size + v_re[1]) gl.glVertex2f(-0.5*w[0]*arrow_head_size + v_re[0], -0.5*w[1]*arrow_head_size + v_re[1]) gl.glVertex2f(vector[0], vector[1]) gl.glEnd() gl.glPopMatrix()
def on_draw(*args): global time, particles t = time / FRAMES window.clear() gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() gl.gluPerspective(90, 1, 0.1, 100) gl.glTranslatef(0, 0, -10) gl.glRotatef(t * 360, 0, 1, 0) # gl.glRotatef(t * 360, 1, 0, 0) batch = pyglet.graphics.Batch() if time == FRAMES: exit() points = [] for i in range(K): points.append(gen_point((i + t) / K, i)) # print(points) points_to_lines(points, batch) batch.draw() save_frame(time % FRAMES) time += 1
def drawBody(self, body): # type: (Dict[str, Any]) -> None """Draw a body""" pos = body['position'] rpy = body['rotation'] r, p, y = rpy[0], rpy[1], rpy[2] gl.glPushMatrix() gl.glTranslatef(pos[0], pos[1], pos[2]) gl.glRotatef(np.rad2deg(y), 0.0, 0.0, 1.0) gl.glRotatef(np.rad2deg(p), 0.0, 1.0, 0.0) gl.glRotatef(np.rad2deg(r), 1.0, 0.0, 0.0) self.drawCoords() rel_pos = body['center'] gl.glTranslatef(rel_pos[0], rel_pos[1], rel_pos[2]) transparent = 'transparent' in body and body['transparent'] dim = body['size3'] gl.glScalef(dim[0], dim[1], dim[2]) self.setMaterial(body['material']) if body['geometry'] is 'box': if transparent: gl.glPolygonMode(gl.GL_FRONT_AND_BACK, gl.GL_LINE) # Wireframe self.drawCube() if transparent: gl.glPolygonMode(gl.GL_FRONT_AND_BACK, gl.GL_FILL) elif body['geometry'] is 'mesh': self.drawMesh(body['name']) gl.glPopMatrix()
def on_draw(): window.clear() # Reset the "eye" back to the default location. glLoadIdentity() # Move the "eye" to the current location on the map. glTranslatef(delta[0], delta[1], 0.0) batch.draw()
def paint(self, peg): batch = self.get_batch() glPushMatrix() glTranslatef(peg.x, peg.y, 0) glRotatef(peg.angle, 0, 0, 1) batch.draw() glPopMatrix()
def draw(self): """Render the view object""" gl.glPushMatrix() gl.glTranslatef(self.x, self.y, 0) gl.glRotatef(self.angle, *self.axis) self.draw_aligned() gl.glPopMatrix()
def key_handler(): global sleep_counter, label # exit if keys[key.ESCAPE]: pyglet.app.exit() if not config.replay: # acceleration and breaking if keys[key.UP] == keys[key.DOWN]: config.car.rolling = True config.car.accelerate = 0 elif keys[key.UP]: config.car.accelerate = 0.03 config.car.rolling = False elif keys[key.DOWN]: config.car.accelerate = -0.1 config.car.rolling = False # turning if keys[key.RIGHT] == keys[key.LEFT]: config.car.rotate(0) elif keys[key.RIGHT]: config.car.rotate(1) elif keys[key.LEFT]: config.car.rotate(-1) if keys[key.SPACE]: config.car.drifting = True else: config.car.drifting = False # restart if keys[key.R]: glTranslatef(-config.camera_position[0], -config.camera_position[1], 0) config.camera_position = [0, 0] label.text = "" sleep_counter = 0 config.car = Car(config.track)
def render(self, draw_bbox, annotated=False): """ Renders the object to screen """ if not self.visible: return from pyglet import gl # Draw the bounding box if draw_bbox: gl.glColor3f(1, 0, 0) gl.glBegin(gl.GL_LINE_LOOP) gl.glVertex3f(self.obj_corners.T[0, 0], 0.01, self.obj_corners.T[1, 0]) gl.glVertex3f(self.obj_corners.T[0, 1], 0.01, self.obj_corners.T[1, 1]) gl.glVertex3f(self.obj_corners.T[0, 2], 0.01, self.obj_corners.T[1, 2]) gl.glVertex3f(self.obj_corners.T[0, 3], 0.01, self.obj_corners.T[1, 3]) gl.glEnd() gl.glPushMatrix() gl.glTranslatef(*self.pos) gl.glScalef(self.scale, self.scale, self.scale) gl.glRotatef(self.y_rot, 0, 1, 0) gl.glColor3f(*self.color) annotated = annotated and self.on_driveable_tile if annotated and self.annotated_mesh is not None: self.annotated_mesh.render(annotated=annotated) else: self.mesh.render(annotated=annotated) gl.glPopMatrix()
def on_draw(self): """ Render the screen. """ start = time.time() float_size = ctypes.sizeof(ctypes.c_float) record_len = 10 * float_size GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT) GL.glMatrixMode(GL.GL_MODELVIEW) GL.glEnableClientState(GL.GL_VERTEX_ARRAY) GL.glColor4ub(255, 0, 0, 255) GL.glBindBuffer(GL.GL_ARRAY_BUFFER, self.rect_vbo.vbo_id) GL.glVertexPointer(2, GL.GL_FLOAT, record_len, 0) for i in range(len(self.shape_list)): shape = self.shape_list[i] GL.glLoadIdentity() GL.glTranslatef(shape.x, shape.y, 0) GL.glDrawArrays(GL.GL_QUADS, i * 8, 8) # GL.glDrawArrays(GL.GL_QUADS, # 0, # self.rect_vbo.size) elapsed = time.time() - start print(elapsed)
def render(self): glTranslatef(mouse[0], mouse[1], 0) for bubble in self.data: bubble.render() glTranslatef(-mouse[0], -mouse[1], 0)
def draw(): window.clear() for x in range(0, window.width + space_img.width, space_img.width): for y in range(0, window.height + space_img.height, space_img.height): space_img.blit(x=x, y=y) exhaust_batch.draw() for y_offset in (-window.height, 0, window.height): gl.glPushMatrix() gl.glTranslatef(0, y_offset, 0) ufo_batch.draw() gl.glPopMatrix() for x_offset in (-window.width, 0, window.width): for y_offset in (-window.height, 0, window.height): gl.glPushMatrix() gl.glTranslatef(x_offset, y_offset, 0) main_batch.draw() gl.glPopMatrix() for i in range(ship.lives): life_sprite.y = 40 life_sprite.x = 30 + 40 * i life_sprite.draw() level_label.draw()
def on_draw(): window.clear() # Reset the "eye" back to the default location. glLoadIdentity() # Move the "eye" to the current location on the map. # glTranslatef(delta[0], delta[1], 0.0) offset = int(-mech.x+(window.width/2)-(mech.frame_size[0]/2)), int(-mech.y+(window.height/2)-(mech.frame_size[1]/2)) glTranslatef(offset[0], offset[1], 0.0) ui_manager.frame_offset = offset # TODO: [21:03] thorbjorn: DR0ID_: You can generally determine the range of tiles that are visible before your drawing loop, which is much faster than looping over all tiles and checking whether it is visible for each of them. # [21:06] DR0ID_: probably would have to rewrite the pyglet demo to use a similar render loop as you mentioned # [21:06] thorbjorn: Yeah. # [21:06] DR0ID_: I'll keep your suggestion in mind, thanks # [21:06] thorbjorn: I haven't written a specific OpenGL renderer yet, so not sure what's the best approach for a tile map. # [21:07] thorbjorn: Best to create a single texture with all your tiles, bind it, set up your vertex arrays and fill it with the coordinates of the tiles currently on the screen, and then let OpenGL draw the bunch. # [21:08] DR0ID_: for each layer? # [21:08] DR0ID_: yeah, probably a good approach # [21:09] thorbjorn: Ideally for all layers at the same time, if you don't have to draw anything in between. # [21:09] DR0ID_: well, the NPC and other dynamic things need to be drawn in between, right? # [21:09] thorbjorn: Right, so maybe once for the bottom layers, then your complicated stuff, and then another time for the layers on top. game.tiles.draw() game.humans.draw() game.characters.draw() game.projectile_batch.draw() game.structure_batch.draw() glLoadIdentity() glTranslatef(0, 0, 0.0) ui_manager.batch.draw() game.message_queue.draw()
def draw(self): # set up projection gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() gl.glViewport(self.x, self.y, self.width, self.height) gl.glOrtho(0, self.width, 0, self.height, self.near, self.far) gl.glMatrixMode(gl.GL_MODELVIEW) fx, fy = self._determine_focus() w2 = self.width / 2 h2 = self.height / 2 x1, y1 = fx - w2, fy - h2 x2, y2 = fx + w2, fy + h2 gl.glPushMatrix() gl.glTranslatef(self.width / 2 - fx, self.height / 2 - fy, 0) for layer in self.layers: if hasattr(layer, 'x'): translate = layer.x or layer.y else: translate = False if translate: gl.glPushMatrix() gl.glTranslatef(layer.x, layer.y, 0) layer.draw() if translate: gl.glPopMatrix() gl.glPopMatrix()
def __enter__(self): gl.glPushMatrix() gl.glLoadIdentity() x, y = self.position gl.glTranslatef(self.w // 2, self.h // 2, 0) gl.glRotatef(degrees(self.angle), 0, 0, -1) gl.glTranslatef(-int(x), -int(y), 0)
def _setup_3d(self): w, h = self.get_size() gl.glEnable(gl.GL_DEPTH_TEST) gl.glDepthFunc(gl.GL_LEQUAL) gl.glEnable(gl.GL_DEPTH_TEST) gl.glEnable(gl.GL_CULL_FACE) viewport = self.get_viewport_size() gl.glViewport(0, 0, max(1, viewport[0]), max(1, viewport[1])) gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() gl.gluPerspective(*self.perspective) gl.glMatrixMode(gl.GL_MODELVIEW) gl.glLoadIdentity() y, x = self.rotation gl.glRotatef(x, 0, 1, 0) gl.glRotatef(-y, math.cos(math.radians(x)), 0, math.sin(math.radians(x))) # NOTE: for GL render, its x-z plane is the ground plane, # so we unpack the position using `(x, z, y)` instead of `(x, y, z)` x, z, y = self.position y += self.perspective_over_drone[0] z += self.perspective_over_drone[1] gl.glTranslatef(-x, -y, -z)
def SetOrigin(self): size = self.GetVirtualSize() self.SetScrollbar(wx.HORIZONTAL, self.GetScrollPos(wx.HORIZONTAL), size[0], self.map.width * 32 * self.zoom, refresh=True) self.SetScrollbar(wx.VERTICAL, self.GetScrollPos(wx.VERTICAL), size[1], self.map.height * 32 * self.zoom, refresh=True) size = self.GetGLExtents() if size.width <= 0: size.width = 1 if size.height <= 0: size.height = 1 self.tilemap.updateDimmingSprite( int(size.width) + 2, int(size.height) + 2, 1 / self.zoom) gl.glViewport(0, 0, size.width, size.height) gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() gl.glOrtho( 0, size.width / self.zoom, 0, size.height / self.zoom, -1, 1) x = (-self.GetScrollPos(wx.HORIZONTAL)) / self.zoom y = ((-(self.map.height * 32) + size.height / self.zoom) + self.GetScrollPos(wx.VERTICAL) / self.zoom) gl.glTranslatef(x, y, 0) self.translateX = -x + size.width / 2 / self.zoom self.translateY = -y + size.height / 2 / self.zoom self.onscreenwidth = int(size.width / self.zoom) self.onscreenheight = int(size.height / self.zoom) self.tilemap.setDimXY(self.translateX - 1, self.translateY + 1) gl.glMatrixMode(gl.GL_MODELVIEW)
def render(self): """ Draws Line """ p1 = self.a2 p2 = self.b2 gl.glColor4f(*self.color) color = (gl.GLfloat * 4)(*self.color) gl.glPushMatrix() gl.glTranslatef(self.x, self.y, -self.z) # translate to GL loc ppint gl.glRotatef(self.rotation, 0, 0, 0.1) if self.style: gl.glEnable(gl.GL_LINE_STIPPLE) gl.glLineStipple(1, self.style) ## else : ## glDisable(GL_LINE_STIPPLE) if self.stroke <= 0: self.stroke = 1 gl.glLineWidth(self.stroke) gl.glBegin(gl.GL_LINES) gl.glVertex2i(int(p1[0]), int(p1[1])) # draw pixel points gl.glVertex2i(int(p2[0]), int(p2[1])) gl.glEnd() if self.style: gl.glDisable(gl.GL_LINE_STIPPLE) gl.glPopMatrix()
def on_draw(self): pyglet.gl.glClearColor(0.2,0.2,0.8,1) self.clear() gl.glPushMatrix() gl.glTranslatef(self.camera[0], self.camera[1], 0) pyglet.gl.glColor4f(1,1,1,1) for territory in self.landmass.land_terrs: for tri in territory.triangles: pyglet.gl.glColor4f(*territory.color) pyglet.graphics.draw( 3, pyglet.gl.GL_TRIANGLES, ('v2f', tri) ) for line in territory.lines: pyglet.gl.glColor4f(*line.color) self.draw_line(line.a.x, line.a.y, line.b.x, line.b.y) for terr in self.landmass.sea_terrs: for line in terr.lines: pyglet.gl.glColor4f(*line.color) self.draw_line(line.a.x, line.a.y, line.b.x, line.b.y) if self.draw_capitals: for terr in itertools.chain(self.landmass.land_terrs, self.landmass.sea_terrs): pyglet.gl.glColor4f(1,1,1,1) w = terr.label.content_width/2+2 h = terr.label.content_height/2 self.draw_rect(terr.x-w, terr.y-h, terr.x+w, terr.y+h) pyglet.gl.glColor4f(1,1,1,1) self.batch.draw() gl.glPopMatrix()
def render(self, mode='human', close=False): if mode == 'human': if self.window is None: self.window = pyglet.window.Window( self.map.width * tile_width, self.map.height * tile_width) glTranslatef(-1, 1, 0) glScalef(2 / self.map.width / tile_width, -2 / self.map.height / tile_width, 1) self.batch = pyglet.graphics.Batch() self.circle = shapes.Circle(self.car.pos.x * tile_width, self.car.pos.y * tile_width, tile_width / 4, color=(255, 128, 128), batch=self.batch) self.map.draw(self.batch) self.circle.x = self.car.pos.x * tile_width self.circle.y = self.car.pos.y * tile_width self.window.clear() # Draw board self.batch.draw() # Draw car self.circle.draw() self.window.flip() if mode == 'trajectories': if self.window is None: self.window = pyglet.window.Window( self.map.width * tile_width, self.map.height * tile_width) glTranslatef(-1, 1, 0) glScalef(2 / self.map.width / tile_width, -2 / self.map.height / tile_width, 1) self.batch = pyglet.graphics.Batch() self.map.draw(self.batch) self.window.clear() self.batch.draw() self.window.flip()
def draw_ents(self, ents): for ent in ents: glPushMatrix() glTranslatef(ent.body.position.x, ent.body.position.y, 0) glRotatef(ent.body.angle * 180 / pi, 0, 0, 1) ent.batch.draw() glPopMatrix()
def nakresli_tank(x, y, rotace, barva): """Nakresli tank na dane pozici, s rotaci a v dane barve.""" # Zacentruj kresleni na stred tela tanku. gl.glTranslatef(x, y, 0.0) # Aplikuj aktualni rotaci. gl.glRotatef(rotace, 0.0, 0.0, 1.0) # Nakresli telo tanku ve zvolene barve. gl.glColor3f(*barva) gl.glBegin(gl.GL_TRIANGLE_FAN) gl.glVertex2f(-TANK_SIRKA / 2, -TANK_DELKA / 2) gl.glVertex2f(-TANK_SIRKA / 2, +TANK_DELKA / 2) gl.glVertex2f(+TANK_SIRKA / 2, +TANK_DELKA / 2) gl.glVertex2f(+TANK_SIRKA / 2, -TANK_DELKA / 2) gl.glEnd() # Nakresli hlaven tanku ve standardni barve. gl.glColor3f(*BARVA_HLAVNE) gl.glBegin(gl.GL_TRIANGLE_FAN) gl.glVertex2f(-HLAVEN_SIRKA / 2, -HLAVEN_DELKA / 2 + HLAVEN_POSUN) gl.glVertex2f(-HLAVEN_SIRKA / 2, +HLAVEN_DELKA / 2 + HLAVEN_POSUN) gl.glVertex2f(+HLAVEN_SIRKA / 2, +HLAVEN_DELKA / 2 + HLAVEN_POSUN) gl.glVertex2f(+HLAVEN_SIRKA / 2, -HLAVEN_DELKA / 2 + HLAVEN_POSUN) gl.glEnd() # Vrat rotaci a centrovani do puvodniho stavu. gl.glRotatef(-rotace, 0.0, 0.0, 1.0) gl.glTranslatef(-x, -y, 0.0)
def draw(self): """Render the particle.""" gl.glPushMatrix() gl.glTranslatef(self.x, self.y, self.z) gl.glRotatef(self.angle, *self.axis) self.draw_aligned() gl.glPopMatrix()
def render(self): """ Draw Circle x, y, z, width in pixel, rotation, color and line width in px style choices are : GLU_LINE, GLU_FILL, GLU_SILHOUETTE, GLU_POINT TO DO : textured circles """ gl.glColor4f(*self.color) gl.glPushMatrix() gl.glTranslatef(self.x, self.y, -self.z) gl.glRotatef(*self.rotation) #angle, bool per axis (x,y,z) if self.radius < 1: self.radius = 1 if self.stroke: inner = self.radius - self.stroke # outline width if inner < 0: inner = 0 else: inner = 0 # filled gl.gluQuadricDrawStyle(self.q, self.style) gl.gluDisk(self.q, inner, self.radius, self.circleresolution, 1) # gluDisk(quad, inner, outer, slices, loops) gl.glPopMatrix()
def on_draw (): window.clear() gl.glLoadIdentity() gl.glTranslatef(0, -128, -512) gl.glRotatef(rotation[1],0,1,0) vertex_list.draw(gl.GL_LINES) window.invalid = False
def draw(self, frame): # The gneneral plan here is: # 1. Get the dots in the range of 0-255. # 2. Create a texture with the dots data. # 3. Draw the texture, scaled up with nearest-neighbor. # 4. Draw a mask over the dots to give them a slightly more realistic look. gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) gl.glLoadIdentity() # Draw the dots in this color: #gl.glColor3f(1.0, 0.5, 0.25) gl.glScalef(1, -1, 1) gl.glTranslatef(0, -DMD_SIZE[1]*DMD_SCALE, 0) #data = frame.get_data_mult() #this new jk_get_data will read the dots using the dmd function #and convert them via the map to rGB. data = self.jk_get_data(frame) image = pyglet.image.ImageData(DMD_SIZE[0], DMD_SIZE[1], 'RGB', data, pitch=DMD_SIZE[0] * 3) gl.glTexParameteri(image.get_texture().target, gl.GL_TEXTURE_MAG_FILTER, gl.GL_NEAREST) image.blit(0, 0, width=DMD_SIZE[0]*DMD_SCALE, height=DMD_SIZE[1]*DMD_SCALE) del image gl.glScalef(DMD_SCALE/float(MASK_SIZE), DMD_SCALE/float(MASK_SIZE), 1.0) gl.glColor4f(1.0, 1.0, 1.0, 1.0) self.mask_texture.blit_tiled(x=0, y=0, z=0, width=DMD_SIZE[0]*MASK_SIZE, height=DMD_SIZE[1]*MASK_SIZE)
def on_draw(): gl.glMatrixMode(gl.GL_MODELVIEW) gl.glLoadIdentity() # Gradient sky l, b = world_to_screen((0, 0)) r, t = world_to_screen((W, H)) horizon = 177 / 255.0, 202 / 255.0, 1.0 zenith = 68 / 255.0, 0.5, 1.0 pyglet.graphics.draw(4, gl.GL_QUADS, ('v2f', [l, b, l, t, r, t, r, b]), ('c3f', sum([horizon, zenith, zenith, horizon], ())), ) cx, cy = camera tx, ty = world_to_screen((-cx + W * 0.5, -cy + H * 0.5)) gl.glTranslatef(tx, ty, 0) batch.draw() # Water l, b = world_to_screen((0, 0)) r, t = world_to_screen((1000, WATER_LEVEL)) gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) pyglet.graphics.draw(4, gl.GL_QUADS, ('v2f', [l, b, l, t, r, t, r, b]), ('c4f', [0, 0.2, 0.8, 0.5] * 4), )
def draw_world_items(self, glyphs): ''' Draw all passed glyphs ''' shader = None for position, orientation, glyph in glyphs: gl.glPushMatrix() gl.glTranslatef(*position) if orientation and orientation != Orientation.Identity: gl.glMultMatrixf(orientation.matrix) if glyph.shader is not shader: shader = glyph.shader shader.use() gl_wrap.glBindVertexArray(glyph.vao) gl.glDrawElements( gl.GL_TRIANGLES, len(glyph.glindices), glyph.index_type, glyph.glindices ) gl.glPopMatrix() gl_wrap.glBindVertexArray(0) Shader.unuse()
def circle(pos, radius, color=(1.0,1.0,1.0), alpha=1.0,segments=6): """ Draws a circle with gluDisk :param pos: center of the circle :type pos: 2-float tuple :param radius: radius of the circle :type radius: float :param color: the color in [0..1] range :type color: 3-float tuple :param alpha: the alpha value in [0..1] range :param segments: number of segments :type segments: int """ glDisable(GL_TEXTURE_2D) c = gluNewQuadric() glColor4f(color[0], color[1], color[2], alpha) glPushMatrix() glTranslatef(pos[0], pos[1], 0) gluDisk(c, 0, radius, segments, 1) glPopMatrix() glColor4f(1.0,1.0,1.0,1.0) glEnable(GL_TEXTURE_2D)
def draw(): window.clear() for x in range(0, window.width+space_img.width, space_img.width): for y in range(0, window.height+space_img.height, space_img.height): space_img.blit(x=x, y=y) exhaust_batch.draw() for y_offset in (-window.height, 0, window.height): gl.glPushMatrix() gl.glTranslatef(0, y_offset, 0) ufo_batch.draw() gl.glPopMatrix() for x_offset in (-window.width, 0, window.width): for y_offset in (-window.height, 0, window.height): gl.glPushMatrix() gl.glTranslatef(x_offset, y_offset, 0) main_batch.draw() gl.glPopMatrix() for i in range(ship.lives): life_sprite.y = 40 life_sprite.x = 30 + 40 * i life_sprite.draw() level_label.draw()
def translate(self,dx,dy,dz): glPushMatrix() glLoadIdentity() glTranslatef(dx,dy,dz) glMultMatrixf(self.matrix) self.matrix=get_model_matrix() glPopMatrix()
def draw(self): glPushMatrix() glTranslatef(self.xoffset, self.yoffset, self.zoffset) def color(i): if i % self.graduations_major == 0: glColor4f(*self.color_grads_major) elif i % (self.graduations_major / 2) == 0: glColor4f(*self.color_grads_interm) else: if self.light: return False glColor4f(*self.color_grads_minor) return True # draw the grid glBegin(GL_LINES) for i in range(0, int(math.ceil(self.width + 1))): if color(i): glVertex3f(float(i), 0.0, 0.0) glVertex3f(float(i), self.depth, 0.0) for i in range(0, int(math.ceil(self.depth + 1))): if color(i): glVertex3f(0, float(i), 0.0) glVertex3f(self.width, float(i), 0.0) glEnd() # draw fill glColor4f(*self.color_fill) glRectf(0.0, 0.0, float(self.width), float(self.depth)) glPopMatrix()
def set_state(self): gl.glPushAttrib(gl.GL_ENABLE_BIT | gl.GL_TRANSFORM_BIT | gl.GL_CURRENT_BIT) gl.glEnable(gl.GL_BLEND) gl.glBlendFuncSeparate(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA, gl.GL_ONE, gl.GL_ONE_MINUS_SRC_ALPHA) # To Allow Normal Rendering when Buffering with FrameBufferObject # Without this option : problem with alpha blending when rendering buffered GUI textures #Also in context.glContext # Disable clipping planes to check culling. gl.glEnable(gl.GL_CLIP_PLANE0) gl.glEnable(gl.GL_CLIP_PLANE1) gl.glEnable(gl.GL_CLIP_PLANE2) gl.glEnable(gl.GL_CLIP_PLANE3) # Left gl.glClipPlane(gl.GL_CLIP_PLANE0, (gl.GLdouble * 4)( 1, 0, 0, -(self._clip_x - 1))) # Top gl.glClipPlane(gl.GL_CLIP_PLANE1, (gl.GLdouble * 4)( 0, -1, 0, self._clip_y)) # Right gl.glClipPlane(gl.GL_CLIP_PLANE2, (gl.GLdouble * 4)( -1, 0, 0, self._clip_x + self._clip_width + 1)) # Bottom gl.glClipPlane(gl.GL_CLIP_PLANE3, (gl.GLdouble * 4)( 0, 1, 0, -(self._clip_y - self._clip_height))) gl.glTranslatef(self.translate_x, self.translate_y, 0)
def rotate(p, state, kwargs): x, y = p.x, p.y xp = x + (p.width / 2) yp = y + (p.height / 2) gl.glTranslatef(xp, yp, 0) gl.glRotatef(kwargs['angle'], 0.0, 0.0, 1.0) gl.glTranslatef(-xp, -yp, 0)
def set_3d(self): """ Configure OpenGL to draw in 3d. """ width, height = self.get_size() gl.glEnable(gl.GL_DEPTH_TEST) gl.glViewport(0, 0, width, height) gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() gl.gluPerspective(65.0, width / float(height), 0.1, DIST) gl.glMatrixMode(gl.GL_MODELVIEW) gl.glLoadIdentity() x, y = self.rotation gl.glRotatef(x, 0, 1, 0) gl.glRotatef(-y, math.cos(math.radians(x)), 0, math.sin(math.radians(x))) x, y, z = self.position gl.glTranslatef(-x, -y, -z) gl.glEnable(gl.GL_LIGHTING) gl.glLightModelfv(gl.GL_LIGHT_MODEL_AMBIENT, GLfloat4(0.05,0.05,0.05,1.0)) gl.glEnable(gl.GL_COLOR_MATERIAL) gl.glColorMaterial(gl.GL_FRONT, gl.GL_AMBIENT_AND_DIFFUSE) #gl.glLightfv(gl.GL_LIGHT1,gl.GL_SPOT_DIRECTION, GLfloat3(0,0,-1)) gl.glLightfv(gl.GL_LIGHT1, gl.GL_AMBIENT, GLfloat4(0.5,0.5,0.5,1.0)) gl.glLightfv(gl.GL_LIGHT1, gl.GL_DIFFUSE, GLfloat4(1.0,1.0,1.0,1.0)) gl.glLightfv(gl.GL_LIGHT1, gl.GL_POSITION, GLfloat4(0.35,1.0,0.65,0.0)) #gl.glLightfv(gl.GL_LIGHT0,gl.GL_SPECULAR, GLfloat4(1,1,1,1)) gl.glDisable(gl.GL_LIGHT0) gl.glEnable(gl.GL_LIGHT1)
def draw(self): self.loadStartPosition() gl.glRotatef(90.0, 0.0, 0.0, 1.0) gl.glBegin(gl.GL_QUADS) gl.glColor3f(1.0, 1.0, 0.0) tenth = math.pi * 2.0 / 10.0 for z in [-0.1, 0.1]: for i in xrange(5): a = float(i) * tenth * 2.0 gl.glVertex3f(0.0, 0.0, z) gl.glVertex3f(0.4 * math.cos(a - tenth), 0.4 * math.sin(a - tenth), z) gl.glVertex3f(math.cos(a), math.sin(a), z) gl.glVertex3f(0.4 * math.cos(a + tenth), 0.4 * math.sin(a + tenth), z) for i in xrange(5): a = float(i) * tenth * 2.0 gl.glVertex3f(0.4 * math.cos(a - tenth), 0.4 * math.sin(a - tenth), 0.1) gl.glVertex3f(math.cos(a), math.sin(a), 0.1) gl.glVertex3f(math.cos(a), math.sin(a), -0.1) gl.glVertex3f(0.4 * math.cos(a - tenth), 0.4 * math.sin(a - tenth), -0.1) gl.glVertex3f(0.4 * math.cos(a + tenth), 0.4 * math.sin(a + tenth), 0.1) gl.glVertex3f(math.cos(a), math.sin(a), 0.1) gl.glVertex3f(math.cos(a), math.sin(a), -0.1) gl.glVertex3f(0.4 * math.cos(a + tenth), 0.4 * math.sin(a + tenth), -0.1) gl.glEnd() self.loadStartPosition() gl.glTranslatef(0.0, 0.0, 0.1) gl.glScalef(0.01, 0.01, 0.0) self.label.draw() gl.glLoadIdentity()
def draw(self, scale=1): gl.glPushMatrix() gl.glEnable(gl.GL_TEXTURE_2D) gl.glTranslatef(*self.position) gl.glRotatef(self.angle, 0, 0, 1) gl.glScalef(scale, scale, scale) self.batch.draw() gl.glPopMatrix()
def draw(self): with gl_matrix(): gl.glLoadIdentity() gl.glTranslatef(0, self.game.camera.y * -0.5, 0) with gl_state(): gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) self.background.blit(-100, -300, height=1600, width=window.width+200)
def on_draw(): glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glLoadIdentity() glTranslatef(0, 0, -4) glRotatef(rz, 0, 0, 1) glRotatef(ry, 0, 1, 0) glRotatef(rx, 1, 0, 0) torus.draw()
def draw(self): if self.gamePlayer is None or self.gamePlayer.finished: return player = self.player glPushMatrix() glTranslatef(self.x, self.y - self.height, 0) self.gamePlayer.draw() glPopMatrix()
def draw(self): for x,y in self.grid: opengl.glPushMatrix() opengl.glTranslatef(x*self.grid_size, y*self.grid_size, 0) opengl.glColor3f(self.color[0], self.color[1], self.color[2]) self.vlist.draw(pyglet.gl.GL_LINE_LOOP) opengl.glPopMatrix() if self.secondary is not None: self.secondary.draw()
def draw(self): glPushMatrix() glRotatef(self.angle, 0., 0., 1.) glScalef(self.scale, 1., 1.) glTranslatef(0., -common_res.ray.height/2, 0.) glColor4f(1., 1., 1., self.alpha) common_res.ray.blit(0, 0) glPopMatrix()
def draw(self): with gl_matrix(): if self.game_is_over: pass with gl_matrix(): gl.glTranslatef(self.pxHorizontalShift, self.pxVerticalShift, 0) for heart in self.hearts: heart.draw()