Skip to content

Commit

Permalink
Move math to c
Browse files Browse the repository at this point in the history
  • Loading branch information
luboslenco committed Sep 8, 2024
1 parent a1f89a2 commit 7888526
Show file tree
Hide file tree
Showing 29 changed files with 1,061 additions and 983 deletions.
1 change: 1 addition & 0 deletions project.js
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@ if (flags.with_iron) {
project.addFile('sources/iron_string.c');
project.addFile('sources/iron_armpack.c');
project.addFile('sources/iron_vec2.c');
project.addFile('sources/iron_vec3.c');
project.addFile('sources/iron_vec4.c');
project.addFile('sources/iron_quat.c');
project.addFile('sources/iron_mat3.c');
Expand Down
12 changes: 8 additions & 4 deletions sources/iron.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@
#include "iron_gc.h"
#include "iron_obj.h"
#include "iron_vec2.h"
#include "iron_vec3.h"
#include "iron_vec4.h"
#include "iron_quat.h"
#include "iron_mat3.h"
#include "iron_mat4.h"
#ifdef KINC_WINDOWS
#include <Windows.h>
#endif
Expand Down Expand Up @@ -1810,12 +1814,12 @@ void iron_g4_set_floats(kinc_g4_constant_location_t *location, buffer_t *values)
kinc_g4_set_floats(*location, (float *)values->buffer, (int)(values->length / 4));
}

void iron_g4_set_matrix4(kinc_g4_constant_location_t *location, /*buffer_t*/ float *matrix) {
kinc_g4_set_matrix4(*location, (kinc_matrix4x4_t *)matrix);
void iron_g4_set_matrix4(kinc_g4_constant_location_t *location, mat4_t m) {
kinc_g4_set_matrix4(*location, &m);
}

void iron_g4_set_matrix3(kinc_g4_constant_location_t *location, /*buffer_t*/ float *matrix) {
kinc_g4_set_matrix3(*location, (kinc_matrix3x3_t *)matrix);
void iron_g4_set_matrix3(kinc_g4_constant_location_t *location, mat3_t m) {
kinc_g4_set_matrix3(*location, &m);
}


Expand Down
81 changes: 81 additions & 0 deletions sources/iron_mat3.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#include "iron_mat3.h"

#include <math.h>
#include <kinc/math/core.h>

mat3_t mat3_create(float _00, float _10, float _20,
float _01, float _11, float _21,
float _02, float _12, float _22) {
mat3_t m;
m.m[0] = _00;
m.m[1] = _01;
m.m[2] = _02;
m.m[3] = _10;
m.m[4] = _11;
m.m[5] = _12;
m.m[6] = _20;
m.m[7] = _21;
m.m[8] = _22;
return m;
}

mat3_t mat3_identity() {
return mat3_create(
1, 0, 0,
0, 1, 0,
0, 0, 1
);
}

mat3_t mat3_translation(float x, float y) {
return mat3_create(
1, 0, x,
0, 1, y,
0, 0, 1
);
}

mat3_t mat3_rotation(float alpha) {
return mat3_create(
cosf(alpha), -sinf(alpha), 0,
sinf(alpha), cosf(alpha), 0,
0, 0, 1
);
}

mat3_t mat3_set_from4(mat4_t m4) {
mat3_t m;
m.m[0] = m4.m[0];
m.m[1] = m4.m[1];
m.m[2] = m4.m[2];
m.m[3] = m4.m[4];
m.m[4] = m4.m[5];
m.m[5] = m4.m[6];
m.m[6] = m4.m[8];
m.m[7] = m4.m[9];
m.m[8] = m4.m[10];
}

mat3_t mat3_multmat(mat3_t a, mat3_t b) {
return mat3_create(
a.m[0] * b.m[0] + a.m[3] * b.m[1] + a.m[6] * b.m[2],
a.m[0] * b.m[3] + a.m[3] * b.m[4] + a.m[6] * b.m[5],
a.m[0] * b.m[6] + a.m[3] * b.m[7] + a.m[6] * b.m[8],
a.m[1] * b.m[0] + a.m[4] * b.m[1] + a.m[7] * b.m[2],
a.m[1] * b.m[3] + a.m[4] * b.m[4] + a.m[7] * b.m[5],
a.m[1] * b.m[6] + a.m[4] * b.m[7] + a.m[7] * b.m[8],
a.m[2] * b.m[0] + a.m[5] * b.m[1] + a.m[8] * b.m[2],
a.m[2] * b.m[3] + a.m[5] * b.m[4] + a.m[8] * b.m[5],
a.m[2] * b.m[6] + a.m[5] * b.m[7] + a.m[8] * b.m[8]
);
}

mat3_t mat3_nan() {
mat3_t m;
m.m[0] = NAN;
return m;
}

bool mat3_isnan(mat3_t m) {
return isnan(m.m[0]);
}
15 changes: 15 additions & 0 deletions sources/iron_mat3.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once

#include <kinc/math/matrix.h>
#include "iron_mat4.h"

mat3_t mat3_create(float _00, float _10, float _20,
float _01, float _11, float _21,
float _02, float _12, float _22);
mat3_t mat3_identity();
mat3_t mat3_translation(float x, float y);
mat3_t mat3_rotation(float alpha);
mat3_t mat3_set_from4(mat4_t m4);
mat3_t mat3_multmat(mat3_t a, mat3_t b);
mat3_t mat3_nan();
bool mat3_isnan(mat3_t m);
Loading

0 comments on commit 7888526

Please sign in to comment.