Lambda Stack - Demo 16¶
Objective¶
Remove repetition in the coordinate transformations, as previous demos had very similar transformations, especially from camera space to NDC space. Each edge of the graph of objects should only be specified once per frame.

Full Cayley graph.¶
Noticing in the previous demos that the lower parts of the transformations have a common pattern, we can create a stack of functions for later application. Before drawing geometry, we add any functions to the top of the stack, apply all of our functions in the stack to our modelspace data to get NDC data, and before we return to the parent node, we pop the functions we added off of the stack, to ensure that we return the stack to the state that the parent node gave us.
To explain in more detail —
What’s the difference between drawing paddle 1 and the square?
Here is paddle 1 code
238 glColor3f(*astuple(paddle1.color))
239 glBegin(GL_QUADS)
240 for p1_v_ms in paddle1.vertices:
241 ms_to_ndc: InvertibleFunction[Vector3D] = compose(
242 # camera space to NDC
243 uniform_scale(1.0 / 10.0),
244 # world space to camera space
245 inverse(translate(camera.position_ws)),
246 # model space to world space
247 compose(translate(paddle1.position),
248 rotate_z(paddle1.rotation)),
249 )
250
251 paddle1_vector_ndc: Vector3D = ms_to_ndc(p1_v_ms)
252 glVertex3f(paddle1_vector_ndc.x,
253 paddle1_vector_ndc.y,
254 paddle1_vector_ndc.z)
255 glEnd()
Here is the square’s code:
259 # draw square
260 glColor3f(0.0, 0.0, 1.0)
261 glBegin(GL_QUADS)
262 for ms in square:
263 ms_to_ndc: InvertibleFunction[Vector3D] = compose(
264 # camera space to NDC
265 uniform_scale(1.0 / 10.0),
266 # world space to camera space
267 inverse(translate(camera.position_ws)),
268 # model space to world space
269 compose(translate(paddle1.position),
270 rotate_z(paddle1.rotation)),
271 # square space to paddle 1 space
272 compose(translate(Vector3D(x=0.0, y=0.0, z=-1.0)),
273 rotate_z(rotation_around_paddle1),
274 translate(Vector3D(x=2.0, y=0.0, z=0.0)),
275 rotate_z(square_rotation)))
276 square_vector_ndc: Vector3D = ms_to_ndc(ms)
277 glVertex3f(square_vector_ndc.x,
278 square_vector_ndc.y,
279 square_vector_ndc.z)
280 glEnd()
The only difference is the square’s modelspace to paddle1 space. Everything else is exactly the same. In a graphics program, because the scene is a hierarchy of relative objects, it is unwise to put this much repetition in the transformation sequence. Especially if we might change how the camera operates, or from perspective to ortho. It would required a lot of code changes. And I don’t like reading from the bottom of the code up. Code doesn’t execute that way. I want to read from top to bottom.
When reading the transformation sequences in the previous demos from top down the transformation at the top is applied first, the transformation at the bottom is applied last, with the intermediate results method-chained together. (look up above for a reminder)
With a function stack, the function at the top of the stack (f5) is applied first, the result of this is then given as input to f4 (second on the stack), all the way down to f1, which was the first fn to be placed on the stack, and as such, the last to be applied. (Last In First Applied - LIFA)
|-------------------|
(MODELSPACE) | |
(x,y,z)-> | f5 |--
|-------------------| |
|
-------------------------
|
| |-------------------|
| | |
->| f4 |--
|-------------------| |
|
-------------------------
|
| |-------------------|
| | |
->| f3 |--
|-------------------| |
|
-------------------------
|
| |-------------------|
| | |
->| f2 |--
|-------------------| |
|
-------------------------
|
| |-------------------|
| | |
->| f1 |--> (x,y,z) NDC
|-------------------|
So, in order to ensure that the functions in a stack will execute in the same order as all of the previous demos, they need to be pushed onto the stack in reverse order.
This means that from modelspace to world space, we can now read the transformations FROM TOP TO BOTTOM!!!! SUCCESS!
Then, to draw the square relative to paddle one, those six transformations will already be on the stack, therefore only push the differences, and then apply the stack to the paddle’s modelspace data.
How to Execute¶
Load src/modelviewprojection/demo16.py in Spyder and hit the play button.
Move the Paddles using the Keyboard¶
Keyboard Input |
Action |
---|---|
w |
Move Left Paddle Up |
s |
Move Left Paddle Down |
k |
Move Right Paddle Down |
i |
Move Right Paddle Up |
d |
Increase Left Paddle’s Rotation |
a |
Decrease Left Paddle’s Rotation |
l |
Increase Right Paddle’s Rotation |
j |
Decrease Right Paddle’s Rotation |
UP |
Move the camera up, moving the objects down |
DOWN |
Move the camera down, moving the objects up |
LEFT |
Move the camera left, moving the objects right |
RIGHT |
Move the camera right, moving the objects left |
q |
Rotate the square around its center |
e |
Rotate the square around paddle 1’s center |
Description¶
Function stack. Internally it has a list, where index 0 is the bottom of the stack. In python we can store any object as a variable, and we will be storing functions which transform a vector to another vector, through the “modelspace_to_ndc” method.
255@dataclass
256class FunctionStack:
257 stack: List[InvertibleFunction[Vector3D]] = field(
258 default_factory=lambda: []
259 )
260
261 def push(self, o: object):
262 self.stack.append(o)
263
264 def pop(self):
265 return self.stack.pop()
266
267 def clear(self):
268 self.stack.clear()
269
270 def modelspace_to_ndc_fn(self) -> InvertibleFunction[Vector3D]:
271 return compose(*self.stack)
272
273
274fn_stack = FunctionStack()
Define four functions, which we will compose on the stack.
Push identity onto the stack, which will will never pop off of the stack.
28def test_fn_stack():
29 def identity(x):
30 return x
31
32 fn_stack.push(identity)
33 assert 1 == fn_stack.modelspace_to_ndc_fn()(1)
34
35 def add_one(x):
36 return x + 1
37
38 fn_stack.push(add_one)
39 assert 2 == fn_stack.modelspace_to_ndc_fn()(1) # x + 1 = 2
40
41 def multiply_by_2(x):
42 return x * 2
43
44 fn_stack.push(multiply_by_2) # (x * 2) + 1 = 3
45 assert 3 == fn_stack.modelspace_to_ndc_fn()(1)
46
47 def add_5(x):
48 return x + 5
49
50 fn_stack.push(add_5) # ((x + 5) * 2) + 1 = 13
51 assert 13 == fn_stack.modelspace_to_ndc_fn()(1)
52
53 fn_stack.pop()
54 assert 3 == fn_stack.modelspace_to_ndc_fn()(1) # (x * 2) + 1 = 3
55
56 fn_stack.pop()
57 assert 2 == fn_stack.modelspace_to_ndc_fn()(1) # x + 1 = 2
58
59 fn_stack.pop()
60 assert 1 == fn_stack.modelspace_to_ndc_fn()(1) # x = 1
Event Loop¶
217while not glfw.window_should_close(window):
...
In previous demos, camera_space_to_ndc_space_fn was always the last function called in the method chained pipeline. Put it on the bottom of the stack, by pushing it first, so that “modelspace_to_ndc” calls this function last. Each subsequent push will add a new function to the top of the stack.

