MATH3D Library

The MATH3D Library
------------------

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"


T E R M S
---------
  • 3D point = (x, y, z)
  • 4D point = (x, y, z, w) where w is normaly 1.0
  • Surface Patch - a surface created by a 4 x 4 lattice of vertex points


    INITIALIZATION
    --------------
    void init_lut(int fmask = MASK_ALL)

    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.


    void dest_lut()

    This function should be called when this the math3d library is no longer used.


    DATATYPES
    ---------

    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;
    


    LOOKUP TABLES/MACROS
    --------------------

    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
    


    VECTOR ARITHMETIC
    -----------------

    Note: "#" can represent any type of vector - (f)loat, (i)nt, (uc)nsigned char

    These macros returns the dotproduct of 2 vectors:

  • dotproduct2(vector2# x, vector2# y)
  • dotproduct3(vector3# x, vector3# y)
  • dotproduct4(vector4# x, vector4# y)
    These macros copy the vector y into x:

  • copyarray2(vector2# x, vector2# y)
  • copyarray3(vector3# x, vector3# y)
  • copyarray4(vector4# x, vector4# y)
    These macros add each element of the vector y to x

  • addarray2(vector2# x, vector2# y)
  • addarray3(vector3# x, vector3# y)
  • addarray4(vector3# x, vector3# y)

    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

  • subarray2(vector2# x, vector2# y)
  • subarray3(vector3# x, vector3# y)
  • subarray4(vector4# x, vector4# y)

    These macros subtract each element of the vector y from x, and places the result in dst

  • subeqarray2(vector2# dst, vector2# x, vector2# y)
  • subeqarray3(vector3# dst, vector3# x, vector3# y)
    This macro multiplies each element of the 3D vector y to x

    multarray3(vector3# x, vector3# y)


    This macro divides each element of the 3D vector x by y

    divarray3(vector3# x, vector3# y)


    These macros add the scalar value y to each element of the vector x

  • saddarray2(vector2# x, scalar y)
  • saddarray3(vector3# x, scalar y)
    This macro subtracts the scalar value y from each element of the vector x

    ssubarray3(vector3# x, scalar y)


    These macros multiply the scalar value y to each element of the vector x

  • smultarray3(vector3# x, scalar y)
  • smultarray4(vector4# x, scalar y)
    These macros divide each element of the vector x by the scalar value y

  • sdivarray3(vector3# x, scalar y)
  • sdivarray4(vector4# x, scalar y)
    This macro returns a boolean value equivalent to "x == y"

    BOOL nsame3(vector3# x, vector3# y)


    This macro copies y (4x4 matrix) into x (4x4 matrix)

    copymx4x4(vector4f x[4], vector4f y[4])


    This macro sets x to a 4x4 identity matrix

    copymx4x4o(vector4f x[4], vector4f y[4])


    This macro is an optimized version of copymx4x4

    init_mx(vector4f x[4])


    These macros print a float vector to stdout

  • print_v3(vector3f x)
  • print_v4(vector4f x)
    This macro prints a 4x4 float matrix to stdout

    print_mx(int i, vector4f x[4])


    These macros return the magnitude of the float vector

  • float magnitude2(vector2f x)
  • float magnitude3(vector3f x)
  • float magnitude4(vector4f x)
    This macro returns the magnitude of a float vector using SQRT macro

    float fmagnitude3(vector3f x)


    This function returns the distance between 2 3D points

    float distance3(vector3f x, vector3f y)


    These functions return true if the distance between x and y are w/in 0.0001

  • int similar2(vector2f x, vector2f y)
  • int similar3(vector3f x, vector3f y)
    These functions make the vector x a "unit" vector

  • float normalize2(vector2f x)
  • float normalize3(vector3f x)
  • double normalize3d(vector3d x)
  • float normalize4(vector4f x)
    These functions make the vector x a "unit" vector using the SQRT macro

    float fnormalize3(vector3f x)


    This function swaps the contents of 2 vectors.

    void swap4(vector4f mx1, vector4f mx2)


    These functions calculate the 3D cross product (x X y) and places the result in dst

  • void xproduct(vector3f dst, vector3f x, vector3f y)
  • void xproductd(vector3d dst, vector3d x, vector3d y)
    This macro returns the 2D cross product x X y. Unlike the 3D version whose result is a 3D vector, the result is a float

    float xproduct2(vector2f x, vector2f y)


    This function calculates the transpose of src and places the result in dst

    float transpose(vector4f dst[4], vector4f src[4])


    This function calculates the transpose of srcdst and places the result in srcdst

    float transpose(vector4f srcdst[4])


    This function calculates the 4D matrix/matrix multplication y X x and places the result in x.

  • void matmatmult(vector4f y[4], vector4f x[4])
  • void matmatmultd(vector4d y[4], vector4d x[4])
    This function calculates the 4D matrix/matrix multplication y X x, assuming the 4 row of x and y is {0,0,0,1} and places the result in x.

  • void matmatmulto(vector4f y[4], vector4f x[4])
  • void matmatmultod(vector4d y[4], vector4d x[4])
    This function concatinates the orientation of y w/ x (does not affect translation)

  • void matmatmulto(vector4f y[4], vector4f x[4], vector4f dst[4])
  • void matmatmultod(vector4d y[4], vector4d x[4], vector4d dst[4])
    This function concatinates the orientation of y w/ x (does not affect translation) and places the result in dst.

  • void matmatmultv(vector4f y[4], vector4f x[4])
  • void matmatmultvd(vector4d y[4], vector4d x[4])
    This function calculates the 4D matrix/vector multiplication x X y and places the result in x.

    void matvecmult(vector4f y[4], vector4f x)


    This function calculates the 4D matrix/vector multplication y X x assuming the 4 row of y is {0,0,0,1} and the x[3] == 1, and places the result in x.

    void matvecmulto(vector4f y[4], vector4f x)


    This function uses the 3x3 portion of y to multiply with x, and places the result in x (ie vector rotation)

    void matvecmultv(vector4f y[4], vector3f x)


    This function calculates the 4D vector/matrix multiplaction x X y and places the result in x.

  • void matvecmultv(vector4f y[4], vector3f x, vector3f dst)
  • void matvecmultvd(vector4d y[4], vector3d x, vector3d dst)
    This function calculates the 4D vector/matrix multiplaction x X y and places the result in dst.

    void vecmatmult(vector4f y[4], vector4f x)


    This function calculates the 4D vector/matrix multplication x X y assuming the 4 row of y is {0,0,0,1} and the x[3] == 1, and places the result in x.

    void vecmatmulto(vector4f y[4], vector4f x)


    This function multiplies x w/ the 3x3 portion of y and places the result in x (ie transpose vector rotation)

    void vecmatmultv(vector4f y[4], vector3f x)


    This function multiplies x w/ the 3x3 portion of y and places the result in dst (ie transpose vector rotation)

    void vecmatmultv(vector4f y[4], vector3f x, vector3f dst)


    This function calculates the 4D inverse matrix of mx and places the result in inverse. It returns 0 if the inverse cannot be determined

    int inversemx(vector4f mx[4], vector4f inverse[4])


    Another inversmx function that isn't as optimized as the previous version, but is based upon the standard technique of calculating an inverse matrix.

    int inversemx2(vector4f mx[4], vector4f inverse[4])


    This inverse matrix function is faster than the previous inverse functions and assumes that mx is composed of ONLY rotation and translations.

  • void inversemxrt(vector4f mx[4], vector4f inverse[4])
  • void inversemxrtd(vector4d mx[4], vector4d inverse[4])
    This function takes 3 clockwise 3D points and calculates the normal to the plane they define.

    void calculate_normal(vector3f v1, vector3f v2, vector3f v3, vector3f normal)


    This function takes 3 clockwise 3D points and determins if a normal can be calculated.

    int query_calculate_normal(vector3f v1, vector3f v2, vector3f v3)


    S L E R P
    ---------

    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)
    

    This function calculates the next incremental step in slerping between v1 and v2. It returns 0 if unsuccessful.

    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.
    


    ORIENTATION
    -----------
    Rradian/degree conversion macros

  • float rad2deg(float radian)
  • float deg2rad(float degree)
    This function converts a Euler radian vector (pitch, yaw, roll) into a 4x4 rotation matrix.

    void calc_euler(vector4f mx[4], vector3f ryp)


    This macro converts a 4x4 rotation matrix into a quaternion.

    void mat2quat(vector4f *mx, vector4f quat);


    This macro converts a quaternion into a 4x4 matrix.

    void calc_quat(vector4f mx[4], vector4f quat)


    This function converts a Euler radian vector (pitch, yaw, roll) into a quaternion.

    void euler2quat(vector3f pyr, vector4f quat)


    This function multiplies 2 quaternions, x and y, then puts the result in x.

    void quatquatmult(vector4f y, vector4f x)


    This function decomposes a rotation matrix, assuming [mx] = [x][y][z]

    void decompose_xyz(vector4f mx[4], float *x, float *y, float *z)


    This function decomposes a rotation matrix, assuming [mx] = [y][x][z]

    void decompose_yxz(vector4f mx[4], float *x, float *y, float *z)


    This function decomposes a rotation matrix, assuming [mx] = [z][x][y]

  • void decompose_zxy(vector4f mx[4], float *x, float *y, float *z)
  • void decompose_zxyd(vector4d mx[4], double *x, double *y, double *z)
    This function decomposes a rotation matrix, assuming [mx] = [z][y][x]

    void decompose_zyx(vector4f mx[4], float *x, float *y, float *z)


    This function decomposes a rotation matrix into roll, pitch, and yaw, assuming [mx] = [x-roll][y-pitch][z-yaw], and limits pitch to +/- 90 degrees

    void decompose_xrypzy(vector4f mx[4], float *roll, float *pitch, float *yaw)


    This function decomposes a rotation matrix into roll, pitch, and yaw, assuming [mx] = [y-roll][x-pitch][z-yaw], and limits pitch to +/- 90 degrees

    void decompose_yrxpzy(vector4f mx[4], float *roll, float *pitch, float *yaw);


    This function decomposes a rotation matrix into roll, pitch, and yaw, assuming [mx] = [y-pitch][x-roll][z-yaw], and limits pitch to +/- 90 degrees

    void decompose_ypxrzy(vector4f mx[4], float *roll, float *pitch, float *yaw);


    This function decomposes a rotation matrix into roll, pitch, and yaw, assuming [mx] = [y-yaw][x-pitch][z-roll], and limits pitch to +/- 90 degrees

    void decompose_yyxpzr(vector4f mx[4], float *roll, float *pitch, float *yaw);


    This function decomposes a rotation matrix into roll, pitch, and yaw, assuming [mx] = [z-yaw][x-roll][y-pitch], and limits pitch to +/- 90 degrees

    void decompose_zyxryp(vector4f mx[4], float *roll, float *pitch, float *yaw)


    This function decomposes a rotation matrix into roll, pitch, and yaw, assuming [mx] = [z-yaw][x-pitch][y-roll], and limits pitch to +/- 90 degrees

  • void decompose_zyxpyr(vector4f mx[4], float *roll, float *pitch, float *yaw)
  • void decompose_zyxpyrd(vector4d mx[4], double *roll, double *pitch, double *yaw)
    This function decomposes a rotation matrix into roll, pitch, and yaw, assuming [mx] = [z-yaw][y-pitch][x-roll], and limits pitch to +/- 90 degrees

  • void decompose_zyypxr(vector4f mx[4], float *roll, float *pitch, float *yaw)
  • void decompose_zyypxrd(vector4d mx[4], double *roll, double *pitch, double *yaw)
    This function applies a radian rotation around the x axis to a rotation matrix. (ie [mx] = [x][mx])

  • void rotate_mx_x(vector4f mx[4], float x)
  • void rotate_mx_xd(vector4d mx[4], double x)
    This function post applies a radian rotation around the x axis to a rotation matrix. (ie [mx] = [mx][x])

    void arotate_mx_x(vector4f mx[4], float x)


    This function applies a radian rotation around the y axis to a rotation matrix. (ie [mx] = [y][mx])

  • void rotate_mx_y(vector4f mx[4], float y)
  • void rotate_mx_yd(vector4d mx[4], double y)
    This function post applies a radian rotation around the y axis to a rotation matrix. (ie [mx] = [mx][y])

    void arotate_mx_y(vector4f mx[4], float y)


    This function applies a radian rotation around the z axis to a rotation matrix. (ie [mx] = [z][mx])

  • void rotate_mx_z(vector4f mx[4], float z)
  • void rotate_mx_zd(vector4d mx[4], double z)
    This function post applies a radian rotation around the z axis to a rotation matrix. (ie [mx] = [mx][z])

    void arotate_mx_z(vector4f mx[4], float z)


    This function rotates a 4D "point" around an arbitrary "axis" by a radian "angle", and stores the "result".

    void rotate_vector(vector4f point, vector3f axis, float angle, vector3f result)


    Given a rotation "center" point, a unit "axis" to rotate around, a "radian" angle to rotate, calculate the correponding rotation "mx"

    void calc_axis_rotate_mx(vector3f center, vector3f axis, float radian, vector4f mx[4])


    This function scales the rotation angle of the rotation matrix. An experimental way of scaling a rotation w/o scaling Euler angles.

    void scale_rotation(vector4f *mx, float scale)


    SPLINES
    -------

    These functions calculate interpolated points between key points using Catmull-Rom splines or B-splines

  • void spline(float s, vector4f *list, vector4f out, int i, int stype)
  • void spline(float s, vector4f Q[4], vector4f out, int stype)
  • void splinet(float s, vector4f key[4], vector4f out, int stype)

            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
    

    These functions calculate normals for a surface patch

  • void spnormal(float s, float t, vector4f *key1, vector4f out, int ptr[4], int stype)
  • void spnormal(float s, float t, vector4f key2[4][4], vector4f out, int stype)
  • void spnormal(float s, float t, float Q[4][4][4], vector4f out, int stype)

            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
    

    This function calculate normal for a surface patch at key[0][0][0]

    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
    

    These functions calculate interpolated points on a surface patch using Catmull-Rom splines or B-splines.

  • void sppatch(float s, float t, vector4f *key1, vector4f out, int ptr[4], int stype)
  • void sppatch(float s, float t, vector4f **key2, vector4f out, int stype)
  • void sppatch(float s, float t, float Q[4][4][4], vector4f out, int stype)

            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
    

    GEOMETRIC
    ---------
    This macro returns the area of a cone

    float cone_area(float radius, float length)


    This function returns the area of a cylinder

    float cylinder_area(float radius, float length)


    This macro returns the area of a sphere

    float sphere_area(float radius)


    Given the min/max value vectors for a box, this function generates the 8 vertices of the corresponding box

    void make_box(vector4f *in, vector4f *out);


    COLLISION
    ---------
    This macro returns true if pt1 and pt2 are "touching".

    BOOL query_point_point_intersect(vector4 pt1, vector4 pt2)


    This function checks if a point and a sphere intersect.

    BOOL query_point_sphere_intersect(vector4f pt, vector4f center, float radius)

            pt      - point
            center  - center of sphere
            radius  - radius of sphere
    

    This function returns true if a point intersects a convex polygon.

    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
    

    This function returns true if a point intersects a convex polygon. Assumes the point must be on the same plane as the polygon.

    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
    

    This function returns true if a point intersects a polygon (convex not a requirement)

    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
    

    This function returns true if the point is on a plane

    BOOL query_point_plane_intersect(vector4f pt, vector4f plane)

            pt      - point
            plane   - (A, B, C, D) as in Ax + By + Cz + D = 0
    

    This function returns true if the point is w/in a 6 sided rectangle.

    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
    

    This function returns true if pt1 lies on the line defined by pt2 and r2.

    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
    

    This macro calculates the distance from a point to a plane. If negative, then the point is "under" the plane

    float dist_pt2plane(vector4f pt,vector4f plane)

            pt      - point
            plane   - (A, B, C, D) as in Ax + By + Cz + D = 0
    

    This function returns true if the two spheres intersect.

    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
    

    This function returns true if the line intersects the 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
    

    This function returns true if the line intersects the sphere.

    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
    

    This function returns true if the sphere is intersected by the line segment.

    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
    

    This function returns true if the sphere is intersected by 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
    

    This macro returns true if the sphere intersects the plane

    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
    

    This function returns true if the sphere intersects the rectangle.

    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
    

    This function returns true if the line intersects the sphere, as well as the "distance".

    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)
    

    Same as above, but assumes that "vector" is normalized/unit length.

    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)
    

    This function returns true if the line intersects the ellipse. The ellipse is assumed to be in its "local" space (ie the ellipse is at (0,0,0) and the ellipses major/minor axes lie on the coordinate axes)

    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)
    

    This function returns true if the polygon intersects a plane.

    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
    

    This function returns true if two planes intersect each other.

    BOOL query_plane_plane_intersect(vector4f plane1, vector4f plane2)

            plane1, plane2  - (A, B, C, D) as in Ax + By + Cz + D = 0
    

    This function returns true if the plane intersects the rectangle;

    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
    

    This function returns true if the line segment intersects the plane, as well as the point of intersection.

    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)
    

    This function returns true if the line intersects the plane, as well as the "distance" to the plane.

    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)
    

    This function returns true if the line intersects the polygon, as well as the point of intersection.

    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)
    

    This function returns true if the line intersects the polygon, as well as the point of intersection. The polygon is assumed to be convex.

    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)
    

    This function returns true if the two rectangles intersect.

    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
    

    This function returns true if the line intersects the rectangle, as well as the "distance".

    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
    

    This function returns true if the two lines intersect, as well as the intersection point.

    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
    

    This function returns true (and the intersection) if 2 lines in 3-space intersect.

    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
    

    This function returns true if the line intersects the cylinder. The cylinder is assumed to be in its "local" space (ie its centered at (0,0,0), and its "length" is along the z axis)

    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"
    

    This function returns true if the line intersects the cone. The cone is assumed to be in its "local" space (ie its centered at (0,0,0), and its "length" is along the z axis)

    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"
    

    This function reflects "vector" around "normal" and returns the result in "vector"

    void reflect(vector3f normal, vector3f vector)


    This function returns true if c1 can be reflected off of c2, as well as the normal of reflection.

    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
    

    This function returns true if a reflection normal can be calculated for reflection off of a plane, as well as the reflection vector of a plane.

    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
    

    This function returns true if the reflection normal can be caculated for a point to reflect of a rectangle, as well as the reflection vector.

    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
    

    This function returns true if the reflection normal can be caculated for a plane to reflect of a rectangle, as well as the reflection vector.

    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
    

    PROCEDURAL/FRACTAL
    ------------------
    Refer to "Texturing and Modeling - A Procedural Approach", by Ebert, Musgrave, Peachey, Perlin, and Worley for further details on the following:
    These functions remap the interval 0..1 to 0..1, but in a non-linear way

  • float bias(float a, float b)
  • float gain(float a, float b)
    These functions calculate repeatable random noise values

  • float noise1(float arg)
  • float noise2(vector2f vec)
  • float noise3(vector3f vec)
    This is a 1/f noise function

    float turbulence(vector3f v, float freq, float ifreq)


    void init_noise();

    This function initializes the Perlin noise tables, used for the fractal algorithms used in the material, terrain, and atmosphere shaders.


    CLASS FRACTAL_PARAMS
  • Reference: matrix.h
  • Library: libmath3d

    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 {

    };


    These functions are implementations of Ken Musgrave's fractal and multifractal terrain algorithms. They are in pairs - the bound function returns the min and max values of the corresponding terrain function.

  • int bound_fBm(fractal_params *params, float offset, float *rmin, float *rmax)
  • float fBm(float *point, fractal_params *params)

  • int bound_fBm2(fractal_params *params, float offset, float *rmin, float *rmax)
  • float fBm2(vector3f point, fractal_params *params)

  • int bound_HeteroTerrain(fractal_params *params, float offset, float *rmin, float *rmax)
  • float Hetero_Terrain(vector3f point, fractal_params *params, float offset)

  • int bound_HybridMultifractal(fractal_params *params, float offset, float *rmin, float *rmax)
  • float HybridMultifractal(vector3f point, fractal_params *params, float offset, float gain)

  • int bound_RidgedMultifractal(fractal_params *params, float offset, float *rmin, float *rmax)
  • float RidgedMultifractal(vector3f point, fractal_params *params, float offset, float gain)

  • int bound_RidgedMultifractal2(fractal_params *params, float offset, float *rmin, float *rmax)
  • float RidgedMultifractal2(vector3f point, fractal_params *params, float offset, float gain)