22. camera — OpenGL camera handling

OpenGL camera handling

Classes defined in module camera

class camera.ViewAngles(data={'right': (90.0, 0.0, 0.0), 'bottom': (0.0, -90.0, 0.0), 'top': (0.0, 90.0, 0.0), 'back': (180.0, 0.0, 0.0), 'iso': (45.0, 45.0, 0.0), 'front': (0.0, 0.0, 0.0), 'left': (270.0, 0.0, 0.0)})

A dict to keep named camera angle settings.

This class keeps a dictionary of named angle settings. Each value is a tuple of (longitude, latitude, twist) camera angles. This is a static class which should not need to be instantiated.

There are seven predefined values: six for looking along global coordinate axes, one isometric view.

Methods

get(name)

Get the angles for a named view.

Returns a tuple of angles (longitude, latitude, twist) if the named view was defined, or None otherwise

class camera.Camera(center=[0.0, 0.0, 0.0], long=0.0, lat=0.0, twist=0.0, dist=1.0)

This class defines a camera for OpenGL rendering.

It provides functions for manipulating the camera position, the viewing direction and the lens parameters.

The camera viewing line can be defined by two points : the position of the camera and the center of the scene the camera is looking at. To enable continuous camera rotations however, it is essential that the camera angles are stored as such, and not be calculated from the camera position and the center point, because the transformation from cartesian to spherical coordinates is not unique. Furthermore, to enable smooth mouse-controlled camera rotation based on the current camera angles, it is essential to store the camera angles as the combined rotation matrix, not as the individual angles.

Therefore we store the camera position/direction as follows:
ctr: [ x,y,z ] : the reference point of the camera: this is always
a point on the viewing axis. Usualy, it is the center point of the scene we are looking at.

rot: twist : rotation angle around the camera’s viewing axis

The default camera is at [0,0,0] and looking in the -z direction. Near and far clipping planes are by default set to 0.1, resp 10 times the camera distance.

Some camera terminology: Position (eye) : position of the camera Scene center (ctr) : the point the camera is looking at. Up Vector : a vector pointing up from the camera. Viewing direction (rotx,roty,rotz) Lens angle (fovy) Aspect ratio (aspect) Clip (front/back) Perspective/Orthogonal

We assume that matrixmode is always MODELVIEW. For other operations we explicitely switch before and afterwards back to MODELVIEW.

Methods

getCenter()

Return the camera reference point (the scene center).

getRot()

Return the camera rotation matrix.

getDist()

Return the camera distance.

setCenter(x, y, z)

Set the center of the camera in global cartesian coordinates.

setAngles(angles)

Set the rotation angles.

angles is either:
  • a tuple of angles (long,lat,twist)
  • a named view corresponding to angles in view_angles
  • None
setRotation(long, lat, twist=0)

Set the rotation matrix of the camera from three angles.

setDist(dist)

Set the distance.

report()

Return a report of the current camera settings.

dolly(val)

Move the camera eye towards/away from the scene center.

This has the effect of zooming. A value > 1 zooms out, a value < 1 zooms in. The resulting enlargement of the view will approximately be 1/val. A zero value will move the camera to the center of the scene. The front and back clipping planes may need adjustment after a dolly operation.

pan(val, axis=0)

Rotate the camera around axis through its eye.

The camera is rotated around an axis through the eye point. For axes 0 and 1, this will move the center, creating a panning effect. The default axis is parallel to the y-axis, resulting in horizontal panning. For vertical panning (axis=1) a convenience alias tilt is created. For axis = 2 the operation is equivalent to the rotate operation.

tilt(val)

Rotate the camera up/down around its own horizontal axis.

The camera is rotated around and perpendicular to the plane of the y-axis and the viewing axis. This has the effect of a vertical pan. A positive value tilts the camera up, shifting the scene down. The value is specified in degrees.

move(dx, dy, dz)

Move the camera over translation (dx,dy,dz) in global coordinates.

The center of the camera is moved over the specified translation vector. This has the effect of moving the scene in opposite direction.

rotate(val, vx, vy, vz)

Rotate the camera around current camera axes.

saveModelView()

Save the ModelView matrix.

setModelView()

Set the ModelView matrix from camera parameters.

loadModelView()

Load the saved ModelView matrix.

loadCurrentRotation()

Load the current ModelView matrix with translations canceled out.

transform(v)

Transform a vertex using the currently saved Modelview matrix.

toWorld(v, trl=False)

Transform a vertex from camera to world coordinates.

The specified vector can have 3 or 4 (homogoneous) components. This uses the currently saved rotation matrix.

setLens(fovy=None, aspect=None)

Set the field of view of the camera.

We set the field of view by the vertical opening angle fovy and the aspect ratio (width/height) of the viewing volume. A parameter that is not specified is left unchanged.

resetArea()

Set maximal camera area.

Resets the camera window area to its maximum values corresponding to the fovy setting, symmetrical about the camera axes.

setArea(hmin, vmin, hmax, vmax, relative=True, center=False, clip=True)

Set the viewable area of the camera.

zoomArea(val=0.5, area=None)

Zoom in/out by shrinking/enlarging the camera view area.

The zoom factor is relative to the current setting. Values smaller than 1.0 zoom in, larger values zoom out.

transArea(dx, dy)

Pan by moving the vamera area.

dx and dy are relative movements in fractions of the current area size.

setClip(near, far)

Set the near and far clipping planes

setPerspective(on=True)

Set perspective on or off

loadProjection(force=False, pick=None, keepmode=False)

Load the projection/perspective matrix.

The caller will have to setup the correct GL environment beforehand. No need to set matrix mode though. This function will switch to GL_PROJECTION mode before loading the matrix

!! CHANGED: does not switch back to GL_MODELVIEW mode!

A pick region can be defined to use the camera in picking mode. pick defines the picking region center and size (x,y,w,h).

This function does it best at autodetecting changes in the lens settings, and will only reload the matrix if such changes are detected. You can optionally force loading the matrix.

project(x, y, z)

Map the object coordinates (x,y,z) to window coordinates.

unProject(x, y, z)

Map the window coordinates (x,y,z) to object coordinates.

setTracking(onoff=True)

Enable/disable coordinate tracking using the camera

Functions defined in module camera

camera.tand(arg)

Return the tan of an angle in degrees.

Documentation

Previous topic

21. viewport — Interactive OpenGL Canvas embedded in a Qt4 widget.

Next topic

23. image — Saving OpenGL renderings to image files.

This Page