Click here to Skip to main content
15,867,750 members
Articles / Desktop Programming / MFC
Article

Mouse Selection in OpenGL Scene

Rate me:
Please Sign up or sign in to vote.
4.82/5 (20 votes)
25 Oct 2002CPOL3 min read 248.1K   12.5K   82   27
One another approach for picking objects with the mouse in OpenGL scene.

Sample Image - OpenGLMouseSellection.jpg

Introduction

This article explains how to implement mouse selection in OpenGL scene, as well as moving, rotating and zooming OpenGL scene and sterilization in MDI applications. The project uses MFC Template classes to hold object primitives. There are several methods how you can implement selection in OpenGL scene. Here is a short description.

Selection

Selection method is OpenGL mechanism and it works in the following way. Draw your scene into the framebuffer, and then you enter selection mode and redraw the scene. When you're in selection mode, the contents of the framebuffer doesn't change until you exit selection mode. When you exit selection mode, OpenGL returns a list of the primitives that intersect the viewing volume. A selection hit is caused by intersection between primitive and viewing volume. The list of primitives is actually returned as an array of integers. You construct the name stack by loading names onto it as you issue primitive drawing commands while in selection mode. Thus, when the list of names is returned, you can use it to determine which primitives might have been selected on the screen by the user.

Feedback

Feedback is similar to selection in that once you're in either mode, no pixels are produced and the screen is frozen. Drawing does not occur; instead, information about primitives that would have been rendered is sent back to the application. The key difference between selection and feedback modes is what information is sent back. In feedback mode, information about transformed primitives is sent back to an array of floating-point values. The values sent back to the feedback array consist of tokens that specify what type of primitive (point, line, polygon, image, or bitmap) has been processed and transformed, followed by vertex, color, or other data for that primitive.

Pick ray

Yet another method involves shooting a pick ray through the mouse location and testing for intersections with the currently displayed objects. OpenGL doesn't contain any function for ray intersections. To generate pick ray, you need OpenGL matrix transformation, and some knowledge about linear algebra.

Implementation

The method which I implemented for mouse selection involves projection of small area from the object (primitive) to the screen and testing the mouse click. The code below shows how you can project the object point to the screen.

C++
//Needs to retrieve OpenGL transformation matrices before projection
glGetIntegerv(GL_VIEWPORT, m_spheres->viewport);
glGetDoublev(GL_MODELVIEW_MATRIX, m_spheres->modelMatrix);
glGetDoublev(GL_PROJECTION_MATRIX, m_spheres->projMatrix);

//Projection 
gluProject(m_spheres->m_xc,m_spheres->m_yc,
        m_spheres->m_zc,m_spheres->modelMatrix, 
        m_spheres->projMatrix,m_spheres->viewport,
        &m_spheres->winx,&y,&m_spheres->winz);
m_spheres->winy=m_spheres->viewport[3] - (GLint) y - 1;

Every shape object contains projected point and rectangle. When you want to select shape, use the LButtonDown message handler and test its point in rectangle. The code below shows the test for Sphere object.

C++
void CMyView::OnLButtonDown(UINT nFlags, CPoint point) 
{
    m_LeftButtonDown = TRUE;
    m_LeftDownPos = point;
    SetCapture();

    CMyDoc* pDoc = (CMyDoc*)GetDocument();
    ASSERT(pDoc);
    CTypedPtrList<CObList,CSphere*>& sphereList =pDoc->m_SphereList;
    POSITION pos = sphereList.GetHeadPosition();
    while (pos != NULL)
    {
        CSphere* spheres = sphereList.GetNext(pos);
        spheres->m_Select=FALSE;
        if(spheres->GetRect().PtInRect(point))
        {
            m_sph=spheres;
            m_sph->m_Select=TRUE;
        }
    }

    Invalidate();
    CView::OnLButtonDown(nFlags, point);
}

Summary

The problem of the implementation is how much can be the projected area of the object. If you set projected area too big, it will be possible that two different areas can be merged. If projected area is too small it's hard to pick it with the mouse.

References

  1. OpenGL Programming Guide The Official Guide to Learning OpenGL, Version 1.1
  2. Open GL Super Bible (Publisher: Macmillan Computer Publishing) Author(s): Waite group Press ISBN: 1571690735 Publication date: 08/01/96

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Bosnia and Herzegovina Bosnia and Herzegovina
Bahrudin Hrnjica holds a Ph.D. degree in Technical Science/Engineering from University in Bihać.
Besides teaching at University, he is in the software industry for more than two decades, focusing on development technologies e.g. .NET, Visual Studio, Desktop/Web/Cloud solutions.

He works on the development and application of different ML algorithms. In the development of ML-oriented solutions and modeling, he has more than 10 years of experience. His field of interest is also the development of predictive models with the ML.NET and Keras, but also actively develop two ML-based .NET open source projects: GPdotNET-genetic programming tool and ANNdotNET - deep learning tool on .NET platform. He works in multidisciplinary teams with the mission of optimizing and selecting the ML algorithms to build ML models.

He is the author of several books, and many online articles, writes a blog at http://bhrnjica.net, regularly holds lectures at local and regional conferences, User groups and Code Camp gatherings, and is also the founder of the Bihac Developer Meetup Group. Microsoft recognizes his work and awarded him with the prestigious Microsoft MVP title for the first time in 2011, which he still holds today.

Comments and Discussions

 
GeneralIncomplete source code Pin
Member 7720130-Jan-03 7:46
Member 7720130-Jan-03 7:46 
GeneralRe: Incomplete source code Pin
Anonymous19-Sep-03 9:39
Anonymous19-Sep-03 9:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.