本文整理汇总了Python中pi3d.Camera.Camera类的典型用法代码示例。如果您正苦于以下问题:Python Camera类的具体用法?Python Camera怎么用?Python Camera使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Camera类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _loop_begin
def _loop_begin(self):
# TODO(rec): check if the window was resized and resize it, removing
# code from MegaStation to here.
if pi3d.USE_PYGAME:
import pygame # although done in __init__ ...python namespaces aarg!!!
if pygame.event.get(pygame.QUIT):
self.destroy()
elif pi3d.PLATFORM != pi3d.PLATFORM_PI and pi3d.PLATFORM != pi3d.PLATFORM_ANDROID:
n = xlib.XEventsQueued(self.opengl.d, xlib.QueuedAfterFlush)
for i in range(n):
if xlib.XCheckMaskEvent(self.opengl.d, KeyPressMask, self.ev):
self.event_list.append(self.ev)
else:
xlib.XNextEvent(self.opengl.d, self.ev)
if self.ev.type == ClientMessage:
if (self.ev.xclient.data.l[0] == self.opengl.WM_DELETE_WINDOW.value):
self.destroy()
self.clear()
with self.lock:
self.sprites_to_load, to_load = set(), self.sprites_to_load
self.sprites.extend(to_load)
self._for_each_sprite(lambda s: s.load_opengl(), to_load)
if MARK_CAMERA_CLEAN_ON_EACH_LOOP:
from pi3d.Camera import Camera
camera = Camera.instance()
if camera is not None:
camera.was_moved = False
if self.tidy_needed:
self._tidy()
示例2: __init__
def __init__(self, shader="post_base", mipmap=False, add_tex=None, divide=1):
""" calls Texture.__init__ but doesn't need to set file name as
texture generated from the framebuffer. Keyword Arguments:
*shader*
to use when drawing sprite, defaults to post_base, a simple
3x3 convolution that does basic edge detection. Can be copied to
project directory and modified as required.
*mipmap*
can be set to True with slight cost to speed, or use fxaa shader
*add_tex*
list of textures. If additional textures can be used by the shader
then they can be added here.
"""
super(PostProcess, self).__init__("postprocess")
# load shader
self.shader = Shader(shader)
dummycam = Camera.instance() # in case this is prior to one being created
self.camera = Camera(is_3d=False)
self.sprite = LodSprite(z=20.0, w=self.ix, h=self.iy, n=divide)
self.sprite.set_2d_size(w=self.ix, h=self.iy)
self.alpha = False
self.blend = True
self.mipmap = mipmap
self.tex_list = [self] # TODO check if this self reference causes graphics memory leaks
if add_tex:
self.tex_list.extend(add_tex)
示例3: _loop_begin
def _loop_begin(self):
# TODO(rec): check if the window was resized and resize it, removing
# code from MegaStation to here.
if not ON_PI:
n = xlib.XEventsQueued(self.opengl.d, xlib.QueuedAfterFlush)
for i in range(n):
if xlib.XCheckMaskEvent(self.opengl.d, KeyPressMask, self.ev):
self.event_list.append(self.ev)
else:
xlib.XNextEvent(self.opengl.d, self.ev)
if self.ev.type == ClientMessage:
if (self.ev.xclient.data.l[0] == self.opengl.WM_DELETE_WINDOW.value):
self.destroy()
self.clear()
with self.lock:
self.sprites_to_load, to_load = set(), self.sprites_to_load
self.sprites.extend(to_load)
self._for_each_sprite(lambda s: s.load_opengl(), to_load)
if MARK_CAMERA_CLEAN_ON_EACH_LOOP:
from pi3d.Camera import Camera
camera = Camera.instance()
if camera:
camera.was_moved = False
if self.tidy_needed:
self._tidy()
示例4: draw
def draw(self, shader=None, txtrs=None, ntl=None, shny=None, camera=None, mlist=[], light_camera=None):
"""If called without parameters, there has to have been a previous call to
set_draw_details() for each Buffer in buf[].
NB there is no facility for setting umult and vmult with draw: they must be
set using set_draw_details or Buffer.set_draw_details.
"""
self.load_opengl() # really just to set the flag so _unload_opengl runs
camera = camera or self._camera or Camera.instance()
if not camera.mtrx_made:
camera.make_mtrx()
if light_camera and not light_camera.mtrx_made:
light_camera.make_mtrx()
if self.MFlg or len(mlist) > 0 or len(self.children) > 0:
# Calculate rotation and translation matrix for this model using numpy.
self.MRaw = self.tr1
if self.rozflg:
self.MRaw = dot(self.roz, self.MRaw)
if self.roxflg:
self.MRaw = dot(self.rox, self.MRaw)
if self.royflg:
self.MRaw = dot(self.roy, self.MRaw)
if self.sclflg:
self.MRaw = dot(self.scl, self.MRaw)
if self.tr2flg:
self.MRaw = dot(self.tr2, self.MRaw)
# child drawing addition #############
newmlist = [m for m in mlist]
newmlist.append(self.MRaw)
if len(self.children) > 0:
for c in self.children:
c.draw(shader, txtrs, ntl, shny, camera, newmlist, light_camera) # TODO issues where child doesn't use same shader
for m in mlist[-1::-1]:
self.MRaw = dot(self.MRaw, m)
######################################
self.M[0,:,:] = self.MRaw[:,:]
#self.M[0:16] = c_floats(self.MRaw.reshape(-1).tolist()) #pypy version
self.M[1,:,:] = dot(self.MRaw, camera.mtrx)[:,:]
#self.M[16:32] = c_floats(dot(self.MRaw, camera.mtrx).reshape(-1).tolist()) #pypy
if light_camera is not None:
self.M[2,:,:] = dot(self.MRaw, light_camera.mtrx)[:,:]
self.MFlg = False
elif camera.was_moved:
# Only do this if it's not done because model moved.
self.M[1,:,:] = dot(self.MRaw, camera.mtrx)[:,:]
if light_camera is not None:
self.M[2,:,:] = dot(self.MRaw, light_camera.mtrx)[:,:]
if camera.was_moved:
self.unif[18:21] = camera.eye[0:3]
for b in self.buf:
# Shape.draw has to be passed either parameter == None or values to pass
# on.
b.draw(self, self.M, self.unif, shader, txtrs, ntl, shny)
示例5: __init__
def __init__(self, shader="post_base", mipmap=False, add_tex=None,
scale=1.0, camera=None, divide=1):
""" calls Texture.__init__ but doesn't need to set file name as
texture generated from the framebuffer. Keyword Arguments:
*shader*
to use when drawing sprite, defaults to post_base, a simple
3x3 convolution that does basic edge detection. Can be copied to
project directory and modified as required.
*mipmap*
can be set to True with slight cost to speed, or use fxaa shader
*add_tex*
list of textures. If additional textures can be used by the shader
then they can be added here.
*scale*
will only render this proportion of the full screen which will
then be mapped to the full uv of the Sprite. The camera object
passed (below) will need to have the same scale set to avoid
perspective distortion
*camera*
the camera to use for rendering to the offscreen texture
*divide*
allow the sprite to be created with intermediate vertices to allow
interesting vertex shader effects
"""
super(PostProcess, self).__init__("postprocess")
self.scale = scale
# load shader
self.shader = Shader(shader)
if camera is None:
self.viewcam = Camera.instance() # in case this is prior to one being created
else:
self.viewcam = camera
self.camera = Camera(is_3d=False)
self.sprite = LodSprite(z=20.0, w=self.ix, h=self.iy, n=divide)
self.sprite.set_2d_size(w=self.ix, h=self.iy)
for b in self.sprite.buf:
b.unib[6] = self.scale # ufact
b.unib[7] = self.scale # vfact
b.unib[9] = (1.0 - self.scale) * 0.5 # uoffset
b.unib[10] = (1.0 - self.scale) * 0.5 # voffset
self.alpha = False
self.blend = True
self.mipmap = mipmap
self.tex_list = [self] # TODO check if this self reference causes graphics memory leaks
if add_tex:
self.tex_list.extend(add_tex)
示例6: draw
def draw(self, shader=None, txtrs=None, ntl=None, shny=None, camera=None, mlist=[]):
"""If called without parameters, there has to have been a previous call to
set_draw_details() for each Buffer in buf[].
NB there is no facility for setting umult and vmult with draw: they must be
set using set_draw_details or Buffer.set_draw_details.
"""
self.load_opengl() # really just to set the flag so _unload_opengl runs
from pi3d.Camera import Camera
camera = camera or self._camera or Camera.instance()
shader = shader or self.shader
shader.use()
if self.MFlg == True or len(mlist):
# Calculate rotation and translation matrix for this model using numpy.
self.MRaw = dot(self.tr2,
dot(self.scl,
dot(self.roy,
dot(self.rox,
dot(self.roz, self.tr1)))))
# child drawing addition #############
newmlist = [m for m in mlist]
newmlist.append(self.MRaw)
if len(self.children) > 0:
for c in self.children:
c.draw(shader, txtrs, ntl, shny, camera, newmlist)
for m in mlist[-1::-1]:
self.MRaw = dot(self.MRaw, m)
######################################
self.M[0:16] = self.MRaw.ravel()
#self.M[0:16] = c_floats(self.MRaw.reshape(-1).tolist()) #pypy version
self.M[16:32] = dot(self.MRaw, camera.mtrx).ravel()
#self.M[16:32] = c_floats(dot(self.MRaw, camera.mtrx).reshape(-1).tolist()) #pypy
self.MFlg = False
elif camera.was_moved:
# Only do this if it's not done because model moved.
self.M[16:32] = dot(self.MRaw, camera.mtrx).ravel()
if camera.was_moved:
self.unif[18:21] = camera.eye[0:3]
opengles.glUniformMatrix4fv(shader.unif_modelviewmatrix, 2,
ctypes.c_int(0),
ctypes.byref(self.M))
opengles.glUniform3fv(shader.unif_unif, 20, ctypes.byref(self.unif))
for b in self.buf:
# Shape.draw has to be passed either parameter == None or values to pass
# on.
b.draw(self, shader, txtrs, ntl, shny)
示例7: __init__
def __init__(self, position, light, scale=10.0):
""" calls Texture.__init__ but doesn't need to set file name as
texture generated from the framebuffer
"""
super(ShadowCaster, self).__init__("shadow_caster")
self.LIGHT_CAM = Camera(is_3d=False, scale=scale)
l_p = light.lightpos
l_len = (l_p[0]**2 + l_p[1]**2 + l_p[2]**2)**0.5
self.OFFSET = [200.0 * i / l_len for i in l_p]
self.LIGHT_CAM.position([position[i] - o for i, o in enumerate(self.OFFSET)])
self.tilt, self.rot = self.LIGHT_CAM.point_at(position)
self.cast_shader = Shader("shadowcast")
示例8: ShadowCaster
class ShadowCaster(OffScreenTexture):
"""For creating a depth-of-field blurring effect on selected objects"""
def __init__(self, position, light, scale=10.0):
""" calls Texture.__init__ but doesn't need to set file name as
texture generated from the framebuffer
"""
super(ShadowCaster, self).__init__("shadow_caster")
self.LIGHT_CAM = Camera(is_3d=False, scale=scale)
l_p = light.lightpos
l_len = (l_p[0]**2 + l_p[1]**2 + l_p[2]**2)**0.5
self.OFFSET = [200.0 * i / l_len for i in l_p]
self.LIGHT_CAM.position([position[i] - o for i, o in enumerate(self.OFFSET)])
self.tilt, self.rot = self.LIGHT_CAM.point_at(position)
self.cast_shader = Shader("shadowcast")
def move_light(self, position):
self.LIGHT_CAM.reset()
self.LIGHT_CAM.rotate(self.tilt, self.rot, 0)
self.LIGHT_CAM.position([position[i] - o for i, o in enumerate(self.OFFSET)])
def start_cast(self, position=None):
if position is not None:
self.move_light(position)
super(ShadowCaster, self)._start()
def cast_shadow(self, shape):
shape.draw(shader=self.cast_shader, light_camera=self.LIGHT_CAM)
def end_cast(self):
super(ShadowCaster, self)._end()
def draw_shadow(self):
self.emap.draw(shader=self.dshader)
def draw_tree(self, tree, shader):
tree.draw(shader, [self])
示例9: _loop_begin
def _loop_begin(self):
# TODO(rec): check if the window was resized and resize it, removing
# code from MegaStation to here.
self.clear()
with self.lock:
self.sprites_to_load, to_load = set(), self.sprites_to_load
self.sprites.extend(to_load)
self._for_each_sprite(lambda s: s.load_opengl(), to_load)
if MARK_CAMERA_CLEAN_ON_EACH_LOOP:
from pi3d.Camera import Camera
camera = Camera.instance()
if camera:
camera.was_moved = False
示例10: __init__
def __init__(self, emap, light):
""" calls Texture.__init__ but doesn't need to set file name as
texture generated from the framebuffer
"""
super(ShadowCaster, self).__init__("shadow_caster")
# load shader for casting shadows and camera
self.cshader = Shader("uv_flat")
self.mshader = Shader("mat_flat")
# keep copy of ElevationMap
self.emap = emap
self.emap.set_material((0.0, 0.0, 0.0)) # hide bits below ground
# TODO doesn't cope with z light positions
self.eye = [-500 * i for i in light.lightpos] # good distance away
if self.eye[1] <= 0: # must have +ve y
self.eye[1] = 500.0
if abs(self.eye[0]) > abs(self.eye[2]): # x val is bigger than z val
# change scale so map just fits on screen
if self.eye[0] < 0:
su, sv = 1.0, 1.0
else:
su, sv = -1.0, -1.0
self.scaleu = float(self.iy) / self.emap.width
self.scalev = float(self.ix) / self.emap.depth
self.eye[2] = 0
self.scaleu = self.scaleu / self.eye[1] * float(self.eye[0] ** 2 + self.eye[1] ** 2) ** 0.5
self.emap.unif[50] = 1.0 # orientation flag
self.emap.unif[53] = -3.0 * su / self.emap.width * self.eye[0] / float(self.eye[1]) # height adjustment
else:
# change scale so map just fits on screen
if self.eye[2] < 0:
su, sv = 1.0, -1.0
else:
su, sv = -1.0, 1.0
self.scaleu = float(self.iy) / self.emap.depth
self.scalev = float(self.ix) / self.emap.width
self.eye[0] = 0
self.scaleu = self.scaleu / self.eye[1] * float(self.eye[2] ** 2 + self.eye[1] ** 2) ** 0.5
self.emap.unif[50] = 0.0
self.emap.unif[53] = -3.0 * su / self.emap.width * self.eye[2] / float(self.eye[1])
if abs(self.scaleu) > abs(self.scalev):
self.scale = 3.0 * self.scalev # multiplication factor to reduce pixeliness
else:
self.scale = 3.0 * self.scaleu
self.scaleu = su * self.scale / self.scaleu # reused later in end_cast
self.scalev = sv * self.scale / self.scalev
self.camera0 = Camera() # default instance created as normal, just in case!
self.camera = Camera(is_3d=False, eye=self.eye, scale=self.scale)
# load shader for drawing map with shadows
self.dshader = Shader("shadowcast")
示例11: rotate_to_direction
def rotate_to_direction(self, direction, forward=[0.0, 0.0, 1.0]):
""" works out the XYZ euler rotations to rotate this shape from
forward to direction vectors
Arguments:
*direction*
3vector tuple, array or numpy array
*forward*
3vector, usually +ve z direction
"""
if type(direction) is not np.ndarray:
direction = np.array(direction)
if type(forward) is not np.ndarray:
forward = np.array(forward)
if self._camera is None:
self._camera = Camera.instance() # TODO may be issues doing this not in main thread (otherwise why not in Shape.__init__()?)
rot_mtrix = self._camera.matrix_from_two_vectors(forward, direction)
rot_euler = self._camera.euler_angles(rot_mtrix)
self.rotateToX(-rot_euler[0]) # unclear why x and y need to be -ve
self.rotateToY(-rot_euler[1]) # something to do with sense of rotation of camera
self.rotateToZ(rot_euler[2])
示例12: draw
def draw(self, shader=None, txtrs=None, ntl=None, shny=None, camera=None):
"""If called without parameters, there has to have been a previous call to
set_draw_details() for each Buffer in buf[].
NB there is no facility for setting umult and vmult with draw: they must be
set using set_draw_details or Buffer.set_draw_details.
"""
from pi3d.Camera import Camera
camera = camera or self._camera or Camera.instance()
shader = shader or self.shader
shader.use()
if self.MFlg == True:
# Calculate rotation and translation matrix for this model using numpy.
self.MRaw = dot(self.tr2,
dot(self.scl,
dot(self.roy,
dot(self.rox,
dot(self.roz, self.tr1)))))
self.M[0:16] = self.MRaw.ravel()
self.M[16:32] = dot(self.MRaw, camera.mtrx).ravel()
self.MFlg = False
elif camera.was_moved:
# Only do this if it's not done because model moved.
self.M[16:32] = dot(self.MRaw, camera.mtrx).ravel()
if camera.was_moved:
self.unif[18:21] = camera.eye[0:3]
opengles.glUniformMatrix4fv(shader.unif_modelviewmatrix, 2,
ctypes.c_int(0),
ctypes.byref(self.M))
opengles.glUniform3fv(shader.unif_unif, 20, ctypes.byref(self.unif))
for b in self.buf:
# Shape.draw has to be passed either parameter == None or values to pass
# on.
b.draw(shader, txtrs, ntl, shny)
示例13: __init__
def __init__(self, shader="uv_flat", mipmap=False, separation=0.4):
""" calls Texture.__init__ but doesn't need to set file name as
texture generated from the framebuffer. Keyword Arguments:
*shader*
to use when drawing sprite, defaults to post_base, a simple
3x3 convolution that does basic edge detection. Can be copied to
project directory and modified as required.
*mipmap*
can be set to True with slight cost to speed, or use fxaa shader
*separation*
distance between the two camera positions - how wide apart the
eye views are.
"""
# load shader
self.shader = Shader(shader)
self.camera_3d = Camera()
self.camera_2d = Camera(is_3d=False)
self.offs = separation / 2.0
self.textures = []
self.sprites = []
self.tex_list = []
for i in range(2):
self.textures.append(OffScreenTexture(name="bin"))
ix, iy = self.textures[i].ix, self.textures[i].iy
#two sprites full width but moved so that they are centred on the
#left and right edges. The offset values then move the uv mapping
#so the image is on the right of the left sprite and left of the
#right sprite
self.sprites.append(Sprite(z=20.0, x=-ix/2.0 + i*ix, w=ix, h=iy, flip=True))
self.sprites[i].set_offset((i * 0.5 - 0.25, 0.0))
self.textures[i].alpha = False
self.textures[i].blend = True
self.textures[i].mipmap = mipmap
self.tex_list.append([self.textures[i]])
示例14: __init__
def __init__(self, emap, light):
""" calls Texture.__init__ but doesn't need to set file name as
texture generated from the framebuffer
"""
super(ShadowCaster, self).__init__("shadow_caster")
from pi3d.Display import Display
self.ix, self.iy = Display.INSTANCE.width, Display.INSTANCE.height
self.im = Image.new("RGBA",(self.ix, self.iy))
self.image = self.im.convert("RGBA").tostring('raw', "RGBA")
self.alpha = True
self.blend = False
self._tex = ctypes.c_int()
self.framebuffer = (ctypes.c_int * 1)()
opengles.glGenFramebuffers(1, self.framebuffer)
self.depthbuffer = (ctypes.c_int * 1)()
opengles.glGenRenderbuffers(1, self.depthbuffer)
# load shader for casting shadows and camera
self.cshader = Shader("shaders/uv_flat")
self.mshader = Shader("shaders/mat_flat")
# keep copy of ElevationMap
self.emap = emap
self.emap.set_material((0.0, 0.0, 0.0)) # hide bits below ground
#TODO doesn't cope with z light positions
self.eye = [-500*i for i in light.lightpos] # good distance away
if self.eye[1] <= 0: # must have +ve y
self.eye[1] = 500.0
if abs(self.eye[0]) > abs(self.eye[2]): #x val is bigger than z val
#change scale so map just fits on screen
if self.eye[0] < 0:
su, sv = 1.0, 1.0
else:
su, sv = -1.0, -1.0
self.scaleu = float(self.iy) / self.emap.width
self.scalev = float(self.ix)/ self.emap.depth
self.eye[2] = 0
self.scaleu = self.scaleu / self.eye[1] * float(self.eye[0]**2 + self.eye[1]**2)**0.5
self.emap.unif[50] = 1.0 #orientation flag
self.emap.unif[53] = -3.0 * su / self.emap.width * self.eye[0] / float(self.eye[1]) #height adjustment
else:
#change scale so map just fits on screen
if self.eye[2] < 0:
su, sv = 1.0, -1.0
else:
su, sv = -1.0, 1.0
self.scaleu = float(self.iy) / self.emap.depth
self.scalev = float(self.ix)/ self.emap.width
self.eye[0] = 0
self.scaleu = self.scaleu / self.eye[1] * float(self.eye[2]**2 + self.eye[1]**2)**0.5
self.emap.unif[50] = 0.0
self.emap.unif[53] = -3.0 * su / self.emap.width * self.eye[2] / float(self.eye[1])
if abs(self.scaleu) > abs(self.scalev):
self.scale = 3.0 * self.scalev # multiplication factor to reduce pixeliness
else:
self.scale = 3.0 * self.scaleu
self.scaleu = su * self.scale / self.scaleu # reused later in end_cast
self.scalev = sv * self.scale / self.scalev
self.camera0 = Camera() # default instance created as normal, just in case!
self.camera = Camera(is_3d=False, eye=self.eye, scale=self.scale)
# load shader for drawing map with shadows
self.dshader = Shader("shaders/shadowcast")
示例15: Camera
from pi3d.Light import Light
from pi3d.Shader import Shader
from pi3d.util.String import String
from pi3d.util.Ttffont import Ttffont
from pi3d.util.Defocus import Defocus
from pi3d.util.Screenshot import screenshot
from pi3d.shape.MergeShape import MergeShape
from pi3d.shape.Sphere import Sphere
from pi3d.shape.Sprite import Sprite
# Setup display and initialise pi3d
DISPLAY = Display.create(x=10, y=10, w=900, h=600, frames_per_second=25)
DISPLAY.set_background(0.4, 0.6, 0.8, 1.0) # r,g,b,alpha
persp_cam = Camera.instance() # default instance camera perspecive view
ortho_cam = Camera(is_3d=False) # 2d orthographic view camera
#setup textures, light position and initial model position
Light((0, 5, 0))
#create shaders
shader = Shader("shaders/uv_reflect")
flatsh = Shader("shaders/uv_flat")
defocus = Defocus()
#Create textures
shapeimg = Texture("textures/straw1.jpg")
shapebump = Texture("textures/floor_nm.jpg", True)
shapeshine = Texture("textures/pong3.png")
#Create shape