239 # camera space to NDC
240 fn_stack.push(uniform_scale(1.0 / 10.0))
Unlike in previous demos in which we read the transformations from modelspace to world space backwards; this time because the transformations are on a stack, the fns on the model stack can be read forwards, where each operation translates/rotates/scales the current space
The camera’s position and orientation are defined relative to world space like so, read top to bottom:

But, since we need to transform world-space to camera space, they must be inverted by reversing the order, and negating the arguments
Therefore the transformations to put the world space into camera space are.

244 # world space to camera space
245 fn_stack.push(inverse(translate(camera.position_ws)))
draw paddle 1¶
Unlike in previous demos in which we read the transformations from modelspace to world space backwards; because the transformations are on a stack, the fns on the model stack can be read forwards, where each operation translates/rotates/scales the current space

249 # paddle 1 model space to world space
250 fn_stack.push(compose(translate(paddle1.position),
251 rotate_z(paddle1.rotation)))
for each of the modelspace coordinates, apply all of the procedures on the stack from top to bottom this results in coordinate data in NDC space, which we can pass to glVertex3f
255 glColor3f(*astuple(paddle1.color))
256 glBegin(GL_QUADS)
257 for p1_v_ms in paddle1.vertices:
258 paddle1_vector_ndc = fn_stack.modelspace_to_ndc_fn()(p1_v_ms)
259 glVertex3f(
260 paddle1_vector_ndc.x,
261 paddle1_vector_ndc.y,
262 paddle1_vector_ndc.z,
263 )
264 glEnd()
draw the square¶
since the modelstack is already in paddle1’s space, and since the blue square is defined relative to paddle1, just add the transformations relative to it before the blue square is drawn. Draw the square, and then remove these 4 transformations from the stack (done below)

