Package examples :: Module pointcloud
[hide private]
[frames] | no frames]

Source Code for Module examples.pointcloud

  1  #!/usr/bin/env python 
  2   
  3  #! This file is a literate Python program. You can compile the documentation 
  4  #! using mylit (http://pypi.python.org/pypi/mylit/). 
  5  ## title = "glitter Example: Point Cloud Renderer" 
  6  ## stylesheet = "pygments_style.css" 
  7   
  8  # <h1><i>glitter</i> Example: Point Cloud Renderer</h1> 
  9   
 10  # <h2>Summary</h2> 
 11   
 12  # This program will open a GLUT window and render a random, colored, rotating point cloud. 
 13   
 14  # <img src="pointcloud.png"> 
 15   
 16  # <h2>Front matter</h2> 
 17   
 18  # <h3>Module docstring</h3> 
 19   
 20  # The module docstring is used as a description of this example in the 
 21  # generated documentation: 
 22  """Viewer for a random point cloud. 
 23   
 24  @author: Stephan Wenger 
 25  @date: 2012-03-13 
 26  """ 
 27   
 28  # <h3>Imports</h3> 
 29   
 30  # Our scene is going to rotate. The rotating modelview matrix is computed using 
 31  # the sine and cosine functions from the math module: 
 32  from numpy import sin, cos, pi 
 33   
 34  # <i>glitter</i> uses <a href="http://numpy.scipy.org/">numpy</a> for 
 35  # representation of array data. We will use numpy's <code>randn()</code> 
 36  # function to generate random point coordinates: 
 37  from numpy.random import randn 
 38   
 39  # We can usually import classes and functions contained in <i>glitter</i> 
 40  # submodules directly from glitter: 
 41  from glitter import VertexArray, State, get_default_program 
 42   
 43  # Modules with external dependencies other than numpy, such as platform 
 44  # dependent parts like methods for the generation of an OpenGL context, 
 45  # however, have to be imported from their respective submodules: 
 46  from glitter.contexts.glut import GlutWindow, main_loop, get_elapsed_time 
 47   
 48  # <h2>Main class</h2> 
 49   
 50  # We wrap all the OpenGL interaction in a class. The class will contain an 
 51  # <code>__init__()</code> method to set up all OpenGL objects, any required 
 52  # callback methods, as well as a <code>run()</code> method to trigger execution 
 53  # of the GLUT main loop. 
54 -class PointcloudRenderer(object):
55 # <h3>Initialization</h3> 56 57 # When a <code>PointcloudRenderer</code> instance is created, we need to 58 # initialize a few OpenGL objects.
59 - def __init__(self):
60 # First, we create a window; this also creates an OpenGL context. 61 self.window = GlutWindow(double=True, multisample=True) 62 63 # Then, we set the GLUT display callback function which will be defined later. 64 self.window.display_callback = self.display 65 66 # In the OpenGL core profile, there is no such thing as a "standard pipeline" 67 # any more. We use the minimalistic <code>defaultpipeline</code> from the 68 # <code>glitter.convenience</code> module to create a shader program instead: 69 self.shader = get_default_program() 70 71 # Here, we create a vertex array that contains buffers for two vertex array 72 # input variables as well as an index array: 73 n_points = 100000 74 self.vao = VertexArray(randn(n_points, 3), randn(n_points, 3))
75 76 # <h3>Callback functions</h3> 77 78 # <h4>Display function</h4> 79 80 # Here we define the display function. It will be called by GLUT whenever the 81 # screen has to be redrawn.
82 - def display(self):
83 # First we clear the default framebuffer: 84 self.window.clear() 85 86 # To draw the vertex array, we use: 87 self.vao.draw() 88 89 # After all rendering commands have been issued, we swap the back buffer to 90 # the front, making the rendered image visible all at once: 91 self.window.swap_buffers()
92 93 # <h4>Timer function</h4> 94 95 # The animation is controlled by a GLUT timer. The timer callback changes the 96 # modelview matrix, schedules the next timer event, and causes a screen redraw:
97 - def timer(self):
98 # We first get the elapsed time from GLUT using <code>get_elapsed_time()</code>: 99 phi = 2 * pi * get_elapsed_time() / 20.0 100 101 # We then set the <code>modelview_matrix</code> uniform variable of the 102 # shader created in the initialization section simply by setting an 103 # attribute: 104 self.shader.modelview_matrix = ((cos(phi), 0, sin(phi), 0), (0, 1, 0, 0), (-sin(phi), 0, cos(phi), 0), (0, 0, 0, 5)) 105 106 # The following line schedules the next timer event to execute after ten milliseconds. 107 self.window.add_timer(10, self.timer) 108 109 # Finally, we tell GLUT to redraw the screen. 110 self.window.post_redisplay()
111 112 # <h3>Running</h3> 113 114 # We will call the <code>run()</code> method later to run the OpenGL code.
115 - def run(self):
116 # To start the animation, we call the timer once; all subsequent timer 117 # calls will be scheduled by the timer function itself. 118 self.timer() 119 120 # The default program is bound by using a <code>with</code> statement: 121 with self.shader: 122 # The <code>State</code> class encapsulates state changes in the 123 # context. For example, to enable depth testing and set the point size 124 # to three for the duration of the following function call, we would 125 # write: 126 with State(depth_test=True, point_size=3): 127 # With the shader bound, depth testing enabled, and the point size 128 # set, we enter the GLUT main loop. 129 main_loop()
130 131 # When the main loop exits, control is handed back to the script. 132 133 # <h2>Main section</h2> 134 135 # Finally, if this program is being run from the command line, we instanciate 136 # the main class and run it. 137 if __name__ == "__main__": 138 PointcloudRenderer().run() 139