dada.h

Go to the documentation of this file.
00001 /*  Dada
00002  *  Copyright (C) 2005 David Griffiths <dave@pawfal.org>
00003  *
00004  *  This program is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  This program is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with this program; if not, write to the Free Software
00016  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017 */ 
00018 // Dada.h 
00019 // A collection of handy classes for 3D graphics.
00020 // (Mostly half finished)
00021 //
00022 
00023 #include <math.h>
00024 #include <iostream>
00025 #include <list>
00026 #include "Trace.h"
00027 
00028 using namespace std;
00029 
00030 #ifndef DADA
00031 #define DADA
00032 
00033 namespace Fluxus
00034 {
00035 
00036 static const float TWO_PI=3.141592654*2.0f;
00037 static const float DEG_CONV = 0.017453292;
00038 static const float RAD_CONV = 1/0.017453292;
00039 
00040 inline void debug(char *s) {Trace::Stream<<"dada debug: "<<s<<endl;}
00041 
00042 void  InitDada();
00043 float RandFloat();
00044 float RandRange(float L, float H);
00045 void  dSinCos(float a, float &s, float &c);
00046 
00047 class dVector
00048 {
00049 public:
00050         float x,y,z,w;
00051         dVector() {x=y=z=0; w=1;}
00052         dVector(float X, float Y, float Z, float W=1) {x=X; y=Y; z=Z; w=W;}
00053         dVector(dVector const &c) {*this=c;}
00054         
00055         float *arr() {return &x;}
00056         int operator==(dVector const &rhs) {return (x==rhs.x&&y==rhs.y&&z==rhs.z);}
00057         dVector &operator=(dVector const &rhs);
00058         dVector operator+(dVector const &rhs) const;
00059         dVector operator-(dVector const &rhs) const;
00060         dVector operator*(dVector const &rhs) const;
00061         dVector operator/(dVector const &rhs) const;
00062         dVector operator+(float rhs) const;
00063         dVector operator-(float rhs) const;
00064         dVector operator*(float rhs) const;
00065         dVector operator/(float rhs) const;
00066         dVector &operator+=(dVector const &rhs);
00067         dVector &operator-=(dVector const &rhs);
00068         dVector &operator*=(float rhs);
00069         dVector &operator/=(float rhs);
00070         dVector cross(dVector const &rhs) const;
00071         float dot(dVector const &rhs) const;
00072         float dist(dVector const &rhs) const;
00073         float distsq(dVector const &rhs) const;
00074         float mag();
00075         void get_euler(float &rx, float &ry, float &rz) const;
00076         void homog() {if (w && w!=1.0) {x/=w; y/=w; z/=w; w=1;}}
00077         dVector &normalise() {*this/=mag(); return *this;}
00078         bool feq(const dVector &other, float epsilon=0.00001);
00079 
00080         void get_rot(float m[16],dVector up); // legacy func 
00081 private:
00082 };
00083 
00084 dVector operator-(dVector rhs);
00085 ostream &operator<<(ostream &os, dVector const &om);
00086 istream &operator>>(istream &is, dVector &om);
00087 
00089 
00090 class dColour
00091 {
00092 public:
00093        float r,g,b,a;
00094        dColour() {r=g=b=0; a=1;}
00095        dColour(float R, float G, float B, float A=1) {r=R; g=G; b=B; a=A;}
00096        dColour(dColour const &c) {*this=c;}
00097        float *arr() {return &r;}
00098 
00099        dColour &operator=(dColour const &rhs);
00100        dColour operator+(dColour const &rhs) const;
00101        dColour operator-(dColour const &rhs) const;
00102        dColour operator*(dColour const &rhs) const;
00103        dColour operator/(dColour const &rhs) const;
00104        dColour operator+(float rhs) const;
00105        dColour operator-(float rhs) const;
00106        dColour operator*(float rhs) const;
00107        dColour operator/(float rhs) const;
00108        dColour &operator+=(dColour const &rhs);
00109        dColour &operator-=(dColour const &rhs);
00110        dColour &operator*=(float rhs);
00111        dColour &operator/=(float rhs);
00112 
00113        void clamp()
00114        {
00115            if (r<0) r=0; if (g<0) g=0; if (b<0) b=0; if (a<0) a=0;
00116            if (r>1) r=1; if (g>1) g=1; if (b>1) b=1; if (a>1) a=1;
00117        }
00118 
00119 private:
00120 };
00121 
00122 ostream &operator<<(ostream &os, dColour const &om);
00123 
00125 
00126 class dVertex
00127 {
00128 public:
00129     dVertex() {}
00130     dVertex(dVector p, dVector n, float S=0, float T=0) {point=p; normal=n; s=S; t=T;}
00131     dVertex(dVector p, dVector n, dColour c, float S=0, float T=0) { point=p; normal=n; col=c; s=S; t=T;}
00132     dVertex(dVertex const &rhs) {*this=rhs;}
00133     dVertex const &operator=(dVertex const &rhs);
00134     void homog() {point.homog(); normal.homog();}
00135     friend ostream&operator<<(ostream &os, dVertex const &v);
00136 
00137     dVector point;
00138     dVector normal;
00139     dColour col;
00140     float s,t;
00141 private:
00142 };
00143 
00144 class dMatrix
00145 {
00146 public:
00147     dMatrix() {init();}
00148     dMatrix(const dMatrix &other) { (*this)=other; }
00149     
00150     dMatrix(float m00, float m10, float m20, float m30, 
00151             float m01, float m11, float m21, float m31, 
00152             float m02, float m12, float m22, float m32, 
00153             float m03, float m13, float m23, float m33);
00154             
00155     void init();
00156     void zero();
00157     float *arr() {return &m[0][0];}
00158     const dMatrix &operator=(dMatrix const &rhs);
00159     dMatrix operator+(dMatrix const &rhs) const;
00160     dMatrix operator-(dMatrix const &rhs) const;
00161     dMatrix operator*(dMatrix const &rhs) const;
00162     dMatrix operator/(dMatrix const &rhs) const;
00163     dMatrix &operator+=(dMatrix const &rhs);
00164     dMatrix &operator-=(dMatrix const &rhs);
00165     dMatrix &operator*=(dMatrix const &rhs);
00166     dMatrix &operator/=(dMatrix const &rhs);
00167     dMatrix operator+(float rhs) const;
00168     dMatrix operator-(float rhs) const;
00169     dMatrix operator*(float rhs) const;
00170     dMatrix operator/(float rhs) const;
00171     dMatrix &translate(dVector &tr);
00172     dMatrix &translate(float x, float y, float z);
00173     void    settranslate(dVector &tr);
00174     dVector gettranslate() const;
00175     dMatrix &rotx(float a);
00176     dMatrix &roty(float a);
00177     dMatrix &rotz(float a);
00178     dMatrix &rotxyz(float x,float y,float z);
00179     dMatrix &scale(float x, float y, float z);
00180     dVector transform_no_trans(dVector const &p) const;
00181     dVector transform(dVector const &p) const;
00182     dVector transform_persp(dVector const &p) const;
00183     dVertex transform(dVertex const &p) const;
00184     void    transpose();
00185     dMatrix inverse() const;
00186     float   determinant() const;
00187     dVector get_hori_i() {return dVector(m[0][0],m[1][0],m[2][0]);}
00188     dVector get_hori_j() {return dVector(m[0][1],m[1][1],m[2][1]);}
00189     dVector get_hori_k() {return dVector(m[0][2],m[1][2],m[2][2]);}
00190     dVector get_vert_i() {return dVector(m[0][0],m[0][1],m[0][2]);}
00191     dVector get_vert_j() {return dVector(m[1][0],m[1][1],m[1][2]);}
00192     dVector get_vert_k() {return dVector(m[2][0],m[2][1],m[2][2]);}
00193     void    remove_scale();
00194     void    extract_euler(float &x, float &y, float &z) const;
00195     void    aim(dVector v, dVector up=dVector(0,0,1));
00196     void    blend(dMatrix other, float amount);
00197     
00198     void load_glmatrix(float glm[16]);
00199     void load_dMatrix(float glm[16]);
00200     
00201     friend ostream &operator<<(ostream &os, dMatrix const &om);
00202 
00203     float m[4][4];
00204 };
00205 
00206 ostream &operator<<(ostream &os, dMatrix const &om);
00207 
00208 class dBoundingBox
00209 {
00210 public:
00211     dBoundingBox() : m_Empty(true) {}
00212     dBoundingBox(const dVector &cmin, const dVector &cmax) : min(cmin), max(cmax) {}
00213     virtual ~dBoundingBox() {}
00214     
00215     bool empty() { return m_Empty; }
00216     void expand(dVector v);
00217     void expand(dBoundingBox v);
00218     void expandby(float a);
00219     bool inside(dVector point) const;
00220     
00221     dVector min;
00222     dVector max;
00223     
00224 private:
00225     bool m_Empty;
00226 };
00227 
00228 class dQuat
00229 {
00230 public:
00231     dQuat():x(0),y(0),z(0),w(1){}
00232     dQuat(float x, float y, float z, float w):x(x),y(y),z(z),w(w){}
00233     dQuat(const dQuat& q):x(q.x),y(q.y),z(q.z),w(q.w){}
00234     
00235     // conversions
00236     dMatrix toMatrix() const;
00237     
00238     // operations
00239     dQuat conjugate() const;
00240     void setaxisangle(dVector axis, float angle);
00241     
00242     // make multiply look like multiply
00243     dQuat operator* (const dQuat&qR) const;
00244     
00245     void renorm();
00246     float *arr() {return &x;}
00247     
00248     // the data
00249     float x,y,z,w;
00250 };
00251 
00253 namespace dGeometry
00254 {
00255 
00256 float pointlinedist(const dVector &p, const dVector &start, const dVector &end);
00257 
00258 };
00259 
00261 
00262 }
00263 
00264 #endif // DADA

Generated on Tue Sep 4 23:22:18 2007 for The Fluxus Renderer (libfluxus) by  doxygen 1.5.1