The MATH3D Libary is a collection of useful geometric, trigometric, fractal and linear/vector/matrix algebra algorithms and definitions, useful for working w/ 2D and 3D graphics. These are all prototyped in: "graph/src/include/matrix.h"
This function initializes the lookup tables provided by this library and MUST be invoked before any of Pixcon's functions are invoked. To activate, just call "init_lut()". "fmask" is preset to initialize all the lookup tables provided by the Pixcon libraries, and is automatically set by your C++ compiler.
This function should be called when this the math3d library is no longer used.
typedef float vector2f[2]; typedef vector2f *pvector2f; typedef float vector3f[3]; typedef vector3f *pvector3f; typedef float vector4f[4]; typedef vector4f *pvector4f; typedef unsigned char vector3uc[3]; typedef vector3uc *pvector3uc; typedef unsigned char vector4uc[4]; typedef vector4uc *pvector4uc;
float COS(float x) - cos() float SIN(float x) - sin() float ACOS(float x) - acos() float ASIN(float x) - asin() float SQRT(float x) - sqrt() float ISQRT(float x) - 1.0/sqrt() float CONVERT_0_255_0_1(unsigned char x) - converts numbers ranging from 0..255 to 0..1 unsigned char CONVERT_0_1_0_255(float x) - converts numbers ranging from 0..1 to 0..255 unsigned char BYTE_LUT(int x) - clamps an integer to a char int FIXED_TO_INT(int xxx) - convert a 16:16 fixed pt number to an integer int INT_TO_FIXED(int xxx) - convert an integer to a 16:16 fixed pt nmber int FIXED_TO_FLOAT(int xxx) - convert a 16:16 fixed pt number to float int FLOAT_TO_FIXED(float xxx) - convert a float to a 16:16 fixed pt number int FIXED_REMAINDER(int xxx) - extract the remander from a a 16:16 fixed pt number
Note: "#" can represent any type of vector - (f)loat, (i)nt, (uc)nsigned char
These macros returns the dotproduct of 2 vectors:
This macros add each element of the vector y and x, and places the result in dst
addeqarray3(vector3# dst, vector3# x, vector3# y);
These macros subtract each element of the vector y from x
These macros subtract each element of the vector y from x, and places the result in dst
multarray3(vector3# x, vector3# y)
This macro divides each element of the 3D vector x by y
divarray3(vector3# x, vector3# y)
ssubarray3(vector3# x, scalar y)
BOOL nsame3(vector3# x, vector3# y)
copymx4x4(vector4f x[4], vector4f y[4])
copymx4x4o(vector4f x[4], vector4f y[4])
init_mx(vector4f x[4])
print_mx(int i, vector4f x[4])
float fmagnitude3(vector3f x)
float distance3(vector3f x, vector3f y)
float fnormalize3(vector3f x)
void swap4(vector4f mx1, vector4f mx2)
float xproduct2(vector2f x, vector2f y)
float transpose(vector4f dst[4], vector4f src[4])
float transpose(vector4f srcdst[4])
void matvecmult(vector4f y[4], vector4f x)
void matvecmulto(vector4f y[4], vector4f x)
void matvecmultv(vector4f y[4], vector3f x)
void vecmatmult(vector4f y[4], vector4f x)
void vecmatmulto(vector4f y[4], vector4f x)
void vecmatmultv(vector4f y[4], vector3f x)
void vecmatmultv(vector4f y[4], vector3f x, vector3f dst)
int inversemx(vector4f mx[4], vector4f inverse[4])
int inversemx2(vector4f mx[4], vector4f inverse[4])
void calculate_normal(vector3f v1, vector3f v2, vector3f v3, vector3f normal)
int query_calculate_normal(vector3f v1, vector3f v2, vector3f v3)
This function calculates the setup for doing incremental spherical linear interpolation (SLERP) between 2 vectors. It returns 0 if unsuccessful.
int vector_interp_setup(vector(3|4)f v1, vector(3|4)f v2, int count, float idelta, vector4f delta, float *theta)
input ----- v1 - starting vector v2 - ending vector count - = 3 if v1,v2 are 3D vectors, 4 if v1,v2 are 4D vectors idelta - size of the increment output ------ delta - slerp increment data theta - cosine value of the angle between v1 and v2 (radians)
void vector_interp_eval(vector(3|4)f v1, vector(3|4)f v2, int count, vector4f delta, vector(3|4) out)
input ----- v1 - starting vector v2 - ending vector count - = 3 if v1,v2,out are 3D vectors, 4 if v1,v2,out are 4D vectors delta - slerp increment data calculated by vector_interp_setup() output out - an interpolated vector between v1, and v2. This vector starts at v1, and interpolates toward v2 each time it's called.
void calc_euler(vector4f mx[4], vector3f ryp)
void mat2quat(vector4f *mx, vector4f quat);
void calc_quat(vector4f mx[4], vector4f quat)
void euler2quat(vector3f pyr, vector4f quat)
void quatquatmult(vector4f y, vector4f x)
void decompose_xyz(vector4f mx[4], float *x, float *y, float *z)
void decompose_yxz(vector4f mx[4], float *x, float *y, float *z)
void decompose_zyx(vector4f mx[4], float *x, float *y, float *z)
void decompose_xrypzy(vector4f mx[4], float *roll, float *pitch, float *yaw)
void decompose_yrxpzy(vector4f mx[4], float *roll, float *pitch, float *yaw);
void decompose_ypxrzy(vector4f mx[4], float *roll, float *pitch, float *yaw);
void decompose_yyxpzr(vector4f mx[4], float *roll, float *pitch, float *yaw);
void decompose_zyxryp(vector4f mx[4], float *roll, float *pitch, float *yaw)
void arotate_mx_x(vector4f mx[4], float x)
void arotate_mx_y(vector4f mx[4], float y)
void arotate_mx_z(vector4f mx[4], float z)
void rotate_vector(vector4f point, vector3f axis, float angle, vector3f result)
void calc_axis_rotate_mx(vector3f center, vector3f axis, float radian, vector4f mx[4])
void scale_rotation(vector4f *mx, float scale)
These functions calculate interpolated points between key points using Catmull-Rom splines or B-splines
s = value between 0..1 representing the interpolation value key = 4 key points that define the spline out = the interpolated point Q = defines a spline matrix list = key point list i = index into key point list that specifies the key points to use (ie element list[i], list[i+1], list[i+2], list[i+3]) stype = defines the interpolation technique M_CR - implies Catmull-Rom spline M_B - implies B-spline
s = value between 0..1 representing the interpolation value along the row axis t = value between 0..1 representing the interpolation value along the col axis key1 = a list of vertex points key2 = a 4 x 4 lattice of points that covers the surface patch Q = a 4 x 4 x 4 surface patch matrix ptr = indicies into the vertex list representing the first index for each row out = the interpolated point stype = defines the interpolation technique M_CR - implies Catmull-Rom spline M_B - implies B-spline
void spnormal_base(vector4f key[4][4], vector4f out, int stype)
key = a 4 x 4 lattice of points that covers the surface patch out = the interpolated point stype = defines the interpolation technique M_CR - implies Catmull-Rom spline M_B - implies B-spline
s = value between 0..1 representing the interpolation value along the row axis t = value between 0..1 representing the interpolation value along the col axis key1 = a list of vertex points key2 = a 4 x 4 lattice of points that covers the surface patch Q = a 4 x 4 x 4 surface patch matrix ptr = indicies into the vertex list representing the first index for each row out = the interpolated point stype = defines the interpolation technique M_CR - implies Catmull-Rom spline M_B - implies B-spline
float cone_area(float radius, float length)
float cylinder_area(float radius, float length)
float sphere_area(float radius)
void make_box(vector4f *in, vector4f *out);
BOOL query_point_point_intersect(vector4 pt1, vector4 pt2)
BOOL query_point_sphere_intersect(vector4f pt, vector4f center, float radius)
pt - point center - center of sphere radius - radius of sphere
BOOL query_point_cpoly_intersect(vector4f pt, vector4f normal, int count, vector4f *vertex)
pt - point normal - (A, B, C, D) as in Ax + By + Cz + D = 0 count - # of vertices in the polygon vertex - list of vertices
BOOL query_point_in_cpoly(vector4f pt, vector4f normal, int count, vector4f *vertex, int *index)
pt - point normal - (A, B, C, D) as in Ax + By + Cz + D = 0 count - # of vertices in the polygon vertex - list of vertices index - index list into the vertex list
BOOL query_point_poly_intersect(vector4f pt, vector4f normal, int count, vector4f *vertex)
pt - point normal - (A, B, C, D) as in Ax + By + Cz + D = 0 count - # of vertices in the polygon vertex - list of vertices
BOOL query_point_plane_intersect(vector4f pt, vector4f plane)
pt - point plane - (A, B, C, D) as in Ax + By + Cz + D = 0
BOOL query_point_rect_intersect(vector4f pt, vector4f rect[6])
pt - point rect - 6 plane normals, (A, B, C, D) as in Ax + By + Cz + D = 0, which represent the 6 sides of the rectangle
BOOL query_point_line_intersect(float *pt1, float *pt2, float *r2);
pt1 - point to check pt2 - point on the line r2 - direction vector of the line
float dist_pt2plane(vector4f pt,vector4f plane)
pt - point plane - (A, B, C, D) as in Ax + By + Cz + D = 0
BOOL query_sphere_sphere_intersect(vector4f center1, float radius1, vector4f center2, float radius2)
center1, radius1 - location and radius of the first sphere center2, radius2 - location and radius of the second sphere
BOOL query_line_sphere_intersect(float radius, vector4f center, vector4f pt, vector3f vector)
center, radius - location and radius of the sphere pt - a point on the line vector - the slope of the line
BOOL query_line_sphere_intersectn(float radius, vector4f center, vector4f pt, vector3f vector)
center, radius - location and radius of the sphere pt - a point on the line vector - normalized slope of the line
BOOL query_lineseg_sphere_intersect(float radius, vector4f center, vector4f start, vector3f ray)
center, radius - location and radius of the sphere start - starting point of the line segment ray - direction vector that represents the direction and magnitude of the line segment
BOOL query_lineseg_sphere_intersect2(float radius, vector4f center, vector4f start, vector4f end)
center, radius - location and radius of the sphere start, end - end points of the line segment
BOOL query_sphere_plane_intersect(vector4f center, float radius, vector4f plane)
center, radius - location and radius of the sphere plane - (A, B, C, D) as in Ax + By + Cz + D = 0
BOOL query_sphere_rect_intersect(vector4f center, float radius, vector4f rect_center, vector4f rect[6])
center, radius - location and radius of the sphere rect_center - center of rectangle rect - 6 plane normals, (A, B, C, D) as in Ax + By + Cz + D = 0, which represent the 6 sides of the rectangle
BOOL line_sphere_intersect(float radius, vector4f center, vector4f pt, vector3f vector, float *t)
center, radius - location and radius of the sphere pt - a point on the line vector - the slope of the line t - the "distance" to intersection (ie intersection = pt + t * vector)
BOOL line_sphere_intersectn(float radius, vector4f center, vector4f pt, vector3f vector, float *t)
center, radius - location and radius of the sphere pt - a point on the line vector - the slope of the line (normalized/unit length) t - the "distance" to intersection (ie intersection = pt + t * vector)
BOOL line_ellipse_intersect(vector3f scale, vector4f pt, vector3f vector, float *t)
scale - "radius" of the ellipses 3 major/minor axes pt - a point on the line vector - the slope of the line t - the "distance" to intersection (ie intersection = pt + t * vector)
BOOL query_poly_plane_intersect(vector4f *vertex, int count, vector4f plane);
vertex - list of vertices in the polygon count - # of vertices in the vertex list plane - (A, B, C, D) as in Ax + By + Cz + D = 0
BOOL query_plane_plane_intersect(vector4f plane1, vector4f plane2)
plane1, plane2 - (A, B, C, D) as in Ax + By + Cz + D = 0
BOOL query_plane_rect_intersect(vector4f plane, vector4f rect[6])
plane - (A, B, C, D) as in Ax + By + Cz + D = 0 rect - 6 plane normals, (A, B, C, D) as in Ax + By + Cz + D = 0, which represent the 6 sides of the rectangle
BOOL lineseg_plane_intersect(vector4f plane, vector4f start, vector4f end, vector3f vector, vector4f intersect, float *t)
plane - (A, B, C, D) as in Ax + By + Cz + D = 0 start, end - end points of the line segment vector - the slope of the line intersect - point of intersection (optional - may be NULL) t - the "distance" to intersection (ie intersection = pt + t * vector)
BOOL line_plane_intersect(vector4f plane, vector4f pt, vector3f vector, vector4f intersect, float *t)
plane - (A, B, C, D) as in Ax + By + Cz + D = 0 pt - a point on the line vector - the slope of the line intersect - point of intersection (optional - may be NULL) t - the "distance" to intersection (ie intersection = pt + t * vector)
BOOL line_poly_intersect(vector4f pt, vector3f vector, vector4f plane, vector4f *vertex, int count, vector4f intersect, float *t)
pt - a point on the line vector - the slope of the line plane - (A, B, C, D) as in Ax + By + Cz + D = 0 vertex - list of vertices count - # of vertices in the polygon intersect - point of intersection t - the "distance" to intersection (ie intersection = pt + t * vector)
BOOL line_cpoly_intersect(vector4f pt, vector3f vector, vector4f plane, vector4f *vertex, int count, vector4f intersect, float *t)
pt - a point on the line vector - the slope of the line plane - (A, B, C, D) as in Ax + By + Cz + D = 0 vertex - list of vertices count - # of vertices in the polygon intersect - point of intersection t - the "distance" to intersection (ie intersection = pt + t * vector)
BOOL query_rect_rect_intersect( vector4f normal1[6], vector4f p1[8], vector4f center1, float radius1, vector4f normal2[6], vector4f p2[8], vector4f center2, float radius2)
center1/2, radius1/2 - center and radius of bounding spheres normal1/2 - plane normals, (A,B,C,D) as in Ax + By + Cz + D = 0 p1, p2 - rectangle vertices, whose order is: 5----6 /| /| / | / | / 4-/--7 1--/-2 / | / | / |/ |/ 0----3
BOOL line_rect_intersect(vector4f pt, vector3f vector, vector4f *p, vector4f *normal, float *t)
pt - a point on the line vector - the slope of the line normal - plane normals, (A,B,C,D) as in Ax + By + Cz + D = 0 t - the "distance" to intersection (ie intersection = pt + t * vector) p - rectangle vertices, whose order is: 5----6 /| /| / | / | / 4-/--7 1--/-2 / | / | / |/ |/ 0----3
BOOL line2d_line2d_intersect(vector3f v1, vector3f v2, vector2f pt)
v1, v2 - line equation constants (A, B, C) as in Ax +By + C = 0 pt - the point of intersection, if any
BOOL line3d_line3d_intersect(float *pt1, float *r1, float *pt2, float *r2, float *intersect);
pt1, r1 - the point and vector for the first line pt2, r2 - the point and vector for the second line It is assumed that r1 and r2 are normalized
BOOL line_cylinder_intersect(vector4f pt, vector4f vector, float length, float radius, float *t, int *side)
pt - a point on the line vector - the slope of the line length - half the length of the cylinder (odd optimization) radius - radius of the cylinder t - the "distance" to intersection (ie intersection = pt + t * vector) side - returns which side the line intersects: CYL_SIDE - intersects the "side" CYL_TOP - intersects the "top" CYL_BOTTOM - intersects the "bottom"
BOOL line_cone_intersect(vector4f pt, vector4f vector, float length, vector2f radius, float *t, int *side)
pt - a point on the line vector - the slope of the line length - half the length of the cone (odd optimization) radius - 2 radii, a top and bottom radius. if the top radius == 0, then its a "true" cone. t - the "distance" to intersection (ie intersection = pt + t * vector) side - returns which side the line intersects: CONE_SIDE - intersects the "side" CONE_TOP - intersects the "top" CONE_BOTTOM - intersects the "bottom"
void reflect(vector3f normal, vector3f vector)
BOOL point_point_reflectn3vf(vector4f c1, vector4f c2, vector3f normal);
c1 - point to reflect off of c2 c2 - point "hit" by c1 normal - returned normal by which c1 is reflected off of c2
BOOL plane_reflectn3vf(vector4f *plane, vector3f normal);
plane - (A, B, C, D) as in Ax + By + Cz + D = 0 normal - returned reflection normal off of the plane
BOOL point_rect_reflectn3vf(vector4f center, vector4f rect_center, vector4f rect[6], vector3f normal);
center - center of point to reflect off of rectangle rect_center - center of rectangle rect - 6 plane normals, (A, B, C, D) as in Ax + By + Cz + D = 0, which represent the 6 sides of the rectangle normal - returned reflection normal off of the plane
BOOL plane_rect_reflectn3vf(vector4f plane, vector4f rect_center, vector4f rect[6], vector3f normal);
plane - (A, B, C, D) as in Ax + By + Cz + D = 0 rect_center - center of rectangle rect - 6 plane normals, (A, B, C, D) as in Ax + By + Cz + D = 0, which represent the 6 sides of the rectangle normal - returned reflection normal off of the plane
float turbulence(vector3f v, float freq, float ifreq)
This function initializes the Perlin noise tables, used for the fractal algorithms used in the material, terrain, and atmosphere shaders.
Each material, terrain, and atmosphere shader uses this class to pass fractal parameters. The user of the fractal algorithm sets "octaves", "H", and "lacunarity", then calls "init()" to setup "freq" with precalculated data.
class fractal_params {
public:
float octaves;
This field defines the fractal dimension of the algorithm.
float H;
This field defines the fractal amplitude of the algorithm.
float lacunarity;
This field defines the wavelength of the algorithm.
float *freq;
This field points to a precalculated table of varying frequencies.
fractal_params();
fractal_params(float o, float h, float l);
virtual ~fractal_params();
void init();
This method is called after "octaves", "H", and "lacunarity" are set - it sets up "freq".
};