proj home

Files   Classes   Functions   Hierarchy  

graphmisc.h

Go to the documentation of this file.
00001 #ifndef GRAPHMISC_H
00002 #define GRAPHMISC_H
00003 
00004 #include <cassert>
00005 #include <string>
00006 #include <sstream>
00007 #include <iostream>
00008 #include <vector>
00009 using namespace std;
00010 
00011 #include <GL/glut.h>
00012 
00013 #include <commandline.h>
00014 #include <mathlib.h>
00015 #include <print.h>
00016 #include <gobj.h>
00017 
00018 
00022 class OpenGLinitialisation
00023 {
00024 public:
00025 
00026   static GLfloat light_ambient[];
00027   static GLfloat light_diffuse[];
00028   static GLfloat light_specular[];
00029   static GLfloat light_position[];
00030 
00031   static GLfloat mat_ambient[];
00032   static GLfloat mat_diffuse[];
00033   static GLfloat mat_specular[];
00034   static GLfloat mat_shininess[];
00035 
00037   OpenGLinitialisation();
00038 
00040   void light0Init();
00042   void materialInit();
00043 
00044 };
00045 
00049 class myRotate : public gobj
00050 {
00051 public:
00052 
00053   point3<double> axis;
00054 
00055   myRotate(point3<double> const & axis_)
00056     : axis(axis_) {}
00057 
00058   static point3<double> const zaxis;
00059 
00060   void draw()
00061   {
00062     GOBJDEBUGCODE
00063     if (axis!=zaxis)
00064     {
00065       assert(axis!=point3<double>(0,0,0));
00066 
00067       point3<double> N;
00068       crossproduct::evalxyz(N,zaxis,axis);
00069 
00070       double angle = acos( axis.dot(zaxis) / axis.distance() );
00071       angle *= 180.0/PI;
00072       glRotated(angle,N.x,N.y,N.z);
00073     }
00074   }
00075 
00076 };
00077     
00078 
00079 
00086 class myglPushMatrixMode
00087 {
00088 public:
00089 
00091   GLint mode;
00092 
00094   myglPushMatrixMode()
00095     { glGetIntegerv(GL_MATRIX_MODE, &mode); }
00096 
00098   ~myglPushMatrixMode()
00099     { glMatrixMode(mode); }
00100 };
00101 
00108 class myglPushMatrix
00109 {
00110 public:
00111 
00113   myglPushMatrix()
00114     { glPushMatrix(); }
00115 
00117   ~myglPushMatrix()
00118     { glPopMatrix(); }
00119 };
00120 
00127 class myglPushAttrib
00128 {
00129   myglPushAttrib() { assert(false); }
00130 public:
00131 
00133   myglPushAttrib( GLbitfield mask )
00134     { glPushAttrib(mask); } 
00135 
00137   ~myglPushAttrib()
00138     { glPopAttrib(); }
00139 };
00140 
00147 class myglCapability
00148 {
00149   myglCapability() { assert(false); }
00150 public:
00151 
00152   GLenum capability;
00153 
00155   myglCapability(GLenum capability_)
00156     : capability(capability_) 
00157     { glEnable(capability); }
00158 
00160   ~myglCapability()
00161     { glDisable(capability); }
00162 };
00163 
00164 class myglMode
00165 {
00166   myglMode() { assert(false); }
00167 public:
00168 
00169   myglMode(GLenum mode)
00170    { glBegin(mode); }
00171 
00172   ~myglMode()
00173     { glEnd(); }
00174 };
00175   
00176 
00177   
00178 
00185 class myLightingTurnOff
00186 {
00187 public:
00188 
00190   myLightingTurnOff()
00191   {     
00192     glPushAttrib(GL_CURRENT_BIT);
00193     glPushAttrib(GL_LIGHTING_BIT);
00194     glDisable(GL_LIGHTING);
00195   }
00196 
00198   ~myLightingTurnOff()
00199     { glPopAttrib(); glPopAttrib(); }
00200 };
00201 
00202 
00203 // Write a vector of triangles. 
00204 //void writeTriangles(vector< point3<double> > const & v);
00205 
00207 void glerrordisplay();
00208 
00210 void axes(doublec length);
00211 
00215 class myaxes : public gobj
00216 {
00217 public:
00218 
00220   point3<float> xaxiscolor;
00222   point3<float> yaxiscolor;
00224   point3<float> zaxiscolor;
00225 
00227   double length;
00228 
00230   myaxes(doublec length_);
00231 
00232   void draw();
00233 
00234 };
00235 
00237 void wirerectangle(floatc x, floatc y, floatc z);
00238 
00242 class camera
00243 {
00244 public:
00245 
00249   static void lookatxz
00250   (
00251     commandline & cmd,
00252     doublec camerax_=0.0, 
00253     doublec cameraz_=2.0 
00254   ); 
00255 
00256   static void lookat
00257   (
00258     commandline & cmd,
00259     doublec camerax_=0.0, 
00260     doublec cameray_=0.0, 
00261     doublec cameraz_=2.0 
00262   ); 
00263 };
00264 
00265 
00266 
00267 
00268 
00276 template< int period = 500 >
00277 class framerate
00278 {
00279   int frame;
00280   int time;
00281   int timebase;
00282   float fps;
00283   string sr;
00284 public:
00285 
00287   float xpos;
00289   float ypos;
00290 
00293   framerate(float xpos_=0.1, float ypos_=0.05) :
00294     frame(0), time(0), timebase(0), fps(0.0), xpos(xpos_), ypos(ypos_) {}
00295 
00297   void update()
00298   {
00299     // glutGet(GLUT_ELAPSED_TIME) returns the time in millisecond units. 
00300     //   1unit=1ms
00301     time = glutGet(GLUT_ELAPSED_TIME);
00302 
00303     // Has a period passed? 
00304     if (time - timebase > period ) 
00305     {
00306       fps = frame*((GLfloat)period)/(time-timebase);
00307       timebase = time;    
00308       frame = 0;
00309 
00310       stringstream ss;
00311       ss << fps;
00312       sr = ss.str();
00313       sr += "fps";
00314     }
00315   }
00316 
00319   void display()
00320   {
00321     //assuming GL_MODELVIEW
00322     glPushMatrix(); //save
00323     glLoadIdentity(); //clear
00324 
00325     myLightingTurnOff temp2;
00326 
00327     glMatrixMode(GL_PROJECTION);
00328     glPushMatrix(); //save
00329     glLoadIdentity(); //clear
00330     //gluOrtho2D(-1.0, 1.0, -1.0, 1.0);
00331     gluOrtho2D(0.0, 1.0, 0.0, 1.0);
00332     glMatrixMode(GL_MODELVIEW);
00333 
00334     ++frame;
00335 
00336     glColor3f(0.0,1.0,0.0);
00337 
00338 //cout << SHOW(xpos) << " " << SHOW(ypos) << endl;
00339 
00340     glRasterPos2f(xpos,ypos);
00341     //glRasterPos2f(0.8,0.2);
00342 
00343 //cout << SHOW(sr) << endl;
00344     int len = sr.length();
00345     for (int i = 0; i < len; ++i)
00346       glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, sr[i]);
00347 
00348     glMatrixMode(GL_PROJECTION);
00349     glPopMatrix(); //restore
00350     glMatrixMode(GL_MODELVIEW);
00351     glPopMatrix(); //restore
00352   }
00353 };
00354 
00355 
00356 
00357 class gltextmsg
00358 {
00359   gltextmsg() { assert(false); }
00360 public:
00361   
00362   string msg;
00363   float pos[2];
00364   float col[3];
00365 
00366   gltextmsg
00367   ( 
00368     string const & msg_,
00369     floatc x, 
00370     floatc y,
00371     floatc c0,
00372     floatc c1,
00373     floatc c2
00374   )
00375     : msg(msg_)
00376   {
00377     pos[0] = x;
00378     pos[1] = y;
00379     col[0] = c0;
00380     col[1] = c1;
00381     col[2] = c2;
00382   }
00383 
00384   template < typename T >
00385   void updatevalue
00386   ( 
00387     T const & v,
00388     string const & post = string("")
00389   )
00390   {  
00391     stringstream ss;
00392     ss << v;
00393     msg = ss.str();
00394     msg += post;
00395   }
00396 
00397   template < typename T >
00398   void updatevalue
00399   ( 
00400     string const & pre,
00401     T const & v,
00402     string const & post = string("")
00403   )
00404   {  
00405     stringstream ss;
00406     ss << v;
00407     msg = pre;
00408     msg += ss.str();
00409     msg += post;
00410   }
00411   
00412 
00413   void display() const
00414   {
00415     myglPushMatrix temp;
00416 
00417     myLightingTurnOff temp2;
00418 
00419     glLoadIdentity();
00420     gluOrtho2D(-1.0, 1.0, -1.0, 1.0);
00421 
00422     glColor3f(col[0],col[1],col[2]);
00423 
00424     glRasterPos2f(pos[0],pos[1]);
00425 
00426     int len = msg.length();
00427     for (int i = 0; i < len; ++i)
00428       glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, msg[i]);
00429   } 
00430 };
00431 
00435 class gobjMyTranslateDraw : public gobj
00436 {
00437   gobjMyTranslateDraw() { assert(false); }
00438 public:
00439 
00440   gobj* target;
00441   bool clean;
00442 
00443   point3<double> shift;
00444 
00446   gobjMyTranslateDraw
00447   (
00448     gobj * target_, 
00449     boolc clean_,
00450     point3<double> const & shift_
00451   )
00452     : target(target_), clean(clean_), shift(shift_) {}
00453 
00454   ~gobjMyTranslateDraw()
00455     { if (clean) delete target; }
00456 
00457 
00458   void draw()
00459   {
00460     GOBJDEBUGCODE
00461     assert(target!=0);
00462 
00463     glPushMatrix();
00464     glTranslatef(shift.x,shift.y,shift.z);
00465     target->draw();
00466     glPopMatrix();
00467   }
00468 };
00469 
00470 class gobjQuadric : public gobj
00471 {
00472 public:
00473 
00474   GLUquadric * quadric;
00475 
00476   // Possible states
00477   float radius;
00478   int slices;
00479   int loops;
00480 
00481   gobjQuadric()
00482     : quadric(gluNewQuadric()), radius(0.2), slices(10), loops(3) {}
00483 
00484   void detailincrease()
00485     { slices *= 2; loops *=2; }
00486 
00487   ~gobjQuadric()
00488     { gluDeleteQuadric(quadric); }
00489 
00490   void draw()
00491     { GOBJDEBUGCODE }
00492 };
00493 
00494 class gobjMyDiscDraw: public gobj
00495 {
00496 public:
00497 
00498   gobjQuadric * d;
00499 
00500   float x;
00501   float y;
00502 
00503   gobjMyDiscDraw()
00504     : d(0), x(0.0), y(0.0) {}
00505 
00506   gobjMyDiscDraw
00507   (
00508     floatc x_,
00509     floatc y_,
00510     gobjQuadric * d_
00511   )
00512     : d(d_), x(x_), y(y_) {}
00513 
00514   gobjMyDiscDraw
00515   (
00516     point2<double> const & x,
00517     gobjQuadric * d_
00518   )
00519     : d(d_), x(x.x), y(x.y) {}
00520 
00521   gobjMyDiscDraw
00522   (
00523     point2<float> const & x,
00524     gobjQuadric * d_
00525   )
00526     : d(d_), x(x.x), y(x.y) {}
00527 
00528   void draw()
00529   {
00530     GOBJDEBUGCODE
00531     assert(d!=0);
00532 
00533     glPushMatrix();
00534     glTranslatef(x,y,0.0);
00535     gluDisk(d->quadric, 0.0, d->radius, d->slices, d->loops);
00536     glPopMatrix();
00537   }
00538 };
00539  
00559 class gobjMySphereDraw : public gobj
00560 {
00561 public:
00562   
00563   gobjQuadric * quadric;
00564 
00565   float x;
00566   float y;
00567   float z;
00568 
00569   gobjMySphereDraw()
00570     : quadric(0), x(0.0), y(0.0), z(0.0) {}
00571 
00572   gobjMySphereDraw
00573   (
00574     floatc x_,
00575     floatc y_,
00576     floatc z_,
00577     gobjQuadric * quadric_
00578   )
00579     : quadric(quadric_), x(x_), y(y_), z(z_) {}
00580 
00581   gobjMySphereDraw
00582   (
00583     doublec x_,
00584     doublec y_,
00585     doublec z_,
00586     gobjQuadric * quadric_
00587   )
00588     : quadric(quadric_), x(x_), y(y_), z(z_) {}
00589 
00590   gobjMySphereDraw
00591   (
00592     point3<double> const & p,
00593     gobjQuadric * quadric_
00594   )
00595     : quadric(quadric_), x(p.x), y(p.y), z(p.z) {}
00596 
00597   gobjMySphereDraw
00598   (
00599     point3<float> const & p,
00600     gobjQuadric * quadric_
00601   )
00602     : quadric(quadric_), x(p.x), y(p.y), z(p.z) {}
00603 
00604   gobjMySphereDraw
00605   (
00606     point2<double> const & x,
00607     gobjQuadric * quadric_
00608   )
00609     : quadric(quadric_), x(x.x), y(x.y), z(0.0) {}
00610 
00611   // Unhappy with reference pass.
00612   gobjMySphereDraw
00613   (
00614     point2<double> const & x,
00615     gobjQuadric& quadricref
00616   )
00617     : quadric(&quadricref), x(x.x), y(x.y), z(0.0) {}
00618 
00619   gobjMySphereDraw
00620   (
00621     point2<float> const & x,
00622     gobjQuadric * quadric_
00623   )
00624     : quadric(quadric_), x(x.x), y(x.y), z(0.0) {}
00625 
00626   void draw()
00627   {
00628     GOBJDEBUGCODE
00629     assert(quadric!=0);
00630 
00631     glPushMatrix();
00632     glTranslatef(x,y,z);
00633     gluSphere(quadric->quadric,quadric->radius,quadric->slices,quadric->loops);
00634     glPopMatrix();
00635   }
00636 
00637 };
00638 
00645 class gobjMyCircle : public gobj
00646 {
00647 public:
00648 
00650   uintc N;
00651 
00653   float * const ptx;
00655   float * const pty;
00656 
00661   gobjMyCircle
00662   (
00663     doublec theta0, 
00664     doublec theta1, 
00665     doublec xaxislength,
00666     doublec yaxislength,
00667     uintc N_=360
00668   );
00669 
00671   gobjMyCircle
00672   (
00673     doublec theta0, 
00674     doublec theta1, 
00675     doublec radius=1.0,
00676     uintc N_=360
00677   );
00678 
00680   gobjMyCircle( uintc N_=360 );
00681 
00683   void draw() { GOBJDEBUGCODE }
00684 
00686   ~gobjMyCircle();
00687 
00688 };
00689 
00693 class gobjMyCircleDraw : public gobj
00694 {
00695   static point3<double> const zaxis;
00696 public:
00697 
00699   double scalex;
00701   double scaley;
00702 
00704   point3<double> center;
00705 
00707   point3<double> axis;
00708 
00710   gobjMyCircle const & cir;
00711 
00712   void radiusset(doublec radius)
00713     { scalex=radius; scaley=radius; }
00714   
00716   template<typename T>
00717   gobjMyCircleDraw
00718   (
00719     point3<T> const & center_,
00720     gobjMyCircle const & cir_
00721   )
00722     : scalex(1.0), scaley(1.0), center(center_.x,center_.y,center_.z), 
00723       axis(zaxis), cir(cir_) {}
00724 
00726   template<typename T>
00727   gobjMyCircleDraw
00728   (
00729     point2<T> const & center_,
00730     gobjMyCircle const & cir_
00731   )
00732     : scalex(1.0), scaley(1.0), center(center_.x,center_.y,0.0), 
00733       axis(zaxis), cir(cir_) {}
00734 
00735 
00737   template<typename T>
00738   gobjMyCircleDraw
00739   (
00740     doublec radius,
00741     point3<T> const & center_,
00742     gobjMyCircle const & cir_
00743   )
00744     : scalex(radius), scaley(radius), center(center_.x,center_.y,center_.z), 
00745       axis(zaxis), cir(cir_) {}
00746 
00749   template<typename T>
00750   gobjMyCircleDraw
00751   (
00752     doublec radius,
00753     point3<T> const & center_,
00754     point3<T> const & axis_,
00755     gobjMyCircle const & cir_
00756   )
00757     : scalex(radius), scaley(radius), center(center_), 
00758       axis(axis_), cir(cir_) {}
00759 
00761   void draw();
00762 
00763 };
00764 
00765 
00766 
00767 
00768 
00771 class gobjMyBitmapCharacter : public gobj
00772 {
00773 public:
00774 
00776   string name;
00777 
00779   point3<float> x;
00780 
00782   void * font;
00783 
00795   gobjMyBitmapCharacter
00796   (
00797     string const & name_,
00798     point3<float> const & x_,  
00799     void* font_=GLUT_BITMAP_HELVETICA_10
00800   )
00801     : name(name_), x(x_), font(font_) {};
00802 
00804   gobjMyBitmapCharacter
00805   (
00806     string const & name_,
00807     point3<double> const & x_,  
00808     void* font_=GLUT_BITMAP_HELVETICA_10
00809   )
00810     : name(name_), x( point3<float>(x_.x,x_.y,x_.z) ), font(font_) {};
00811 
00812   void draw();
00813 
00814 };
00815 
00819 class gobjMyArrow : public gobj
00820 {
00821 public:
00822 
00824   point3<double> p0;
00826   point3<double> p1;
00828   doublec headlength;
00830   doublec headwidth;
00831   double delta;
00832 
00833   gobjMyArrow
00834   (
00835     point3<double> const & p0_,
00836     point3<double> const & p1_,
00837     doublec headlength_,
00838     doublec headwidth_,
00839     doublec delta_
00840   );
00841 
00842 /*
00843   gobjMyArrow
00844   (
00845     point2<double> const & _p0,
00846     point2<double> const & _p1,
00847     doublec _delta
00848   );
00849 */
00850 
00851   void draw();
00852 
00853 };
00854 
00855 class gobjMyDiskDraw : public gobj
00856 {
00857 public:
00858   
00859   gobjQuadric * d;
00860 
00861   float x;
00862   float y;
00863   float z;
00864 
00865   gobjMyDiskDraw()
00866     : d(0), x(0.0), y(0.0), z(0.0) {}
00867 
00868   gobjMyDiskDraw
00869   (
00870     floatc x_,
00871     floatc y_,
00872     floatc z_,
00873     gobjQuadric * d_
00874   )
00875     : d(d_), x(x_), y(y_), z(z_) {}
00876 
00877   gobjMyDiskDraw
00878   (
00879     point3<double> x,
00880     gobjQuadric * d_
00881   )
00882     : d(d_), x(x.x), y(x.y), z(x.z) {}
00883 
00884   gobjMyDiskDraw
00885   (
00886     point3<float> x,
00887     gobjQuadric * d_
00888   )
00889     : d(d_), x(x.x), y(x.y), z(x.z) {}
00890 
00891   void draw()
00892   {
00893     GOBJDEBUGCODE
00894     assert(d!=0);
00895 
00896     glPushMatrix();
00897     glTranslatef(x,y,z);
00898     gluDisk(d->quadric,0.0,d->radius,d->slices,d->loops);
00899     glPopMatrix();
00900   }
00901 
00902 };
00903 
00904 
00910 class colorfunction
00911 {
00912 public:
00913 
00915   template< class T >
00916   point3<T>& operator () (point3<T>& p, T const v) const;
00918   template< class T >
00919   void operator () (T& x, T& y, T& z, T const v) const;
00920 
00921 };
00922 
00923 
00935 class gridsquare : public gobjContainer
00936 {
00937 public:
00938 
00940   gridsquare
00941   (
00942     GLint factor,
00943     GLushort pattern,
00944     uintc xcolumns,
00945     uintc ycolumns,
00946     doublec xmax,
00947     doublec ymax
00948   );
00949 
00950 
00951 };
00952 
00956 class graphmisc
00957 {
00958 public:
00959 
00961   static void colornormalize(point3<double> & p1, point3<uint> const & p2);
00962 
00964   static void colornormalize(point3<double> & p1, commandline & cmd, stringc tag );
00965 
00966 };
00967 
00968 
00969 //----------------------------------------------------------
00970 // Implementation
00971 
00972 template< class T >
00973 point3<T>& colorfunction::operator () (point3<T>& p, T const v) const
00974 {
00975   assert(v>=0.0);
00976   assert(v<=1.0);
00977 
00978   p.x =  p.z = 0.0;
00979   p.y = 1.0;
00980 
00981   if (v>=0.75)
00982     p.x = 1.0;  
00983   else
00984     if (v>0.5)
00985       p.x = (v-0.5)/0.25;
00986 
00987   if (v<=0.25)
00988     p.z = 1.0;
00989   else
00990     if (v<0.5)
00991       p.z = 1.0 - (v-0.25)/0.25;
00992 
00993   if (v>0.75)
00994     p.y = 1.0 - (v-0.75)/0.25;
00995   else
00996     if (v<0.25)
00997       p.y = v/0.25;
00998 
00999   return p;
01000 }
01001 
01002 template< class T >
01003 void colorfunction::operator () (T& x, T& y, T& z, T const v) const
01004 {
01005   assert(v>=0.0);
01006   assert(v<=1.0);
01007 
01008   x = z = 0.0;
01009   y = 1.0;
01010 
01011   if (v>=0.75)
01012     x = 1.0;  
01013   else
01014     if (v>0.5)
01015       x = (v-0.5)/0.25;
01016 
01017   if (v<=0.25)
01018     z = 1.0;
01019   else
01020     if (v<0.5)
01021       z = 1.0 - (v-0.25)/0.25;
01022 
01023   if (v>0.75)
01024     y = 1.0 - (v-0.75)/0.25;
01025   else
01026     if (v<0.25)
01027       y = v/0.25;
01028 }
01029 
01030 
01031 
01032 #endif
01033 
01034 
01035 

Generated on Fri Mar 4 00:49:27 2011 for Chelton Evans Source by  doxygen 1.5.8