Hash :
62d98c9c
Author :
Thomas de Grivel
Date :
2024-01-11T16:29:42
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
/* c3
* Copyright 2022,2023 kmx.io <contact@kmx.io>
*
* Permission is hereby granted to use this software granted the above
* copyright notice and this permission paragraph are included in all
* copies and substantial portions of this software.
*
* THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
* PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
* AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
* THIS SOFTWARE.
*/
#include <math.h>
#include <libc3/c3.h>
#include "gl_point_3f.h"
s_gl_point_3f * gl_point_3f_init (s_gl_point_3f *p, f32 x, f32 y, f32 z)
{
assert(p);
p->x = x;
p->y = y;
p->z = z;
return p;
}
s_gl_point_3f * gl_point_3f_init_copy (s_gl_point_3f *p,
const s_gl_point_3f *src)
{
assert(p);
assert(src);
p->x = src->x;
p->y = src->y;
p->z = src->z;
return p;
}
s_gl_point_3f * gl_point_3f_init_normalize (s_gl_point_3f *p,
const s_gl_point_3f *src)
{
f32 norm;
f32 ratio;
assert(p);
assert(src);
norm = gl_point_3f_norm(src);
ratio = 1.0f / norm;
p->x = src->x * ratio;
p->y = src->y * ratio;
p->z = src->z * ratio;
return p;
}
s_gl_point_3f * gl_point_3f_init_product (s_gl_point_3f *p,
const s_gl_matrix_4f *m,
const s_gl_point_3f *s)
{
assert(p);
assert(m);
assert(s);
p->x = m->xx * s->x + m->yx * s->y + m->zx * s->z + m->tx;
p->y = m->xy * s->x + m->yy * s->y + m->zy * s->z + m->ty;
p->z = m->xz * s->x + m->yz * s->y + m->zz * s->z + m->tz;
return p;
}
s_gl_point_3f * gl_point_3f_init_zero (s_gl_point_3f *p)
{
assert(p);
p->x = 0.0;
p->y = 0.0;
p->z = 0.0;
return p;
}
void gl_point_3f_delete (s_gl_point_3f *p)
{
free(p);
}
s_gl_point_3f * gl_point_3f_new (f32 x, f32 y, f32 z)
{
s_gl_point_3f *p;
p = calloc(1, sizeof(s_gl_point_3f));
if (! p) {
err_puts("gl_point_3f_new: failed to allocate memory");
return NULL;
}
gl_point_3f_init(p, x, y, z);
return p;
}
s_gl_point_3f * gl_point_3f_new_copy (const s_gl_point_3f *src)
{
s_gl_point_3f *p;
p = calloc(1, sizeof(s_gl_point_3f));
if (! p) {
err_puts("gl_point_3f_new: failed to allocate memory");
return NULL;
}
gl_point_3f_init_copy(p, src);
return p;
}
s_gl_point_3f * gl_point_3f_new_product (const s_gl_matrix_4f *m,
const s_gl_point_3f *s)
{
s_gl_point_3f *p;
assert(m);
assert(s);
p = calloc(1, sizeof(s_gl_point_3f));
if (! p) {
err_puts("gl_point_3f_new: failed to allocate memory");
return NULL;
}
gl_point_3f_init_product(p, m, s);
return p;
}
s_gl_point_3f * gl_point_3f_new_zero (void)
{
s_gl_point_3f *p;
p = calloc(1, sizeof(s_gl_point_3f));
if (! p) {
err_puts("gl_point_3f_new: failed to allocate memory");
return NULL;
}
gl_point_3f_init_zero(p);
return p;
}
f32 gl_point_3f_norm (const s_gl_point_3f *p)
{
assert(p);
return sqrtf(p->x * p->x + p->y * p->y + p->z * p->z);
}
void gl_point_3f_normalize (s_gl_point_3f *p)
{
f32 norm;
f32 ratio;
assert(p);
norm = gl_point_3f_norm(p);
ratio = 1.0f / norm;
p->x *= ratio;
p->y *= ratio;
p->z *= ratio;
}
void gl_point_3f_transform (s_gl_point_3f *p,
const s_gl_matrix_4f *matrix)
{
s_gl_point_3f tmp;
assert(p);
assert(matrix);
gl_point_3f_init_product(&tmp, matrix, p);
*p = tmp;
}