Click here to Skip to main content
15,034,692 members
Articles / Desktop Programming / MFC
Posted 29 Oct 2003


52 bookmarked

How to select an object using OpenGL

Rate me:
Please Sign up or sign in to vote.
4.61/5 (19 votes)
29 Oct 2003CPOL1 min read
This article explains how to select an object using OpenGL.

Sample Image


I am Vietnamese and my English is not good. But I hope you will understand my article. I tried to write a game as my project in school. I decided to write it using 3D technique, and I used OpenGL for this. I looked for a way to select an object on a lot object overunder on 3D and I have found it. In this article I want to introduce you to how to do it.

Using the code

In this example I use MFC wizard. All my code was written on CView.

Add variables:

    GLdouble gldAspect;
    GLsizei glnWidth, glnHeight;
    HGLRC hRC;
    Cube cube[4];
    double r;

Cube is defined in MyOpenGL.h.


On another article in CodeProject about OpenGL you can see the code for WM_SIZE and WM_PAINT, WM_CREATE (I use the code again! Thanks to the owner's code!).

Additionally, on WM_CREATE, init for some variables and start the timer for animation.

int CTry_OpenGLView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
     if (CView::OnCreate(lpCreateStruct) == -1)
          return -1;
     // TODO: Add your specialized creation code here
     for(int i=0;i<4;i++) cube[i].x=i*2-3;

     return 0;

Of course, kill timer in OnDestroy().


void CTry_OpenGLView::OnTimer(UINT nIDEvent) 
     if(r>=360) r=0;


In OnEraseBkgnd: delete all (if not, the screen will "flick").

And now, the code to pick an object:

void CTry_OpenGLView::OnLButtonDown(UINT nFlags, CPoint point) 
     // TODO: Add your message handler code here and/or call default
    GLuint selectBuf[BUFSIZE];
    GLint hits;
    GLint viewport[4];

     HDC hDC = ::GetDC(this->m_hWnd);

    glGetIntegerv (GL_VIEWPORT, viewport);
    glSelectBuffer (BUFSIZE, selectBuf);

    glRenderMode(GL_SELECT); // Enter the SELECT render mode

    glMatrixMode (GL_PROJECTION);
    glPushMatrix ();
    glLoadIdentity ();
    gluPickMatrix((GLdouble) point.x, 
          (GLdouble) (viewport[3] - point.y), 
          5.0, 5.0, viewport);
    PaintScreen(GL_SELECT);//function paint the objects.
    glPopMatrix ();
    glFlush ();

    hits = glRenderMode (GL_RENDER);

     /*for(int i=0;i<hits;i++)
     }//select all (include the hide object)!
     if (hits)
          int n=0;double minz=selectBuf[1];
          for(int i=1;i<hits;i++)
               if (selectBuf[1+i*4]<minz) 
     }//only select the object we see (nearest object)!

     wglMakeCurrent( NULL, NULL );
    ::ReleaseDC( this->m_hWnd, hDC );

     CView::OnLButtonDown(nFlags, point);

Points of interest

Is it easy, isn't it? My source has some wastes, I think. Thanks a lot if you can help me to remove it! ^_^


  • This is the first!


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


About the Author

Software Developer (Junior)
Vietnam Vietnam
My subject is Information technology. I like programming and learn more about everything.

Comments and Discussions

QuestionObject picking Pin
CursorByte13-Nov-17 7:00
MemberCursorByte13-Nov-17 7:00 
Questionwell done Pin
wshcdr10-Feb-14 21:47
Memberwshcdr10-Feb-14 21:47 
GeneralNice One Pin
mzajewel12-Jun-12 0:00
Membermzajewel12-Jun-12 0:00 
GeneralMy vote of 4 Pin
chizhaojuan28-Jul-10 16:41
Memberchizhaojuan28-Jul-10 16:41 
GeneralGood 1 OpenGL Pin
M_A_Hameed 8-Apr-08 1:05
MemberM_A_Hameed 8-Apr-08 1:05 
QuestionHow to move an object follow mouse? Pin
zrockmanx21-Nov-06 21:25
Memberzrockmanx21-Nov-06 21:25 
AnswerRe: How to move an object follow mouse? Pin
ICE_WIZARD22-Nov-06 5:14
MemberICE_WIZARD22-Nov-06 5:14 
QuestionRe: How to move an object follow mouse? Pin
zrockmanx26-Nov-06 2:44
Memberzrockmanx26-Nov-06 2:44 
AnswerRe: How to move an object follow mouse? Pin
ICE_WIZARD28-Nov-06 5:12
MemberICE_WIZARD28-Nov-06 5:12 
GeneralgluUnProject Pin
Nigel Atkinson3-Jul-05 16:56
MemberNigel Atkinson3-Jul-05 16:56 
QuestionAnother method to realize the function? Pin
aqing25-Jun-04 20:16
Memberaqing25-Jun-04 20:16 
GeneralNEWBIE Pin
tarzanviet7-Jun-04 15:43
Membertarzanviet7-Jun-04 15:43 
GeneralRe: NEWBIE Pin
zrockmanx21-Nov-06 21:14
Memberzrockmanx21-Nov-06 21:14 
GeneralRe: NEWBIE Pin
ICE_WIZARD22-Nov-06 5:04
MemberICE_WIZARD22-Nov-06 5:04 
GeneralRe: NEWBIE Pin
zrockmanx26-Nov-06 2:49
Memberzrockmanx26-Nov-06 2:49 
QuestionHow to run 2 different(or same) OpenGL objects in one DialogBox? Pin
werter13-May-04 21:16
Memberwerter13-May-04 21:16 
AnswerRe: How to run 2 different(or same) OpenGL objects in one DialogBox? Pin
ICE_WIZARD4-May-04 3:12
MemberICE_WIZARD4-May-04 3:12 
QuestionHow the pros do it Pin
dog_spawn30-Oct-03 5:09
Memberdog_spawn30-Oct-03 5:09 
The method you present is the one commonly found on tutorial websites and university courses, etc. It is good to see OGL articles, so good work.

The pros use a different technique though:

Essentially, a 'ray' is calculated from the mouse position into the scene. The first object that intersects with the 'ray' is the one being clicked on. To implement this properly you need a good matrix class and a good understanding of the model and projection transformations. You have to use a bit of skill when it comes to a large scrolling map (for example a RTS game) as you obviously only want to test against objects that are on the screen rather than the entire map.

On this site someone had a go at it (not very good article)

You will find using the selection buffer technique to be of limited use when you move onto bigger ogl projects.
AnswerRe: How the pros do it Pin
EpicBoy31-Oct-03 4:31
MemberEpicBoy31-Oct-03 4:31 
GeneralRe: How the pros do it Pin
dog_spawn31-Oct-03 4:59
Memberdog_spawn31-Oct-03 4:59 
GeneralRe: How the pros do it Pin
François Gasnier2-Nov-03 12:13
MemberFrançois Gasnier2-Nov-03 12:13 
GeneralRe: How the pros do it Pin
Nigel Atkinson3-Jul-05 16:53
MemberNigel Atkinson3-Jul-05 16:53 

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.