Skip to content

Using vectors

Zuzu-Typ edited this page Nov 10, 2019 · 11 revisions
  1. Initialization

Initialization

There are dozens of ways of constructing a vector.
For simplicity, if the same initialization process applies to all vector types, it will only be shown for glm.vec2.

Initialization with no arguments

Initializing a vector without any additional arguments will set all of it's components to zero (of the respective type).
i.e. glm.vec2() returns vector (0.0, 0.0).
A boolean vector would also be initialized with zero (or False if you will).

Initialization with a single number

Initializing a vector with a number will set all of it's components to the given number (which may be converted if necessary).
i.e. glm.vec2(2.43) returns vector (2.43, 2.43).

Initializing all components with numbers

A vector vecN can be initialized with N numbers, which will be copied (or may be converted) to their components.
i.e. glm.vec2(1, 2) returns vector (1.0, 2.0)
glm.vec3(4, 5, 6) returns vector (4.0, 5.0, 6.0)
glm.ivec4(9, 8, 7, 6) returns vector (9, 8, 7, 6)

Copying a vector

A copy of a vector can be obtained by initializing a vector with an instance of a vector.
i.e. glm.vec2(glm.vec2(3, 2)) returns vector (3.0, 2.0)
This is what's known as the copy constructor.

Initializing vectors with larger vectors

You can initialize any vector with a larger vector (which will discard any values that don't fit into the new vector).
i.e. glm.vec1(glm.vec3(1, 2, 3)) returns vector (1.0)
likewise glm.vec2(glm.vec4(5, 6, 7, 8)) returns vector (5.0, 6.0)

Constructing vectors from other vectors and numbers

As long as you don't use any vec1s in your equation, you can construct any vector from a combination of vectors and / or numbers if their sum equals the length of the target vector.
i.e. glm.vec4(glm.vec2(1, 2), 3, 4) returns vector (1.0, 2.0, 3.0, 4.0)
likewise glm.vec3(5, glm.vec2(4, 3)) returns vector (5.0, 4.0, 3.0)

but glm.vec2(glm.vec1(1), 2) doesn't work.
glm.vec3(glm.vec2(1, 2), glm.vec2(3, 4)) also doesn't work.

Lists (and other iterables)

Instead of using vectors to initialize vectors, you can also use lists and other iterables.
e.g. glm.vec2([1, 2]) returns vector (1.0, 2.0)
or glm.vec3((3, 4), 5) returns vector (3.0, 4.0, 5.0)
This also applies to any function of PyGLM that takes a vector.
(if you do not need this functionality, you might want to use PyGLM_FAST - see Building PyGLM)

Objects that support the buffer protocol (numpy, bytes)

A few objects in Python support a functionality called the buffer protocol.
One such example would be the Python bytes type or numpy.array.
PyGLM also supports this protocol and thus can be converted to or from any other object that supports it, granted it's in a fitting format.
e.g. bytes(glm.u8vec2(1,2)) returns b'\x01\x02'
and glm.u8vec2(b'\x01\x02') returns an 8-bit unsigned integer vector (1, 2)

or glm.vec3(numpy.array([4,5,6])) returns vector (4.0, 5.0, 6.0)
and numpy.array(glm.vec3(4, 5, 6)) returns array([4., 5., 6.], dtype=float32)

Note: objects that use the buffer protocol may request a reference instead of a copy of the object, meaning that if you change the 'copy', you'll also change the original.

Same as with iterators, this also applies to any function of PyGLM that takes a vector.
(if you do not need this functionality, you might want to use PyGLM_FAST - see Building PyGLM)

Clone this wiki locally