Skip to content

Commit 39610e2

Browse files
committed
Set the W component on 3D vectors
1 parent 620f488 commit 39610e2

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

mathc.c

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,7 @@ void pvector3_add(struct vec *a, struct vec *b, struct vec *result)
457457
result->x = a->x + b->x;
458458
result->y = a->y + b->y;
459459
result->z = a->z + b->z;
460+
result->w = 1.0f;
460461
}
461462

462463
MATHC_EXTERN_INLINE struct vec vector3_add(struct vec a, struct vec b)
@@ -471,6 +472,7 @@ void pvector3_subtract(struct vec *a, struct vec *b, struct vec *result)
471472
result->x = a->x - b->x;
472473
result->y = a->y - b->y;
473474
result->z = a->z - b->z;
475+
result->w = 1.0f;
474476
}
475477

476478
MATHC_EXTERN_INLINE struct vec vector3_subtract(struct vec a, struct vec b)
@@ -485,6 +487,7 @@ void pvector3_scale(struct vec *a, float scale, struct vec *result)
485487
result->x = a->x * scale;
486488
result->y = a->y * scale;
487489
result->z = a->z * scale;
490+
result->w = 1.0f;
488491
}
489492

490493
MATHC_EXTERN_INLINE struct vec vector3_scale(struct vec a, float scale)
@@ -499,6 +502,7 @@ void pvector3_multiply(struct vec *a, struct vec *b, struct vec *result)
499502
result->x = a->x * b->x;
500503
result->y = a->y * b->y;
501504
result->z = a->z * b->z;
505+
result->w = 1.0f;
502506
}
503507

504508
MATHC_EXTERN_INLINE struct vec vector3_multiply(struct vec a, struct vec b)
@@ -513,6 +517,7 @@ void pvector3_divide(struct vec *a, struct vec *b, struct vec *result)
513517
result->x = a->x / b->x;
514518
result->y = a->y / b->y;
515519
result->z = a->z / b->z;
520+
result->w = 1.0f;
516521
}
517522

518523
MATHC_EXTERN_INLINE struct vec vector3_divide(struct vec a, struct vec b)
@@ -527,6 +532,7 @@ void pvector3_negative(struct vec *a, struct vec *result)
527532
result->x = -a->x;
528533
result->y = -a->y;
529534
result->z = -a->z;
535+
result->w = 1.0f;
530536
}
531537

532538
MATHC_EXTERN_INLINE struct vec vector3_negative(struct vec a)
@@ -553,6 +559,7 @@ void pvector3_inverse(struct vec *a, struct vec *result)
553559
} else {
554560
result->z = 0.0f;
555561
}
562+
result->w = 1.0f;
556563
}
557564

558565
MATHC_EXTERN_INLINE struct vec vector3_inverse(struct vec a)
@@ -567,6 +574,7 @@ void pvector3_abs(struct vec *a, struct vec *result)
567574
result->x = fabsf(a->x);
568575
result->y = fabsf(a->y);
569576
result->z = fabsf(a->z);
577+
result->w = 1.0f;
570578
}
571579

572580
MATHC_EXTERN_INLINE struct vec vector3_abs(struct vec a)
@@ -581,6 +589,7 @@ void pvector3_floor(struct vec *a, struct vec *result)
581589
result->x = floorf(a->x);
582590
result->y = floorf(a->y);
583591
result->z = floorf(a->z);
592+
result->w = 1.0f;
584593
}
585594

586595
MATHC_EXTERN_INLINE struct vec vector3_floor(struct vec a)
@@ -595,6 +604,7 @@ void pvector3_ceil(struct vec *a, struct vec *result)
595604
result->x = ceilf(a->x);
596605
result->y = ceilf(a->y);
597606
result->z = ceilf(a->z);
607+
result->w = 1.0f;
598608
}
599609

600610
MATHC_EXTERN_INLINE struct vec vector3_ceil(struct vec a)
@@ -609,6 +619,7 @@ void pvector3_round(struct vec *a, struct vec *result)
609619
result->x = roundf(a->x);
610620
result->y = roundf(a->y);
611621
result->z = roundf(a->z);
622+
result->w = 1.0f;
612623
}
613624

614625
MATHC_EXTERN_INLINE struct vec vector3_round(struct vec a)
@@ -623,6 +634,7 @@ void pvector3_max(struct vec *a, struct vec *b, struct vec *result)
623634
result->x = fmaxf(a->x, b->x);
624635
result->y = fmaxf(a->y, b->y);
625636
result->z = fmaxf(a->z, b->z);
637+
result->w = 1.0f;
626638
}
627639

