Rays and geometries in C for graphics and physics. Various implementations of ray casting and intersections algorithms between different primitives in 2D and 3D.
- 2D and 3D Ray Casting
- 2D and 3D Triangle Implementations
- Basic Shapes and Intersection Algorithms
The complete API can be found in the single header of the library, photon.h.
#include <photon.h>
/* Check if a ray intersects a triangle in 3D */
int main(void)
{
/* ... */
Hit3D hit;
Ray3D ray = ray3D_new(origin, direction);
Tri3D tri = {p1, p2, p3};
if (tri2D_hit(&tri, &ray, &hit)) {
vec3 p = ray3D_at(&ray, hit.t);
printf("Hit At: %f, %f, %f\n", p.x, p.y, p.z);
printf("Normal: %f, %f, %f\n", hit.normal.x, hit.normal.y, hit.normal.z);
} else {
printf("Ray did not hit triangle\n");
}
/* ... */
}
The single project on which photon depend is pretty tiny and depends only on the standard C math library, so it can be built easily.
- fract: Small 2D and 3D math library for games and graphics
Be sure to clone fract into the repository, otherwise you won't be able to link successfully.
git clone --recursive https://github.com/LogicEu/photon.git
You can compile the library with either of the two build scripts in this repo using make or bash. The compiled library will be placed at /bin
make all -j # or ./build.sh all
There is a simple installation function that builds the library and puts it in the /usr/local directory:
sudo make install # or sudo ./build.sh install
Clean object files and binaries created during compilation in /tmp and /bin with:
make clean # or ./build.sh clean