summaryrefslogtreecommitdiffstats
path: root/math3d.h
diff options
context:
space:
mode:
Diffstat (limited to 'math3d.h')
-rw-r--r--math3d.h130
1 files changed, 130 insertions, 0 deletions
diff --git a/math3d.h b/math3d.h
new file mode 100644
index 0000000..e33b387
--- /dev/null
+++ b/math3d.h
@@ -0,0 +1,130 @@
+#ifndef _MATH3D_H_
+#define _MATH3D_H_
+
+#include <GL/glew.h>
+#include <GL/freeglut.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <math.h>
+
+#define M3D_PI (3.14159265358979323846)
+#define M3D_PI_DIV_180 (0.017453292519943296)
+#define M3D_INV_PI_DIV_180 (57.2957795130823229)
+
+#define m3dDegToRad(x) ((x)*M3D_PI_DIV_180)
+#define m3dRadToDeg(x) ((x)*M3D_INV_PI_DIV_180)
+
+typedef float M3DMatrix44f[16]; /* 4x4 matrix */
+typedef float M3DVector3f[3]; /* vector of 3 floats */
+typedef float M3DVector2f[2]; /* vector of 2 floats */
+
+/* this is the targa header, pragmas are needed to do the voodoo magic */
+/* http://gcc.gnu.org/onlinedocs/gcc-4.4.0/gcc/ */
+#pragma pack(1)
+typedef struct
+{
+ GLbyte identsize; /* Size of id field that follows header (0) */
+ GLbyte imageType; /* 0 = none, 1 = indexed, 2 = rgb, 3 = grey, +8 = rle */
+ GLbyte colorMapType; /* 0 = none, 1 = paletted */
+ unsigned short colorMapStart; /* first colour map entry */
+ unsigned short colorMapLength; /* number of colors */
+ unsigned char colorMapBits; /* bits per palette entry */
+ unsigned short xstart; /* image x origin */
+ unsigned short ystart; /* image y origin */
+ unsigned short width; /* width in pixels */
+ unsigned short height; /* height in pixels */
+ GLbyte bits; /* bits per pixel (8 16, 24, 32) */
+ GLbyte descriptor; /* image descriptor */
+} TGAHEADER;
+#pragma pack(1)
+
+/* math function prototypes */
+void m3dFindNormal(M3DVector3f, const M3DVector3f, const M3DVector3f,
+ const M3DVector3f);
+void m3dLoadIdentity44(M3DMatrix44f);
+void m3dRotationMatrix44(M3DMatrix44f, float, float, float, float);
+void gltDrawTorus(GLfloat, GLfloat, GLint, GLint);
+void DrawJet(int);
+void gltDrawUnitAxes(void);
+void m3dMatrixMultiply44(M3DMatrix44f, const M3DMatrix44f, const M3DMatrix44f);
+
+/* other function prototypes */
+GLint gltWriteTGA(const char *);
+GLbyte *gltLoadTGA(const char *, GLint *, GLint *, GLint *, GLenum *);
+
+static inline void m3dCrossProduct(M3DVector3f result, const M3DVector3f u,
+ const M3DVector3f v)
+{
+ result[0] = u[1] * v[2] - v[1] * u[2];
+ result[1] = -u[0] * v[2] + v[0] * u[2];
+ result[2] = u[0] * v[1] - v[0] * u[1];
+}
+
+static inline void m3dTransformVector3(M3DVector3f vOut, const M3DVector3f v,
+ const M3DMatrix44f m)
+{
+ vOut[0] = m[0] * v[0] + m[4] * v[1] + m[8] * v[2] + m[12]; /* * v[3]; */
+ vOut[1] = m[1] * v[0] + m[5] * v[1] + m[9] * v[2] + m[13]; /* * v[3]; */
+ vOut[2] = m[2] * v[0] + m[6] * v[1] + m[10] * v[2] + m[14]; /* * v[3]; */
+ /* vOut[3] = m[3] * v[0] + m[7] * v[1] + m[11] * v[2] + m[15] * v[3]; */
+}
+
+static inline void m3dScaleVector3(M3DVector3f v, float scale)
+{
+ v[0] *= scale;
+ v[1] *= scale;
+ v[2] *= scale;
+}
+
+static inline float m3dGetVectorLengthSquared(const M3DVector3f u)
+{
+ return (u[0] * u[0]) + (u[1] * u[1]) + (u[2] * u[2]);
+}
+
+static inline float m3dGetVectorLength(const M3DVector3f u)
+{
+ return (float) (sqrt((double) (m3dGetVectorLengthSquared(u))));
+}
+
+static inline void m3dNormalizeVector(M3DVector3f u)
+{
+ m3dScaleVector3(u, 1.0f / m3dGetVectorLength(u));
+}
+
+static inline void m3dTranslateMatrix44(M3DMatrix44f m, float x, float y,
+ float z)
+{
+ m[12] += x;
+ m[13] += y;
+ m[14] += z;
+}
+
+static inline void m3dScaleMatrix44(M3DMatrix44f m, float x, float y, float z)
+{
+ m[0] *= x;
+ m[5] *= y;
+ m[10] *= z;
+}
+
+/* transpose / invert, only 4x4 matricies supported */
+#define TRANSPOSE44(dst, src) \
+{ \
+ int i, j; \
+ for (j = 0; j < 4; j++) \
+ { \
+ for (i = 0; i < 4; i++) \
+ { \
+ dst[(j*4)+i] = src[(i*4)+j]; \
+ } \
+ } \
+}
+
+static inline void m3dTransposeMatrix44(M3DMatrix44f dst,
+ const M3DMatrix44f src)
+{
+ TRANSPOSE44(dst, src);
+}
+
+#endif
+