Main Page | File List

Picking.cpp

00001 #include <iterator>
00002 #include <iostream>
00003 #include <cfloat>
00004 #include <cstdio>
00005 #include "Picking.h"
00006 #include <climits>
00007 
00008 using namespace std;
00009 
00010 void projection();                  // projection transformation in main.cpp
00011 void render_main_window();          // assume you are rendering multiple viewports. Otherwise, simply use display().
00012 
00013 int Picking :: processHits (int hits)
00014 {
00015   unsigned int i, j;
00016   GLuint names, *ptr = (GLuint *) selectBuf;
00017   double minz = DBL_MAX;
00018 
00019   cout << "hits = " << hits << endl;
00020     
00021   for (unsigned i = 0; i < hits; i++) 
00022   {  
00023     GLuint names = *ptr;                             // # of names on name stack when this hit occurs.  should be 1.
00024     double cur_minz = (double) *++ptr/0x7fffffff;    // minz (followed by maxz, and name stack content) 
00025     ptr += names + 1;                                // point to last name in cur hit record.
00026        
00027     if( cur_minz < minz) 
00028     {
00029       minz = cur_minz;
00030       selected_id = *ptr;
00031     }
00032     ++ptr;                                           // starting of next hit record.
00033   }
00034   return selected_id;
00035 }
00036 
00037 int Picking :: operator() (int x, int y, double width, double height)
00038 {
00039   UnSelect();
00040 
00041   glSelectBuffer (hit_buff_sz, selectBuf);
00042   glRenderMode (GL_SELECT);
00043 
00044   glInitNames();   
00045   glPushName(UINT_MAX);
00046  
00047   glMatrixMode (GL_PROJECTION);
00048   glPushMatrix();
00049   {
00050     glLoadIdentity ();
00051     {
00052       GLint viewport[4];
00053       glGetIntegerv(GL_VIEWPORT, viewport);
00054       gluPickMatrix (x, y, width, height, viewport);
00055     }
00056     projection();
00057     
00058     render_main_window(); 
00059   }
00060   glMatrixMode (GL_PROJECTION);
00061   glPopMatrix();
00062     
00063   return processHits( glRenderMode (GL_RENDER) );
00064 }

Generated on Sun Aug 15 16:22:50 2004 by doxygen 1.3.6