628640
MATHC_EXTERN_INLINE struct vec vector3_max(struct vec a, struct vec b)
@@ -637,6 +649,7 @@ void pvector3_min(struct vec *a, struct vec *b, struct vec *result)
637649
result->x = fminf(a->x, b->x);
638650
result->y = fminf(a->y, b->y);
639651
result->z = fminf(a->z, b->z);
652+
result->w = 1.0f;
640653
}
641654

642655
MATHC_EXTERN_INLINE struct vec vector3_min(struct vec a, struct vec b)
@@ -661,6 +674,7 @@ void pvector3_cross(struct vec *a, struct vec *b, struct vec *result)
661674
result->x = a->y * b->z - a->z * b->y;
662675
result->y = a->z * b->x - a->x * b->z;
663676
result->z = a->x * b->y - a->y * b->x;
677+
result->w = 1.0f;
664678
}
665679

666680
MATHC_EXTERN_INLINE struct vec vector3_cross(struct vec a, struct vec b)
@@ -703,6 +717,7 @@ void pvector3_normalize(struct vec *a, struct vec *result)
703717
result->y = 0.0f;
704718
result->z = 0.0f;
705719
}
720+
result->w = 1.0f;
706721
}
707722

708723
MATHC_EXTERN_INLINE struct vec vector3_normalize(struct vec a)
@@ -718,6 +733,7 @@ void pvector3_slide(struct vec *a, struct vec *normal, struct vec *result)
718733
result->x = a->x - normal->x * d;
719734
result->y = a->y - normal->y * d;
720735
result->y = a->z - normal->z * d;
736+
result->w = 1.0f;
721737
}
722738

723739
MATHC_EXTERN_INLINE struct vec vector3_slide(struct vec a, struct vec normal)
@@ -733,6 +749,7 @@ void pvector3_reflect(struct vec *a, struct vec *normal, struct vec *result)
733749
result->x = a->x - normal->x * d;
734750
result->y = a->y - normal->y * d;
735751
result->z = a->z - normal->z * d;
752+
result->w = 1.0f;
736753
}
737754

738755
MATHC_EXTERN_INLINE struct vec vector3_reflect(struct vec a, struct vec normal)
@@ -767,6 +784,7 @@ void pvector3_linear_interpolation(struct vec *a, struct vec *b, float p, struct
767784
result->x = a->x + (b->x - a->x) * p;
768785
result->y = a->y + (b->y - a->y) * p;
769786
result->z = a->z + (b->z - a->z) * p;
787+
result->w = 1.0f;
770788
}
771789

772790
MATHC_EXTERN_INLINE struct vec vector3_linear_interpolation(struct vec a, struct vec b, float p)
@@ -812,7 +830,7 @@ void pquaternion_subtract(struct vec *a, struct vec *b, struct vec *result)
812830
result->x = a->x - b->x;
813831
result->y = a->y - b->y;
814832
result->y = a->z - b->z;
815-
result->y = a->w - b->w;
833+
result->w = a->w - b->w;
816834
}
817835

818836
MATHC_EXTERN_INLINE struct vec quaternion_subtract(struct vec a, struct vec b)
@@ -839,10 +857,10 @@ MATHC_EXTERN_INLINE struct vec quaternion_scale(struct vec a, float scale)
839857

840858
void pquaternion_multiply(struct vec *a, struct vec *b, struct vec *result)
841859
{
842-
result->w = a->w * b->w - a->x * b->x - a->y * b->y - a->z * b->z;
843860
result->x = a->w * b->x + a->x * b->w + a->y * b->z - a->z * b->y;
844861
result->y = a->w * b->y + a->y * b->w + a->z * b->x - a->x * b->z;
845862
result->z = a->w * b->z + a->z * b->w + a->x * b->y - a->y * b->x;
863+
result->w = a->w * b->w - a->x * b->x - a->y * b->y - a->z * b->z;
846864
}
847865

848866
MATHC_EXTERN_INLINE struct vec quaternion_multiply(struct vec a, struct vec b)
@@ -1516,6 +1534,7 @@ void pmatrix_get_translation(struct mat *m, struct vec *result)
15161534
result->x = m->m14;
15171535
result->y = m->m24;
15181536
result->z = m->m34;
1537+
result->w = 1.0f;
15191538
}
15201539

15211540
MATHC_EXTERN_INLINE struct vec matrix_get_translation(struct mat m)
@@ -1734,7 +1753,7 @@ float quintic_ease_in_out(float p)
17341753
f = 16.0f * p * p * p * p * p;
17351754
} else {
17361755
f = ((2.0f * p) - 2.0f);
1737-
f = 0.5f * f * f * f * f * f + 1.0f;
1756+
f = 0.5f * f * f * f * f * f + 1.0f;
17381757
}
17391758
return f;
17401759
}

0 commit comments

Comments
 (0)