proj C++ Projects home

Files   Classes   Functions   Hierarchy  

zpr Class Reference

#include <zpr.h>

Collaboration diagram for zpr:List of all members.


Detailed Description

3D Mouse support for OpenGL.

Mouse Functionality. The left mouse button rotates about the global origin. The right mouse button pans. The third mouse button (hold down left and right for 2 button mouse) zooms.
Acknowledgements: Thanks to Nigel Stewart at nigels.com for zpr.h and zpr.cpp. I requested permission to use the code as mouse support is great. For the original version see http://www.nigels.com/glt/gltzpr/ .
I have modified the code from C to C++. Additional functionality was added along with a minor optimization.

zpr is currently a singleton class. An instance initializes OpenGL with a reasonable default projection and model view.

Example
  int wx=800; 
  int wy=600;
  glutInitWindowSize(wx,wy);
  glutCreateWindow("");

  zpr zz;
You can make direct OpenGL calls yourself and then have this class read in the new state with zpr::update(). ie

zpr works correctly when its state mirrors that of OpenGL. For example a call to glFrustrum or gluLookAt will change the viewing frustrum and zpr's values will not be the same as OpenGL's, hence zpr will break. Call zpr::update() to update zpr after a change.

Example
  zpr zz;
  ...
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  gluLookAt
  (
    2.0,0.0,3.0, 
    2.0,0.0,0.0, 
    0.0,1.0,0.0
  );

  zz.update();

Definition at line 73 of file zpr.h.

Public Member Functions

 zpr ()
 Look at the origin from (0.0,0.0,2.0) with up as (0.0,1.0,0.0) and the field of view 30 degrees.
void readModelView ()
 Read the model view matrix from OpenGL.
void readProjection ()
 Read the projection matrix from OpenGL and calculate left, right, bottom, top, zNear and zFar.
void readScreenDimensions ()
 Read the current screen size from OpenGL.
void update ()
 Read in the current OpenGL state.
void write ()
 Write the current projection from zpr's parameters.
void writeDefault ()
 Write a default projection from zpr.
void writefromXaxis ()
 Draw having the x and y axes with equal scales, zpr::top needs to be re-calculated.
void readMouse (GLdouble *px, GLdouble *py, GLdouble *pz, intc x, intc y) const
 Get the world mouse co-ordinates.
void printInfo () const
 Print the frustrum values.

Static Public Member Functions

static void motion (int x, int y)
 Glut motion call back function.
static void mouse (int button, int state, int x, int y)
 Glut mouse call back function.
static void reshape (int w, int h)
 Glut reshape call back function.
static double vlen (GLdouble x, GLdouble y, GLdouble z)
 Distance function.

Public Attributes

GLdouble left
 The minimum x screen coordinate.
GLdouble right
 The maximum x screen coordinate.
GLdouble bottom
 The minimum y screen coordinate.
GLdouble top
 The maximum y screen coordinate.
GLdouble zNear
 The closest z clipping plane.
GLdouble zFar
 The farthest z clipping plane.
int width
 Screen pixel width.
int height
 Screen pixel height.
int mouseX
 The mouse X position in pixeles.
int mouseY
 The mouse Y position in pixeles.
bool mouseLeft
 Was the left mouse button pressed?
bool mouseMiddle
 Was the middle mouse button pressed?
bool mouseRight
 Was the right mouse button pressed?
GLdouble mouseXworld
 The mouse X position in world coordinates.
GLdouble mouseYworld
 The mouse Y position in world coordinates.
GLdouble mouseZworld
 The mouse Z position in world coordinates.
GLdouble matrix [16]
 The model view matrix.
GLdouble matrixI [16]
 The inverse of the model view matrix.

Static Public Attributes

static zprglobal = 0
 zpr is a singleton with a global pointer.


Constructor & Destructor Documentation

zpr::zpr (  ) 

Look at the origin from (0.0,0.0,2.0) with up as (0.0,1.0,0.0) and the field of view 30 degrees.

Definition at line 322 of file zpr.cpp.

References glerrordisplay(), global, height, motion(), mouse(), mouseLeft, mouseMiddle, mouseRight, mouseXworld, mouseYworld, mouseZworld, readScreenDimensions(), reshape(), update(), width, zFar, and zNear.

