summaryrefslogtreecommitdiffstats
path: root/src/main/java/dev/figboot/cuberender/math/Vector4f.java
blob: 1995622a6dbda52201ed1e194a9734763546e74b (plain) (blame)
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
package dev.figboot.cuberender.math;

import lombok.AllArgsConstructor;

@AllArgsConstructor
public class Vector4f {
    public float x, y, z, w;

    public Vector4f() {
        this(0, 0, 0, 0);
    }

    public Vector4f(float x, float y, float z) {
        this(x, y, z, 1f);
    }

    public Vector4f(Vector4f vec) {
        this(vec.x, vec.y, vec.z, vec.w);
    }

    public Vector4f fromARGB(int argb) {
        return fromARGB(argb, this);
    }

    public Vector4f fromARGB(int argb, Vector4f target) {
        target.x = ((argb & 0x00FF0000) >>> 16) / 255f;
        target.y = ((argb & 0x0000FF00) >>> 8) / 255f;
        target.z = ((argb & 0x000000FF)) / 255f;
        target.w = ((argb & 0xFF000000) >>> 24) / 255f;
        return target;
    }

    public int toARGB() {
        return ((int)(MathUtil.clamp(w, 0, 1) * 255) << 24)
                | ((int)(MathUtil.clamp(x, 0, 1) * 255) << 16)
                | ((int)(MathUtil.clamp(y, 0, 1) * 255) << 8)
                | ((int)(MathUtil.clamp(z, 0, 1) * 255));
    }

    public Vector4f times(float fact) {
        return times(fact, this);
    }

    public Vector4f times(float fact, Vector4f target) {
        target.x = x * fact;
        target.y = y * fact;
        target.z = z * fact;
        target.w = w * fact;
        return target;
    }

    public float dot(Vector4f that) {
        return this.x * that.x + this.y * that.y + this.z * that.z + this.w * that.w;
    }

    public float lengthSquared() {
        // dot(this, this)
        return this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w;
    }

    public float length() {
        return (float)Math.sqrt(lengthSquared());
    }

    public Vector4f normalize() {
        return normalize(this);
    }

    public Vector4f normalize(Vector4f target) {
        float len = length();
        target.x = this.x / len;
        target.y = this.y / len;
        target.z = this.z / len;
        target.w = this.w / len;
        return target;
    }

    public void copyFrom(Vector4f src) {
        x = src.x;
        y = src.y;
        z = src.z;
        w = src.w;
    }
}