本文整理汇总了Python中pyglet.graphics.Batch.add方法的典型用法代码示例。如果您正苦于以下问题:Python Batch.add方法的具体用法?Python Batch.add怎么用?Python Batch.add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyglet.graphics.Batch
的用法示例。
在下文中一共展示了Batch.add方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Body
# 需要导入模块: from pyglet.graphics import Batch [as 别名]
# 或者: from pyglet.graphics.Batch import add [as 别名]
class Body(object):
"A list of primitives. Creates a single batch to draw these primitives."
def __init__(self, items=None):
self.primitives = []
if items:
self.add_items(items)
self.batch = None
def add_items(self, items):
"'items' may contain primitives and/or bodys"
for item in items:
if isinstance(item, Body):
for prim in item.primitives:
self.primitives.append(prim)
else:
self.primitives.append(item)
def batch_draw(self):
if self.batch is None:
self.batch = Batch()
for primitive in self.primitives:
batchVerts = \
[primitive.verts[0], primitive.verts[1]] + \
primitive.verts + \
[primitive.verts[-2], primitive.verts[-1]]
numverts = len(batchVerts) / 2
self.batch.add(
numverts,
primitive.primtype,
None, # draw order group to be implemented later
('v2f/static', batchVerts),
('c3B/static', primitive.color * numverts),
)
self.batch.draw()
示例2: cif_power_strip
# 需要导入模块: from pyglet.graphics import Batch [as 别名]
# 或者: from pyglet.graphics.Batch import add [as 别名]
def cif_power_strip(x, y, z, width=1, height=1, depth=1):
batch = Batch()
outlet = True
for i in range(width):
for j in range(height):
for k in range(depth):
if outlet:
strip_texture = texture.textures['cif_power_strip']
else:
strip_texture = texture.textures['cif_power_outlet']
outlet = not outlet
tile = cube_tile.CubeTile((x+i, y+j, z+k), textures={
'top': texture.textures['cif_power_strip']['master_coordinates'],
'right': strip_texture['master_coordinates'],
'bottom': texture.textures['cif_power_strip']['master_coordinates'],
'left': strip_texture['master_coordinates'],
'front': strip_texture['master_coordinates'],
'back': strip_texture['master_coordinates'],
})
vertex_list = tile.get_vertex_list()
texture_list = tile.get_texture_list()
vertices = len(vertex_list) / 3
batch.add(vertices, GL_QUADS, texture.textures['master']['group'], ('v3f/static', vertex_list), ('t2f/static', texture_list))
return batch
示例3: Shape
# 需要导入模块: from pyglet.graphics import Batch [as 别名]
# 或者: from pyglet.graphics.Batch import add [as 别名]
class Shape(object):
'''
A list of primitives
'''
def __init__(self, items=None):
self.primitives = []
if items:
self.add_items(items)
self.batch = None
def add_items(self, items):
"Add a list of primitives and shapes"
for item in items:
if isinstance(item, Shape):
self.add_shape(item)
else:
self.primitives.append(item)
def add_shape(self, other):
"Add the primitives from a given shape"
for prim in other.primitives:
self.primitives.append(prim)
def get_batch(self):
if self.batch is None:
self.batch = Batch()
for prim in self.primitives:
flatverts = prim.get_flat_verts()
numverts = len(flatverts) / 2
self.batch.add(
numverts,
prim.primtype,
None,
('v2f/static', flatverts),
('c3B/static', prim.color * numverts)
)
return self.batch
def transform(self,M):
""" applies matrix M to all self primitives
"""
for prim in self.primitives:
prim.transform(M)
def get_aabb(self):
aabb = namedtuple('AABB',['xmin','xmax','ymin','ymax'])
_allx=[]
_ally=[]
for prim in self.primitives:
for v in prim.verts:
_allx.append(v[0])
_ally.append(v[1])
minx=min(_allx)
miny=min(_ally)
maxx=max(_allx)
maxy=max(_ally)
box = (minx,miny,maxx,maxy)
return (box)
示例4: OrbitingBody
# 需要导入模块: from pyglet.graphics import Batch [as 别名]
# 或者: from pyglet.graphics.Batch import add [as 别名]
class OrbitingBody(Body, metaclass=ABCMeta):
"""
An orbiting body in the solarsystem
"""
def __init__(self, parent, name, texturename, color, radius, orbit, axial_tilt, sidereal_rotation_period, mass, renderer=OrbitingBodyRenderer()):
"""
Creates a new body with the given parameters
:param parent: Parent body in the system
:type parent: :class:`Body`, None
:param name: Name of the body
:type name: str
:param texturename: Name of the texture
:type texturename: str
:param color: Dictionary with r, g and b values
:type color: dict
:param radius: Radius of the body
:type radius: float
:param orbit: Orbit of the body
:type orbit: :class:`solarsystem.orbit.Orbit`
:param axial_tilt: Axial Tilt in degrees
:type axial_tilt: float
:param sidereal_rotation_period: Rotation period (siderial) around its own axis
:type sidereal_rotation_period: float
"""
super().__init__(parent, name, texturename, color, radius, axial_tilt, sidereal_rotation_period, mass, renderer=renderer)
self.orbit = orbit
self.orbit.body = self
self.orbit_line_batch = Batch()
def post_init(self):
self.orbit.post_init()
# Plot the orbit to a pyglet batch for faster drawing
orbit_line = []
for pos in self.orbit.plot(plot_steps):
orbit_line.append(pos.x)
orbit_line.append(pos.y)
orbit_line.append(pos.z)
self.orbit_line_batch.add(int(len(orbit_line) / 3), GL_LINE_LOOP, None, ('v3f', tuple(orbit_line)))
def update(self, time):
"""
Update the body (Calculate current orbit position)
:param time: Delta Time
:type time: float
"""
super().update(time)
self.xyz = self.orbit.calculate(time)
if self.parent:
self.xyz += self.parent.xyz
示例5: OrbitalObject
# 需要导入模块: from pyglet.graphics import Batch [as 别名]
# 或者: from pyglet.graphics.Batch import add [as 别名]
class OrbitalObject(AstronomicalObject):
"""
An astronomical Object which moves around another
"""
def __init__(self, parent, name, texture_name, radius, axial_tilt, sidereal_rotation_period, mass, orbit,
renderer=AOOrbitingRenderer()):
"""
Create a new orbiting astronomical Object
:param parent: The objects parent (i.e. Sun for Earth)
:param name: Name of the object (Earth, Saturn, Pluto, ...)
:param texture_name: Name of the texture in the `res` directory
:param radius: Radius of object
:param axial_tilt: In astronomy, axial tilt is the angle between a planet's rotational axis at
its north pole and a line perpendicular to the orbital plane of the planet - given in degrees.
:param sidereal_rotation_period: The time required (in days) for a body within the solar system to complete
one revolution with respect to the fixed stars—i.e., as observed from some fixed point outside the system.
:param mass: Mass of the object in kilograms
:param orbit: Orbit Class of this body
"""
super().__init__(parent, name, texture_name, radius, axial_tilt, sidereal_rotation_period, mass,
renderer=renderer)
self.orbit = orbit
self.orbit.body = self
self.orbit_line_batch = Batch()
def config(self):
"""
Configure the Object
"""
self.orbit.config()
orbit_line = []
for pos in self.orbit.pos(1024):
orbit_line.append(pos.x)
orbit_line.append(pos.y)
orbit_line.append(pos.z)
self.orbit_line_batch.add(int(len(orbit_line) / 3), GL_LINE_LOOP, None, ('v3f', tuple(orbit_line)))
def update(self, time):
"""
Update the time in the solar system and
position the object on its right coordinates
:param time: Current solar time
"""
super().update(time)
self.xyz = self.orbit.calculate(time)
示例6: Body
# 需要导入模块: from pyglet.graphics import Batch [as 别名]
# 或者: from pyglet.graphics.Batch import add [as 别名]
class Body(object):
"A list of shapes. Creates a single batch to draw these shapes."
__metaclass__ = IterRegistry
_registry = []
def __init__(self,items=None,anchor=[0,0],angle=0,drawable=True):
self.shapes = []
self._registry.append(self)
self.body=body
self.anchor=anchor
self.angle=angle
self.drawable=drawable
if items:
self.add_items(items)
self.batch = None
def add_items(self, items):
"'items' may contain shapes and/or bodies"
for item in items:
if isinstance(item, Body):
for shp in item.shapes:
self.shapes.append(shp)
else:
self.shapes.append(item)
def batch_draw(self):
if self.batch is None:
self.batch = Batch()
for shape in self.shapes:
batchVerts = \
[shape.verts[0], shape.verts[1]] + \
shape.verts + \
[shape.verts[-2], shape.verts[-1]]
numverts = len(batchVerts) / 2
self.batch.add(
numverts,
shape.primtype,
None, # draw order group to be implemented later
('v2f/static', batchVerts),
('c3B/static', shape.color * numverts),
)
self.batch.draw()
def paint_all():
for z in Zulu:
glPushMatrix()
glTranslatef(z.anchor[0],z.anchor[1],0) # Move bac
glRotatef(z.angle, 0, 0, 1)
z.body.batch_draw()
glPopMatrix()
示例7: floor
# 需要导入模块: from pyglet.graphics import Batch [as 别名]
# 或者: from pyglet.graphics.Batch import add [as 别名]
def floor(floor_texture, x, y, z, width=1, height=1, depth=1):
batch = Batch()
for i in range(width):
for j in range(height):
for k in range(depth):
tile = plane_tile.PlaneTile((x+i, y+j, z+k), side='top', textures={
'top': floor_texture,
})
vertex_list = tile.get_vertex_list()
texture_list = tile.get_texture_list()
vertices = len(vertex_list) / 3
batch.add(vertices, GL_QUADS, texture.textures['master']['group'], ('v3f/static', vertex_list), ('t2f/static', texture_list))
return batch
示例8: CloudRenderer
# 需要导入模块: from pyglet.graphics import Batch [as 别名]
# 或者: from pyglet.graphics.Batch import add [as 别名]
class CloudRenderer(CloudChunk):
def __init__(self, X, Generator):
super(CloudRenderer, self).__init__(X, Generator)
self.Batch = Batch()
def GenerateFinshed(self):
super(CloudRenderer, self).GenerateFinshed()
self.Batch.add(self.Length, GL_POINTS, None,
('v2i/static', self.Points),
('c4f/static', self.Colours)
)
def Draw(self, X):
super(CloudRenderer, self).Draw(X)
self.Batch.draw()
示例9: Shape
# 需要导入模块: from pyglet.graphics import Batch [as 别名]
# 或者: from pyglet.graphics.Batch import add [as 别名]
class Shape(object):
'''
A list of primitives
'''
def __init__(self, items=None):
self.primitives = []
if items:
self.add_items(items)
self.batch = None
def add_items(self, items):
"Add a list of primitives and shapes"
for item in items:
if isinstance(item, Shape):
self.add_shape(item)
else:
self.primitives.append(item)
def add_shape(self, other):
"Add the primitives from a given shape"
for prim in other.primitives:
self.primitives.append(prim)
def get_batch(self):
if self.batch is None:
self.batch = Batch()
for prim in self.primitives:
flatverts = prim.get_flat_verts()
numverts = len(flatverts) / 2
self.batch.add(
numverts,
prim.primtype,
None,
('v2f/static', flatverts),
('c3B/static', prim.color * numverts)
)
return self.batch
def transform(self,M):
""" applies matrix M to all self primitives
"""
for prim in self.primitives:
prim.transform(M)
示例10: Map
# 需要导入模块: from pyglet.graphics import Batch [as 别名]
# 或者: from pyglet.graphics.Batch import add [as 别名]
class Map(object):
LAYERS = {
'soil': OrderedGroup(0),
'ground': OrderedGroup(1),
'bodies': OrderedGroup(2),
'trees': OrderedGroup(3),
'berries': OrderedGroup(4)
}
def __init__(self, width, height):
self.width = width
self.height = height
self._map = Batch()
self._create()
self._add_hero()
self._add_lost()
def _create(self):
for x in range(0, self.width, Soil.size()[0]):
for y in range(0, self.height, Soil.size()[1]):
soil = Soil(x, y)
self.add(soil)
try:
soil.grow(self, x, y)
except NotFertileError as e:
logger.debug(str(e))
def add(self, thing):
thing.vertex_list = self._map.add(
len(thing.vertices) // 2,
GL_QUADS,
self.LAYERS[thing.LAYER],
('v2i/dynamic', thing.vertices),
('c4B/static', thing.colors)
)
return thing.vertex_list
def _add_body(self, body_name, kind):
body = getattr(things, body_name)(*START_POS[kind])
setattr(self, kind, body)
self.add(body)
return body
@info("Adding {}".format(BODY_HERO))
def _add_hero(self):
self._add_body(BODY_HERO, 'hero')
@info("Hiding {}".format(BODY_LOST))
def _add_lost(self):
self._add_body(BODY_LOST, 'lost') # keep a list of every tree to hide him
def draw(self):
self._map.draw()
示例11: cif_old_mac
# 需要导入模块: from pyglet.graphics import Batch [as 别名]
# 或者: from pyglet.graphics.Batch import add [as 别名]
def cif_old_mac(x, y, z, rotate=0):
batch = Batch()
bottom_side = texture.textures['old_mac_bottom_side']['master_coordinates']
top_side = texture.textures['old_mac_top_side']['master_coordinates']
bottom_front = texture.textures['old_mac_bottom_front']['master_coordinates']
top_front = texture.textures['old_mac_top_front']['master_coordinates']
if rotate == 0:
sides = ['right', 'back', 'front']
elif rotate == 1:
sides = ['back', 'left', 'right']
elif rotate == 2:
sides = ['left', 'back', 'front']
elif rotate == 3:
sides = ['front', 'left', 'right']
front_x = 1 if rotate == 0 else -1 if rotate == 2 else 0
front_z = 1 if rotate == 3 else -1 if rotate == 1 else 0
tiles = [
# Front side
plane_tile.PlaneTile((x-front_x, y, z-front_z), side=sides[0], textures={sides[0]: bottom_front}),
plane_tile.PlaneTile((x-front_x, y+1, z-front_z), side=sides[0], textures={sides[0]: top_front}),
# Left side
plane_tile.PlaneTile((x, y, z), side=sides[1], textures={sides[1]: bottom_side}),
plane_tile.PlaneTile((x, y+1, z), side=sides[1], textures={sides[1]: top_side}),
# Right side
plane_tile.PlaneTile((x, y, z), side=sides[2], textures={sides[2]: bottom_side}),
plane_tile.PlaneTile((x, y+1, z), side=sides[2], textures={sides[2]: top_side}),
# Top side
plane_tile.PlaneTile((x, y+1, z), side='top', textures={'top': top_side}),
]
for tile in tiles:
vertex_list = tile.get_vertex_list()
texture_list = tile.get_texture_list()
vertices = len(vertex_list) / 3
batch.add(vertices, GL_QUADS, texture.textures['master']['group'], ('v3f/static', vertex_list), ('t2f/static', texture_list))
return batch
示例12: Shape
# 需要导入模块: from pyglet.graphics import Batch [as 别名]
# 或者: from pyglet.graphics.Batch import add [as 别名]
class Shape(object):
"A list of primitives. Creates a single batch to draw these primitives."
def __init__(self, items=None):
self.primitives = []
if items:
self.add_items(items)
self.batch = None
def add_items(self, items):
"'items' may contain primitives and/or shapes"
for item in items:
if isinstance(item, Shape):
for prim in item.primitives:
self.primitives.append(prim)
else:
self.primitives.append(item)
def get_batch(self):
if self.batch is None:
self.batch = Batch()
for primitive in self.primitives:
batchVerts = \
[primitive.verts[0], primitive.verts[1]] + \
primitive.verts + \
[primitive.verts[-2], primitive.verts[-1]]
numverts = len(batchVerts) / 2
self.batch.add(
numverts,
primitive.primtype,
None, # group
('v2f/static', batchVerts),
('c3B/static', primitive.color * numverts),
)
return self.batch
def offset(self, dx, dy):
newprims = []
for prim in self.primitives:
newprims.append(prim.offset(dx, dy))
return Shape(newprims)
示例13: cif_red_shelf
# 需要导入模块: from pyglet.graphics import Batch [as 别名]
# 或者: from pyglet.graphics.Batch import add [as 别名]
def cif_red_shelf(x, y, z):
batch = Batch()
side = texture.textures['cif_red_shelf_side']['master_coordinates']
front = texture.textures['cif_red_shelf_front']['master_coordinates']
tiles = [
# Left side
plane_tile.PlaneTile((x, y, z), side='back', textures={'back': side}),
plane_tile.PlaneTile((x, y+1, z), side='back', textures={'back': side}),
plane_tile.PlaneTile((x, y+2, z), side='back', textures={'back': side}),
plane_tile.PlaneTile((x, y+3, z), side='back', textures={'back': side}),
plane_tile.PlaneTile((x, y+4, z), side='back', textures={'back': side}),
# Right side
plane_tile.PlaneTile((x, y, z), side='front', textures={'front': side}),
plane_tile.PlaneTile((x, y+1, z), side='front', textures={'front': side}),
plane_tile.PlaneTile((x, y+2, z), side='front', textures={'front': side}),
plane_tile.PlaneTile((x, y+3, z), side='front', textures={'front': side}),
plane_tile.PlaneTile((x, y+4, z), side='front', textures={'front': side}),
# Shelves
plane_tile.PlaneTile((x, y, z), side='top', textures={'top': side}),
plane_tile.PlaneTile((x, y+1, z), side='top', textures={'top': side}),
plane_tile.PlaneTile((x, y+2, z), side='top', textures={'top': side}),
plane_tile.PlaneTile((x, y+3, z), side='top', textures={'top': side}),
plane_tile.PlaneTile((x, y+4, z), side='top', textures={'top': side}),
# Front side
plane_tile.PlaneTile((x, y, z), side='right', textures={'right': front}),
plane_tile.PlaneTile((x, y+1, z), side='right', textures={'right': front}),
plane_tile.PlaneTile((x, y+2, z), side='right', textures={'right': front}),
plane_tile.PlaneTile((x, y+3, z), side='right', textures={'right': front}),
plane_tile.PlaneTile((x, y+4, z), side='right', textures={'right': front}),
]
for tile in tiles:
vertex_list = tile.get_vertex_list()
texture_list = tile.get_texture_list()
vertices = len(vertex_list) / 3
batch.add(vertices, GL_QUADS, texture.textures['master']['group'], ('v3f/static', vertex_list), ('t2f/static', texture_list))
return batch
示例14: cif_table
# 需要导入模块: from pyglet.graphics import Batch [as 别名]
# 或者: from pyglet.graphics.Batch import add [as 别名]
def cif_table(x, y, z, rotate=False):
batch = Batch()
table = texture.textures['cif_red_shelf_side']['master_coordinates']
leg = texture.textures['cif_cabinet_base']['master_coordinates']
table_size = (3, .5, 6) if rotate else (6, .5, 3)
leg_size = (.5, 2, .5)
leg_texture = {
'top': leg,
'right': leg,
'bottom': leg,
'left': leg,
'front': leg,
'back': leg,
}
top = cube_tile.CubeTile((x, y, z), table_size, textures={
'top': table,
'right': table,
'bottom': table,
'left': table,
'front': table,
'back': table,
})
leg_x = table_size[0] / 2.0 - leg_size[0]
leg_z = table_size[2] / 2.0 - leg_size[2]
front_left_leg = cube_tile.CubeTile((x-leg_x, y-1, z+leg_z), leg_size, textures=leg_texture)
front_right_leg = cube_tile.CubeTile((x+leg_x, y-1, z+leg_z), leg_size, textures=leg_texture)
back_left_leg = cube_tile.CubeTile((x-leg_x, y-1, z-leg_z), leg_size, textures=leg_texture)
back_right_leg = cube_tile.CubeTile((x+leg_x, y-1, z-leg_z), leg_size, textures=leg_texture)
for tile in [top, front_left_leg, front_right_leg, back_left_leg, back_right_leg]:
vertex_list = tile.get_vertex_list()
texture_list = tile.get_texture_list()
vertices = len(vertex_list) / 3
batch.add(vertices, GL_QUADS, texture.textures['master']['group'], ('v3f/static', vertex_list), ('t2f/static', texture_list))
return batch
示例15: __init__
# 需要导入模块: from pyglet.graphics import Batch [as 别名]
# 或者: from pyglet.graphics.Batch import add [as 别名]
class World:
def __init__(self):
# A Batch is a collection of vertex lists for batched rendering.
self.batch = Batch()
# A TextureGroup manages an OpenGL texture.
self.texture_group = {}
# A mapping from position to the texture of the block at that position.
# This defines all the blocks that are currently in the world.
self.objects = {}
# Same mapping as `world` but only contains blocks that are shown.
self.shown = {}
# Mapping from position to a pyglet `VertextList` for all shown blocks.
self._shown = {}
# Which sector the player is currently in.
self.sector = None
# Mapping from sector to a list of positions inside that sector.
self.sectors = {}
self.shader = None
self.show_hide_queue = OrderedDict()
PycraftOpenGL()
self.init_shader()
def hit_test(self, position, vector, max_distance=8):
"""Line of sight search from current position. If a block is
intersected it is returned, along with the block previously in the line
of sight. If no block is found, return None, None.
Parameters
----------
position : tuple of len 3
The (x, y, z) position to check visibility from.
vector : tuple of len 3
The line of sight vector.
max_distance : int
How many blocks away to search for a hit.
"""
m = 8
x, y, z = position
dx, dy, dz = vector
previous = None
for _ in range(max_distance * m):
key = normalize((x, y, z))
if key != previous and key in self.objects:
return key, previous
previous = key
x, y, z = x + dx / m, y + dy / m, z + dz / m
return None, None
def exposed(self, position):
"""Returns False is given `position` is surrounded on all 6 sides by
blocks, True otherwise.
"""
x, y, z = position
for dx, dy, dz in FACES:
if (x + dx, y + dy, z + dz) not in self.objects:
return True
return False
def add_block(self, position, texture, immediate=True):
"""Add a block with the given `texture` and `position` to the world.
Parameters
----------
position : tuple of len 3
The (x, y, z) position of the block to add.
texture : list of len 3
The coordinates of the texture squares. Use `tex_coords()` to
generate.
immediate : bool
Whether or not to draw the block immediately.
"""
if position in self.objects:
self.remove_block(position, immediate)
self.objects[position] = texture
self.sectors.setdefault(sectorize(position), []).append(position)
if immediate:
if self.exposed(position):
self.show_block(position)
self.check_neighbors(position)
def remove_block(self, position, immediate=True):
"""Remove the block at the given `position`.
Parameters
----------
position : tuple of len 3
The (x, y, z) position of the block to remove.
immediate : bool
Whether or not to immediately remove block from canvas.
"""
del self.objects[position]
self.sectors[sectorize(position)].remove(position)
if immediate:
if position in self.shown:
self.hide_block(position)
self.check_neighbors(position)
def check_neighbors(self, position):
#.........这里部分代码省略.........