Click here to Skip to main content
11,575,635 members (63,949 online)
Click here to Skip to main content

Mouse Selection in OpenGL Scene

, 25 Oct 2002 CPOL 190.6K 10.4K 75
Rate this:
Please Sign up or sign in to vote.
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.

 
//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.

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)

Share

About the Author

Bahrudin Hrnjica
Software Developer (Senior)
Bosnia And Herzegovina Bosnia And Herzegovina
Senior Software Developer at daenet.eu and Microsoft MVP for Visual C#.

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 5 Pin
Mohamed Aissiou21-Dec-12 20:24
memberMohamed Aissiou21-Dec-12 20:24 
Generalabout gluUnProject Pin
feifly26-Apr-10 16:02
memberfeifly26-Apr-10 16:02 
Questioni want help about mousesel project Pin
suuunil25-May-09 19:18
membersuuunil25-May-09 19:18 
GeneralGood Job~! Pin
defineconst29-Apr-09 23:11
memberdefineconst29-Apr-09 23:11 
Questionanother way? Pin
Porolfi4-Jun-07 22:31
memberPorolfi4-Jun-07 22:31 
AnswerRe: another way? Pin
Porolfi5-Jun-07 0:55
memberPorolfi5-Jun-07 0:55 
Questionhow about gluUnProject Pin
Merlinblack11-Apr-05 16:26
memberMerlinblack11-Apr-05 16:26 
I've been looking at something similar. I was thinking of doing this.

1. Get a bounding rectangle from the user ie click-drag, so I have two 2d points in window co-ords.
2. Give both points z co-ords, the first with the smallest possible z and the other with the biggest.
3. Use gluUnproject which is very similar to gluProject except it does the reverse, to give two points in object or world space.
4. Use them to define a bounding box.
5. Select everything inside the box.

Almost seems easy! Wink | ;-) A lot simpler than the selection or feedback modes.

What do you think?

"Land a'hoy!" * CRASH * "I should av said that sooner eh?" - Eckles, The Goon Show
AnswerRe: how about gluUnProject Pin
Bahrudin Hrnjica13-Apr-05 20:02
memberBahrudin Hrnjica13-Apr-05 20:02 
GeneralRe: how about gluUnProject Pin
Merlinblack14-Apr-05 17:47
memberMerlinblack14-Apr-05 17:47 
GeneralRe: how about gluUnProject Pin
Bahrudin Hrnjica17-Apr-05 19:52
memberBahrudin Hrnjica17-Apr-05 19:52 
GeneralFinishing the project in VC6? Thank you very much! Pin
aqing19-Jun-04 18:57
memberaqing19-Jun-04 18:57 
GeneralRe: Finishing the project in VC6? Thank you very much! Pin
Bahrudin Hrnjica20-Jun-04 7:45
memberBahrudin Hrnjica20-Jun-04 7:45 
GeneralRe: Finishing the project in VC6? Thank you very much! Pin
aqing20-Jun-04 16:51
memberaqing20-Jun-04 16:51 
GeneralRe: Finishing the project in VC6? Thank you very much! Pin
Anonymous21-Jun-04 2:34
sussAnonymous21-Jun-04 2:34 
GeneralRe: Finishing the project in VC6? Thank you very much! Pin
aqing25-Jun-04 20:48
memberaqing25-Jun-04 20:48 
Generalfile not found Pin
gok16-Feb-05 19:33
membergok16-Feb-05 19:33 
GeneralProblem existing after converting the code form the .NET to VC6 by prjconverter Pin
aqing31-May-04 22:23
memberaqing31-May-04 22:23 
GeneralRe: Problem existing after converting the code form the .NET to VC6 by prjconverter Pin
Bahrudin Hrnjica1-Jun-04 2:26
memberBahrudin Hrnjica1-Jun-04 2:26 
GeneralRe: Problem existing after converting the code form the .NET to VC6 by prjconverter Pin
Merlinblack11-Apr-05 16:42
memberMerlinblack11-Apr-05 16:42 
QuestionHow to run 2 different(or same) OpenGL objects in one DialogBox? Pin
werter13-May-04 21:17
memberwerter13-May-04 21:17 
AnswerRe: How to run 2 different(or same) OpenGL objects in one DialogBox? Pin
Bahrudin Hrnjica25-May-04 9:54
memberBahrudin Hrnjica25-May-04 9:54 
GeneralOpenGL + JAI / JAVA Pin
prabu_univ12-Apr-04 2:10
memberprabu_univ12-Apr-04 2:10 
Questionhow to run i t Pin
wjiybb25-Nov-03 20:21
memberwjiybb25-Nov-03 20:21 
AnswerRe: how to run i t Pin
Bahrudin Hrnjica26-Nov-03 11:22
memberBahrudin Hrnjica26-Nov-03 11:22 
GeneralRe: how to run i t Pin
infinitecmdz23-Oct-06 22:20
memberinfinitecmdz23-Oct-06 22:20 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150624.2 | Last Updated 26 Oct 2002
Article Copyright 2002 by Bahrudin Hrnjica
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid