Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411430 Posts in 69363 Topics- by 58416 Members - Latest Member: JamesAGreen

April 20, 2024, 12:25:47 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)MATHC - a pure C math library for 2D and 3D programming
Pages: [1]
Print
Author Topic: MATHC - a pure C math library for 2D and 3D programming  (Read 1833 times)
ferreiradaselva
Level 3
***



View Profile
« on: October 04, 2017, 04:58:08 PM »

As I was writing my C game engine, I decided to decouple the math implementation and make its own library.

It has implementation for 2D vectors, 3D vectors, 4D vectors, quaternions and matrices.

It's mainly for game programming.

Github: https://github.com/ferreiradaselva/mathc
« Last Edit: August 02, 2018, 06:27:48 PM by ferreiradaselva » Logged

ThemsAllTook
Administrator
Level 10
******



View Profile WWW
« Reply #1 on: October 04, 2017, 07:35:40 PM »

Very nice!
Logged

ferreiradaselva
Level 3
***



View Profile
« Reply #2 on: October 05, 2017, 07:43:52 AM »

Thanks ^^

I'm working on some improvements, like unions and version of the functions that take pointers (but I'm still keeping the functions that pass the structures as values).
Logged

qMopey
Level 6
*


View Profile WWW
« Reply #3 on: October 05, 2017, 03:28:06 PM »

I like it! You should try quaternion slerp.

I actually recently found un-expected value for this kind of code: glue code. Say we have a nice OpenGL API and it uses 4x4 matrices in a few places, like perspective transforms. Then we need to talk to a 3D math lib that works with 3x4 compact matrices. Having a quick C lib to write glue conversions functions is really nice to have, and can save a bunch of headache.

You should try adding a simple function to convert your matrices or vectors into a float array, that way people don't have to write down a typecast themselves, and can be sure the conversion to float array is correct. Oh just noticed you already did this, nice  Waaagh!

You should try looking into the outer product. It can be really good to project vectors onto planes or lines.

Try adding in really common but hard to solve functions, that are possibly difficult to find on the internet. Like converting a click point on screen into a point + direction on the clipping plane in world space, so people can do world space raycasts easily. This is something I recently added to my own tinygl lib (but have yet to release it).

Add in a function to decompose a transform into a scale, rotation and translate matrix. This should be written in a very simple way, which implies efficient; no determinants or full matrix inversions. Then write an inverse function that looks like:

Code:
cmatrix inverse(cvector translate, cmatrix rotate, cvector scale);

Logged
ferreiradaselva
Level 3
***



View Profile
« Reply #4 on: October 06, 2017, 10:27:51 AM »

I like it! You should try quaternion slerp.

I actually recently found un-expected value for this kind of code: glue code. Say we have a nice OpenGL API and it uses 4x4 matrices in a few places, like perspective transforms. Then we need to talk to a 3D math lib that works with 3x4 compact matrices. Having a quick C lib to write glue conversions functions is really nice to have, and can save a bunch of headache.

You should try adding a simple function to convert your matrices or vectors into a float array, that way people don't have to write down a typecast themselves, and can be sure the conversion to float array is correct. Oh just noticed you already did this, nice  Waaagh!

You should try looking into the outer product. It can be really good to project vectors onto planes or lines.

Try adding in really common but hard to solve functions, that are possibly difficult to find on the internet. Like converting a click point on screen into a point + direction on the clipping plane in world space, so people can do world space raycasts easily. This is something I recently added to my own tinygl lib (but have yet to release it).

Add in a function to decompose a transform into a scale, rotation and translate matrix. This should be written in a very simple way, which implies efficient; no determinants or full matrix inversions. Then write an inverse function that looks like:

Code:
cmatrix inverse(cvector translate, cmatrix rotate, cvector scale);



Really good ideas. I will check some books that I got for 2D/3D math. I think I can find the math behind those functions you mentioned and translate it. Grin
Logged

qMopey
Level 6
*


View Profile WWW
« Reply #5 on: October 06, 2017, 12:44:08 PM »

I released that mouse to world function, you can try using it for reference: https://github.com/RandyGaul/tinyheaders/blob/master/tinymath.h#L583-L602

I'm not really sure I like it inside of tinymath. I may move it to tinygl. It's sort of one of those in-between functions of the grey area. Is it more math thing? Or a graphics thing? Not sure. Feels out of place in the simd header though, so I will probably move it to tinygl later...

Function takes a camera inverse matrix, which should probably also exist in your math lib! People commonly enjoy the gluLookAt function, so you can implement a similar one, but make sure it also outputs the inverse. I implemented it like this: https://github.com/RandyGaul/tinyheaders/blob/master/tinymath.h#L510-L562

The way I implemented mouse to world, is it takes a float pointer as an array of 16 floats (matrix 4x4). Works with GL, have not tried using a DX matrix. This function also has v3 dependency on my math header, so that also kinda stinks. Would be nice if it didn't rely on *anything* except floats; that way anyone could copy paste it into their project and go, without any dependencies. Maybe you could try implementing a better version? Let me know if you do.

