proj home

Files   Classes   Functions   Hierarchy  

Public Member Functions | Static Public Member Functions | Public Attributes | Static Public Attributes | List of all members
zpr Class Reference

3D Mouse support for OpenGL. More...

#include <zpr.h>

Collaboration diagram for zpr:
Collaboration graph

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(..) when a mouse click occures.

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(), and reshape().

{
global = this;
mouseX = 0;
mouseY = 0;
mouseLeft = false;
mouseRight = false;
mouseMiddle = false;
mouseXworld = 0.0;
mouseYworld = 0.0;
mouseZworld = 0.0;
//glMatrixMode(GL_MODELVIEW);
//glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
zNear=1.0;
zFar=10.0;
gluPerspective(30, (GLfloat) width/(GLfloat) height, zNear, zFar);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt
(
0.0, 0.0, 2.0, // Eye - creates a x: [-2,2] y: [-2,2] screen.
0.0, 0.0, 0.0, // Center
0.0, 1.0, 0.0 // Up
);
update();
glutReshapeFunc(reshape);
glutMouseFunc(mouse);
glutMotionFunc(motion);
}

Member Function Documentation

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

Glut motion call back function.

Definition at line 259 of file zpr.cpp.

{
assert(global!=0);
global->motionzpr(x,y);
}
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.

{
assert(global!=0);
global->mousezpr(button,state,x,y);
}
void zpr::printInfo ( ) const

Print the frustrum values.

Definition at line 379 of file zpr.cpp.

References SHOW.

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

{
cout << SHOW(left) << endl;
cout << SHOW(right) << endl;
cout << SHOW(bottom) << endl;
cout << SHOW(top) << endl;
cout << SHOW(zNear) << endl;
cout << SHOW(zFar) << endl;
cout << SHOW(width) << endl;
cout << SHOW(height) << endl;
}
void zpr::readModelView ( )

Read the model view matrix from OpenGL.

Definition at line 142 of file zpr.cpp.

References zprGLmatrix::invertMatrix().

{
glGetDoublev(GL_MODELVIEW_MATRIX,matrix);
}
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().

{
// Use the ortho projection and viewport information
// to map from mouse co-ordinates back into world
// co-ordinates
int viewport[4];
glGetIntegerv(GL_VIEWPORT,viewport);
*px = (GLdouble)(x-viewport[0])/(GLdouble)(viewport[2]);
*py = (GLdouble)(y-viewport[1])/(GLdouble)(viewport[3]);
*px = left + (*px)*(right-left);
*py = top + (*py)*(bottom-top);
*pz = zNear;
}
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(), and glerrordisplay().

Referenced by zprtest::test03().

{
static GLdouble pmatrix[16];
glGetDoublev(GL_PROJECTION_MATRIX,pmatrix);
zprGLmatrix mat(pmatrix);
GLdouble a[6];
a[0] = mat.access(0,0);
a[1] = mat.access(1,1);
a[2] = mat.access(0,2);
a[3] = mat.access(1,2);
a[4] = mat.access(2,2);
a[5] = mat.access(2,3);
// This is essentially an inverse of a glFrustrum(...) call.
// Here the input parameters to the glFrustrum call are
// calculated from the projection matrix.
GLdouble c0 = (1.0-a[4])/(1.0+a[4]);
//GLdouble z0 = a[5]*(c0+1.0)*-0.5/c0;
GLdouble z0 = a[5]*(1.0+1.0/c0)*-0.50;
GLdouble z1 = -c0*z0;
GLdouble x0 = z0*(a[2]-1.0)/a[0];
GLdouble x1 = (z0*2.0+a[0]*x0)/a[0];
GLdouble y0 = z0*(a[3]-1.0)/a[1];
GLdouble y1 = (z0*2.0+a[1]*y0)/a[1];
left = x0;
right = x1;
bottom = y0;
top = y1;
zNear = z0;
zFar = z1;
}
void zpr::readScreenDimensions ( )

Read the current screen size from OpenGL.

Definition at line 469 of file zpr.cpp.

References glerrordisplay().

{
GLint viewport[4];
glGetIntegerv( GL_VIEWPORT, viewport );
width = viewport[2];
height = viewport[3];
}
void zpr::reshape ( int  width_,
int  height_ 
)
static

Glut reshape call back function.

Definition at line 148 of file zpr.cpp.

References global.

{
assert(zpr::global!=0);
global->reshapezpr(width_,height_);
}
void zpr::update ( )
inline
static double zpr::vlen ( GLdouble  x,
GLdouble  y,
GLdouble  z 
)
inlinestatic

Distance function.

Definition at line 179 of file zpr.h.

{ 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 glerrordisplay().

{
glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
assert(zNear>0.0);
assert(zFar>zNear);
glFrustum(left,right,bottom,top,zNear,zFar);
glMatrixMode(GL_MODELVIEW);
}
void zpr::writeDefault ( )

Write a default projection from zpr.

Definition at line 181 of file zpr.cpp.

{
assert( height>0.0);
assert( width>0.0);
left=-1.0;
right=1.0;
double dy = (double) height / (double)width;
bottom=-dy;
top=dy;
zNear=1.0;
zFar=6.0;
write();
}
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.

{
GLdouble const xlen = right - left;
GLdouble const ylen = xlen * (GLdouble) height / (GLdouble) width;
top = bottom + ylen;
write();
}

Member Data Documentation

GLdouble zpr::bottom

The minimum y screen coordinate.

Definition at line 92 of file zpr.h.

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 reshape().

int zpr::height

Screen pixel height.

Definition at line 103 of file zpr.h.

Referenced by buttonpanel02::draw(), mazedisp03::f01(), zprmouse::mouseratio(), zprmouse::mouseratioInv(), and buttonpanel02::update().

GLdouble zpr::left

The minimum x screen coordinate.

Definition at line 88 of file zpr.h.

GLdouble zpr::matrix[16]

The model view matrix.

Definition at line 152 of file zpr.h.

GLdouble zpr::matrixI[16]

The inverse of the model view matrix.

Definition at line 154 of file zpr.h.

fnobj0<void>* zpr::mousecallback

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 maze004::eval(), and maze005::eval().

bool zpr::mouseLeft

Was the left mouse button pressed?

Definition at line 138 of file zpr.h.

bool zpr::mouseMiddle

Was the middle mouse button pressed?

Definition at line 140 of file zpr.h.

bool zpr::mouseRight

Was the right mouse button pressed?

Definition at line 142 of file zpr.h.

int zpr::mouseX

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().

int zpr::mouseY

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().

GLdouble zpr::mouseZworld

The mouse Z position in world coordinates.

Definition at line 149 of file zpr.h.

Referenced by zprmouse::world().

GLdouble zpr::right

The maximum x screen coordinate.

Definition at line 90 of file zpr.h.

GLdouble zpr::top

The maximum y screen coordinate.

Definition at line 94 of file zpr.h.

int zpr::width

Screen pixel width.

Definition at line 101 of file zpr.h.

Referenced by buttonpanel02::draw(), mazedisp03::f01(), zprmouse::mouseratio(), zprmouse::mouseratioInv(), and buttonpanel02::update().

GLdouble zpr::zFar

The farthest z clipping plane.

Definition at line 98 of file zpr.h.

GLdouble zpr::zNear

The closest z clipping plane.

Definition at line 96 of file zpr.h.


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