Debian Codegraph @

doc/programming_guide/math.rst

raw
1 .. _guide_math:
2
3 Matrix and Vector Math
4 ======================
5
6 Modern OpenGL depends on matrixes for projection, translation, and other things. To
7 provide out-of-the-box functionality, pyglet's :py:mod:`~pyglet.math` module includes
8 the necessary Matrix and Vector types to cover common use cases. Provided types are:
9 :py:class:`~pyglet.math.Vec2`, :py:class:`~pyglet.math.Vec3`, :py:class:`~pyglet.math.Vec4`,
10 :py:class:`~pyglet.math.Mat3`, and :py:class:`~pyglet.math.Mat4`.
11
12 See the :py:mod:`~pyglet.math` API documentation for a full list of methods and operations
13 that these objects support.
14
15 .. note:: Matrix and Vector types are ``tuple`` subclasses. They are therefore
16 immutable - all operations return a **new** object; they are not updated in-place.
17
18
19 Creating a Matrix
20 -----------------
21 A Matrix can be created with no arguments, or by passing the initial values::
22
23 my_matrix = Mat4()
24 # or
25 my_matrix = Mat4(1.0, 0.0, 0.0, 0.0,
26 0.0, 1.0, 0.0, 0.0,
27 0.0, 0.0, 1.0, 0.0,
28 0.0, 0.0, 0.0, 1.0)
29
30 If no arguments are given on creation, an "identity" matrix will be created by default.
31 (1.0 on the main diagonal, as shown here).
32
33 More conveniently, you can also create matrixes from one of their helper class methods.
34
35
36 Matrix Helper Methods
37 ---------------------
38 When working with 3D graphics in OpenGL, A common operation is setting a perspective
39 projection matrix. The ``Mat4`` class includes a helper method for this task. The arguments
40 are similar to what you will find in popular OpenGL math libraries::
41
42 # Perspective (3D) projection matrix:
43 projection = Mat4.perspective_projection(aspect_ratio, z_near=0.1, z_far=255, fov=60)
44
45 For setting a projection matrix on the current OpenGL context, pyglet Windows have
46 a ``projection`` property. This points to a projection matrix used by all of pyglet's
47 built-in classes, and can be set as follows::
48
49 my_matrix = Mat4.perspective_projection(aspect_ratio, z_near=0.1, z_far=255)
50 window.projection = my_matrix
51
52 For working with 2D graphics, a helper method also exists for creating orthogonal projection
53 matrixes, which is created and set in a similar way::
54
55 # Orthographic (2D) projection matrix:
56 my_matrix = Mat4.orthogonal_projection(0, width, 0, height, z_near=-255, z_far=255)
57 window.projection = my_matrix
58
59 By default, pyglet is already set up with an orthographic projection matrix. It automatically
60 updates this 2D matrix whenever a Window is created or resized. This is done inside of the
61 Window's ``on_resize`` event handler. If you plan to set your own 3D or 2D projection matrix,
62 a useful pattern is to override the default ``on_resize`` event handler with your own::
63
64 @window.event
65 def on_resize(width, height):
66 # window.viewport = (0, 0, width, height)
67 window.projection = Mat4.perspective_projection(window.aspect_ratio, z_near=0.1, z_far=255)
68 return pyglet.event.EVENT_HANDLED # Don't call the default handler
69
70
71 In addition to the ``projection`` property, pyglet Windows also contain a ``view`` property that
72 points to another matrix that is used internally by pyglet's built-in classes. The view matrix
73 gets multiplied with the projection matrix, controlling the final positioning and rendering of
74 pyglet's built-in objects like Sprites, Shapes, and text. Unlike the projection matrix the view
75 matrix defaults to an identity matrix, and therefore has no effect initially. This makes it useful
76 for users to set their own matrix on it, and control zooming or translating (panning) a "camera".
77 The ``Mat4`` class contains helper methods for creating these types of matrixes::
78
79 # translate the camera +100 on the X axis:
80 view_matrix = Mat4.from_translation(vector=Vec3(x=100, y=0, z=0))
81 # or scale by 2X (be careful of the Z values):
82 view_matrix = Mat4.from_scale(vector=Vec3(x=2.0, y=2.0, z=1.0))
83
84 window.view = view_matrix
85
86 You can of course create multiple matrixes and multiply them together, such as to zoom and
87 then translate, or translate and then zoom, etc. Be careful of multiplication order, as matrix
88 multiplication is non-commutative.
89
90 See the :py:mod:`~pyglet.math` API documentation for a full list of helper class methods.
91
92
93 Matrix Multiplication
94 ---------------------
95 Matrix classes in pyglet use the Python ``matmul`` (``@``) operator for matrix multiplication.
96 The ``star`` operator (``*``) is not allowed, and will raise an exception. For example::
97
98 new_matrix = rotation_matrix @ translation_matrix
99
100
101 Creating Vectors
102 ----------------
103 pyglet includes classes for 2D, 3D, and 4D vectors: :py:class:`~pyglet.math.Vec2`,
104 :py:class:`~pyglet.math.Vec3` and :py:class:`~pyglet.math.Vec4`. These Vec types support
105 most mathematical operations, and implement helper methods for most common vector operations.
106 Vecs are created with their values.
107
108 myvec2 = Vec2() # same as Vec(x=0.0, y=0.0)
109 myvec2 = Vec2(-4.5, 3.0)
110 myvec3 = Vec3(100.0, 400.0, 1.0)
111
112
113 Vector Operations
114 -----------------
115 TBD