Adding Depth - Z axis Demo 14

Objective

Do the same stuff as the previous demo, but use 3D coordinates, where the negative z axis goes into the screen (because of the right hand rule). Positive z comes out of the monitor towards your face.

Things that this demo doesn’t end up doing correctly:

  • The blue square is always drawn, even when its z-coordinate in world space is less than the paddle’s. The solution will be z-buffering https://en.wikipedia.org/wiki/Z-buffering, and it is implemented in the next demo.

Demo 14

Demo 14

Camera Space

Camera Space

Camera Space

Camera Space

How to Execute

Load src/modelviewprojection/demo14.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

  • Vector data will now have an X, Y, and Z component.

  • Rotations around an angle in 3D space follow the right hand rule. Here’s a link to them in matrix form, which we have not yet covered.

Right hand rule
  • With open palm, fingers on the x axis, rotating the fingers to y axis, means that the positive z axis is in the direction of the thumb. Positive Theta moves in the direction that your fingers did.

  • starting on the y axis, rotating to z axis, thumb is on the positive x axis.

  • starting on the z axis, rotating to x axis, thumb is on the positive y axis.

src/modelviewprojection/mathutils3d
61@dataclasses.dataclass
62class Vector3D(mu2d.Vector2D):
63    z: float  #: The z-component of the 3D Vector

Rotate Z

Rotate Z is the same rotate that we’ve used so far, but doesn’t affect the z component at all.

Rotate Z
src/modelviewprojection/mathutils3d.py
140def rotate_z(angle_in_radians: float) -> mu.InvertibleFunction:
141    def create_rotate_function(r) -> typing.Callable[[mu.Vector], mu.Vector]:
142        def f(vector: mu.Vector) -> mu.Vector:
143            assert isinstance(vector, Vector3D)
144            xy_on_xy: mu.Vector = mu2d.Vector2D(x=vector.x, y=vector.y)
145            rotated_xy_on_xy: mu.Vector = r(xy_on_xy)
146            assert isinstance(rotated_xy_on_xy, mu2d.Vector2D)
147            return Vector3D(
148                x=rotated_xy_on_xy.x, y=rotated_xy_on_xy.y, z=vector.z
149            )
150
151        return f
152
153    r: mu.InvertibleFunction = mu2d.rotate(angle_in_radians)
154    return mu.InvertibleFunction(
155        create_rotate_function(r), create_rotate_function(mu.inverse(r))
156    )

Rotate X

Rotate X
src/modelviewprojection/mathutils3d.py
 99def rotate_x(angle_in_radians: float) -> mu.InvertibleFunction:
100    def create_rotate_function(r) -> typing.Callable[[mu.Vector], mu.Vector]:
101        def f(vector: mu.Vector) -> mu.Vector:
102            assert isinstance(vector, Vector3D)
103            yz_on_xy: mu.Vector = mu2d.Vector2D(x=vector.y, y=vector.z)
104            rotated_yz_on_xy: mu.Vector = r(yz_on_xy)
105            return Vector3D(
106                x=vector.x, y=rotated_yz_on_xy.x, z=rotated_yz_on_xy.y
107            )
108
109        return f
110
111    r = mu2d.rotate(angle_in_radians)
112    return mu.InvertibleFunction(
113        create_rotate_function(r), create_rotate_function(mu.inverse(r))
114    )

Rotate Y

Rotate Y
src/modelviewprojection/mathutils3d.py
119def rotate_y(angle_in_radians: float) -> mu.InvertibleFunction:
120    def create_rotate_function(r) -> typing.Callable[[mu.Vector], mu.Vector]:
121        def f(vector: mu.Vector) -> mu.Vector:
122            assert isinstance(vector, Vector3D)
123            zx_on_xy: mu.Vector = mu2d.Vector2D(x=vector.z, y=vector.x)
124            rotated_zx_on_xy: mu.Vector = r(zx_on_xy)
125            assert isinstance(rotated_zx_on_xy, mu2d.Vector2D)
126            return Vector3D(
127                x=rotated_zx_on_xy.y, y=vector.y, z=rotated_zx_on_xy.x
128            )
129
130        return f
131
132    r = mu2d.rotate(angle_in_radians)
133    return mu.InvertibleFunction(
134        create_rotate_function(r), create_rotate_function(mu.inverse(r))
135    )

Scale

src/modelviewprojection/mathutils.py
419def uniform_scale(m: float) -> InvertibleFunction:
420    def f(vector: Vector) -> Vector:
421        return vector * m
422
423    def f_inv(vector: Vector) -> Vector:
424        if m == 0.0:
425            raise ValueError("Not invertible.  Scaling factor cannot be zero.")
426
427        return vector * (1.0 / m)
428
429    return InvertibleFunction(f, f_inv)

Code

The only new aspect of the code below is that the paddles have a z-coordinate of 0 in their modelspace.

src/modelviewprojection/demo14.py
 94paddle1: Paddle = Paddle(
 95    vertices=[
 96        mu3d.Vector3D(x=-1.0, y=-3.0, z=0.0),
 97        mu3d.Vector3D(x=1.0, y=-3.0, z=0.0),
 98        mu3d.Vector3D(x=1.0, y=3.0, z=0.0),
 99        mu3d.Vector3D(x=-1.0, y=3.0, z=0.0),
100    ],
101    color=colorutils.Color3(r=0.578123, g=0.0, b=1.0),
102    position=mu3d.Vector3D(x=-9.0, y=0.0, z=0.0),
103)
104
105paddle2: Paddle = Paddle(
106    vertices=[
107        mu3d.Vector3D(x=-1.0, y=-3.0, z=0.0),
108        mu3d.Vector3D(x=1.0, y=-3.0, z=0.0),
109        mu3d.Vector3D(x=1.0, y=3.0, z=0.0),
110        mu3d.Vector3D(x=-1.0, y=3.0, z=0.0),
111    ],
112    color=colorutils.Color3(r=1.0, g=1.0, b=0.0),
113    position=mu3d.Vector3D(x=9.0, y=0.0, z=0.0),
114)