268 fn_stack.push(compose(translate(Vector3D(x=0.0, y=0.0, z=-1.0)),
269 rotate_z(rotation_around_paddle1),
270 translate(Vector3D(x=2.0, y=0.0, z=0.0)),
271 rotate_z(square_rotation)))
274 glColor3f(0.0, 0.0, 1.0)
275 glBegin(GL_QUADS)
276 for ms in square:
277 square_vector_ndc = fn_stack.modelspace_to_ndc_fn()(ms)
278 glVertex3f(
279 square_vector_ndc.x,
280 square_vector_ndc.y,
281 square_vector_ndc.z,
282 )
283 glEnd()
Now we need to remove fns from the stack so that the lambda stack will convert from world space to NDC. This will allow us to just add the transformations from world space to paddle2 space on the stack.
287 fn_stack.pop() # pop off square space to paddle 1 space
288 # current space is paddle 1 space
289 fn_stack.pop() # # pop off paddle 1 model space to world space
290 # current space is world space
since paddle2’s modelspace is independent of paddle 1’s space, only leave the view and projection fns (1) - (4)
draw paddle 2¶

294 fn_stack.push(compose(translate(paddle2.position),
295 rotate_z(paddle2.rotation)))
299 # draw paddle 2
300 glColor3f(*astuple(paddle2.color))
301 glBegin(GL_QUADS)
302 for p2_v_ms in paddle2.vertices:
303 paddle2_vector_ndc = fn_stack.modelspace_to_ndc_fn()(p2_v_ms)
304 glVertex3f(
305 paddle2_vector_ndc.x,
306 paddle2_vector_ndc.y,
307 paddle2_vector_ndc.z,
308 )
309 glEnd()
remove all fns from the function stack, as the next frame will set them clear makes the list empty, as the list (stack) will be repopulated the next iteration of the event loop.
314 # done rendering everything for this frame, just go ahead and clear all functions
315 # off of the stack, back to NDC as current space
316 fn_stack.clear()
317
Swap buffers and execute another iteration of the event loop
Notice in the above code, adding functions to the stack is creating a shared context for transformations, and before we call “glVertex3f”, we always call “modelspace_to_ndc” on the modelspace vector. In Demo 19, we will be using OpenGL 2.1 matrix stacks. Although we don’t have the code for the OpenGL driver, given that you’ll see that we pass modelspace data directly to “glVertex3f”, it should be clear that the OpenGL implementation must fetch the modelspace to NDC transformations from the ModelView and Projection matrix stacks.