Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gl_pick.cpp
Go to the documentation of this file.
1 /* GL selection buffer code
2  */
3 
4 //#include <GL/gl.h>
5 #include "gl_globals.h"
6 #include "gfxlib.h"
7 #include "gl_matrix.h"
8 #include <GL/glu.h>
9 #include <assert.h>
10 #define SELECTBUF_SIZE MAX_PICK*4
11 const float epsilon = 0.001;
12 
13 static vector< PickData > *picked_objects = new vector< PickData > ();
14 static unsigned selectbuf[SELECTBUF_SIZE];
15 
16 using namespace GFXMatrices;
17 
18 void GFXBeginPick( int x, int y, int xsize, int ysize )
19 {
20  //save and change gl projection matrix
21  GLint viewport[4];
22 
23  float left, right, bottom, top, fnear, ffar;
24  GFXGetFrustumVars( true, &left, &right, &bottom, &top, &fnear, &ffar );
25 
26  glMatrixMode( GL_PROJECTION );
27  glPushMatrix();
28  glLoadIdentity();
29  glGetIntegerv( GL_VIEWPORT, viewport );
30  gluPickMatrix( x, viewport[3]-y, xsize, ysize, viewport );
31  glMultMatrixf( projection );
32 
33  glSelectBuffer( SELECTBUF_SIZE, selectbuf );
34  glRenderMode( GL_SELECT );
35  glGetError();
36 
37  delete picked_objects;
38  picked_objects = new vector< PickData > ();
39 }
40 
41 void GFXSetPickName( int name )
42 {
43  glInitNames();
44  glPushName( name );
45  glLoadName( name );
46  glGetError();
47 }
48 
49 void drawRects( GLenum mode )
50 {
51  glLoadName( 1 );
52  glBegin( GL_QUADS );
53  glColor3f( 1.0, 1.0, 0.0 );
54  glVertex3i( 2, 0, 0 );
55  glVertex3i( 2, 6, 0 );
56  glVertex3i( 6, 6, 0 );
57  glVertex3i( 6, 0, 0 );
58  glEnd();
59  glLoadName( 2 );
60  glBegin( GL_QUADS );
61  glColor3f( 0.0, 1.0, 1.0 );
62  glVertex3i( 3, 2, -1 );
63  glVertex3i( 3, 8, -1 );
64  glVertex3i( 8, 8, -1 );
65  glVertex3i( 8, 2, -1 );
66  glEnd();
67  glLoadName( 3 );
68  glBegin( GL_QUADS );
69  glColor3f( 1.0, 0.0, 1.0 );
70  glVertex3i( 0, 2, -2 );
71  glVertex3i( 0, 7, -2 );
72  glVertex3i( 5, 7, -2 );
73  glVertex3i( 5, 2, -2 );
74  glEnd();
75 }
76 
77 //Don't call this function
79 {
80  assert( 0 );
81  glFlush();
82  int num_hits = glRenderMode( GL_RENDER );
83  glRenderMode( GL_SELECT );
84  glGetError();
85  assert( num_hits >= 0 && num_hits <= 1 );
86  return num_hits > 0;
87 }
88 
89 vector< PickData > * GFXEndPick()
90 {
91  int num_hits = glRenderMode( GL_RENDER );
92  assert( num_hits != -1 );
93  //num_hits could == -1, check for this case
94  unsigned *ptr = selectbuf;
95  for (int a = 0; a < num_hits; a++) {
96  picked_objects->push_back( PickData( ptr[3], ptr[1], ptr[2] ) );
97  ptr += ptr[0]+3;
98  }
99  //restore gl projection matrix
100  glMatrixMode( GL_PROJECTION );
101  glPopMatrix();
102  return picked_objects;
103 }
104