OpenGLContext.move.viewplatform
Mobile camera implementation using quaternions
Functions
Classes
class ViewPlatform(
object
):
Mobile Viewing Platform
The ViewPlatform is, loosely speaking, a camera which
sets up the projection and model-view matrices for
an OpenGLContext scene.
Most Context's will have an associated ViewPlatform
thanks to the ViewPlatformMixIn class, which
instantiates the ViewPlatform. Shadow-rendering
Context's will actually use a subclass which
generates "infinite" perspective views required by
the particular stencil-buffer shadowing algorithm.
See:
OpenGLContext.viewplatformmixin.ViewPlatformMixIn
OpenGLContext.shadow.shadowcontext.InfViewPlatform
Attributes:
frustum -- OpenGL-friendly storage of frustum values,
(field of view, aspect ratio, near, far)
- position
- object-space position of the viewing platform, a four-component array
- quaternion
- quaternion representing the current view-orientation for the viewing platform
__init__(
self
,
position
= (0, 0, 10)
,
orientation
= (0, 1, 0, 0)
,
fieldOfView
= 1.0471975511965976
,
aspect
= 1.0
,
near
= 0.3
,
far
= 50000
)
Initialize the ViewPlatform
- position
- 3D coordinate position of the "camera"
- orientation
- VRML97-style 4-component orientation, that is, axis as three floats followed by radian rotation as a single float.
- fieldOfView
- radian angle field of view
- aspect
- float aspect ratio of window, width/height
- near
- object-space distance to the near clipping plane
- far
- object-space distance to the far clipping plane
getNearFar(
self
)
Return the near and far frustum depths
This method isn't actually used in OpenGLContext,
as I use gluUnProject, which uses the information as
encoded in the perspective matrix. There are, however,
instances where knowing the near and far clipping planes
is useful.
Returns the near and far values as set by the
setFrustum method, the last two components of the
self.frustum attribute.
moveRelative(
self
,
x
= 0.0
,
y
= 0.0
,
z
= 0.0
)
Move platform to a position relative to the current position
x,y,z -- float vector along which to be moved from
the current position within the camera orientation
relativeOrientation(
self
,
deltaOrientation
= (0, 1, 0, 0.7853981633974483)
)
Calculate rotation within the current orientation
In essence, this allows you to "turn your head"
which gives you the commonly useful ability to
function from your own frame of reference.
For example:
turn( 1,0,0,angle ) will rotate the camera up
from its current view orientation
turn( 0,1,0,angle ) will rotate the camera about
the current horizon
This method is implemented almost entirely within
the quaternion class. Quaternion's have considerable
advantages for this type of work, as they do not
become "warped" with successive rotations.
relativePosition(
self
,
x
= 0.0
,
y
= 0.0
,
z
= 0.0
)
Calculate a view-relative position from current position/orientation
render(
self
,
mode
= None
,
identity
= False
)
Perform the actual view-platform setup during rendering
This is really quite a trivial function, given the
amount of setup that's been done before-hand. The
gluPerspective function takes care of the perspective-
matrix setup, while self.quaternion (Quaternion)
takes care of the rotation, and positioning is a
simple call to glTranslate.
See:
gluPerspective
OpenGLContext.shadow.shadowcontext.InfViewPlatform
OpenGLContext.shadow.pinfperspective -- alternate
gluPerspective implementation for shadowing contexts
setFrustum(
self
,
fieldOfView
= 1.5707963267948966
,
aspect
= None
,
near
= None
,
far
= None
)
Set the current frustum values for the "camera"
- fieldOfView
- radian angle field of view
- aspect
- float aspect ratio of window, width/height
- near
- object-space distance to the near clipping plane
- far
- object-space distance to the far clipping plane
setOrientation(
self
,
orientation
)
Set the current "camera orientation"
- orientation
- VRML97-style 4-component orientation, that is, axis as three floats followed by radian rotation as a single float.
Alternately, a quaternion.Quaternion instance
representing the orientation. Note that the
orientation will likely be 180 degrees from
what you expect, this method reverses the
rotation value when passed a VRML97-style
orientation.
setPosition(
self
,
position
)
Set the current "camera position"
- position
- 3D coordinate position to which to teleport the "camera"
setViewport(
self
,
x
,
y
)
Set the current viewport/window dimensions
x,y -- integer width and height (respectively) of the window
This method simply updates the frustum attribute to reflect
the new aspect ratio.
straighten(
self
)
Re-orient the camera so the horizon is "level"
Commonly needed after a few camera-relative orientation
changes have built up. This method creates a new
orientation which is solely rotated about the y-axis,
that is, where the camera-relative horizon matches the
object-space horizon.