source: branches/VUT/0.2/chcdemo/MathStuff.h @ 10

Revision 10, 6.0 KB checked in by gametools, 20 years ago (diff)

vienna university of technology first files

Line 
1//Copyright and Disclaimer:
2//This code is copyright Vienna University of Technology, 2004.
3
4/*
5a few simple mathematical routines; not the most efficient ones, but easy to understand.
6*/
7
8#ifndef MathStuffH
9#define MathStuffH
10
11#include "DataTypes.h"
12
13//PI
14extern const double PI;
15//PI/2
16extern const double PI_2;
17//PI/180
18extern const double PI_180;
19
20//vector = (0,0,0)
21extern const Vector3 ZERO;
22//vector = (1,0,0)
23extern const Vector3 UNIT_X;
24//vector = (0,1,0)
25extern const Vector3 UNIT_Y;
26//vector = (0,0,1)
27extern const Vector3 UNIT_Z;
28
29//[1,0,0,0]
30//[0,1,0,0]
31//[0,0,1,0]
32//[0,0,0,1]
33extern const Matrix4x4 IDENTITY;
34
35//absolut value of double
36extern double absDouble(const double);
37//signum of double
38extern double signDouble(const double);
39//clamp value in between min and max borders
40extern void clamp(double* value, const double min, const double max);
41//result = pos+t*dir
42extern void linCombVector3(Vector3 result, const Vector3 pos, const Vector3 dir, const double t);
43extern double dot(const Vector3, const Vector3);
44extern void cross(Vector3, const Vector3, const Vector3);
45extern void normalize(Vector3);
46extern double squaredLength(const Vector3);
47
48//[x,0,0,0]
49//[0,y,0,0]
50//[0,0,z,0]
51//[0,0,0,1]
52extern void makeScaleMtx(Matrix4x4 output, const double x, const double y, const double z);
53//rotation around x axis
54extern void makeRotationXMtx(Matrix4x4 output, const double rad);
55//rotation around y axis
56extern void makeRotationYMtx(Matrix4x4 output, const double rad);
57//rotation around z axis
58extern void makeRotationZMtx(Matrix4x4 output, const double rad);
59//translation
60extern void makeTranslationMtx(Matrix4x4 output, Vector3 translation);
61
62//output = a*b (matrix product)
63extern void mult(Matrix4x4 output, const Matrix4x4 a, const Matrix4x4 b);
64//output = i^(-1)
65extern void invert(Matrix4x4 output, const Matrix4x4 i);
66//output = look from position:pos into direction:dir with up-vector:up
67extern void look(Matrix4x4 output, const Vector3 pos, const Vector3 dir, const Vector3 up);
68//make a scaleTranslate matrix that includes the two values vMin and vMax
69extern void scaleTranslateToFit(Matrix4x4 output, const Vector3 vMin, const Vector3 vMax);
70//output is initialized with the same result as glPerspective vFovy in degrees
71extern void perspectiveDeg(Matrix4x4 output, const double vFovy, const double vAspect,
72        const double vNearDis, const double vFarDis);
73
74//calc matrix-vector product; input has assumed homogenous component w = 1
75//before the output is  written homogen division is performed (w = 1)
76extern void mulHomogenPoint(Vector3 output, const Matrix4x4 m, const Vector3 v);
77//min and max are the two extreme points of an AABB containing all the points
78extern void calcCubicHull(Vector3 min, Vector3 max, const Vector3* ps, const int size);
79//calculates the world coordinates of the view frustum corner points
80//input matrix is the (eyeProj*eyeView)^(-1) matrix
81extern void calcViewFrustumWorldCoord(Vector3x8, const Matrix4x4);
82// mulHomogenPoint each point of VecPoint
83extern void transformVecPoint(struct VecPoint* , const Matrix4x4);
84// transformVecPoint each VecPoint of Object
85extern void transformObject(struct Object*, const Matrix4x4);
86//min and max are the two extreme points of an AABB containing all the points of the object
87extern void calcObjectCubicHull(Vector3 min, Vector3 max, const struct Object);
88
89//calculates the six polygons defining an view frustum
90extern void calcViewFrustObject(struct Object*, const Vector3x8);
91//the given object is clipped by the given AABox; the object is assumed closed
92//and is closed after the clipping
93extern void clipObjectByAABox(struct Object*, const struct AABox);
94//extrudes the object into -lightDir and clippes by the AABox the defining points are returned
95extern void includeObjectLightVolume(struct VecPoint* points, const struct Object,
96        const Vector3 lightDir, const struct AABox sceneAABox);
97//calculates the ViewFrustum Object     clippes this Object By the sceneAABox and
98//extrudes the object into -lightDir and clippes by the AABox the defining points are returned
99extern void calcFocusedLightVolumePoints(struct VecPoint* points,const Matrix4x4 invEyeProjView,
100        const Vector3 lightDir,const struct AABox sceneAABox);
101// calculates the index of the nearest vertex in the AAB vertex set
102// obtainened by calcAABoxPoints according to the normal vector of a clip plane
103extern int calcAABNearestVertexIdx(Vector3 clipPlaneNormal);
104// calculates the index of the farthest vertex in the AAB vertex set
105// obtainened by calcAABoxPoints according to the normal vector of a clip plane
106extern int calcAABFarthestVertexIdx(Vector3 clipPlaneNormal);
107
108extern double pointPlaneDistance(const struct Plane A, const Vector3 p);
109
110extern int pointBeforePlane(const struct Plane A, const Vector3 p);
111// calculates the view frustum planes of the frustum defined by the eyeProjView matrix
112extern void calcViewFrustumPlanes(struct VecPlane* planes, const Matrix4x4 eyeProjView);
113// calculates the np-indices of an aab for a set of 6 clipping planes
114// the indices are stored in the form (n0, p0, n1, p1, ..., n11, p11) in the array vertexIdx
115extern void calcAABNPVertexIndices(int *vertexIndices, const struct VecPlane planes);
116// combines two AABs, stores result in a
117extern void combineAABoxes(struct AABox *a, const struct AABox b);
118// calculates volume of this aab
119extern double calcAABoxVolume(const struct AABox aab);
120// calculates surface of this aab
121double calcAABoxSurface(const struct AABox aab);
122// clips box aab by box enclosed
123extern void clipAABoxByAABox(struct AABox *aab, const struct AABox enclosed);
124// computes world position center of an aab
125extern void calcAABoxCenter(Vector3 vec, const struct AABox aab);
126
127extern void rotateVectorX(Vector3 v, double rad);
128extern void rotateVectorY(Vector3 v, double rad);
129extern void rotateVectorZ(Vector3 v, double rad);
130// rotates vector around axis(must be unit vector)
131extern void rotateVector(Vector3 result, const Vector3 input, const float rad, const Vector3 axis);
132// generates normal vector of a vector
133extern void vectorNormal(Vector3 result, const Vector3 vec);
134#endif
Note: See TracBrowser for help on using the repository browser.