Files Classes Functions Hierarchy
3D Mouse support for OpenGL. More...
#include <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 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 zpr * | global = 0 |
| zpr is a singleton with a global pointer. | |
3D Mouse support for OpenGL.
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.
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.
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.
| 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 }
| void zpr::motion | ( | int | x, | |
| int | y | |||
| ) | [static] |
| void zpr::mouse | ( | int | button, | |
| int | state, | |||
| int | x, | |||
| int | y | |||
| ) | [static] |
| void zpr::printInfo | ( | ) | const |
| 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 }
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] |
| void zpr::update | ( | ) | [inline] |
Read in the current OpenGL state.
Definition at line 117 of file zpr.h.
References readModelView(), readProjection(), and readScreenDimensions().
Referenced by bsptree001::bsptree001(), buttonpanel01test::buttonpanel01test(), maze005::eval(), maze004::eval(), maze003::eval(), maze002::eval(), maze001::eval(), test01obj< P, PD >::eval(), visenv::graphicsloop(), menusystemtest02::menusystemtest02(), menusystemtest03::menusystemtest03(), menusystemtest04::menusystemtest04(), cubegui::prog01(), gobjtest::test001(), windowscaleD2test::test002(), graphmisctest::test002(), gobjtest::test002(), gobjtest::test003(), triangletest::test01(), tetrahedrontest::test01(), plotpolartest::test01(), planeinttest::test01(), diskinttest::test01(), d2simplextest::test01(), circleD2test::test01(), boxOBBhalfspaceD2test::test01(), menusystemtest::test01(), pathlinesegtest::test01(), test01(), vrmltest::test02(), triangletest::test02(), tetrahedrontest::test02(), diskinttest::test02(), menusystemtest::test02(), regionD2linkedtest::test02(), meshpatchtest::test02(), helixtestscope::helixtest::test03(), triangletest::test03(), diskinttest::test03(), treeindexedD2test::test03(), meshpatchtest::test03(), meshpatchtest::test04(), triangles3Tdisplaytest::test05(), treeindexedD2test::test05(), partitionstest::test06(), zprmouse::update(), zpr(), and openwindowresource::~openwindowresource().
00118 { 00119 readScreenDimensions(); 00120 readModelView(); 00121 readProjection(); 00122 }
| static double zpr::vlen | ( | GLdouble | x, | |
| GLdouble | y, | |||
| GLdouble | z | |||
| ) | [inline, static] |
| 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().
| void zpr::writeDefault | ( | ) |
| void zpr::writefromXaxis | ( | ) |
| 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] |
| int zpr::height |
Screen pixel height.
Definition at line 103 of file zpr.h.
Referenced by buttonpanel02::draw(), mazedisp03::f01(), zprmouse::mouseratio(), zprmouse::mouseratioInv(), printInfo(), readScreenDimensions(), buttonpanel02::update(), write(), writeDefault(), writefromXaxis(), 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] |
| GLdouble zpr::matrixI[16] |
The inverse of the model view matrix.
Definition at line 154 of file zpr.h.
Referenced by readModelView().
| 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 maze005::eval(), maze004::eval(), and zpr().
| 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(), and zpr().
| 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(), 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().
| int zpr::width |
Screen pixel width.
Definition at line 101 of file zpr.h.
Referenced by buttonpanel02::draw(), mazedisp03::f01(), zprmouse::mouseratio(), zprmouse::mouseratioInv(), printInfo(), readScreenDimensions(), buttonpanel02::update(), write(), writeDefault(), writefromXaxis(), and zpr().
| 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().
1.6.1