summaryrefslogtreecommitdiff
path: root/src/math/vec4.c
blob: 3d6df46c742c51dc0654b55b0498fa005e4b0922 (plain)
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
#include <math.h>

#include "math.h"
#include "../util/assert.h"
#include "../core/mem.h"

Vec4 vec4zero = { 0, 0, 0,0 };

static Vec4 sharedmat4;

void vec4_dividew(Vec4* v, Vec3* out) {
	ssr_assert(out && v);
	float w = 1.f / v->w;
	out->x = v->x * w ;
	out->y = v->y * w;
	out->z = v->z * w;
}

void vec4_dividewnoz(Vec4* v, Vec4* out) {
	ssr_assert(out && v);
	float w = 1.f / v->w;
	out->x = v->x * w;
	out->y = v->y * w;
	out->z = v->z;
	out->w = v->w;
}

void vec4_tostring(Vec4* v, char buf[]) {
	sprintf(buf, "%8.3f %8.3f %8.3f %8.3f", v->x, v->y, v->z, v->w);
}

void vec4_print(Vec4* v) {
	vec4_tostring(v, printbuffer);
	printf("\n%s\n", printbuffer);
}

void vec4_lerp(Vec4* a, Vec4* b, float t, Vec4* out) {
	out->x = lerp(a->x, b->x, t);
	out->y = lerp(a->y, b->y, t);
	out->z = lerp(a->z, b->z, t);
	out->w = lerp(a->w, b->w, t);
}

void vec4_scale(Vec4* v, float t, Vec4* out) {
	out->x = v->x * t;
	out->y = v->y * t;
	out->z = v->z * t;
	out->w = v->w * t;
}

void vec4_add(Vec4* v1, Vec4* v2, Vec4* out) {
	out->x = v1->x + v2->x;
	out->y = v1->y + v2->y;
	out->z = v1->z + v2->z;
	out->w = v1->w + v2->w;
}