klampt.vis.glprogram module

This module defines convenient classes for building 3D GUI programs.

  • GLProgram takes care of basic user input.
  • GLNavigationProgram allows 3D navigation with the mouse.
  • GLRealtimeProgram calls a subclass-defined idle() function roughly on a constant time step.
class klampt.vis.glprogram.GLNavigationProgram(name)[source]

Bases: klampt.vis.glprogram.GLProgram

A more advanced form of GLProgram that allows you to navigate a camera around a 3D world. Click-drag rotates, Control-drag translates, Shift-drag zooms.

get_view()[source]

Returns a GLViewport describing the viewport, which could be saved to file.

motionfunc(x, y, dx, dy)[source]
mousefunc(button, state, x, y)[source]
prepare_GL()[source]
set_view(v)[source]

Sets the viewport to a tuple previously returned by get_view(), e.g. a prior view that was saved to file.

class klampt.vis.glprogram.GLPluginProgram(name='GLWidget')[source]

Bases: klampt.vis.glprogram.GLRealtimeProgram

This base class should be used with a GLPluginBase object to handle the GUI functionality (see glcommon.py). Call setPlugin() on this object to set the currently used plugin. pushPlugin()/popPlugin() can also be used to set a hierarchy of plugins.

display()[source]
display_screen()[source]
displayfunc()[source]
idle()[source]
initialize()[source]
keyboardfunc(c, x, y)[source]
keyboardupfunc(c, x, y)[source]
motionfunc(x, y, dx, dy)[source]
mousefunc(button, state, x, y)[source]
popPlugin()[source]
pushPlugin(plugin)[source]
reshapefunc(w, h)[source]
setPlugin(plugin)[source]
set_view(v)[source]
class klampt.vis.glprogram.GLProgram(name='OpenGL Program')[source]

A basic OpenGL visualization, run as part of some _GLBackend. For the most part there is a one-to-one correspondence and the backend just relays the input / drawing messages

Assumes that glinit.py has been imported to define _GLBackend.

name

title of the window (only has an effect before calling run())

Type:str
window

the QtBackend or GLUTBackend instance

view

describes the OpenGL viewport. If this is provided to an empty _GLBackend window, the w,h gives a hint to the size of the window. It is then updated by the user and setting the viewport size has no effect on the window.

Type:GLViewport
clearColor

the RGBA floating point values of the background color.

Type:list of 4 floats
actions

the list of actions. Must be populated using add_action before init().

Type:list of GLProgramAction
add_action(hook, short_text, key, description=None)[source]

Defines a new generic GUI action. The action will be available in a menu in Qt or as keyboard commands in GLUT.

closefunc()[source]

Called by the window when it is closed

display()[source]

Do drawing of objects in world

display_screen()[source]

Do drawing of objects on screen

displayfunc()[source]

All OpenGL calls go here. May be overridden, although you may wish to override display() and display_screen() instead.

draw_text(point, text, size=12, color=None)[source]
idlefunc()[source]

Called on idle. Default value stops all additional idle calls. Must be overridden if you want to do something in the idle loop.

idlesleep(duration=inf)[source]

Sleeps the idle callback for t seconds. If t is not provided, the idle callback is slept forever

initialize()[source]

Called after the GL context is initialized, but before main loop. May be overridden. Users should not call this directly!

keyboardfunc(c, x, y)[source]

Called on keypress down. May be overridden. c is either the ASCII/unicode character of the key pressed or a string describing the character (up,down,left,right, home,end,delete,enter,f1,…,f12)

keyboardupfunc(c, x, y)[source]

Called on keyboard up (if your system allows it). May be overridden.

modifiers()[source]

Retrieves a list of currently pressed keyboard modifiers. Values can be any combination of ‘ctrl’, ‘shift’, ‘alt’.

motionfunc(x, y, dx, dy)[source]

Called when the mouse moves on screen. May be overridden.

mousefunc(button, state, x, y)[source]

Called when the mouse is clicked. May be overridden.

prepare_GL()[source]

Prepare drawing in world coordinate frame

prepare_screen_GL()[source]

Prepare drawing on screen

print_help()[source]
refresh()[source]

Call this to redraw the screen on the next event loop

reshape(w, h)[source]

Asks to resize the GL window

reshapefunc(w, h)[source]

Called on window resize. May be overridden.

run()[source]

Starts a new event loop with this object as the main program. Note: might not return, in the case of GLUT.

save_screen(fn, multithreaded=True)[source]

Saves a screenshot

class klampt.vis.glprogram.GLProgramAction(hook, short_text, key, description=None)[source]
class klampt.vis.glprogram.GLRealtimeProgram(name)[source]

Bases: klampt.vis.glprogram.GLNavigationProgram

A GLNavigationProgram that refreshes the screen at a given frame rate.

ttotal

total elapsed time assuming a constant frame rate

Type:float
fps

the frame rate in Hz

Type:float
dt

1.0/fps

Type:float
counter

a frame counter

Type:int
lasttime

time.time() value on the last frame.

Type:float
idle()[source]
idlefunc()[source]
class klampt.vis.glprogram.GLViewport[source]

A class describing an OpenGL camera view.

x,y

upper left hand corner of the view in the OpenGL canvas, in screen pixels

Type:int
w,h

width and height of the view, in screen pixels

Type:int
screenDeviceScale

if not 1, multiply screen pixel coordinates by this to get openGL pixel coordinates (usually Mac Retina displays)

Type:float
orthogonal

if true, does an orthogonal projection. (Not supported)

Type:bool
camera

an orbit camera (see orbit)

fov

the camera field of view in x direction, in degrees

Type:float
clippingplanes

a pair containing the near and far clipping planes

Type:pair of floats
click_ray(x, y)[source]

Returns a pair of 3-tuples indicating the ray source and direction in world coordinates for a screen-coordinate point (x,y)

contains(x, y)[source]
fit(center, radius)[source]

Fits the viewport to an object filling a sphere of a certain center and radius

project(pt, clip=True)[source]

Given a point in world space, returns the (x,y,z) coordinates of the projected pixel. z is given in absolute coordinates, while x,y are given in pixel values.

If clip=True and the point is out of the viewing volume, then None is returned. Otherwise, if the point is exactly at the focal plane then the middle of the viewport is returned.

setCurrentGL()[source]

Sets up the view in the current OpenGL context

toViewport()[source]

Returns a Klampt C++ Viewport() instance corresponding to this view. This is used to interface with the Widget classes