00323 {
00324   global = this;
00325 
00326   mouseX = 0;
00327   mouseY = 0;
00328  
00329   mouseLeft   = false;
00330   mouseRight  = false;
00331   mouseMiddle = false;
00332 
00333   mouseXworld = 0.0;
00334   mouseYworld = 0.0;
00335   mouseZworld = 0.0;
00336  
00337   readScreenDimensions();
00338 
00339   glMatrixMode(GL_PROJECTION);
00340   glLoadIdentity();
00341 
00342   zNear=1.0;
00343   zFar=10.0;
00344   gluPerspective(30, (GLfloat) width/(GLfloat) height, zNear, zFar);
00345 
00346   glMatrixMode(GL_MODELVIEW);
00347   glLoadIdentity();
00348   gluLookAt
00349   (
00350     0.0, 0.0, 2.0,  // Eye
00351     0.0, 0.0, 0.0,  // Center
00352     0.0, 1.0, 0.0   // Up
00353   );
00354 
00355   glerrordisplay();
00356   update();
00357 
00358   glutReshapeFunc(reshape);
00359   glutMouseFunc(mouse);
00360   glutMotionFunc(motion);
00361 }


Member Function Documentation

void zpr::readModelView (  ) 

Read the model view matrix from OpenGL.

Definition at line 141 of file zpr.cpp.

References zprGLmatrix::invertMatrix(), matrix, and matrixI.

Referenced by update().

00142 {
00143   glGetDoublev(GL_MODELVIEW_MATRIX,matrix);
00144   zprGLmatrix::invertMatrix(matrixI,matrix);
00145 }

void zpr::readProjection (  ) 

Read the projection matrix from OpenGL and calculate left, right, bottom, top, zNear and zFar.

Definition at line 464 of file zpr.cpp.

References zprGLmatrix::access(), bottom, glerrordisplay(), left, right, top, zFar, and zNear.

Referenced by zprtest::test03(), and update().

00465 {
00466   static GLdouble pmatrix[16];
00467 
00468   glerrordisplay();
00469 
00470   glGetDoublev(GL_PROJECTION_MATRIX,pmatrix);
00471 
00472   glerrordisplay();
00473 
00474   zprGLmatrix mat(pmatrix);
00475 
00476   GLdouble a[6];
00477   a[0] = mat.access(0,0);
00478   a[1] = mat.access(1,1);
00479   a[2] = mat.access(0,2);
00480   a[3] = mat.access(1,2);
00481   a[4] = mat.access(2,2);
00482   a[5] = mat.access(2,3);
00483 
00484   // This is essentially an inverse of a glFrustrum(...) call.
00485   // Here the input parameters to the glFrustrum call are
00486   // calculated from the projection matrix.
00487   GLdouble c0 = (1.0-a[4])/(1.0+a[4]);
00488   //GLdouble z0 = a[5]*(c0+1.0)*-0.5/c0;
00489   GLdouble z0 = a[5]*(1.0+1.0/c0)*-0.50;
00490   GLdouble z1 = -c0*z0;
00491   GLdouble x0 = z0*(a[2]-1.0)/a[0];
00492   GLdouble x1 = (z0*2.0+a[0]*x0)/a[0];
00493   GLdouble y0 = z0*(a[3]-1.0)/a[1];
00494   GLdouble y1 = (z0*2.0+a[1]*y0)/a[1];
00495 
00496   left = x0;
00497   right = x1;
00498   bottom = y0;
00499   top = y1;
00500   zNear = z0;
00501   zFar = z1;
00502 
00503   glerrordisplay();
00504 }

void zpr::readScreenDimensions (  ) 

Read the current screen size from OpenGL.

Definition at line 453 of file zpr.cpp.

References glerrordisplay(), height, and width.

Referenced by update(), and zpr().

00454 {
00455   GLint viewport[4];
00456   glGetIntegerv( GL_VIEWPORT, viewport );
00457 
00458   glerrordisplay();
00459 
00460   width = viewport[2];
00461   height = viewport[3];
00462 }

void zpr::update (  )  [inline]

Read in the current OpenGL state.

Definition at line 116 of file zpr.h.

References readModelView(), readProjection(), and readScreenDimensions().

Referenced by test01obj< P, PD >::eval(), cubegui::prog01(), gobjtest::test001(), graphmisctest::test002(), triangletest::test01(), tetrahedrontest::test01(), plotpolartest::test01(), planeinttest::test01(), diskinttest::test01(), d2simplextest::test01(), circleD2test::test01(), boxOBBhalfspaceD2test::test01(), menusystemtest::test01(), test01(), vrmltest::test02(), triangletest::test02(), tetrahedrontest::test02(), diskinttest::test02(), menusystemtest::test02(), regionD2linkedtest::test02(), meshpatchtest::test02(), helixtest::test03(), triangletest::test03(), diskinttest::test03(), treeindexedtest::test03(), meshpatchtest::test03(), meshpatchtest::test04(), triangles3Tdisplaytest::test05(), treeindexedtest::test05(), partitionstest::test06(), and zpr().

00117   {
00118     readScreenDimensions();
00119     readModelView();
00120     readProjection();
00121   }

void zpr::write (  ) 

Write the current projection from zpr's parameters.

Definition at line 153 of file zpr.cpp.

References bottom, glerrordisplay(), height, left, right, top, width, zFar, and zNear.

Referenced by writeDefault(), and writefromXaxis().

00154 {
00155   glViewport(0,0,width,height);
00156 
00157   glMatrixMode(GL_PROJECTION);
00158   glLoadIdentity();
00159   assert(zNear>0.0);
00160   assert(zFar>zNear);
00161   glFrustum(left,right,bottom,top,zNear,zFar);
00162   glMatrixMode(GL_MODELVIEW);
00163 
00164   glerrordisplay();
00165 }

void zpr::writeDefault (  ) 

Write a default projection from zpr.

Definition at line 177 of file zpr.cpp.

References bottom, height, left, right, top, width, write(), zFar, and zNear.

00178 {
00179   assert( height>0.0);
00180   assert( width>0.0);
00181 
00182   left=-1.0;
00183   right=1.0;
00184 
00185   double dy = 2.0 * (double) height / (double)width;
00186   bottom=-dy*0.5;
00187   top=dy*0.5;
00188 
00189   zNear=1.0;
00190   zFar=6.0;
00191   write();
00192 }

void zpr::writefromXaxis (  ) 

Draw having the x and y axes with equal scales, zpr::top needs to be re-calculated.

This function is useful in plotting maths graphs.

Definition at line 375 of file zpr.cpp.

References bottom, height, left, right, top, width, and write().

00376 {
00377   GLdouble const xlen = right - left;
00378   GLdouble const ylen = xlen * (GLdouble) height / (GLdouble) width;
00379   top = bottom + ylen;
00380 
00381   write();
00382 }

void zpr::motion ( int  x,
int  y 
) [static]

Glut motion call back function.

Definition at line 250 of file zpr.cpp.

References global, and motionzpr().

Referenced by zpr().

00251 {
00252   assert(global!=0);
00253   global->motionzpr(x,y);
00254 }

void zpr::mouse ( int  button,
int  state,
int  x,
int  y 
) [static]

Glut mouse call back function.

Definition at line 194 of file zpr.cpp.

References global, and mousezpr().

Referenced by zpr().

00195 {
00196   assert(global!=0);
00197 
00198   global->mousezpr(button,state,x,y);
00199 }

void zpr::reshape ( int  w,
int  h 
) [static]

Glut reshape call back function.

Definition at line 147 of file zpr.cpp.

References global, and reshapezpr().

Referenced by zpr().

00148 {
00149   assert(zpr::global!=0);
00150   global->reshapezpr(_width,_height);
00151 }

void zpr::readMouse ( GLdouble px,
GLdouble py,
GLdouble pz,
intc  x,
intc  y 
) const

Get the world mouse co-ordinates.

Definition at line 202 of file zpr.cpp.

References bottom, left, right, top, and zNear.

00209 {
00210 //    Use the ortho projection and viewport information
00211 //    to map from mouse co-ordinates back into world 
00212 //    co-ordinates
00213 
00214   int viewport[4];
00215   glGetIntegerv(GL_VIEWPORT,viewport);
00216 
00217   *px = (GLdouble)(x-viewport[0])/(GLdouble)(viewport[2]);
00218   *py = (GLdouble)(y-viewport[1])/(GLdouble)(viewport[3]);
00219 
00220   *px = left + (*px)*(right-left);
00221   *py = top  + (*py)*(bottom-top);
00222   *pz = zNear;
00223 }

static double zpr::vlen ( GLdouble  x,
GLdouble  y,
GLdouble  z 
) [inline, static]

Distance function.

Definition at line 173 of file zpr.h.

00174     { return sqrt(x*x+y*y+z*z); }

void zpr::printInfo (  )  const

Print the frustrum values.

Definition at line 363 of file zpr.cpp.

References bottom, height, left, right, SHOW, top, width, zFar, and zNear.

Referenced by zprtest::test01(), and zprtest::test03().

00364 {
00365 cout << SHOW(left) << endl;
00366 cout << SHOW(right) << endl;
00367 cout << SHOW(bottom) << endl;
00368 cout << SHOW(top) << endl;
00369 cout << SHOW(zNear) << endl;
00370 cout << SHOW(zFar) << endl;
00371 cout << SHOW(width) << endl;
00372 cout << SHOW(height) << endl;
00373 }


Member Data Documentation

zpr * zpr::global = 0 [static]

zpr is a singleton with a global pointer.

Definition at line 84 of file zpr.h.

Referenced by motion(), mouse(), reshape(), and zpr().

GLdouble zpr::left

The minimum x screen coordinate.

Definition at line 87 of file zpr.h.

Referenced by printInfo(), readMouse(), readProjection(), write(), writeDefault(), and writefromXaxis().

GLdouble zpr::right

The maximum x screen coordinate.

Definition at line 89 of file zpr.h.

Referenced by printInfo(), readMouse(), readProjection(), write(), writeDefault(), and writefromXaxis().

GLdouble zpr::bottom

The minimum y screen coordinate.

Definition at line 91 of file zpr.h.

Referenced by printInfo(), readMouse(), readProjection(), write(), writeDefault(), and writefromXaxis().

GLdouble zpr::top

The maximum y screen coordinate.

Definition at line 93 of file zpr.h.

Referenced by printInfo(), readMouse(), readProjection(), write(), writeDefault(), and writefromXaxis().

GLdouble zpr::zNear

The closest z clipping plane.

Definition at line 95 of file zpr.h.

Referenced by printInfo(), readMouse(), readProjection(), write(), writeDefault(), and zpr().

GLdouble zpr::zFar

The farthest z clipping plane.

Definition at line 97 of file zpr.h.

Referenced by printInfo(), readProjection(), write(), writeDefault(), and zpr().

int zpr::width

Screen pixel width.

Definition at line 100 of file zpr.h.

Referenced by printInfo(), readScreenDimensions(), write(), writeDefault(), writefromXaxis(), and zpr().

int zpr::height

Screen pixel height.

Definition at line 102 of file zpr.h.

Referenced by printInfo(), readScreenDimensions(), write(), writeDefault(), writefromXaxis(), and zpr().

int zpr::mouseX

The mouse X position in pixeles.

Definition at line 133 of file zpr.h.

int zpr::mouseY

The mouse Y position in pixeles.

Definition at line 135 of file zpr.h.

bool zpr::mouseLeft

Was the left mouse button pressed?

Definition at line 137 of file zpr.h.

Referenced by zpr().

bool zpr::mouseMiddle

Was the middle mouse button pressed?

Definition at line 139 of file zpr.h.

Referenced by zpr().

bool zpr::mouseRight

Was the right mouse button pressed?

Definition at line 141 of file zpr.h.

Referenced by zpr().

GLdouble zpr::mouseXworld

The mouse X position in world coordinates.

Definition at line 144 of file zpr.h.

Referenced by zpr().

GLdouble zpr::mouseYworld

The mouse Y position in world coordinates.

Definition at line 146 of file zpr.h.

Referenced by zpr().

GLdouble zpr::mouseZworld

The mouse Z position in world coordinates.

Definition at line 148 of file zpr.h.

Referenced by zpr().

GLdouble zpr::matrix[16]

The model view matrix.

Definition at line 151 of file zpr.h.

Referenced by readModelView().

GLdouble zpr::matrixI[16]

The inverse of the model view matrix.

Definition at line 153 of file zpr.h.

Referenced by readModelView().


The documentation for this class was generated from the following files:
Generated on Sun Sep 30 19:06:01 2007 for Chelton Evans Source by  doxygen 1.5.2