Gama C Library
Gama C API Documentation
transform.h
Go to the documentation of this file.
1/**
2 * @file transform.h
3 * @brief Defines structures and functions for 3D transformations (position, rotation, scale).
4 *
5 * This file provides the `gm3Transform` structure to encapsulate the
6 * translation, rotation, and scaling of 3D objects, along with helper
7 * functions to apply these transformations to 3D points and vectors.
8 */
9#pragma once
10
11#include "position.h"
12
13/**
14 * @brief Represents a 3D transformation, including position, rotation, and scale.
15 */
16typedef struct {
17 gm3Pos position; /**< Translation vector. */
18 gm3Pos rotation; /**< Rotation vector (Euler angles in radians, X, Y, Z). */
19 gm3Pos scale; /**< Scaling vector. */
21
22/**
23 * @brief A default `gm3Transform` instance.
24 *
25 * Initializes a transform with default position `{0, 0, 20}`, no rotation,
26 * and uniform scale `{1, 1, 1}`.
27 */
29 .position = {0, 0, 20}, .rotation = {0}, .scale = {1, 1, 1}};
30
31/**
32 * @brief Rotates a `gm3Pos` vector by the given Euler angles (X, Y, Z).
33 *
34 * The rotation is applied sequentially around the X, then Y, then Z axes.
35 *
36 * @param res A pointer to the `gm3Pos` vector to rotate (modified in place).
37 * @param rot A pointer to a `gm3Pos` containing the Euler angles (radians) for rotation around X, Y, and Z axes.
38 */
39void gm3_pos_rotate(gm3Pos *res, const gm3Pos *rot) {
40 if (!res || !rot)
41 return;
42 double temp;
43
44 // 1. Rotate around X-axis
45 temp = res->y * cos(rot->x) - res->z * sin(rot->x);
46 res->z = res->y * sin(rot->x) + res->z * cos(rot->x);
47 res->y = temp;
48
49 // 2. Rotate around Y-axis
50 temp = res->x * cos(rot->y) + res->z * sin(rot->y);
51 res->z = -res->x * sin(rot->y) + res->z * cos(rot->y);
52 res->x = temp;
53
54 // 3. Rotate around Z-axis
55 temp = res->x * cos(rot->z) - res->y * sin(rot->z);
56 res->y = res->x * sin(rot->z) + res->y * cos(rot->z);
57 res->x = temp;
58}
59/**
60 * @brief Applies a `gm3Transform` to a `gm3Pos` vector.
61 *
62 * The transformation is applied in the order: scale, then rotate, then translate.
63 *
64 * @param p A pointer to the `gm3Pos` vector to transform (modified in place).
65 * @param t A pointer to the `gm3Transform` to apply.
66 */
68 gm3_pos_mul(p, &t->scale);
70 gm3_pos_add(p, &t->position);
71}
72
73/**
74 * @brief Creates a new `gm3Transform` struct initialized to identity.
75 *
76 * The position and rotation are set to zero, and scale is set to one.
77 *
78 * @return A new identity `gm3Transform` instance.
79 */
84 t.scale.x = 1;
85 t.scale.y = 1;
86 t.scale.z = 1;
87 return t;
88}
void gm3_pos_mul(gm3Pos *res, const gm3Pos *trans)
Multiplies the components of a gm3Pos vector by the corresponding components of another gm3Pos vector...
Definition position.h:180
#define gm3_pos_reset(p)
Resets the coordinates of a gm3Pos struct to (0, 0, 0).
Definition position.h:154
double cos(double x)
Calculates the cosine of an angle (in radians).
Definition math.h:105
double sin(double x)
Calculates the sine of an angle (in radians).
Definition math.h:128
Represents a 3D position or vector.
Definition position.h:11
double y
Definition position.h:12
double z
Definition position.h:12
double x
Definition position.h:12
Represents a 3D transformation, including position, rotation, and scale.
Definition transform.h:16
gm3Pos scale
Definition transform.h:19
gm3Pos rotation
Definition transform.h:18
gm3Pos position
Definition transform.h:17
const gm3Transform gm3_default_transform
A default gm3Transform instance.
Definition transform.h:28
void gm3_pos_rotate(gm3Pos *res, const gm3Pos *rot)
Rotates a gm3Pos vector by the given Euler angles (X, Y, Z).
Definition transform.h:39
void gm3_transform_pos(gm3Pos *p, const gm3Transform *t)
Applies a gm3Transform to a gm3Pos vector.
Definition transform.h:67
gm3Transform gm3_transform()
Creates a new gm3Transform struct initialized to identity.
Definition transform.h:80