0

我正在尝试通过拾取来识别 Qt 中 OpenGL 中 mousPressEvent 上的绘制对象。

我做了一些研究,但无法找到问题所在。

很明显,它可以识别某些东西(因为 glRenderMode(GL_RENDER) 的返回值通常是大于 0 的整数),但当我单击对象时不一定。

我认为 gluPerspective 是这里的问题,但我只是不知道如何解决它。

鼠标按下事件:

void WorldView::mousePressEvent(QMouseEvent *e)
{
       GLuint buff[256];
       GLint hits;
       GLint view[4];

       //Buffer to store selection data
       glSelectBuffer(256, buff);

       //Viewport information
       glGetIntegerv(GL_VIEWPORT, view);

       //Switch to select mode
       glRenderMode(GL_SELECT);

       //Clear the name stack!
       glInitNames();

       //Restric viewing volume
       glMatrixMode(GL_PROJECTION);
       glPushMatrix();
       glLoadIdentity();

       //Restrict draw area
       gluPickMatrix(e->x(), e->y(), 1.0, 1.0, view);
       gluPerspective(40.0f, (GLfloat)view[2]/(GLfloat)view[3], 1.0, 100.0);

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

       //Draw only the names in the stack
        paintGL();

       //Back into projection mode to push the matrix
       glMatrixMode(GL_PROJECTION);
       glPopMatrix();

       hits = glRenderMode(GL_RENDER);//number of recognized objects

       printf("\n%d\n",hits);

       //Back to modelview mode
       glMatrixMode(GL_MODELVIEW);

}

绘图功能:

void WorldView::paintGL ()
{
    this->dayOfYear = (this->dayOfYear+1);
    this->hourOfDay = (this->hourOfDay+1) % 24;

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glLoadIdentity();

    // store current matrix
    glMatrixMode( GL_MODELVIEW );
    glPushMatrix( );

    gluLookAt(camPosx ,camPosy ,camPosz,
        camViewx,camViewy,camViewz,
        camUpx, camUpy, camUpz );

    //Draw Axes
    glDisable( GL_LIGHTING );
    glBegin(GL_LINES);
    glColor3f(1.0, 0.0, 0.0);
    glVertex3f(0.0, 0.0, 0.0);
    glVertex3f(10.0, 0.0, 0.0);
    glColor3f(0.0, 1.0, 0.0);
    glVertex3f(0.0, 0.0, 0.0);
    glVertex3f(0.0, 10.0, 0.0);
    glColor3f(0.0, 0.0, 1.0);
    glVertex3f(0.0, 0.0, 0.0);
    glVertex3f(0.0, 0.0, 10.0);
    glEnd();

    //Draw objects we want to pick
    glPushName(0);
    glBegin(GL_TRIANGLES);
        glVertex3d(1,1,1);
        glVertex3d(2,3,2);
        glVertex3d(5,2,2);
    glEnd();
    glPopName();
     glPushName(1);
     glBegin(GL_TRIANGLES);
         glVertex3d(7,-5,1);
         glVertex3d(10,3,2);
         glVertex3d(10,2,2);
     glEnd();
     glPopName();
     glPushName(2);
      glBegin(GL_TRIANGLES);
          glVertex3d(1,-5,7);
          glVertex3d(2,3,9);
          glVertex3d(5,2,9);
      glEnd();
      glPopName();
}

EDIT1:也许完成代码会有所帮助?

初始化器:

void WorldView::initializeGL ()
{

    this->dayOfYear = 0;
    this->hourOfDay = 0;

    // Initialize QGLWidget (parent)
    QGLWidget::initializeGL();

    glShadeModel(GL_SMOOTH);

    // Black canvas
    glClearColor(0.0f,0.0f,0.0f,0.0f);

    // Place light
    glEnable( GL_LIGHTING );
    glEnable( GL_LIGHT0 );
    glEnable(GL_DEPTH_TEST);

    GLfloat light0_position [] = {0.1f, 0.1f, 0.1f, 0.1f};
    GLfloat light_diffuse []={ 1.0, 1.0, 1.0, 1.0 };
    glLightfv ( GL_LIGHT0, GL_POSITION, light0_position );
    glLightfv ( GL_LIGHT0, GL_DIFFUSE, light_diffuse );
}

调整大小:

void WorldView::resizeGL ( int width, int height )
{
    if ((width<=0) || (height<=0))
        return;

    //set viewport
    glViewport(0,0,width,height);

    glMatrixMode(GL_PROJECTION);
        glLoadIdentity();

    //set persepective
    //change the next line order to have a different perspective
    GLdouble aspect_ratio=(GLdouble)width/(GLdouble)height;
    gluPerspective(40.0f, aspect_ratio, 1.0, 100.0);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}
4

1 回答 1

0

使用子弹射线投射而不是 gl_Select 太慢和笨拙。这也将使您摆脱手动调用paintGL 和其他glCalls ......在qt mousepressevent 中。不要这样做!

于 2014-05-24T14:29:32.343 回答