Code:
void compute_mouse_ray( float mouse_x, float mouse_y, float fov, float viewport_w, float viewport_h, float* cam_inv, float near_plane_dist, v3* mouse_pos, v3* mouse_dir )
{
float aspect = (float)viewport_w / (float)viewport_h;
float px = 2.0f * aspect * mouse_x / viewport_w - aspect;
float py = -2.0f * mouse_y / viewport_h + 1.0f;
float pz = -1.0f / tanf( fov / 2.0f );
v3 point_in_view_space( px, py, pz );

v3 cam_pos( cam_inv[ 12 ], cam_inv[ 13 ], cam_inv[ 14 ] );
float pf[ 4 ] = { getx( point_in_view_space ), gety( point_in_view_space ), getz( point_in_view_space ), 1.0f };
tgMulv( cam_inv, pf );
v3 point_on_clipping_plane( pf[ 0 ] , pf[ 1 ], pf[ 2 ] );
v3 dir_in_world_space = point_on_clipping_plane - cam_pos;

v3 dir = norm( dir_in_world_space );
v3 cam_forward( cam_inv[ 8 ], cam_inv[ 9 ], cam_inv[ 10 ] );

*mouse_dir = dir;
*mouse_pos = cam_pos + dir * dot( dir, cam_forward ) * vfloat( near_plane_dist );
}
« Last Edit: October 06, 2017, 12:49:22 PM by qMopey » Logged
ferreiradaselva
Level 3
***



View Profile
« Reply #6 on: October 07, 2017, 07:00:04 PM »

I released that mouse to world function, you can try using it for reference: https://github.com/RandyGaul/tinyheaders/blob/master/tinymath.h#L583-L602

I'm not really sure I like it inside of tinymath. I may move it to tinygl. It's sort of one of those in-between functions of the grey area. Is it more math thing? Or a graphics thing? Not sure. Feels out of place in the simd header though, so I will probably move it to tinygl later...

I think it would be more accessible if you leave it on tinymath, since that's what people will more often be looking at.

I don't know if I could make a simpler implementation than the one you already made. I would probably use the vector structures, too.

Function takes a camera inverse matrix, which should probably also exist in your math lib! People commonly enjoy the gluLookAt function, so you can implement a similar one, but make sure it also outputs the inverse. I implemented it like this: https://github.com/RandyGaul/tinyheaders/blob/master/tinymath.h#L510-L562

I have a LookAt function, but not sure if it's the same as the gluLookAt. I know there are some variants, so I will check later.

I implemented the slerp function *wipes sweat from the face*. I was using Godot results to check If I was getting the correct new quaternion, but the values were never the same. I spent all day on that, then I tried the Blender implementation (python) and I was actually getting correct results. Maybe the Godot implementation has some difference to make it work with the rest of the engine (because I saw some videos, and the Godot implementation works too ¯\_(ツ)_/¯).

First time I got a repo trending no github :D (at least on the C category)
« Last Edit: October 07, 2017, 08:57:41 PM by ferreiradaselva » Logged

ferreiradaselva
Level 3
***



View Profile
« Reply #7 on: October 09, 2017, 03:43:25 PM »

I'm probably going to remove `cvector2` and keep `cvector3`. I detailed on this issue: https://github.com/ferreiradaselva/mathc/issues/7
Logged

ferreiradaselva
Level 3
***



View Profile
« Reply #8 on: November 11, 2017, 04:29:07 AM »

I started using semantic versioning (currently version 0.2.0) and the library almost suffice any needs for 2D and 3D programming.

These are some examples that are in my other project (CGDFW):


Switching from projection and orthographic matrices for 3D | Using rotation matrices


Using orthographic matrices and rotation matrices for 2D


Using bezier functions
Logged

ferreiradaselva
Level 3
***



View Profile
« Reply #9 on: January 10, 2018, 07:42:56 AM »

I've been working on MATHC2, with lots of improvements from the feedback that I got from the first version.

There's a discussion happening here: https://github.com/ferreiradaselva/mathc/issues/17
Logged

qMopey
Level 6
*


View Profile WWW
« Reply #10 on: April 05, 2018, 12:51:41 PM »

Just saw you put up over 100 github commits. I was wondering where you went! Looks like you were working on things but just not pushing the work till now?  Shrug

Lots of stuff being done. Awesome!  Kiss
Logged
ferreiradaselva
Level 3
***



View Profile
« Reply #11 on: April 11, 2018, 06:06:16 AM »

Just saw you put up over 100 github commits. I was wondering where you went! Looks like you were working on things but just not pushing the work till now?  Shrug

Lots of stuff being done. Awesome!  Kiss

hey, randy! Yep, I wasn't pushing the commits, but I was working on it :D
Logged

Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic