Click here to Skip to main content
6,632,253 members and growing! (18,039 online)
Email Password   helpLost your password?
Multimedia » OpenGL » General     Intermediate License: The Code Project Open License (CPOL)

Mouse Selection in OpenGL Scene

By Bahrudin Hrnjica

One another approach for picking objects with the mouse in OpenGL scene.
VC7Win2K, WinXP, MFC, OpenGL, Dev
Posted:25 Oct 2002
Views:128,897
Bookmarked:44 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
18 votes for this article.
Popularity: 5.74 Rating: 4.57 out of 5
1 vote, 6.7%
1

2
1 vote, 6.7%
3
1 vote, 6.7%
4
12 votes, 80.0%
5

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)

About the Author

Bahrudin Hrnjica


Member
Independent Computer Software Professional with .NET and C#.
Occupation: Software Developer
Location: Bosnia And Herzegovina Bosnia And Herzegovina

Other popular OpenGL articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 25 (Total in Forum: 25) (Refresh)FirstPrevNext
Questioni want help about mousesel project Pinmembersuuunil20:18 25 May '09  
GeneralGood Job~! Pinmemberdefineconst0:11 30 Apr '09  
Generalanother way? PinmemberPorolfi23:31 4 Jun '07  
GeneralRe: another way? PinmemberPorolfi1:55 5 Jun '07  
Generalhow about gluUnProject PinmemberMerlinblack17:26 11 Apr '05  
GeneralRe: how about gluUnProject PinmemberBahrudin Hrnjica21:02 13 Apr '05  
GeneralRe: how about gluUnProject PinmemberMerlinblack18:47 14 Apr '05  
GeneralRe: how about gluUnProject PinmemberBahrudin Hrnjica20:52 17 Apr '05  
GeneralFinishing the project in VC6? Thank you very much! Pinmemberaqing19:57 19 Jun '04  
GeneralRe: Finishing the project in VC6? Thank you very much! PinmemberBahrudin Hrnjica8:45 20 Jun '04  
GeneralRe: Finishing the project in VC6? Thank you very much! Pinmemberaqing17:51 20 Jun '04  
GeneralRe: Finishing the project in VC6? Thank you very much! PinsussAnonymous3:34 21 Jun '04  
GeneralRe: Finishing the project in VC6? Thank you very much! Pinmemberaqing21:48 25 Jun '04  
Generalfile not found Pinmembergok20:33 16 Feb '05  
GeneralProblem existing after converting the code form the .NET to VC6 by prjconverter Pinmemberaqing23:23 31 May '04  
GeneralRe: Problem existing after converting the code form the .NET to VC6 by prjconverter PinmemberBahrudin Hrnjica3:26 1 Jun '04  
GeneralRe: Problem existing after converting the code form the .NET to VC6 by prjconverter PinmemberMerlinblack17:42 11 Apr '05  
GeneralHow to run 2 different(or same) OpenGL objects in one DialogBox? Pinmemberwerter122:17 3 May '04  
GeneralRe: How to run 2 different(or same) OpenGL objects in one DialogBox? PinmemberBahrudin Hrnjica10:54 25 May '04  
GeneralOpenGL + JAI / JAVA Pinmemberprabu_univ3:10 12 Apr '04  
Generalhow to run i t Pinmemberwjiybb21:21 25 Nov '03  
GeneralRe: how to run i t PinmemberBahrudin Hrnjica12:22 26 Nov '03  
GeneralRe: how to run i t Pinmemberinfinitecmdz23:20 23 Oct '06  
GeneralIncomplete source code PinmemberAnonymous8:46 30 Jan '03  
GeneralRe: Incomplete source code PinsussAnonymous10:39 19 Sep '03  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 25 Oct 2002
Editor: Smitha Vijayan
Copyright 2002 by Bahrudin Hrnjica
Everything else Copyright © CodeProject, 1999-2009
Web18 | Advertise on the Code Project