proj home

Files   Classes   Functions   Hierarchy  

zpr Class Reference

3D Mouse support for OpenGL. More...

#include <zpr.h>

Collaboration diagram for zpr:
Collaboration graph

List of all members.

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 width_, int height_)
 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.
fnobj0< void > * mousecallback
 If not zero callback after mousezpr(.

Static Public Attributes

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

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=800;
  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 74 of file zpr.h.


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 331 of file zpr.cpp.

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

00332 {
00333   global = this;
00334 
00335   mouseX = 0;
00336   mouseY = 0;
00337  
00338   mouseLeft   = false;
00339   mouseRight  = false;
00340   mouseMiddle = false;
00341 
00342   mouseXworld = 0.0;
00343   mouseYworld = 0.0;
00344   mouseZworld = 0.0;
00345 
00346   mousecallback = 0;
00347 
00348   //glMatrixMode(GL_MODELVIEW);
00349   //glLoadIdentity();
00350  
00351   readScreenDimensions();
00352 
00353   glMatrixMode(GL_PROJECTION);
00354   glLoadIdentity();
00355 
00356   zNear=1.0;
00357   zFar=10.0;
00358   gluPerspective(30, (GLfloat) width/(GLfloat) height, zNear, zFar);
00359 
00360   glMatrixMode(GL_MODELVIEW);
00361 
00362   glLoadIdentity();
00363   gluLookAt
00364   (
00365     0.0, 0.0, 2.0,  // Eye  - creates a x: [-2,2] y: [-2,2] screen.
00366     0.0, 0.0, 0.0,  // Center
00367     0.0, 1.0, 0.0   // Up
00368   );
00369 
00370 
00371   glerrordisplay();
00372   update();
00373 
00374   glutReshapeFunc(reshape);
00375   glutMouseFunc(mouse);
00376   glutMotionFunc(motion);
00377 }


Member Function Documentation

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

Glut motion call back function.

Definition at line 259 of file zpr.cpp.

References global.

Referenced by zpr().

00260 {
00261   assert(global!=0);
00262   global->motionzpr(x,y);
00263 }

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

Glut mouse call back function.

Definition at line 198 of file zpr.cpp.

References global.

Referenced by zpr().

00199 {
00200   assert(global!=0);
00201 
00202   global->mousezpr(button,state,x,y);
00203 }

void zpr::printInfo (  )  const

Print the frustrum values.

Definition at line 379 of file zpr.cpp.

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

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

00380 {
00381 cout << SHOW(left) << endl;
00382 cout << SHOW(right) << endl;
00383 cout << SHOW(bottom) << endl;
00384 cout << SHOW(top) << endl;
00385 cout << SHOW(zNear) << endl;
00386 cout << SHOW(zFar) << endl;
00387 cout << SHOW(width) << endl;
00388 cout << SHOW(height) << endl;
00389 }

void zpr::readModelView (  ) 

Read the model view matrix from OpenGL.

Definition at line 142 of file zpr.cpp.

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

Referenced by update().

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

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

Get the world mouse co-ordinates.

Definition at line 206 of file zpr.cpp.

Referenced by zprmouse::world().

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

void zpr::readProjection (  ) 

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

Definition at line 480 of file zpr.cpp.

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

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

00481 {
00482   static GLdouble pmatrix[16];
00483 
00484   glerrordisplay();
00485 
00486   glGetDoublev(GL_PROJECTION_MATRIX,pmatrix);
00487 
00488   glerrordisplay();
00489 
00490   zprGLmatrix mat(pmatrix);
00491 
00492   GLdouble a[6];
00493   a[0] = mat.access(0,0);
00494   a[1] = mat.access(1,1);
00495   a[2] = mat.access(0,2);
00496   a[3] = mat.access(1,2);
00497   a[4] = mat.access(2,2);
00498   a[5] = mat.access(2,3);
00499 
00500   // This is essentially an inverse of a glFrustrum(...) call.
00501   // Here the input parameters to the glFrustrum call are
00502   // calculated from the projection matrix.
00503   GLdouble c0 = (1.0-a[4])/(1.0+a[4]);
00504   //GLdouble z0 = a[5]*(c0+1.0)*-0.5/c0;
00505   GLdouble z0 = a[5]*(1.0+1.0/c0)*-0.50;
00506   GLdouble z1 = -c0*z0;
00507   GLdouble x0 = z0*(a[2]-1.0)/a[0];
00508   GLdouble x1 = (z0*2.0+a[0]*x0)/a[0];
00509   GLdouble y0 = z0*(a[3]-1.0)/a[1];
00510   GLdouble y1 = (z0*2.0+a[1]*y0)/a[1];
00511 
00512   left = x0;
00513   right = x1;
00514   bottom = y0;
00515   top = y1;
00516   zNear = z0;
00517   zFar = z1;
00518 
00519   glerrordisplay();
00520 }

void zpr::readScreenDimensions (  ) 

Read the current screen size from OpenGL.

Definition at line 469 of file zpr.cpp.

References glerrordisplay(), height, and width.

Referenced by update(), and zpr().

00470 {
00471   GLint viewport[4];
00472   glGetIntegerv( GL_VIEWPORT, viewport );
00473 
00474   glerrordisplay();
00475 
00476   width = viewport[2];
00477   height = viewport[3];
00478 }

void zpr::reshape ( int  width_,
int  height_ 
) [static]

Glut reshape call back function.

Definition at line 148 of file zpr.cpp.

References global.

Referenced by zpr().

00149 {
00150   assert(zpr::global!=0);
00151   global->reshapezpr(width_,height_);
00152 }

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

Distance function.

Definition at line 179 of file zpr.h.

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

void zpr::write (  ) 

Write the current projection from zpr's parameters.

Definition at line 154 of file zpr.cpp.

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

Referenced by writeDefault(), and writefromXaxis().

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

void zpr::writeDefault (  ) 

Write a default projection from zpr.

Definition at line 181 of file zpr.cpp.

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

00182 {
00183   assert( height>0.0);
00184   assert( width>0.0);
00185 
00186   left=-1.0;
00187   right=1.0;
00188 
00189   double dy = (double) height / (double)width;
00190   bottom=-dy;
00191   top=dy;
00192 
00193   zNear=1.0;
00194   zFar=6.0;
00195   write();
00196 }

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 391 of file zpr.cpp.

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

00392 {
00393   GLdouble const xlen = right - left;
00394   GLdouble const ylen = xlen * (GLdouble) height / (GLdouble) width;
00395   top = bottom + ylen;
00396 
00397   write();
00398 }


Member Data Documentation

GLdouble zpr::bottom

The minimum y screen coordinate.

Definition at line 92 of file zpr.h.

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

zpr * zpr::global = 0 [static]

zpr is a singleton with a global pointer.

Global singelton pointer initially set to zero.

Definition at line 85 of file zpr.h.

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

GLdouble zpr::left

The minimum x screen coordinate.

Definition at line 88 of file zpr.h.

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

GLdouble zpr::matrix[16]

The model view matrix.

Definition at line 152 of file zpr.h.

Referenced by readModelView().

GLdouble zpr::matrixI[16]

The inverse of the model view matrix.

Definition at line 154 of file zpr.h.

Referenced by readModelView().

If not zero callback after mousezpr(.

.) when a mouse click occures. mousecallback pointer is not memory managed.

Definition at line 159 of file zpr.h.

Referenced by maze005::eval(), maze004::eval(), and zpr().

Was the left mouse button pressed?

Definition at line 138 of file zpr.h.

Referenced by zpr().

Was the middle mouse button pressed?

Definition at line 140 of file zpr.h.

Referenced by zpr().

Was the right mouse button pressed?

Definition at line 142 of file zpr.h.

Referenced by zpr().

The mouse X position in pixeles.

Definition at line 134 of file zpr.h.

Referenced by zprmouse::mouse(), zprmouse::mouseint(), and zprmouse::mouseratio().

GLdouble zpr::mouseXworld

The mouse X position in world coordinates.

Definition at line 145 of file zpr.h.

Referenced by zprmouse::world(), and zpr().

The mouse Y position in pixeles.

Definition at line 136 of file zpr.h.

Referenced by zprmouse::mouse(), zprmouse::mouseint(), and zprmouse::mouseratio().

GLdouble zpr::mouseYworld

The mouse Y position in world coordinates.

Definition at line 147 of file zpr.h.

Referenced by zprmouse::world(), and zpr().

GLdouble zpr::mouseZworld

The mouse Z position in world coordinates.

Definition at line 149 of file zpr.h.

Referenced by zprmouse::world(), and zpr().

GLdouble zpr::right

The maximum x screen coordinate.

Definition at line 90 of file zpr.h.

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

GLdouble zpr::top

The maximum y screen coordinate.

Definition at line 94 of file zpr.h.

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

GLdouble zpr::zFar

The farthest z clipping plane.

Definition at line 98 of file zpr.h.

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

GLdouble zpr::zNear

The closest z clipping plane.

Definition at line 96 of file zpr.h.

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


The documentation for this class was generated from the following files:

Generated on 27 Nov 2012 for Chelton Evans Source by  doxygen 1.6.1