123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413 |
- #ifndef __c3dlas_h__
- #define __c3dlas_h__
- #include <stdlib.h> // rand() et al.
- #include <math.h> // fmin/fmax
- #define F_PI ((float)3.1415926535897932384626433832795028841971693993751)
- #define D_PI ((double)3.1415926535897932384626433832795028841971693993751)
- #define F_2PI ((float)6.2831853071795864769252867665590057683943387987502)
- #define D_2PI ((double)6.2831853071795864769252867665590057683943387987502)
- #define F_1_PI ((float)0.3183098861837906715377675267450287240689192914809)
- #define D_1_PI ((double)0.3183098861837906715377675267450287240689192914809)
- #define F_PI_2 ((float)1.5707963267948966192313216916397514420985846996875)
- #define D_PI_2 ((double)1.5707963267948966192313216916397514420985846996875)
- #define F_3PI_2 ((float)4.7123889803846898576939650749192543262957540990626)
- #define D_3PI_2 ((double)4.7123889803846898576939650749192543262957540990626)
- #define F_GOLDEN ((float)1.61803398874989484820458683436563811772030917980576f)
- #define D_GOLDEN ((double)1.61803398874989484820458683436563811772030917980576)
- #define RAD2DEG (57.29577951308232087679815481410517033240547246656432154916024386)
- #define DEG2RAD (0.0174532925199432957692369076848861271344287188854172545609719144)
- #define FLT_CMP_EPSILON 0.000001
- #define C3DLAS_COPLANAR (0)
- #define C3DLAS_FRONT (1)
- #define C3DLAS_BACK (2)
- #define MAX(a,b) ({ \
- __typeof__ (a) _a = (a); \
- __typeof__ (b) _b = (b); \
- _a > _b ? _a : _b; \
- })
- #define MIN(a,b) ({ \
- __typeof__ (a) _a = (a); \
- __typeof__ (b) _b = (b); \
- _a < _b ? _a : _b; \
- })
- #define MAXE(a,b) ({ \
- __typeof__ (a) _a = (a); \
- __typeof__ (b) _b = (b); \
- _a >= _b ? _a : _b; \
- })
- #define MINE(a,b) ({ \
- __typeof__ (a) _a = (a); \
- __typeof__ (b) _b = (b); \
- _a <= _b ? _a : _b; \
- })
- typedef struct {
- float x,y;
- } Vector2;
- typedef struct {
- float x,y,z;
- } Vector;
- typedef struct {
- float x,y,z,w;
- } Vector4;
- typedef struct Vector2i {
- int x,y;
- } Vector2i;
- typedef struct {
- Vector o;
- Vector d;
- Vector id;
- } Ray;
- typedef struct {
- Vector start, end;
- } LineSegment;
- typedef struct BezierSplineSegment {
- Vector e, c;
- struct BezierSplineSegment* next;
- } BezierSplineSegment;
- typedef struct {
- int length;
- unsigned char isLoop;
- BezierSplineSegment* segments;
- } BezierSpline;
- typedef struct BezierSplineSegment2 {
- Vector2 e, c;
- struct BezierSplineSegment2* next;
- } BezierSplineSegment2;
- typedef struct {
- int length;
- unsigned char isLoop;
- BezierSplineSegment2* segments;
- } BezierSpline2;
- typedef struct {
- Vector n;
- float d;
- } Plane;
- typedef struct {
- Vector v[4];
- } Quad;
- typedef struct {
- Vector2 v[4];
- } Quad2;
- typedef struct {
- Vector2i v[4];
- } Quad2i;
- typedef struct {
- float m[16];
- } Matrix;
- typedef struct MatrixStack {
- short size;
- short top;
- Matrix* stack;
- } MatrixStack;
- typedef struct AABB {
- Vector min;
- Vector max;
- } AABB;
- typedef struct AABB2 {
- Vector2 min;
- Vector2 max;
- } AABB2;
- typedef struct AABB2i {
- Vector2i min;
- Vector2i max;
- } AABB2i;
- extern const Matrix IDENT_MATRIX;
- static inline float frand(float low, float high) {
- return low + ((high - low) * ((float)rand() / (float)RAND_MAX));
- }
- static inline float frandNorm() {
- return ((float)rand() / (float)RAND_MAX);
- }
- static inline double drand(double low, double high) {
- return low + ((high - low) * ((double)rand() / (double)RAND_MAX));
- }
- static inline double drandNorm() {
- return ((double)rand() / (double)RAND_MAX);
- }
- static inline float fclamp(float val, float min, float max) {
- return fmin(max, fmax(min, val));
- }
- static inline float fclampNorm(float val) {
- return fclamp(val, 0.0f, 1.0f);
- }
- static inline int iclamp(int val, int min, int max) {
- return MIN(max, MAX(min, val));
- }
- static inline float flerp(float a, float b, float t) {
- return a + ((b - a) * t);
- }
- int vEq(Vector* a, Vector* b);
- int vEqEp(Vector* a, Vector* b, float epsilon);
- void vCopy(const Vector* src, Vector* dst);
- void vSwap(Vector* a, Vector* b);
- void vAdd(Vector* a, Vector* b, Vector* out);
- void vSub(Vector* from, Vector* what, Vector* diff);
- void vScale(Vector* v, float scalar, Vector* out);
- void vLerp(Vector* a, Vector* b, float t, Vector* out);
- void vInverse(Vector* v, Vector* out);
- float vMag(Vector* v);
- float vDot(Vector* a, Vector* b);
- float vDist(Vector* from, Vector* to);
- void vNorm(Vector* v, Vector* out);
- void vUnit(Vector* v, Vector* out);
- void vCross(Vector* a, Vector* b, Vector* out);
- float vScalarTriple(Vector* a, Vector* b, Vector* c);
- void vProject(Vector* what, Vector* onto, Vector* out);
- void vProjectNorm(Vector* what, Vector* onto, Vector* out);
- void vMin(Vector* a, Vector* b, Vector* out);
- void vMax(Vector* a, Vector* b, Vector* out);
- void vSet(float x, float y, float z, Vector* out);
- void vRandom(Vector* end1, Vector* end2, Vector* out);
- void vRandomNorm(Vector* out);
- void vLerp4(Vector4* a, Vector4* b, float t, Vector4* out);
- void vReflectAcross(Vector* v, Vector* pivot, Vector* out);
- void vTriFaceNormal(Vector* a, Vector* b, Vector* c, Vector* out);
- void vProjectOntoPlane(Vector* v, Plane* p, Vector* out);
- void vProjectOntoPlaneNormalized(Vector* v, Plane* p, Vector* out);
- void planeFromTriangle(Vector* v1, Vector* v2, Vector* v3, Plane* out);
- void planeCopy(Plane* in, Plane* out);
- void planeInverse(Plane* in, Plane* out);
- int planeClassifyPoint(Plane* p, Vector* pt);
- int planeClassifyPointEps(Plane* p, Vector* pt, float epsilon);
- int vEq2(Vector2* a, Vector2* b);
- int vEqEp2(Vector2* a, Vector2* b, float epsilon);
- void vCopy2(const Vector2* src, Vector2* dst);
- void vSwap2(Vector2* a, Vector2* b);
- void vAdd2(Vector2* a, Vector2* b, Vector2* out);
- void vSub2(Vector2* from, Vector2* what, Vector2* diff);
- void vScale2(Vector2* v, float scalar, Vector2* out);
- float vDist2(Vector2* a, Vector2* b);
- void vLerp2(Vector2* a, Vector2* b, float t, Vector2* out);
- void vInverse2(Vector2* v, Vector2* out);
- float vMag2(Vector2* v);
- float vDot2(Vector2* a, Vector2* b);
- void vNorm2(Vector2* v, Vector2* out);
- void vUnit2(Vector2* v, Vector2* out);
- void vMin2(Vector2* a, Vector2* b, Vector2* out);
- void vMax2(Vector2* a, Vector2* b, Vector2* out);
- void vSet2(float x, float y, Vector2* out);
- void vReflectAcross2(Vector2* v, Vector2* pivot, Vector2* out);
- void vRoundAway2(const Vector2* in, const Vector2* center, Vector2i* out);
- void vRoundToward2(const Vector2* in, const Vector2* center, Vector2i* out);
- float triArea2(Vector2* a, Vector2* b, Vector2* c);
- int triPointInside2(Vector2* p, Vector2* a, Vector2* b, Vector2* c);
- int vEq2i(Vector2i* a, Vector2i* b);
- void vCopy2i(const Vector2i* src, Vector2i* dst);
- void vSwap2i(Vector2i* a, Vector2i* b);
- void vAdd2i(Vector2i* a, Vector2i* b, Vector2i* out);
- void vSub2i(Vector2i* from, Vector2i* what, Vector2i* diff);
- void vScale2i(Vector2i* v, int scalar, Vector2i* out);
- int vDot2i(Vector2i* a, Vector2i* b);
- void vMin2i(Vector2i* a, Vector2i* b, Vector2i* out);
- void vMax2i(Vector2i* a, Vector2i* b, Vector2i* out);
- void vSet2i(int x, int y, Vector2i* out);
- float vDist2i(Vector2i* a, Vector2i* b);
- float pvDist(Plane* p, Vector* v);
- void vMatrixMul(Vector* in, Matrix* m, Vector* out);
- void vMatrixMulf(float x, float y, float z, Matrix* m, Vector* out);
- void mIdent(Matrix* m);
- void mCopy(Matrix* in, Matrix* out);
- void mFastMul(Matrix* a, Matrix* b, Matrix* out);
- void mMul(Matrix* a, Matrix* out);
- void mTransv(Vector* v, Matrix* out);
- void mTrans3f(float x, float y, float z, Matrix* out);
- void mScalev(Vector* v, Matrix* out);
- void mScale3f(float x, float y, float z, Matrix* out);
- void mRotv(Vector* v, float theta, Matrix* out);
- void mRot3f(float x, float y, float z, float theta, Matrix* out);
- void mRotX(float theta, Matrix* out);
- void mRotY(float theta, Matrix* out);
- void mRotZ(float theta, Matrix* out);
- void mTranspose(Matrix* in, Matrix* out);
- void mTransposeFast(Matrix* in, Matrix* out);
- float mDeterminate(Matrix* m);
- int mInverse(Matrix* in, Matrix* out);
- void mFrustum(float left, float right, float top, float bottom, float near, float far, Matrix* out);
- void mPerspective(double fov, float aspect, float near, float far, Matrix* out);
- void mOrtho(float left, float right, float top, float bottom, float near, float far, Matrix* out);
- void mLookAt(Vector* eye, Vector* center, Vector* up, Matrix* out);
- void mPrint(Matrix* m, FILE* f);
- void msAlloc(int size, MatrixStack* ms);
- void msFree(MatrixStack* ms);
- int msPush(MatrixStack* ms);
- void msPop(MatrixStack* ms);
- Matrix* msGetTop(MatrixStack* ms);
- void msPrintAll(MatrixStack* ms, FILE* f);
- void msIdent(MatrixStack* ms);
- void msCopy(Matrix* in, MatrixStack* ms);
- void msMul(Matrix* a, MatrixStack* ms);
- void msTransv(Vector* v, MatrixStack* ms);
- void msTrans3f(float x, float y, float z, MatrixStack* ms);
- void msScalev(Vector* v, MatrixStack* ms);
- void msScale3f(float x, float y, float z, MatrixStack* ms);
- void msRotv(Vector* v, float theta, MatrixStack* ms);
- void msRot3f(float x, float y, float z, float theta, MatrixStack* ms);
- void msFrustum(float left, float right, float top, float bottom, float near, float far, MatrixStack* ms);
- void msPerspective(double fov, float aspect, float near, float far, MatrixStack* ms);
- void msOrtho(float left, float right, float top, float bottom, float near, float far, MatrixStack* ms);
- void msLookAt(Vector* eye, Vector* center, Vector* up, MatrixStack* ms);
- void evalBezier(Vector* e1, Vector* e2, Vector* c1, Vector* c2, float t, Vector* out);
- void evalBezierTangent(Vector* e1, Vector* e2, Vector* c1, Vector* c2, float t, Vector* out);
- void evalBezierNorm(Vector* e1, Vector* e2, Vector* c1, Vector* c2, float t, Vector* out);
- float evalBezier1D(float e1, float e2, float c1, float c2, float t);
- float evalBezier1D_dt(float e1, float e2, float c1, float c2, float t);
- float evalBezier1D_ddt(float e1, float e2, float c1, float c2, float t);
- float evalQBezier1D(float e1, float e2, float c1, float t);
- void evalQBezier2D(Vector2* e1, Vector2* e2, Vector2* c1, float t, Vector2* out);
- void evalQBezier(Vector* e1, Vector* e2, Vector* c1, float t, Vector* out);
- int boxDisjoint(const AABB* a, const AABB* b);
- int boxOverlaps(const AABB* a, const AABB* b);
- int boxContainsPoint(const AABB* b, const Vector* p);
- void boxCenter(const AABB* b, Vector* out);
- void boxSize(const AABB* b, Vector* out);
- void makeRay(Vector* origin, Vector* direction, Ray* out);
- int boxRayIntersectFast(const AABB* b, const Ray* r);
- int boxRayIntersect(const AABB* b, const Ray* r, Vector* ipoint, float* idist);
- int boxDisjoint2(const AABB2* a, const AABB2* b);
- int boxOverlaps2(const AABB2* a, const AABB2* b);
- int boxContainsPoint2(const AABB2* b, const Vector2* p);
- void boxCenter2(const AABB2* b, Vector2* out);
- void boxSize2(const AABB2* b, Vector2* out);
- void boxQuadrant2(const AABB2* in, char ix, char iy, AABB2* out);
- int boxDisjoint2i(const AABB2i* a, const AABB2i* b);
- int boxOverlaps2i(const AABB2i* a, const AABB2i* b);
- int boxContainsPoint2i(const AABB2i* b, const Vector2i* p);
- void boxCenter2i(const AABB2i* b, Vector2* out);
- void boxSize2i(const AABB2i* b, Vector2* out);
- void boxQuadrant2i(const AABB2i* in, char ix, char iy, AABB2i* out);
- void quadCenter2(const Quad2* in, Vector2* out);
- void quadRoundOutward2(const Quad2* in, Quad2i* out);
- void quadRoundInward2(const Quad2* in, Quad2i* out);
- #endif // __c3dlas_h__
|