Modelspace - Demo 06

Purpose

Learn about modelspace.

Demo 06

Demo 06

How to Execute

On Linux or on MacOS, in a shell, type “python src/demo06/demo.py”. On Windows, in a command prompt, type “python src\demo06\demo.py”.

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

Modelspace

Normalized-device-coordinates are not a natural system of numbers for use by humans. Imagine that the paddles in the previous chapters exist in real life, and are 20 meters wide and 60 meters tall. The graphics programmer should be able to use those numbers directly; they shouldn’t have to manually transform the distances into normalized-device-coordinates.

Whatever a convenient numbering system is (i.e. coordinate system) for modeling objects is called “model-space”. Since a paddle has four corners, which corner should be at the origin (0,0)? If you don’t already know what you want at the origin, then none of the corners should be; instead put the center of the object at the origin (Because by putting the center of the object at the origin, scaling and rotating the object are trivial, as shown in later chapters).

Representing a Paddle using Modelspace

Representing a Paddle using Modelspace

Modelspace - the coordinate system (origin plus axes), in which some object’s verticies are defined.

WorldSpace

WorldSpace is a toplevel space, independent of NDC, that we choose to use. It’s arbitrary. If you were to model a racetrack for a racing game, the origin of WorldSpace may be the center of that racetrack. If you were modelling our solar system, the center of the sun could be the origin of “WorldSpace”. I personally would put the center of our flat earth at the origin, but reasonable people can disagree.

For our demo with paddles, the author arbitralily defines the WorldSpace to be 200 units wide, 200 units tall, with the origin at the center.

Demo 06

Demo 06

Modelspace to WorldSpace

The author prefers to view transformations as changes to the graph paper, as compared to view tranformations as changes to points.

As such, for placing paddle1, we can view the translation as a change to the graph paper relative to world space coordinates (only incidentally bringing the vertices along with it) and then resetting the graph paper (i.e. both origin and axes) back to it’s original position and orientation. Although we will think of the paddle’s vertices as relative to its own space (i.e. -10 to 10 horizontally, -30 to 30 vertically), we will not look at the numbers of what they are in world space coordinates, as doing so

  • Will not give us any insight

  • Will distract us from thinking clearly about what’s happening

Translating Paddle 1

Translating Paddle 1

  • As an example, figure out the world space coordinate of the upper rights corner of the paddle after it’s been translated, and ask yourself what that means and what insight did you gain?

The animation above shows multiple steps, shown now without animation.

Modelspace of Paddle 1

Paddle 1's Modelspace

Paddle 1’s Modelspace

Modelspace of Paddle 1 Superimposed on Worldspace after the translation

Paddle 1’s graph paper gets translated -90 units in the x direction, and some number of units in the y direction, 0 during the first frame, based off of user input. The origin is translated, and the graph paper comes with it, onto which you can plot the vertices. Notice that the coordinate system labels below the plot and to the left of the plot is unchanged. That is world space, which has not changed.

Paddle 1's Modelspace Superimposed on World Space

Paddle 1’s Modelspace Superimposed on World Space

Paddle 1’s vertices in WorldSpace Coordinates

Paddle 1's Verticies in World Space

Paddle 1’s Verticies in World Space. Don’t concern yourself with what the numbers are.

Now that the transformation has happened, the vertices are all in world space. You could calculate their values in world space, but that will not give you any insight. The only numbers that matter for insight as that the entire graph paper of modelspace, which originally was the same as world space, has changed, bringing the verticies along with it.

Same goes for Paddle 2’s modelspace, relative to it’s translation, which are different values.

Translating Paddle 2

Translating Paddle 2

The animation above shows multiple steps, shown now without animation.

Modelspace of Paddle 2

Paddle 1's Modelspace

Paddle 2’s Modelspace

Modelspace of Paddle 2 Superimposed on Worldspace after the translation

Paddle 1's Modelspace Superimposed on World Space

Paddle 2’s Modelspace Superimposed on World Space

Paddle 2’s vertices in WorldSpace Coordinates

Paddle 1's Verticies in World Space

Paddle 2’s Verticies in World Space. Don’t concern yourself with what the numbers are.

Scaling

Our paddles are now well outside of NDC, and as such, they would not be displayed, as they would be clipped out. Their values are outside of -1.0 to 1.0. All we will need to do to convert them from world space to NDC is divide each component, x and y, by 100.

As a demonstration of how scaling works, let’s make an object’s width twice as large, and height three times as large. (The author tried doing the actual scaling of 1/100 in an animated gif, and it looked awful, therefore a different scaling gif is showed here, but the concept is the same).

We can expand or shrink the size of an object by “scale”ing each component of the vertices by some coefficient.

Scaling

Scaling

Modelspace

Modelspace

Modelspace Superimposed on World Space

Modelspace Superimposed on World Space

Worldspace

Worldspace. Don’t concern yourself with what the numbers are.

Our global space is -100 to 100 in both dimesnions, and to get it into NDC, we need to scale by dividing by 100

Demo 06

Demo 06

\begin{bmatrix}
x_{w} \\
y_{w}
\end{bmatrix}  =
\vec{f}_{p1}^{w}(
\begin{bmatrix}
x_{p1} \\
y_{p1}
\end{bmatrix})  =
\begin{bmatrix}
x_{p1} \\
y_{p1}
\end{bmatrix} +
\begin{bmatrix}
{p1}_{x} \\
{p1}_{y}
\end{bmatrix}