The only new aspect of the square below is that the paddles have a z-coordinate of 0 in their modelspace. N.B that since we do a sequence transformations to the modelspace data to get to world-space coordinates, the X, Y, and Z coordinates are subject to be different.

src/modelviewprojection/demo14.py
119@dataclasses.dataclass
120class Camera:
121    position_ws: mu3d.Vector3D = dataclasses.field(
122        default_factory=lambda: mu3d.Vector3D(x=0.0, y=0.0, z=0.0)
123    )
124
125
126camera: Camera = Camera()

The camera now has a z-coordinate of 0 also.

src/modelviewprojection/demo14.py
130square: list[mu3d.Vector3D] = [
131    mu3d.Vector3D(x=-0.5, y=-0.5, z=0.0),
132    mu3d.Vector3D(x=0.5, y=-0.5, z=0.0),
133    mu3d.Vector3D(x=0.5, y=0.5, z=0.0),
134    mu3d.Vector3D(x=-0.5, y=0.5, z=0.0),
135]

Event Loop

src/modelviewprojection/demo14.py
188while not glfw.window_should_close(window):
...
  • Draw Paddle 1

src/modelviewprojection/demo14.py
208    GL.glColor3f(*iter(paddle1.color))
209    GL.glBegin(GL.GL_QUADS)
210    for p1_v_ms in paddle1.vertices:
211        ms_to_ndc: mu3d.InvertibleFunction = mu3d.compose(
212            [
213                # camera space to NDC
214                mu3d.uniform_scale(1.0 / 10.0),
215                # world space to camera space
216                mu3d.inverse(mu3d.translate(camera.position_ws)),
217                # model space to world space
218                mu3d.compose(
219                    [
220                        mu3d.translate(paddle1.position),
221                        mu3d.rotate_z(paddle1.rotation),
222                    ]
223                ),
224            ]
225        )
226
227        paddle1_vector_ndc: mu3d.Vector3D = ms_to_ndc(p1_v_ms)
228        GL.glVertex3f(
229            paddle1_vector_ndc.x, paddle1_vector_ndc.y, paddle1_vector_ndc.z
230        )
231    GL.glEnd()

The square should not be visible when hidden behind the paddle1, as we do a translate by -1. But in running the demo, you see that the square is always drawn over the paddle.

  • Draw the Square

src/modelviewprojection/demo14.py
235    # draw square
236    GL.glColor3f(0.0, 0.0, 1.0)
237    GL.glBegin(GL.GL_QUADS)
238    for ms in square:
239        ms_to_ndc: mu3d.InvertibleFunction = mu3d.compose(
240            [
241                # camera space to NDC
242                mu3d.uniform_scale(1.0 / 10.0),
243                # world space to camera space
244                mu3d.inverse(mu3d.translate(camera.position_ws)),
245                # model space to world space
246                mu3d.compose(
247                    [
248                        mu3d.translate(paddle1.position),
249                        mu3d.rotate_z(paddle1.rotation),
250                    ]
251                ),
252                # square space to paddle 1 space
253                mu3d.compose(
254                    [
255                        mu3d.translate(mu3d.Vector3D(x=0.0, y=0.0, z=-1.0)),
256                        mu3d.rotate_z(rotation_around_paddle1),
257                        mu3d.translate(mu3d.Vector3D(x=2.0, y=0.0, z=0.0)),
258                        mu3d.rotate_z(square_rotation),
259                    ]
260                ),
261            ]
262        )
263        square_vector_ndc: mu3d.Vector3D = ms_to_ndc(ms)
264        GL.glVertex3f(
265            square_vector_ndc.x, square_vector_ndc.y, square_vector_ndc.z
266        )
267    GL.glEnd()

This is because without depth buffering, the object drawn last clobbers the color of any previously drawn object at the pixel. Try moving the square drawing code to the beginning, and you will see that the square can be hidden behind the paddle.

  • Draw Paddle 2

src/modelviewprojection/demo14.py
271    # draw paddle 2
272    GL.glColor3f(*iter(paddle2.color))
273    GL.glBegin(GL.GL_QUADS)
274    for p2_v_ms in paddle2.vertices:
275        ms_to_ndc: mu3d.InvertibleFunction = mu3d.compose(
276            [
277                # camera space to NDC
278                mu3d.uniform_scale(1.0 / 10.0),
279                # world space to camera space
280                mu3d.inverse(mu3d.translate(camera.position_ws)),
281                # model space to world space
282                mu3d.compose(
283                    [
284                        mu3d.translate(paddle2.position),
285                        mu3d.rotate_z(paddle2.rotation),
286                    ]
287                ),
288            ]
289        )
290
291        paddle2_vector_ndc: mu3d.Vector3D = ms_to_ndc(p2_v_ms)
292        GL.glVertex3f(
293            paddle2_vector_ndc.x, paddle2_vector_ndc.y, paddle2_vector_ndc.z
294        )
295    GL.glEnd()

Added translate in 3D. Added scale in 3D. These are just like the 2D versions, just with the same process applied to the z axis.

They direction of the rotation is defined by the right hand rule.

https://en.wikipedia.org/wiki/Right-hand_rule