40 #include <OpenGL/gl.h>
41 #include <OpenGL/glu.h>
42 #include <GLUT/glut.h>
63 const char version[] =
"CUGL V2 2009.11.24";
66 const double PI = 4 * atan(1.0);
181 Point(GLfloat
x = 0, GLfloat
y = 0, GLfloat
z = 0, GLfloat
w = 1);
188 Point (GLfloat coordinates[]);
238 void light(GLenum lightNum)
const;
368 return sqrt((p.
x - q.
x) * (p.
x - q.
x) + (p.
y - q.
y) * (p.
y - q.
y) + (p.
z - q.
z) * (p.
z - q.
z));
479 Plane(GLfloat
a = 0, GLfloat
b = 1, GLfloat
c = 0, GLfloat
d = 0);
636 Vector(GLfloat coordinates[]);
724 GLfloat
norm()
const;
955 const GLfloat m00,
const GLfloat m10,
const GLfloat m20,
const GLfloat m30,
956 const GLfloat m01,
const GLfloat m11,
const GLfloat m21,
const GLfloat m31,
957 const GLfloat m02,
const GLfloat m12,
const GLfloat m22,
const GLfloat m32,
958 const GLfloat m03,
const GLfloat m13,
const GLfloat m23,
const GLfloat m33 );
988 return m[0][0] +
m[1][1] +
m[2][2] +
m[3][3];
1052 const GLfloat &
operator()(
int i,
int j)
const;
1390 GLfloat
norm()
const;
1428 double angle()
const;
1448 void euler(
double & xr,
double & yr,
double & zr)
const;
1463 void trackball(GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2);
1486 return (
angle == 0) ?
1498 return (
angle == 0) ?
2017 void read(
const char *bmpFileName);
2030 void read(GLint x, GLint y, GLsizei w, GLsizei h, GLenum mode = GL_FRONT);
2273 void draw(
bool showNormals =
false);
2296 double eccentricity;
2329 return angle *
PI / 180;
2337 return angle * 180 /
PI;
2349 static unsigned int seed = 12345678;
2350 unsigned int result;
2351 const unsigned int bucket =
static_cast<unsigned int>(4294967296.0 / max);
2354 seed = 1664525 * seed + 1013904223;
2355 result = seed / bucket;
2357 while (result >= max);
2364 return randInt(2 * max + 1) - max;
2371 return double(
randInt(BIG)) / double(BIG);
2419 void revolve(
int numSteps, GLfloat coor[][2],
int numSlices,
bool drawNormals =
false);
2434 eye[0], eye[1], eye[2],
2449 eye[0], eye[1], eye[2],
2450 model[0], model[1], model[2],
2465 eye[0], eye[1], eye[2],
2466 model[0], model[1], model[2],
2574 GLfloat ambR, GLfloat ambG, GLfloat ambB, GLfloat ambA,
2575 GLfloat difR, GLfloat difG, GLfloat difB, GLfloat difA,
2576 GLfloat speR, GLfloat speG, GLfloat speB, GLfloat speA,
2635 glVertex4f(
x,
y,
z,
w);
2645 glLightfv(lightNum, GL_POSITION, p);
2650 glTranslatef(
x/
w,
y/
w,
z/
w);
2667 return Point(s * p.
x, s * p.
y, s * p.
z, s * p.
w);
2672 return Point(s * p.
x, s * p.
y, s * p.
z, s * p.
w);
2677 return p.
x == q.
x && p.
y == q.
y && p.
z == q.
z && p.
w == q.
w;
2687 return p.
x * s.
a + p.
y * s.
b + p.
z * s.
c + p.
w * s.
d;
2692 return s.
a * p.
x + s.
b * p.
y + s.
c * p.
z + s.
d * p.
w;
2710 return m.
s == n.
s && m.
f == n.
f;
2731 return p.
a == q.
a && p.
b == q.
b && p.
c == q.
c && p.
d == q.
d;
2752 if (p.
w == 0 || q.
w == 0)
2760 x = p.
x/p.
w - q.
x/q.
w;
2761 y = p.
y/p.
w - q.
y/q.
w;
2762 z = p.
z/p.
w - q.
z/q.
w;
2806 return x *
x +
y *
y +
z *
z;
2811 return GLfloat(sqrt(
norm()));
2816 glTranslatef(
x,
y,
z);
2825 0.0f, 0.0f, 0.0f, 1.0f );
2830 glNormal3f(
x,
y,
z);
2837 (p + (*this)).
draw();
2845 if (p.
w == 0 || q.
w == 0)
2863 return Vector(s * v.
x, s * v.
y, s * v.
z);
2868 return Vector(s * v.
x, s * v.
y, s * v.
z);
2874 u.
y * v.
z - u.
z * v.
y,
2875 u.
z * v.
x - u.
x * v.
z,
2876 u.
x * v.
y - u.
y * v.
x );
2882 u.
y * v.
z - u.
z * v.
y,
2883 u.
z * v.
x - u.
x * v.
z,
2884 u.
x * v.
y - u.
y * v.
x );
2889 return u.
x * v.
x + u.
y * v.
y + u.
z * v.
z;
2894 return u.
x == v.
x && u.
y == v.
y && u.
z == v.
z;
2917 GLfloat m00, GLfloat m10, GLfloat m20, GLfloat m30,
2918 GLfloat m01, GLfloat m11, GLfloat m21, GLfloat m31,
2919 GLfloat m02, GLfloat m12, GLfloat m22, GLfloat m32,
2920 GLfloat m03, GLfloat m13, GLfloat m23, GLfloat m33 )
2946 m[0][0] * p.
x +
m[0][1] * p.
y +
m[0][2] * p.
z +
m[0][3] * p.
w,
2947 m[1][0] * p.
x +
m[1][1] * p.
y +
m[1][2] * p.
z +
m[1][3] * p.
w,
2948 m[2][0] * p.
x +
m[2][1] * p.
y +
m[2][2] * p.
z +
m[2][3] * p.
w,
2949 m[3][0] * p.
x +
m[3][1] * p.
y +
m[3][2] * p.
z +
m[3][3] * p.
w
2955 glMultMatrixf(&
m[0][0]);
2983 m[0][0] * v[0] +
m[0][1] * v[1] +
m[0][2] * v[2],
2984 m[1][0] * v[0] +
m[1][1] * v[1] +
m[1][2] * v[2],
2985 m[2][0] * v[0] +
m[2][1] * v[1] +
m[2][2] * v[2]
3035 for (
int c = 0; c < 4; ++c)
3036 for (
int r = 0; r < 4; ++r)
3037 m[c][r] += rhs(c,r);
3050 for (
int c = 0; c < 4; ++c)
3051 for (
int r = 0; r < 4; ++r)
3052 m[c][r] -= rhs(c,r);
3065 *
this = (*this) * rhs;
3072 r(0,0) = m(0,0) * n(0,0) + m(0,1) * n(1,0) + m(0,2) * n(2,0) + m(0,3) * n(3,0);
3073 r(0,1) = m(0,0) * n(0,1) + m(0,1) * n(1,1) + m(0,2) * n(2,1) + m(0,3) * n(3,1);
3074 r(0,2) = m(0,0) * n(0,2) + m(0,1) * n(1,2) + m(0,2) * n(2,2) + m(0,3) * n(3,2);
3075 r(0,3) = m(0,0) * n(0,3) + m(0,1) * n(1,3) + m(0,2) * n(2,3) + m(0,3) * n(3,3);
3076 r(1,0) = m(1,0) * n(0,0) + m(1,1) * n(1,0) + m(1,2) * n(2,0) + m(1,3) * n(3,0);
3077 r(1,1) = m(1,0) * n(0,1) + m(1,1) * n(1,1) + m(1,2) * n(2,1) + m(1,3) * n(3,1);
3078 r(1,2) = m(1,0) * n(0,2) + m(1,1) * n(1,2) + m(1,2) * n(2,2) + m(1,3) * n(3,2);
3079 r(1,3) = m(1,0) * n(0,3) + m(1,1) * n(1,3) + m(1,2) * n(2,3) + m(1,3) * n(3,3);
3080 r(2,0) = m(2,0) * n(0,0) + m(2,1) * n(1,0) + m(2,2) * n(2,0) + m(2,3) * n(3,0);
3081 r(2,1) = m(2,0) * n(0,1) + m(2,1) * n(1,1) + m(2,2) * n(2,1) + m(2,3) * n(3,1);
3082 r(2,2) = m(2,0) * n(0,2) + m(2,1) * n(1,2) + m(2,2) * n(2,2) + m(2,3) * n(3,2);
3083 r(2,3) = m(2,0) * n(0,3) + m(2,1) * n(1,3) + m(2,2) * n(2,3) + m(2,3) * n(3,3);
3084 r(3,0) = m(3,0) * n(0,0) + m(3,1) * n(1,0) + m(3,2) * n(2,0) + m(3,3) * n(3,0);
3085 r(3,1) = m(3,0) * n(0,1) + m(3,1) * n(1,1) + m(3,2) * n(2,1) + m(3,3) * n(3,1);
3086 r(3,2) = m(3,0) * n(0,2) + m(3,1) * n(1,2) + m(3,2) * n(2,2) + m(3,3) * n(3,2);
3087 r(3,3) = m(3,0) * n(0,3) + m(3,1) * n(1,3) + m(3,2) * n(2,3) + m(3,3) * n(3,3);
3093 for (
int i = 0; i < 4; i++)
3094 for (
int j = 0; j < 4; j++)
3095 if (m(i,j) != n(i,j))
3102 for (
int i = 0; i < 4; i++)
3103 for (
int j = 0; j < 4; j++)
3104 if (m(i,j) != n(i,j))
3112 for (
int i = 0; i < 4; i++)
3113 for (
int j = 0; j < 4; j++)
3114 sm(i, j) = s * m(i, j);
3121 for (
int i = 0; i < 4; i++)
3122 for (
int j = 0; j < 4; j++)
3123 ms(i, j) = s * m(i, j);
3168 -(-w[0] *
v[0] - w[1] *
v[1] - w[2] *
v[2]) *
v[0] +
s * (
s * w[0] + w[1] *
v[2] - w[2] *
v[1])
3169 -
v[1] * (
s * w[2] + w[0] *
v[1] - w[1] *
v[0]) +
v[2] * (
s * w[1] + w[2] *
v[0] - w[0] *
v[2]),
3170 -(-w[0] *
v[0] - w[1] *
v[1] - w[2] *
v[2]) *
v[1] +
s * (
s * w[1] + w[2] *
v[0] - w[0] *
v[2])
3171 -
v[2] * (
s * w[0] + w[1] *
v[2] - w[2] *
v[1]) +
v[0] * (
s * w[2] + w[0] *
v[1] - w[1] *
v[0]),
3172 -(-w[0] *
v[0] - w[1] *
v[1] - w[2] *
v[2]) *
v[2] +
s * (
s * w[2] + w[0] *
v[1] - w[1] *
v[0])
3173 -
v[0] * (
s * w[1] + w[2] *
v[0] - w[0] *
v[2]) +
v[1] * (
s * w[0] + w[1] *
v[2] - w[2] *
v[1])
3194 return GLfloat(sqrt(
norm()));
3219 GLfloat ns =
s * q.
s -
v[0] * q.
v[0] -
v[1] * q.
v[1] -
v[2] * q.
v[2];
3221 q.
s *
v[0] +
s * q.
v[0] +
v[1] * q.
v[2] -
v[2] * q.
v[1],
3222 q.
s *
v[1] +
s * q.
v[1] +
v[2] * q.
v[0] -
v[0] * q.
v[2],
3223 q.
s *
v[2] +
s * q.
v[2] +
v[0] * q.
v[1] -
v[1] * q.
v[0] );
3233 return q.
s * r.
s +
dot(q.
v, r.
v);
3250 q.
s * r.
s - q.
v[0] * r.
v[0] - q.
v[1] * r.
v[1] - q.
v[2] * r.
v[2],
3251 r.
s * q.
v[0] + q.
s * r.
v[0] + q.
v[1] * r.
v[2] - q.
v[2] * r.
v[1],
3252 r.
s * q.
v[1] + q.
s * r.
v[1] + q.
v[2] * r.
v[0] - q.
v[0] * r.
v[2],
3253 r.
s * q.
v[2] + q.
s * r.
v[2] + q.
v[0] * r.
v[1] - q.
v[1] * r.
v[0]
3261 - v[0] * q.
v[0] - v[1] * q.
v[1] - v[2] * q.
v[2],
3262 q.
s * v[0] + v[1] * q.
v[2] - v[2] * q.
v[1],
3263 q.
s * v[1] + v[2] * q.
v[0] - v[0] * q.
v[2],
3264 q.
s * v[2] + v[0] * q.
v[1] - v[1] * q.
v[0]
3272 - q.
v[0] * v[0] - q.
v[1] * v[1] - q.
v[2] * v[2],
3273 q.
s * v[0] + q.
v[1] * v[2] - q.
v[2] * v[1],
3274 q.
s * v[1] + q.
v[2] * v[0] - q.
v[0] * v[2],
3275 q.
s * v[2] + q.
v[0] * v[1] - q.
v[1] * v[0]
3281 GLfloat den = q.
norm();
3282 GLfloat ns = (
s * q.
s +
v[0] * q.
v[0] +
v[1] * q.
v[1] +
v[2] * q.
v[2]) / den;
3285 (q.
s *
v[0] -
s * q.
v[0] -
v[1] * q.
v[2] +
v[2] * q.
v[1]) / den,
3286 (q.
s *
v[1] -
s * q.
v[1] -
v[2] * q.
v[0] +
v[0] * q.
v[2]) / den,
3287 (q.
s *
v[2] -
s * q.
v[2] -
v[0] * q.
v[1] +
v[1] * q.
v[0]) / den
3305 return q.
s == r.
s && q.
v == r.
v;
virtual void apply() const =0
void moveUp(GLfloat distance)
void update(const Point &e, const Point &m)
void set(const Point &eye, const Point &model)
void moveForward(GLfloat distance)
void set(const Point &eye)
Camera(const Point &eye, const Point &model, const Vector &up)
void set(const Point &eye, const Point &model, const Vector &up)
void moveLeft(GLfloat distance)
void tiltUp(double angle)
friend std::ostream & operator<<(std::ostream &os, const Camera &c)
void panLeft(double angle)
Camera(const Point &eye, const Point &model)
void getMatrix(double t, GL_Matrix m) const
void apply(double t) const
void set(const Quaternion &qFirst, const Quaternion &qLast)
Quaternion getQuaternion(double t) const
Interpolator(const Quaternion &qFirst, const Quaternion &qLast)
virtual void apply() const
friend bool operator!=(const Line &x, const Line &y)
Line(const Point &p, const Point &q)
friend Point meet(const Line &k, const Plane &p)
friend bool operator==(const Line &x, const Line &y)
friend std::ostream & operator<<(std::ostream &os, const Line &k)
friend std::ostream & operator<<(std::ostream &os, const Matrix &m)
Matrix & operator/=(GLfloat s)
void shadow(const Point &lightPos, const Plane &plane)
Quaternion quaternion() const
GLfloat & operator()(int i, int j)
friend Matrix operator-(const Matrix &m, const Matrix &n)
Matrix(const Vector &axis, double theta)
Matrix(const Quaternion &q)
friend bool operator==(const Matrix &x, const Matrix &y)
Matrix operator-=(const Matrix &rhs)
Matrix operator+=(const Matrix &rhs)
Matrix(const Vector &u, const Vector &v)
void reflect(const Plane &p)
friend Matrix operator+(const Matrix &m, const Matrix &n)
friend Matrix operator*(const Matrix &m, const Matrix &n)
friend Matrix operator/(const Matrix &m, GLfloat s)
Matrix operator*=(const Matrix &rhs)
friend bool operator!=(const Matrix &x, const Matrix &y)
void read(GLint x, GLint y, GLsizei w, GLsizei h, GLenum mode=GL_FRONT)
void read(const char *bmpFileName)
void setTexture(GLuint name)
bool allocate(unsigned long newSize)
unsigned long getColumns() const
PixelMap(const PixelMap &pm)
friend std::ostream & operator<<(std::ostream &os, const PixelMap &pm)
unsigned long getSize() const
PixelMap(GLint x, GLint y, GLsizei w, GLsizei h)
void write(const char *bmpFileName)
friend void mix(const PixelMap &m1, const PixelMap &m2, PixelMap &res, double prop)
friend bool compatible(const PixelMap &m1, const PixelMap &m2)
void select(const PixelMap &src, int xp, int yp, int width, int height)
PixelMap(const char *bmpFileName)
unsigned long getRows() const
void setMipmaps(GLuint name)
Plane(GLfloat a=0, GLfloat b=1, GLfloat c=0, GLfloat d=0)
friend bool operator!=(const Plane &x, const Plane &y)
Plane(const Vector &v, const Point &p)
friend GLfloat dist(const Point &p, const Plane &s)
Plane(const Line &s, const Point &p)
void clipPlane(GLenum index) const
friend Point meet(const Line &k, const Plane &p)
Plane(const Point &p, const Point &q, const Point &r)
friend std::ostream & operator<<(std::ostream &os, const Plane &p)
friend bool operator==(const Plane &x, const Plane &y)
Point operator+=(const Vector &v)
Point(const Quaternion &q)
Point operator-=(const Vector &v)
friend bool operator!=(const Point &p, const Point &q)
GLfloat & operator[](int i)
friend GLfloat dist(const Point &p, const Plane &s)
friend Vector operator-(const Point &p, const Point &q)
friend Point operator+(const Vector &v, const Point &p)
Point operator/(GLfloat s) const
friend double dist(const Point &p, const Point &q)
friend std::ostream & operator<<(std::ostream &os, const Point &p)
friend Point operator*(const Point &p, GLfloat s)
friend Point meet(const Line &k, const Plane &p)
Point(GLfloat x=0, GLfloat y=0, GLfloat z=0, GLfloat w=1)
const GLfloat & operator[](int i) const
friend bool operator==(const Point &p, const Point &q)
void light(GLenum lightNum) const
Quaternion & operator/=(const Quaternion &q)
Quaternion(Vector axis, double angle)
Quaternion operator+=(const Quaternion &q)
friend Quaternion log(const Quaternion &q)
friend Quaternion exp(const Vector &v)
void trackball(GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2)
Quaternion(double xr, double yr, double zr)
friend Quaternion exp(const Quaternion &q)
Quaternion(const Point &p)
friend GLfloat dot(const Quaternion &q, const Quaternion &r)
Quaternion(const Vector &v)
friend Quaternion operator*(const Quaternion &q, const Quaternion &r)
Quaternion operator/(GLfloat scale) const
void euler(double &xr, double &yr, double &zr) const
friend Quaternion operator+(const Quaternion &q, const Quaternion &r)
friend std::ostream & operator<<(std::ostream &os, const Quaternion &q)
GLfloat magnitude() const
Quaternion(const Vector &u, const Vector &v)
void matrix(Matrix &m) const
void integrate(const Vector &omega, double dt)
Quaternion(GLfloat s, const Vector &v)
friend bool operator==(const Quaternion &x, const Quaternion &y)
friend Quaternion operator-(const Quaternion &q, const Quaternion &r)
friend bool operator!=(const Quaternion &x, const Quaternion &y)
Quaternion(GLfloat s, GLfloat x, GLfloat y, GLfloat z)
Quaternion & operator*=(const Quaternion &q)
void matrix(GL_Matrix m) const
Quaternion operator~() const
Quaternion operator-=(const Quaternion &q)
void setSlices(int slices)
Revolute(int numSteps, GLfloat profile[][2])
Revolute(const Revolute &)
const Revolute & operator=(const Revolute &)
void draw(bool showNormals=false)
void setEccentricity(double ecc)
Vector(GLfloat x, GLfloat y, GLfloat z)
Vector operator/(GLfloat scale) const
Vector operator/=(GLfloat scale)
friend bool operator==(const Vector &x, const Vector &y)
Vector(Point points[], int numPoints)
Vector operator-=(const Vector &v)
GLfloat & operator[](int i)
friend Vector operator*(const Vector &u, const Vector &v)
Vector operator+=(const Vector &v)
Vector operator*=(GLfloat scale)
friend Vector operator+(const Vector &u, const Vector &v)
friend std::ostream & operator<<(std::ostream &os, const Vector &v)
const GLfloat & operator[](int i) const
void draw(const Point &p=Point()) const
friend bool operator!=(const Vector &x, const Vector &y)
friend GLfloat dot(const Vector &u, const Vector &v)
friend Vector cross(const Vector &u, const Vector &v)
Vector cross(const Vector &u, const Vector &v)
Point operator*(const Point &p, GLfloat s)
Matrix operator/(const Matrix &m, GLfloat s)
double degrees(double angle)
void triStripNormals(Point points[], Vector normals[], int numPoints, bool neg=false)
bool operator==(const Point &p, const Point &q)
void revolve(int numSteps, GLfloat coor[][2], int numSlices, bool drawNormals=false)
void setMaterial(const int m, GLenum face=GL_FRONT)
Point operator+(const Point &p, const Vector &v)
double radians(double angle)
void buildPlane(bool shadow=false)
int addMaterial(GLfloat ambR, GLfloat ambG, GLfloat ambB, GLfloat ambA, GLfloat difR, GLfloat difG, GLfloat difB, GLfloat difA, GLfloat speR, GLfloat speG, GLfloat speB, GLfloat speA, GLfloat shine)
const char * getErrorString(CUGLErrorType err)
bool operator!=(const Point &p, const Point &q)
void axes(GLfloat size=1)
GLfloat dot(const Vector &u, const Vector &v)
int randSym(unsigned int max)
unsigned int randInt(unsigned int max)
GLfloat dist(const Point &p, const Plane &s)
GLuint makePlaneList(bool shadow=false)
Vector operator-(const Point &p, const Point &q)