|
| 1 | +Struct API |
| 2 | +================================ |
| 3 | + |
| 4 | +Struct API is alternative API to array api to use **cglm** with improved type safety and easy to use. |
| 5 | +Since struct api is built top of array api, every struct API is not documented here. |
| 6 | +See array api documentation for more information for individual functions. |
| 7 | + |
| 8 | +By default struct api adds `s` suffix to every type name e.g. vec3s, mat4s, versors etc. |
| 9 | +Also struct api `s` suffix to namespace e.g. `glms_vec3_add`, `glms_mat4_mul` etc. |
| 10 | + |
| 11 | +By starting v0.9.0, struct api namespace is configurable. We can omit **glms_** namespace or |
| 12 | +even change it with custom name to move existing api integrations to **cglm** more easliy... |
| 13 | +We can also add **s** to functin names if we want e.g. `glms_vec3_add()` -> `vec3_add()` or `vec3s_add()`. |
| 14 | + |
| 15 | +By including **cglm/struct.h** header you will include all struct api. It will also include **cglm/cglm.h** too. |
| 16 | +Since struct apis are inline you don't need to build or link *cglm* against |
| 17 | +your project unless if you want to use pre-built call-api too. |
| 18 | + |
| 19 | +Struct API is built top of array api. So you can mix them. |
| 20 | +Use **.raw** union member to access raw array data to use it with array api. |
| 21 | + |
| 22 | +Unlike array api ([0], [1], [0][0] ...), it is also possible to use struct api |
| 23 | +with **.x**, **.y**, **.z**, **.w**, **.r**, **.g**, **.b**, **.a**, **.m00**, **m01**... |
| 24 | +accessors to access individual elements/properties of vectors and matrices. |
| 25 | + |
| 26 | +Struct API usage: |
| 27 | +----------------- |
| 28 | + |
| 29 | +.. code-block:: c |
| 30 | +
|
| 31 | + #include <cglm/struct.h> |
| 32 | + |
| 33 | + mat4s m1 = glms_mat4_identity(); /* init... */ |
| 34 | + mat4s m2 = glms_mat4_identity(); /* init... */ |
| 35 | + mat4s m3 = glms_mat4_mul(glms_mat4_mul(m1, m2), glms_mat4_mul(m3, m4)); |
| 36 | + |
| 37 | + vec3s v1 = { 1.0f, 0.0f, 0.0f }; |
| 38 | + vec3s v2 = { 0.0f, 1.0f, 0.0f }; |
| 39 | + vec4s v4 = { 0.0f, 1.0f, 0.0f, 0.0f }; |
| 40 | + vec4 v5a = { 0.0f, 1.0f, 0.0f, 0.0f }; |
| 41 | + |
| 42 | + mat4s m4 = glms_rotate(m3, M_PI_2, |
| 43 | + glms_vec3_cross(glms_vec3_add(v1, v6) |
| 44 | + glms_vec3_add(v1, v7))); |
| 45 | +
|
| 46 | + v1.x = 1.0f; v1.y = 0.0f; v1.z = 0.0f; |
| 47 | + // or |
| 48 | + v1.raw[0] = 1.0f; v1.raw[1] = 0.0f; v1.raw[2] = 0.0f; |
| 49 | +
|
| 50 | + /* use struct api with array api (mix them). */ |
| 51 | + /* use .raw to access array and use it with array api */ |
| 52 | +
|
| 53 | + glm_vec4_add(m4.col[0].raw, v5a, m4.col[0].raw); |
| 54 | + glm_mat4_mulv(m4.raw, v4.raw, v5a); |
| 55 | +
|
| 56 | +or omit `glms_` namespace completely (see options below): |
| 57 | + |
| 58 | +.. code-block:: c |
| 59 | +
|
| 60 | + #define CGLM_OMIT_NS_FROM_STRUCT_API |
| 61 | +
|
| 62 | + #include <cglm/struct.h> |
| 63 | +
|
| 64 | + mat4s m1 = mat4_identity(); /* init... */ |
| 65 | + mat4s m2 = mat4_identity(); /* init... */ |
| 66 | + mat4s m3 = mat4_mul(mat4_mul(m1, m2), mat4_mul(m3, m4)); |
| 67 | + |
| 68 | + vec3s v1 = { 1.0f, 0.0f, 0.0f }; |
| 69 | + vec3s v2 = { 0.0f, 1.0f, 0.0f }; |
| 70 | + vec4s v4 = { 0.0f, 1.0f, 0.0f, 0.0f }; |
| 71 | + vec4 v5a = { 0.0f, 1.0f, 0.0f, 0.0f }; |
| 72 | + |
| 73 | + mat4s m4 = glms_rotate(m3, M_PI_2, |
| 74 | + vec3_cross(vec3_add(v1, v6) |
| 75 | + vec3_add(v1, v7))); |
| 76 | + |
| 77 | + v1.x = 1.0f; v1.y = 0.0f; v1.z = 0.0f; |
| 78 | + // or |
| 79 | + v1.raw[0] = 1.0f; v1.raw[1] = 0.0f; v1.raw[2] = 0.0f; |
| 80 | +
|
| 81 | + /* use struct api with array api (mix them) */ |
| 82 | + glm_vec4_add(m4.col[0].raw, v5a, m4.col[0].raw); |
| 83 | + glm_mat4_mulv(m4.raw, v4.raw, v5a); |
| 84 | + |
| 85 | +Configuring the Struct API: |
| 86 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 87 | + |
| 88 | +To configure the Struct API namespace, you can define the following macros before including the cglm/struct.h header: |
| 89 | + |
| 90 | +- **CGLM_OMIT_NS_FROM_STRUCT_API**: omits CGLM_STRUCT_API_NS (`glms_`) namespace completely if there is sub namespace e.g `mat4_`, `vec4_` ... DEFAULT is not defined |
| 91 | +- **CGLM_STRUCT_API_NS**: define name space for struct api, DEFAULT is **glms** |
| 92 | +- **CGLM_STRUCT_API_NAME_SUFFIX**: define name suffix, DEFAULT is **empty** e.g defining it as #define CGLM_STRUCT_API_NAME_SUFFIX s will add s suffix to mat4_mul -> mat4s_mul |
| 93 | + |
| 94 | + |
| 95 | +Detailed documentation for Struct API: |
| 96 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 97 | + |
| 98 | +Since struct api if built top of array api, see array api functions for more information about individual functions. |
0 commit comments