Пример #1
0
    void gl_select(int x, int y){
        GLuint buff[64] = {0};
        GLint hits, view[4];

        glSelectBuffer(64, buff);

        glGetIntegerv(GL_VIEWPORT, view);

        glRenderMode(GL_SELECT);

        glInitNames();

        glPushName(0);

        glMatrixMode(GL_PROJECTION);
        glPushMatrix();
        glLoadIdentity();
        gluPickMatrix(x,y,1.0,1.0,view);
        gluPerspective(45,1.0,0.0001,1000.0);

        glMatrixMode(GL_MODELVIEW);

        glutSwapBuffers();
        draw();

        glMatrixMode(GL_PROJECTION);
        glPopMatrix();

        hits = glRenderMode(GL_RENDER);

        list_hits(hits,buff);

        glMatrixMode(GL_MODELVIEW);
    }
Пример #2
0
void GLwidget::mousePressEvent(QMouseEvent *ev)
{
    float posX = ev->x();
    float posy = ev->y();
    GLuint buff[64] = {0};
      GLint hits, view[4];

      /*
         This choose the buffer where store the values for the selection data
         */
      glSelectBuffer(64, buff);

      /*
         This retrieve info about the viewport
         */
      glGetIntegerv(GL_VIEWPORT, view);

      /*
         Switching in selecton mode
         */
      glRenderMode(GL_SELECT);

      /*
         Clearing the name's stack
         This stack contains all the info about the objects
         */
      glInitNames();

      /*
         Now fill the stack with one element (or glLoadName will generate an error)
         */
      glPushName(0);

      /*
         Now modify the vieving volume, restricting selection area around the cursor
         */
      glMatrixMode(GL_PROJECTION);
      glPushMatrix();
      glLoadIdentity();

      /*
         restrict the draw to an area around the cursor
         */
      gluPickMatrix(posX, posy, 1.0, 1.0, view);
      gluPerspective(60.0, this->width()/this->height(), 0.0001, 1000.0);

      /*
         Draw the objects onto the screen
         */
      glMatrixMode(GL_MODELVIEW);

      /*
         draw only the names in the stack, and fill the array
         */
      //glutSwapBuffers();
      swapBuffers();
      //gl_draw();
      paintGL();

      /*
         Do you remeber? We do pushMatrix in PROJECTION mode
         */
      glMatrixMode(GL_PROJECTION);
      glPopMatrix();

      /*
         get number of objects drawed in that area
         and return to render mode
         */
      hits = glRenderMode(GL_RENDER);
      qDebug() << hits;
      /*
         Print a list of the objects
         */
      list_hits(hits, buff);

      /*
         uncomment this to show the whole buffer
       * /
       gl_selall(hits, buff);
       */

      glMatrixMode(GL_MODELVIEW);
}
Пример #3
0
std::vector<Shape3D*> ofEasyFingerCam::glSelect(int x, int y)
{
    std::vector<Shape3D*> result = std::vector<Shape3D*>();
    GLuint buff[512] = {0};
    GLint hits, view[4];
    GLfloat proj_matrix[16];
    /*
     This choose the buffer where store the values for the selection data
     */
    glSelectBuffer(256, buff);
    /*
     This retrieves info about the viewport
     */
    glGetIntegerv(GL_VIEWPORT, view);
    glGetFloatv(GL_PROJECTION_MATRIX, proj_matrix);
    /*
     Switching in selecton mode
     */
    glRenderMode(GL_SELECT);
    /*
     Clearing the names' stack
     This stack contains all the info about the objects
     */
    glInitNames();
    /*
     Now modify the viewing volume, restricting selection area around the cursor
     */
    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    glLoadIdentity();
    /*
     restrict the draw to an area around the cursor
     */
    gluPickMatrix(x, ofGetHeight() - y, 15.0, 15.0, view);

    float width = ofGetWidth();
    float height = ofGetHeight();
    float viewW = ofGetViewportWidth();
    float viewH = ofGetViewportHeight();
    float fov = 60;
    float eyeX = viewW / 2;
    float eyeY = viewH / 2;
    float halfFov = PI * fov / 360;
    float theTan = tanf(halfFov);
    float dist = eyeY / theTan;
    float aspect = (float) viewW / viewH;
    float nearDist = dist / 10.0f;
    float farDist = dist * 15.0f;
  
    gluPerspective(fov, aspect, nearDist, farDist);
    /*
     Draw the objects onto the screen
     */
    glMatrixMode(GL_MODELVIEW);
    /*
     draw only the names in the stack, and fill the array
     */
    draw();
    
    glMatrixMode(GL_PROJECTION);
    glPopMatrix();
    /*
     get number of objects drawed in that area
     and return to render mode
     */
    hits = glRenderMode(GL_RENDER);
    /*
     Print a list of the objects
     */
    result = list_hits(hits, buff);
    glMatrixMode(GL_MODELVIEW);
    return result;
}