Important: Please read the Qt Code of Conduct -

QT OpenGL object selection

  • Hi,

    I am currently working on Graphics Application which uses QT OpenGL (QOpenGLWidget class) . Actually I have implemented object selection by converting to world coordinates and finding that coordinates within the bounding box. Object is getting selected. Problem is when I click inside the rectangle object, the rectangle object is getting selected. But I want object gets selected only if i click on its edges not inside the rectangle box. How to go about this problem in QT OpenGL ? Please suggest me .

    Thanks in advance

  • Lifetime Qt Champion


    Since you already have the code to find whether the mouse is within the geometry of the object, why not use that information and test the edges of that rectangle.

  • Hi,

    Thanks SGaist. How to find the edges? can I get some code sample in QT OpenGL?

  • Lifetime Qt Champion

    How are you doing it currently ?

  • @SGaist

    I am using the below code to find the World Coordinates

    QVector3D cameraPos = QVector3D(0.0f, 2.0f, 4.0f);
    QVector3D cameraFront = QVector3D(0.0f, 0.0f, 0.0f);
    QVector3D cameraUp = QVector3D(0.0f, 1.0f, 0.0f);

    	QMatrix4x4 viewMatrix;
    	viewMatrix.lookAt(cameraPos, cameraPos + cameraFront, cameraUp);
    	QMatrix4x4 projection;
    	projection.perspective(45.0f, 1.0f * _scrWidth / _scrHeight, 0.1f, 100.0f);
    	qDebug() << _scrWidth << _scrHeight;
            float posZ;
            float posY = _scrHeight - pickBox.pnt1().y - 1.0f;
       	glReadPixels((int)pickBox.pnt1().x, (int)posY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &posZ);
                		qDebug() << 2.0f * posZ - 1.0f;
               QVector4D clickedPointOnScreen(pickBox.pnt1().x, posY, 0.961635, 1.0f);
             	qDebug() << clickedPointOnScreen;

    worldPosition = QVector3D(pickBox.pnt1().x, _scrHeight- pickBox.pnt1().y,posZ).unproject(viewMatrix, projection, QRect(0, 0, _scrWidth,_scrHeight));

    qDebug() << worldPosition.x() << worldPosition.y();

    From this World Position I am finding out the object.

    Can u suggest me how to go about finding the edge of an object?

  • Lifetime Qt Champion

    Sorry I thought you where using Qt3D which may have better tools to do that.

    Can you describe how you are handling your objects ?

  • Sorry for the delay. We are in the process of Migrating the Old OpenGL to Modern OpenGL. The Old openGL code is below. How to replace this selection api glRenderMode(GL_SELECT) in QT OpenGL. Is there any api ? Because the old OpenGL API was giving me the number of hits on the object selected. Based on that we were deciding weather object is getting selected or not. Any idea How to replace this api in QT?

                 glSelectBuffer(_maxBuff, _pickBufferP);
    	**(void) glRenderMode(GL_SELECT);**
    	glOrtho(box.left(), box.right(), box.bottom(),,
    		-10000, 10000);

  • Lifetime Qt Champion

    What is wrong with using this method now ?

  • Because glRenderMode(GL_SELECT) method is deprecated. It is not working in OpenGL 4.2. So I am looking is there any similar method in OpenGL QT?

  • Lifetime Qt Champion

    The Qt OpenGL module does not re-invent the wheel. It makes using OpenGL easier and cross-platform.

    Depending on what you are doing, you might want to check Qt3D.

Log in to reply