proj home

Files   Classes   Functions   Hierarchy  

graphmisc.cpp

Go to the documentation of this file.
00001 #include <graphmisc.h>
00002 #include <OpenGLtemplated.h>
00003 
00004 
00005 void camera::lookatxz
00006 (
00007   commandline & cmd,
00008   doublec camerax_, 
00009   doublec cameraz_ 
00010 )
00011 {
00012   lookat(cmd,camerax_,0.0,cameraz_);
00013 }
00014 
00015 
00016 void camera::lookat
00017 (
00018   commandline & cmd,
00019   doublec camerax_, 
00020   doublec cameray_, 
00021   doublec cameraz_ 
00022 )
00023 {
00024   double camerax(camerax_);
00025   double cameray(cameray_);
00026   double cameraz(cameraz_);
00027   
00028   cmd.mapvar(camerax,"camerax");
00029   cmd.mapvar(cameray,"cameray");
00030   cmd.mapvar(cameraz,"cameraz");
00031 
00032   glMatrixMode(GL_MODELVIEW);
00033 
00034   glLoadIdentity();
00035   gluLookAt
00036   (
00037     camerax, cameray, cameraz,  // Eye  - creates a x: [-2,2] y: [-2,2] screen.
00038     camerax, cameray, 0.0,  // Center
00039     0.0, 1.0, 0.0   // Up
00040   );
00041   glutPostRedisplay();
00042 }
00043 
00044 point3<double> const myRotate::zaxis(0.0,0.0,1.0);
00045 
00046 
00047 void wirerectangle
00048 (
00049   floatc x, 
00050   floatc y, 
00051   floatc z
00052 )
00053 {
00054   myglPushMatrix temp;
00055   glScalef (x,y,z);
00056   glutWireCube (1.0);
00057 }
00058 
00059 myaxes::myaxes(doublec _length)
00060   : length(_length)
00061 {
00062   xaxiscolor=point3<float>(1.0,0.0,0.0);
00063   yaxiscolor=point3<float>(0.0,1.0,0.0);
00064   zaxiscolor=point3<float>(0.0,0.0,1.0);
00065 }
00066 
00067 void myaxes::draw()
00068 {
00069   GOBJDEBUGCODE
00070   myLightingTurnOff temp;
00071 
00072   glBegin(GL_LINES);
00073    
00074   glColor3T<GLfloat>()(xaxiscolor);
00075   glVertex3f(0.0,0.0,0.0);
00076   glVertex3f(length,0.0,0.0);
00077 
00078   glColor3T<GLfloat>()(yaxiscolor);
00079   glVertex3f(0.0,0.0,0.0);
00080   glVertex3f(0.0,length,0.0);
00081   
00082   glColor3T<GLfloat>()(zaxiscolor);
00083   glVertex3f(0.0,0.0,0.0);
00084   glVertex3f(0.0,0.0,length);
00085 
00086   glEnd();
00087 }
00088 
00089 void axes(doublec length)
00090 {
00091   myLightingTurnOff temp;
00092 
00093   glBegin(GL_LINES);
00094    
00095   glColor3f(1.0,0.0,0.0);
00096   glVertex3f(0.0,0.0,0.0);
00097   glVertex3f(length,0.0,0.0);
00098 
00099   glColor3f(0.0,1.0,0.0);
00100   glVertex3f(0.0,0.0,0.0);
00101   glVertex3f(0.0,length,0.0);
00102   
00103   glColor3f(0.0,0.0,1.0);
00104   glVertex3f(0.0,0.0,0.0);
00105   glVertex3f(0.0,0.0,length);
00106 
00107   glEnd();
00108 }
00109 
00110 void glerrordisplay()
00111 {
00112   GLenum errval = glGetError();
00113   if (errval != GL_NO_ERROR)
00114      cout << gluErrorString( errval ) << endl;
00115 }
00116 
00117 
00118 
00119 
00120 GLfloat OpenGLinitialisation::light_ambient[] = 
00121   { 0.0, 0.0, 0.0, 1.0 };
00122 GLfloat OpenGLinitialisation::light_diffuse[] = 
00123   { 1.0, 1.0, 1.0, 1.0 };
00124 GLfloat OpenGLinitialisation::light_specular[] = 
00125   { 1.0, 1.0, 1.0, 1.0 };
00126 GLfloat OpenGLinitialisation::light_position[] = 
00127   { 1.0, 1.0, 1.0, 0.0 };
00128 
00129 GLfloat OpenGLinitialisation::mat_ambient[] = 
00130   { 0.7, 0.7, 0.7, 1.0 };
00131 GLfloat OpenGLinitialisation::mat_diffuse[] = 
00132   { 0.8, 0.8, 0.8, 1.0 };
00133 GLfloat OpenGLinitialisation::mat_specular[] = 
00134   { 1.0, 1.0, 1.0, 1.0 };
00135 GLfloat OpenGLinitialisation::mat_shininess[] = 
00136   { 100.0 };
00137 
00138 void OpenGLinitialisation::light0Init()
00139 {
00140   glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
00141   glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
00142   glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
00143   glLightfv(GL_LIGHT0, GL_POSITION, light_position);
00144 }
00145 
00146 void OpenGLinitialisation::materialInit()
00147 {
00148   glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
00149   glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
00150   glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
00151   glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
00152 }
00153 
00154 OpenGLinitialisation::OpenGLinitialisation()
00155 {
00156   light0Init();
00157   materialInit();
00158 
00159   glEnable(GL_LIGHTING);
00160   glEnable(GL_LIGHT0);
00161   glDepthFunc(GL_LESS);
00162   glEnable(GL_DEPTH_TEST);
00163   //glEnable(GL_NORMALIZE);
00164   glEnable(GL_COLOR_MATERIAL);
00165 }
00166 
00167 
00168 
00169 //  <TODO>  Go through ALL my code
00170 //  and change the gobjMySphereDraw ordering of the
00171 //  constructors :
00172 //
00173 //
00174 //  gobjMySphereDraw
00175 //  (
00176 //    gobjQuadric * _d,
00177 //    floatc _x,
00178 //    floatc _y,
00179 //    floatc _z
00180 //  )
00181 // and 
00182 //  gobjMySphereDraw
00183 //  (
00184 //    gobjQuadric * _d,
00185 //    point3<double> const & x
00186 //  )
00187 // and ...
00188 
00189 //  The motivation is that it is quite possible that the
00190 //    quadric will be and should be modified.
00191 //    In contrast the initial point while it can be modified
00192 //    has its own variables.  The quadric does not do its own memory
00193 //    management, but uses the pointer passed to it.
00194 
00195 //  The memory stategy is also unclear, if I am not going to own 
00196 //    the object why not have d as a reference?  
00197 //
00198 //  PS: gobjMySphereDraw does not want to own the quadratic because it 
00199 //    gets reused.
00200   
00201 void gobjMyBitmapCharacter::draw()
00202 {
00203   GOBJDEBUGCODE
00204   glRasterPos3f(x.x,x.y,x.z);
00205   uintc kmax = name.size();
00206 
00207   for (uint k=0; k<kmax; ++k)
00208     glutBitmapCharacter(font, name[k]);
00209 }
00210 
00211 gobjMyArrow::gobjMyArrow
00212 (
00213   point3<double> const & _p0,
00214   point3<double> const & _p1,
00215   doublec _headlength,
00216   doublec _headwidth,
00217   doublec _delta
00218 )
00219   : p0(_p0), p1(_p1), headlength(_headlength),
00220     headwidth(_headwidth), delta(_delta)
00221 {
00222   assert(delta>=0.0);
00223   assert(delta*2.0<1.0);
00224   // The arrow head length must be <= the arrow length.
00225   double len = (p1-p0).distance();
00226   assert(headlength <= len*(1.0-2.0*delta));
00227 }
00228 
00229 void gobjMyArrow::draw()
00230 {
00231   GOBJDEBUGCODE
00232   point3<double> p01 = p1 - p0;
00233   double len = p01.distance();
00234 
00235   if (len==0.0)
00236     return;
00237 
00238   p01 *= 1.0/len;
00239 
00240   // p2 is the start of the arrow.
00241   point3<double> p2 = p0 + p01*delta;
00242   // p3 is the end of the arrow.
00243   point3<double> p3 = p0 + p01*(1.0-delta);
00244 
00245   point3<double> q(-p01.y,p01.x,p01.z);
00246   //q.normalize();
00247   point3<double> p4 = p3 - p01*headlength;
00248   point3<double> p5 = p4 + q*headwidth;
00249   point3<double> p6 = p4 - q*headwidth;
00250 
00251   glBegin(GL_LINES);
00252   glVertex3f(p2.x,p2.y,p2.z);
00253   glVertex3f(p3.x,p3.y,p3.z);
00254   glEnd();
00255 
00256   glBegin(GL_TRIANGLES);
00257   glVertex3f(p3.x,p3.y,p3.z);
00258   glVertex3f(p5.x,p5.y,p5.z);
00259   glVertex3f(p6.x,p6.y,p6.z);
00260   glVertex3f(p6.x,p6.y,p6.z);
00261   glVertex3f(p5.x,p5.y,p5.z);
00262   glVertex3f(p3.x,p3.y,p3.z);
00263   glEnd();
00264 }
00265 
00266  
00267 gobjMyCircle::gobjMyCircle
00268 (
00269   doublec theta0, 
00270   doublec theta1, 
00271   doublec xaxislength,
00272   doublec yaxislength,
00273   uintc _N
00274 )
00275   : N(_N), ptx(new float[N]),
00276     pty(new float[N])
00277 {
00278   //Just plot the curve 
00279   assert(theta1>theta0);  
00280 
00281   float t=theta0;
00282   float dt=(theta1-theta0)/(N-1);
00283 
00284   for (uint i=0; i<N; ++i)
00285   {
00286     ptx[i] = cos(t)*xaxislength;
00287     pty[i] = sin(t)*yaxislength;
00288     t += dt;
00289   }
00290 }
00291 
00292 gobjMyCircle::gobjMyCircle
00293 (
00294   doublec theta0, 
00295   doublec theta1, 
00296   doublec radius,
00297   uintc _N
00298 )
00299   : N(_N), ptx(new float[N]),
00300     pty(new float[N])
00301 {
00302   assert(radius>0.0);
00303 
00304   float t=theta0;
00305   float thetadiff = theta1-theta0;
00306   if (thetadiff<0.0)
00307     thetadiff += PI*2.0;
00308   float dt=thetadiff/(N-1);
00309 /*
00310 cout << SHOW(theta1-theta0) << " " << SHOW(thetadiff);
00311 cout << " " << SHOW(dt) << endl;
00312 */
00313 
00314 /*
00315   if (dt<0.0)
00316     dt += 2.0*PI;
00317 */
00318 
00319   for (uint i=0; i<N; ++i)
00320   {
00321     ptx[i] = cos(t)*radius;
00322     pty[i] = sin(t)*radius;
00323     t += dt;
00324   }
00325 
00326 /*
00327 cout << SHOW(theta1-theta0) << " " << SHOW(dt) << endl;
00328 cout << SHOW(theta0) << " " << SHOW(theta1) << endl;
00329 cout << SHOW(cos(theta0)) << " " << SHOW(sin(theta0)) << endl;
00330 cout << SHOW(radius) << endl;
00331 cout << "start: (" << ptx[0] << "," << pty[0] << ")" << endl;
00332 cout << "end:   (" << ptx[N-1] << "," << pty[N-1] << ")" << endl;
00333 */
00334 
00335 }
00336 
00337 
00338 gobjMyCircle::gobjMyCircle
00339 (
00340   uintc _N
00341 )
00342   : N(_N), ptx(new float[N]),
00343     pty(new float[N])
00344 {
00345   float t=0.0;
00346   float dt=2.0*PI/(N-1);
00347 
00348   for (uint i=0; i<N; ++i)
00349   {
00350     ptx[i] = cos(t);
00351     pty[i] = sin(t);
00352     t += dt;
00353   }
00354 }
00355 
00356 
00357 gobjMyCircle::~gobjMyCircle()
00358 {
00359   delete[] ptx; 
00360   delete[] pty;
00361 }
00362 
00363 point3<double> const gobjMyCircleDraw::zaxis(0.0,0.0,1.0);
00364 
00365 void gobjMyCircleDraw::draw()
00366 {
00367   GOBJDEBUGCODE
00368   uintc const N(cir.N);
00369   float * const ptx(cir.ptx);
00370   float * const pty(cir.pty);
00371 
00372   glPushMatrix();
00373 
00374   glTranslatef(center.x,center.y,center.z);
00375 
00376   if (axis!=zaxis)
00377   {
00378     assert(axis!=point3<double>(0,0,0));
00379 
00380     point3<double> N;
00381     crossproduct::evalxyz(N,zaxis,axis);
00382 
00383     double angle = acos( axis.dot(zaxis) / axis.distance() );
00384     angle *= 180.0/PI;
00385     glRotated(angle,N.x,N.y,N.z);
00386   }
00387 
00388   //glBegin(GL_LINE_LOOP);
00389   glBegin(GL_LINE);
00390 
00391   if ( (scalex==1.0)&&(scaley==1.0))
00392   {
00393     for (uint i=0; i<N-1; ++i)
00394     {
00395       glVertex2f(ptx[i],pty[i]);
00396       glVertex2f(ptx[i+1],pty[i+1]);
00397     }
00398   }
00399   else
00400   {
00401     for (uint i=0; i<N-1; ++i)
00402     {
00403       glVertex2f(ptx[i]*scalex,pty[i]*scaley);
00404       glVertex2f(ptx[i+1]*scalex,pty[i+1]*scaley);
00405     }
00406   }
00407     //glVertex2f(ptx[i]*radius,pty[i]*radius);
00408 
00409   glEnd();
00410 
00411   glPopMatrix();
00412 }
00413 
00414 
00415 gridsquare::gridsquare
00416 (
00417   GLint factor,
00418   GLushort pattern,
00419   uintc xcolumns,
00420   uintc ycolumns,
00421   doublec xmax,
00422   doublec ymax
00423 )
00424   : gobjContainer(true)
00425 {
00426   if ((xcolumns==0)||(ycolumns==0))
00427     return;
00428 
00429   double x=0.0;
00430   double y=0.0;
00431 
00432   doublec dx = xmax / xcolumns;
00433   doublec dy = ymax / ycolumns;
00434 
00435 //  doublec xmax = dx * xcolumns;
00436 //  doublec ymax = dy * ycolumns;
00437 
00438   push( new gobjglEnable(GL_LINE_STIPPLE));
00439   push( new gobjglLineStipple(factor,pattern) );
00440 
00441   push(new gobjglBegin(GL_LINES));
00442 
00443   for (uint i=0; i<=xcolumns; ++i)
00444   {
00445     x = dx * i;
00446     push(new gobjglVertex2f(x,0.0));
00447     push(new gobjglVertex2f(x,ymax));
00448   }
00449 
00450   for (uint i=0; i<=ycolumns; ++i)
00451   {
00452     y = dy * i;
00453     push(new gobjglVertex2f(0.0,y));
00454     push(new gobjglVertex2f(xmax,y));
00455   }
00456 
00457   push(new gobjglEnd());
00458 
00459   push(new gobjglDisable(GL_LINE_STIPPLE));
00460 }
00461 
00462 void graphmisc::colornormalize
00463 (
00464   point3<double> & p1, 
00465   point3<uint> const & p2
00466 )
00467 {
00468   p1.x = (double)p2.x / 255.0;
00469   p1.y = (double)p2.y / 255.0;
00470   p1.z = (double)p2.z / 255.0;
00471 }
00472 
00473 void graphmisc::colornormalize
00474 (
00475   point3<double> & p1, 
00476   commandline & cmd, 
00477   stringc tag 
00478 )
00479 {
00480   point3<uint> p2;
00481   string p2str="";
00482   cmd.mapvar(p2str,tag);
00483   if (!p2str.empty())
00484   {
00485     p2.serializeInverseBrackets(p2str);
00486     colornormalize(p1,p2); 
00487   }
00488 }
00489 
00490 
00491 
00492 
00493 
00494 

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