OpenGL Mathematics (GLM) library for Python providing comprehensive vector and matrix manipulation capabilities for graphics programming.
npx @tessl/cli install tessl/pypi-pyglm@2.8.0OpenGL Mathematics (GLM) library for Python providing comprehensive vector and matrix manipulation capabilities specifically designed for graphics programming, 3D graphics applications, and physics computations. PyGLM is a Python extension written in C++ that brings the power of the OpenGL Mathematics library to Python with GLSL-compatible operations and high performance.
pip install pyglmfrom pyglm import glmTyping support (optional):
from pyglm import glm_typingfrom pyglm import glm
# Create 3D vectors
v1 = glm.vec3(1, 2, 3)
v2 = glm.vec3(4, 5, 6)
# Vector operations
v3 = v1 + v2 # Vector addition
cross_product = glm.cross(v1, v2) # Cross product
dot_product = glm.dot(v1, v2) # Dot product
normalized = glm.normalize(v1) # Normalize vector
# Create 4x4 identity matrix
matrix = glm.mat4()
# Transformation matrices
angle = glm.radians(45) # Convert degrees to radians
rotation_matrix = glm.rotate(matrix, angle, glm.vec3(0, 0, 1))
translation_matrix = glm.translate(matrix, glm.vec3(1, 2, 3))
scaling_matrix = glm.scale(matrix, glm.vec3(2, 2, 2))
# Quaternions for rotations
axis = glm.vec3(0, 0, 1)
angle = glm.radians(45)
quat = glm.angleAxis(angle, axis)
quat_matrix = glm.mat4_cast(quat)
# Projection matrices for 3D graphics
projection = glm.perspective(glm.radians(45), 16.0/9.0, 0.1, 100.0)
view = glm.lookAt(glm.vec3(0, 0, 3), glm.vec3(0, 0, 0), glm.vec3(0, 1, 0))PyGLM provides a comprehensive linear algebra library structured around several key components:
The library maintains compatibility with OpenGL's GLSL shader language while providing Python-friendly interfaces including operator overloading, buffer protocol support, and seamless integration with numpy arrays.
Comprehensive vector mathematics including creation, arithmetic, geometric operations, and comparison functions. Supports vectors from 1 to 4 components in multiple precisions (boolean, 8/16/32/64-bit integers, 32/64-bit floats).
# Vector types (examples - full list includes all precision variants)
class vec1: ...
class vec2: ...
class vec3: ...
class vec4: ...
class dvec2: ... # Double precision
class ivec3: ... # Integer
class uvec4: ... # Unsigned integer
class bvec2: ... # Boolean
# Geometric operations
def dot(x, y): ...
def cross(x, y): ...
def normalize(x): ...
def length(x): ...
def distance(x, y): ...
def reflect(I, N): ...
def refract(I, N, eta): ...Complete matrix mathematics including creation, arithmetic, linear algebra operations, and transformation utilities. Supports matrices from 2×2 to 4×4 in multiple precisions.
# Matrix types (examples - full list includes all precision variants)
class mat2: ... # 2×2 matrix
class mat3: ... # 3×3 matrix
class mat4: ... # 4×4 matrix
class dmat3: ... # Double precision 3×3
class imat4x3: ... # Integer 4×3 matrix
# Matrix operations
def transpose(m): ...
def inverse(m): ...
def determinant(m): ...
def matrixCompMult(x, y): ...
def outerProduct(c, r): ...High-level transformation matrix creation for 3D graphics including projection, view, and model transformations. Essential for OpenGL and DirectX applications.
# Projection matrices
def perspective(fovy, aspect, near, far): ...
def perspectiveFov(fov, width, height, near, far): ...
def ortho(left, right, bottom, top, near, far): ...
def frustum(left, right, bottom, top, near, far): ...
# View matrices
def lookAt(eye, center, up): ...
def lookAtLH(eye, center, up): ...
def lookAtRH(eye, center, up): ...
# Model transformations
def translate(m, v): ...
def rotate(m, angle, axis): ...
def scale(m, v): ...Efficient 3D rotation representation with comprehensive quaternion mathematics, conversions, and interpolation functions.
# Quaternion types
class quat: ... # Float precision
class dquat: ... # Double precision
# Quaternion operations
def angleAxis(angle, axis): ...
def slerp(x, y, a): ...
def conjugate(q): ...
def mat4_cast(q): ...
def quat_cast(m): ...
def eulerAngles(q): ...Complete GLSL-compatible mathematical function library covering trigonometry, exponentials, common functions, and interpolation with support for both scalar and vector inputs.
# Basic math functions
def abs(x): ...
def min(x, y): ...
def max(x, y): ...
def clamp(x, minVal, maxVal): ...
def mix(x, y, a): ...
def smoothstep(edge0, edge1, x): ...
# Trigonometric functions
def sin(angle): ...
def cos(angle): ...
def tan(angle): ...
def radians(degrees): ...
def degrees(radians): ...
# Exponential functions
def pow(x, y): ...
def exp(x): ...
def log(x): ...
def sqrt(x): ...Random number generation and noise functions for procedural content generation and simulation applications.
def linearRand(min, max): ...
def gaussRand(mean, deviation): ...
def circularRand(radius): ...
def sphericalRand(radius): ...
def perlin(p): ...
def simplex(p): ...Type conversion, memory access, testing utilities, and packing/unpacking functions for interfacing with graphics APIs and external libraries.
def value_ptr(x): ...
def sizeof(type): ...
def make_vec3(ptr): ...
def packHalf2x16(v): ...
def unpackHalf2x16(p): ...
def floatBitsToInt(value): ...
def isinf(x): ...
def isnan(x): ...Advanced mathematical and utility functions that extend PyGLM's core capabilities with specialized algorithms for graphics programming, procedural generation, and scientific computing.
# Color space conversion
def convertLinearToSRGB(color): ...
def convertSRGBToLinear(color): ...
# Rounding extensions
def ceilMultiple(value, multiple): ...
def ceilPowerOfTwo(value): ...
def floorPowerOfTwo(value): ...
# Norm functions
def l1Norm(vec): ...
def l2Norm(vec): ...
def lMaxNorm(vec): ...
# Coordinate conversion
def euclidean(polar): ...
def polar(euclidean): ...
# Projection functions
def project(obj, model, proj, viewport): ...
def unProject(win, model, proj, viewport): ...
# Euler angles
def euler(angles): ...
def eulerAngles(quat): ...# Scalar type aliases
bool_ = ctypes.c_bool
int8 = ctypes.c_byte
int16 = ctypes.c_short
int32 = ctypes.c_long
int64 = ctypes.c_longlong
uint8 = ctypes.c_ubyte
uint16 = ctypes.c_ushort
uint32 = ctypes.c_ulong
uint64 = ctypes.c_ulonglong
float32 = ctypes.c_float
float64 = ctypes.c_doubleAll vector, matrix, and quaternion types support:
[i] access and assignmentfor loops+, -, *, /, etc.)==, !=, <, <=, >, >=)len(), abs(), etc.)to_list(), to_tuple(), to_bytes())# Mathematical constants
e = 2.71828... # Euler's number
euler = 2.71828... # Alias for e
pi = 3.14159... # Pi
half_pi = 1.57079... # π/2
quarter_pi = 0.78539... # π/4
two_pi = 6.28318... # 2π
three_over_two_pi = 4.71238... # 3π/2
one_over_pi = 0.31830... # 1/π
two_over_pi = 0.63661... # 2/π
four_over_pi = 1.27323... # 4/π
one_over_two_pi = 0.15915... # 1/(2π)
# Root constants
root_two = 1.41421... # √2
root_three = 1.73205... # √3
root_five = 2.23606... # √5
root_pi = 1.77245... # √π
root_half_pi = 1.25331... # √(π/2)
root_ln_four = 1.32934... # √(ln(4))
root_two_pi = 2.50662... # √(2π)
one_over_root_two = 0.70710... # 1/√2
two_over_root_pi = 1.12837... # 2/√π
# Logarithmic constants
ln_two = 0.69314... # ln(2)
ln_ten = 2.30258... # ln(10)
ln_ln_two = -0.36651... # ln(ln(2))
# Other mathematical constants
golden_ratio = 1.61803... # φ (golden ratio)
epsilon = 1.19209e-07 # Machine epsilon
zero = 0.0 # Zero constant
one = 1.0 # One constant
third = 0.33333... # 1/3
two_thirds = 0.66666... # 2/3