where x_p1, y_p1 are the modelspace coordinates of the paddle’s vertices, and where p1_center_x_worldspace, p1_center_y_worldspace, are the offset from the world space’s origin to the center of the paddle, i.e. the translation.

\begin{bmatrix}
x_{w} \\
y_{w}
\end{bmatrix}  =
\vec{f}_{p2}^{w} (
\begin{bmatrix}
x_{p2} \\
y_{p2}
\end{bmatrix}) =
\begin{bmatrix}
x_{p2} \\
y_{p2}
\end{bmatrix} +
\begin{bmatrix}
{p2}_{x} \\
{p2}_{y}
\end{bmatrix}

Now, the coordinates for paddle 1 and for paddle 2 are in world space, and we need the match to take any world space coordinates and convert them to NDC.

\begin{bmatrix}
x_{ndc} \\
y_{ndc}
\end{bmatrix} =
\vec{f}_{w}^{ndc} (
\begin{bmatrix}
x_{w} \\
y_{w}
\end{bmatrix})  = 1/100 *
\begin{bmatrix}
x_{w} \\
y_{w}
\end{bmatrix}

@dataclass
class Vertex:
    x: float
    y: float

    def translate(self: Vertex, tx: float, ty: float) -> Vertex:
        return Vertex(x=(self.x + tx), y=(self.y + ty))

    def scale(self: Vertex, scale_x: float, scale_y: float) -> Vertex:
        return Vertex(x=(self.x * scale_x), y=(self.y * scale_y))

  • NEW – Add the ability to scale a vertex, to stretch or to shrink

paddle1: Paddle = Paddle(
    vertices=[
        Vertex(x=-10.0, y=-30.0),
        Vertex(x=10.0, y=-30.0),
        Vertex(x=10.0, y=30.0),
        Vertex(x=-10.0, y=30.0),
    ],
    r=0.578123,
    g=0.0,
    b=1.0,
    position=Vertex(-90.0, 0.0),
)

paddle2: Paddle = Paddle(
    vertices=[
        Vertex(x=-10.0, y=-30.0),
        Vertex(x=10.0, y=-30.0),
        Vertex(x=10.0, y=30.0),
        Vertex(x=-10.0, y=30.0),
    ],
    r=1.0,
    g=0.0,
    b=0.0,
    position=Vertex(90.0, 0.0),
)
  • paddles are using modelspace coordinates instead of NDC

def handle_movement_of_paddles() -> None:
    global paddle1, paddle2

    if glfw.get_key(window, glfw.KEY_S) == glfw.PRESS:
        paddle1.position.y -= 10.0
    if glfw.get_key(window, glfw.KEY_W) == glfw.PRESS:
        paddle1.position.y += 10.0
    if glfw.get_key(window, glfw.KEY_K) == glfw.PRESS:
        paddle2.position.y -= 10.0
    if glfw.get_key(window, glfw.KEY_I) == glfw.PRESS:
        paddle2.position.y += 10.0


  • Movement code needs to happen in Modelspace’s units.

Code

The Event Loop

while not glfw.window_should_close(window):
    while (
        glfw.get_time() < time_at_beginning_of_previous_frame + 1.0 / TARGET_FRAMERATE
    ):
        pass
    time_at_beginning_of_previous_frame = glfw.get_time()

    glfw.poll_events()

    width, height = glfw.get_framebuffer_size(window)
    glViewport(0, 0, width, height)
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

    draw_in_square_viewport()
    handle_movement_of_paddles()

Rendering Paddle 1

    glColor3f(paddle1.r, paddle1.g, paddle1.b)

    glBegin(GL_QUADS)
    for model_space in paddle1.vertices:
Paddle 1's Modelspace

Paddle 1’s Modelspace

        world_space: Vertex = model_space.translate(tx=paddle1.position.x,
                                                    ty=paddle1.position.y)
Paddle 1's Modelspace Superimposed on World Space

Paddle 1’s Modelspace Superimposed on World Space

Paddle 1's Modelspace Superimposed on World Space

Reset coordinate system.

The coordinate system now resets back to the coordinate system specified on the left and below. Now, we must scale everything by 1/100. I have not included a picture of that here. Scaling happens relative to the origin, so the picture would be the same, just with different labels on the bottom and on the left.

        ndc_space: Vertex = world_space.scale(scale_x=1.0 / 100.0,
                                              scale_y=1.0 / 100.0)
        glVertex2f(ndc_space.x, ndc_space.y)

    glEnd()

Rendering Paddle 2

    glColor3f(paddle2.r, paddle2.g, paddle2.b)

    glBegin(GL_QUADS)
    for model_space in paddle2.vertices:
Paddle 2's Modelspace

Paddle 2’s Modelspace

        world_space: Vertex = model_space.translate(tx=paddle2.position.x,
                                                    ty=paddle2.position.y)
Paddle 2's Modelspace Superimposed on World Space

Paddle 2’s Modelspace Superimposed on World Space

Paddle 2's Modelspace Superimposed on World Space

Reset coordinate system.

        ndc_space: Vertex = world_space.scale(scale_x=1.0 / 100.0,
                                              scale_y=1.0 / 100.0)
        glVertex2f(ndc_space.x, ndc_space.y)
    glEnd()

The coordinate system is reset. Now scale everything by 1/100. I have not included a picture of that here. Scaling happens relative to the origin, so the picture would be the same, just with different labels on the bottom and on the left.

    glfw.swap_buffers(window)