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         dVector reflect(dVector const &rhs) const;
00072         float dot(dVector const &rhs) const;
00073         float dist(dVector const &rhs) const;
00074         float distsq(dVector const &rhs) const;
00075         float mag();
00076         void get_euler(float &rx, float &ry, float &rz) const;
00077         void homog() {if (w && w!=1.0) {x/=w; y/=w; z/=w; w=1;}}
00078         dVector &normalise() {*this/=mag(); return *this;}
00079         bool feq(const dVector &other, float epsilon=0.00001);
00080 
00081         void get_rot(float m[16],dVector up); // legacy func 
00082 private:
00083 };
00084 
00085 dVector operator-(dVector rhs);
00086 ostream &operator<<(ostream &os, dVector const &om);
00087 istream &operator>>(istream &is, dVector &om);
00088 
00090 
00091 class dColour
00092 {
00093 public:
00094        float r,g,b,a;
00095        dColour() {r=g=b=0; a=1;}
00096        dColour(float R, float G, float B, float A=1) {r=R; g=G; b=B; a=A;}
00097        dColour(dColour const &c) {*this=c;}
00098        float *arr() {return &r;}
00099 
00100        dColour &operator=(dColour const &rhs);
00101        dColour operator+(dColour const &rhs) const;
00102        dColour operator-(dColour const &rhs) const;
00103        dColour operator*(dColour const &rhs) const;
00104        dColour operator/(dColour const &rhs) const;
00105        dColour operator+(float rhs) const;
00106        dColour operator-(float rhs) const;
00107        dColour operator*(float rhs) const;
00108        dColour operator/(float rhs) const;
00109        dColour &operator+=(dColour const &rhs);
00110        dColour &operator-=(dColour const &rhs);
00111        dColour &operator*=(float rhs);
00112        dColour &operator/=(float rhs);
00113 
00114        void clamp()
00115        {
00116            if (r<0) r=0; if (g<0) g=0; if (b<0) b=0; if (a<0) a=0;
00117            if (r>1) r=1; if (g>1) g=1; if (b>1) b=1; if (a>1) a=1;
00118        }
00119 
00120 private:
00121 };
00122 
00123 ostream &operator<<(ostream &os, dColour const &om);
00124 
00126 
00127 class dVertex
00128 {
00129 public:
00130     dVertex() {}
00131     dVertex(dVector p, dVector n, float S=0, float T=0) {point=p; normal=n; s=S; t=T;}
00132     dVertex(dVector p, dVector n, dColour c, float S=0, float T=0) { point=p; normal=n; col=c; s=S; t=T;}
00133     dVertex(dVertex const &rhs) {*this=rhs;}
00134     dVertex const &operator=(dVertex const &rhs);
00135     void homog() {point.homog(); normal.homog();}
00136     friend ostream&operator<<(ostream &os, dVertex const &v);
00137 
00138     dVector point;
00139     dVector normal;
00140     dColour col;
00141     float s,t;
00142 private:
00143 };
00144 
00145 class dMatrix
00146 {
00147 public:
00148     dMatrix() {init();}
00149     dMatrix(const dMatrix &other) { (*this)=other; }
00150     
00151     dMatrix(float m00, float m10, float m20, float m30, 
00152             float m01, float m11, float m21, float m31, 
00153             float m02, float m12, float m22, float m32, 
00154             float m03, float m13, float m23, float m33);
00155             
00156     void init();
00157     void zero();
00158     float *arr() {return &m[0][0];}
00159     const dMatrix &operator=(dMatrix const &rhs);
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) const;
00164     dMatrix &operator+=(dMatrix const &rhs);
00165     dMatrix &operator-=(dMatrix const &rhs);
00166     dMatrix &operator*=(dMatrix const &rhs);
00167     dMatrix &operator/=(dMatrix const &rhs);
00168     dMatrix operator+(float rhs) const;
00169     dMatrix operator-(float rhs) const;
00170     dMatrix operator*(float rhs) const;
00171     dMatrix operator/(float rhs) const;
00172     dMatrix &translate(dVector &tr);
00173     dMatrix &translate(float x, float y, float z);
00174     void    settranslate(dVector &tr);
00175     dVector gettranslate() const;
00176     dMatrix &rotx(float a);
00177     dMatrix &roty(float a);
00178     dMatrix &rotz(float a);
00179     dMatrix &rotxyz(float x,float y,float z);
00180     dMatrix &scale(float x, float y, float z);
00181     dVector transform_no_trans(dVector const &p) const;
00182     dVector transform(dVector const &p) const;
00183     dVector transform_persp(dVector const &p) const;
00184     dVertex transform(dVertex const &p) const;
00185     void    transpose();
00186     dMatrix inverse() const;
00187     float   determinant() const;
00188     dVector get_hori_i() {return dVector(m[0][0],m[1][0],m[2][0]);}
00189     dVector get_hori_j() {return dVector(m[0][1],m[1][1],m[2][1]);}
00190     dVector get_hori_k() {return dVector(m[0][2],m[1][2],m[2][2]);}
00191     dVector get_vert_i() {return dVector(m[0][0],m[0][1],m[0][2]);}
00192     dVector get_vert_j() {return dVector(m[1][0],m[1][1],m[1][2]);}
00193     dVector get_vert_k() {return dVector(m[2][0],m[2][1],m[2][2]);}
00194     void    remove_scale();
00195     void    extract_euler(float &x, float &y, float &z) const;
00196     void    aim(dVector v, dVector up=dVector(0,0,1));
00197     void    blend(dMatrix other, float amount);
00198     
00199     void load_glmatrix(float glm[16]);
00200     void load_dMatrix(float glm[16]);
00201     
00202     friend ostream &operator<<(ostream &os, dMatrix const &om);
00203 
00204     float m[4][4];
00205 };
00206 
00207 ostream &operator<<(ostream &os, dMatrix const &om);
00208 
00209 class dBoundingBox
00210 {
00211 public:
00212     dBoundingBox() : m_Empty(true) {}
00213     dBoundingBox(const dVector &cmin, const dVector &cmax) : min(cmin), max(cmax) {}
00214     virtual ~dBoundingBox() {}
00215     
00216     bool empty() { return m_Empty; }
00217     void expand(dVector v);
00218     void expand(dBoundingBox v);
00219     void expandby(float a);
00220     bool inside(dVector point) const;
00221     
00222     dVector min;
00223     dVector max;
00224     
00225 private:
00226     bool m_Empty;
00227 };
00228 
00229 class dQuat
00230 {
00231 public:
00232     dQuat():x(0),y(0),z(0),w(1){}
00233     dQuat(float x, float y, float z, float w):x(x),y(y),z(z),w(w){}
00234     dQuat(const dQuat& q):x(q.x),y(q.y),z(q.z),w(q.w){}
00235     
00236     // conversions
00237     dMatrix toMatrix() const;
00238     
00239     // operations
00240     dQuat conjugate() const;
00241     void setaxisangle(dVector axis, float angle);
00242     
00243     // make multiply look like multiply
00244     dQuat operator* (const dQuat&qR) const;
00245     
00246     void renorm();
00247     float *arr() {return &x;}
00248     
00249     // the data
00250     float x,y,z,w;
00251 };
00252 
00254 namespace dGeometry
00255 {
00256 
00257 float pointlinedist(const dVector &p, const dVector &start, const dVector &end);
00258 
00259 };
00260 
00262 
00263 }
00264 
00265 #endif // DADA

Generated on Mon Feb 11 06:54:25 2008 for The Fluxus Renderer (libfluxus) by  doxygen 1.5.1