1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
#include <GL/gl.h>
#include <GL/glut.h>
#include <math.h>
#include <stdio.h>
#define PI 3.14159265358979323846
typedef double vector[3];
typedef double matrix[9];
//Convert angles between degrees and radians
double degreesToRadians(double angle)
{
return (PI/180)*angle;
}
double radiansToDegrees(double angle)
{
return (180/PI)/angle;
}
// Vector
class Vector
{
private:vector data;
public: // Constructor
Vector(vector v)
{
data = new vector;
}
~Vector()
{
delete data;
}
// Print to stdout
void print()
{
printf("(%3.3f, %3.3f, %3.3f)\n", data[0], data[1], data[2]);
}
friend class Matrix;
}
// Matrix
class Matrix
{
private: matrix data;
public: // Default constructor, identity matrix
Matrix()
{
data = {1.0,0.0,0.0,
0.0,1.0,0.0,
0.0,0.0,1.0 };
}
// Constructor
Matrix(matrix data_)
{
data = data_;
}
// Print to stdout
void print()
{
for(int i=0; i<9; i+=3)
{
printf("[%3.3f\t%3.3f\t%3.3f]\n", data[i+0], data[i+1], data[i+2]);
}
printf("\n");
}
// Multiply by matrix
Matrix operator*(Matrix m)
{
matrix temp;
for(int x=0; x<3; x++)
{
for(int y=0; y<3; y++)
{
temp[3*x+y] = 0.0;
for(int i=0; i<3; i++)
{
temp[3*x+y] = temp[3*x+y] + m.data[3*x+i]*data[3*i+y];
}
}
}
return Matrix(temp);
}
// Multiply by vector
Vector operator*(Vector v)
{
vector temp = {0.0,0.0,0.0};
for(int x=0; x<3; x++)
{
for(int y=0; y<3; y++)
{
temp[y] += data[3*x+y]*v.data[x];
}
}
return Vector(temp);
}
// Multiply by scalar
Matrix operator*(double s)
{
matrix temp;
for(int i=0; i<9; i++)
{
temp[i] = data[i]*s;
}
return Matrix(temp);
}
//Load into the modelview matrix
void load()
{
glMatrixMode(GL_MODELVIEW);
glLoadMatrixf(this.transpose());
}
//Place a reflection matrix across y=0 into M
Matrix reflect()
{
matrix temp = {-1, 0, 0,
0, 1, 0,
0, 0, 1 };
return Matrix(temp);
}
//Place the scale transform of magnitude s into M
Matrix scale(double s)
{
matrix temp = { s,0,0,
0,s,0,
0,0,s };
return Matrix(temp);
}
//Place the translation transform of <x,y> into M
Matrix translate(double x, double y)
{
matrix temp = { 1,0,x,
0,1,y,
0,0,1 };
return Matrix(temp);
}
//Place the transpose of matrix M into matrix N
Matrix transpose()
{
matrix temp = { data[3*0 + 0], data[3*1 + 0], data[3*2 + 0],
data[3*0 + 1], data[3*1 + 1], data[3*2 + 1],
data[3*0 + 2], data[3*1 + 2], data[3*2 + 2] };
return Matrix(temp);
}
}
|