Go to the documentation of this file.00001 #ifndef OPENTISSUE_UTILITY_GL_GL_PICKING_H
00002 #define OPENTISSUE_UTILITY_GL_GL_PICKING_H
00003
00004
00005
00006
00007
00008
00009
00010 #include <OpenTissue/configuration.h>
00011
00012 #include <OpenTissue/utility/gl/gl.h>
00013
00014 namespace OpenTissue
00015 {
00016
00017 namespace gl
00018 {
00019
00023 class Picking
00024 {
00025 protected:
00026
00032 GLuint inFront( GLint hits, GLuint buffer[] )
00033 {
00034 GLuint depth = ( GLuint ) - 1;
00035 GLuint* p = buffer;
00036 GLuint picked = 0;
00037 for ( GLint i = 0;i < hits;++i )
00038 {
00039 GLboolean save = GL_FALSE;
00040 GLuint num_names = *p;
00041 p++;
00042
00043 if ( *p <= depth )
00044 {
00045 depth = *p;
00046 save = GL_TRUE;
00047 }
00048 p++;
00049 if ( *p <= depth )
00050 {
00051 depth = *p;
00052 save = GL_TRUE;
00053 }
00054 p++;
00055
00056 if ( save )
00057 picked = *p;
00058
00059 p += num_names;
00060 }
00061 return picked;
00062 }
00063
00064 public:
00065
00082 template< typename draw_functor >
00083 GLuint operator() ( int x, int y, draw_functor const & draw )
00084 {
00085 GLdouble projMatrix[ 16 ];
00086 GLdouble modelViewMatrix[ 16 ];
00087 GLint viewport[ 4 ];
00088 GLint mode;
00089
00090 GLsizei size = 512;
00091 GLuint buffer[ 512 ];
00092
00093 glGetIntegerv( GL_MATRIX_MODE, &mode );
00094 glGetDoublev( GL_MODELVIEW_MATRIX, modelViewMatrix );
00095 glGetDoublev( GL_PROJECTION_MATRIX, projMatrix );
00096 glGetIntegerv( GL_VIEWPORT, viewport );
00097
00098 glSelectBuffer( size, buffer );
00099
00100 glRenderMode( GL_SELECT );
00101 glInitNames();
00102
00103
00104 glMatrixMode( GL_PROJECTION );
00105 glPushMatrix();
00106 glLoadIdentity();
00107
00108 gluPickMatrix( ( GLdouble ) x, ( GLdouble ) ( viewport[ 3 ] - y ), 5.0, 5.0, viewport );
00109 glMultMatrixd( projMatrix );
00110
00111
00112 glMatrixMode( GL_MODELVIEW );
00113 glPushMatrix();
00114 glLoadIdentity();
00115 glMultMatrixd( modelViewMatrix );
00116
00117
00118 draw();
00119
00120 glFlush();
00121 glPopMatrix();
00122 glMatrixMode( GL_PROJECTION );
00123 glPopMatrix();
00124
00125 GLint hits = glRenderMode( GL_RENDER );
00126 GLuint picked = inFront( hits, buffer );
00127
00128 glMatrixMode( mode );
00129
00130 return picked;
00131 }
00132 };
00133
00134 }
00135
00136 }
00137
00138
00139 #endif