1

I have 3 OpenGL objects which are shown at the same time. If the user touches any one of them, then that particular OpenGL object alone should display in screen.

Kromster
  • 7,181
  • 7
  • 63
  • 111
Sivakumar
  • 11
  • 1
  • 1
    It's true that the selection buffer is not supported for OpenGL ES, but why not use gluUnProject? – tkerwin Jun 23 '11 at 14:26
  • possible duplicate of [OpenGL ES (iPhone) Touch Picking](http://stackoverflow.com/questions/2231433/opengl-es-iphone-touch-picking) – Brad Larson Jun 24 '11 at 15:29

2 Answers2

4

Just use gluUnProject to convert your touch point to a point on your near clipping plane and a point on your far clipping plane. Use the ray between those two points in a ray-triangle intersection algorithm. Figure out which triangle was closest, and whatever object that triangle is part of is your object. Another approach is to give each object a unique ID color. Then, whenever the user touches the screen, render using your unique ID colors with no lighting, but don't present the render buffer. Now you can just check the color of the pixel where the user touched and compare it against your list of object color ID's. Quick and easy, and it supports up to 16,581,375 unique objects.

Davido
  • 2,913
  • 24
  • 38
  • Hi I included my code in this . i calculated the near plan and far plan. then after this how to find the touch on the opengl object.. could you pls help me.. – Sivakumar Jun 24 '11 at 06:25
  • -(Boolean) checkCollission:(CGPoint)winPos { winPos.y = (float)__viewport[3] - winPos.y; Point3D nearPoint; Point3D farPoint; Point3D rayVector; //Retreiving position projected on near plan gluUnProject( winPos.x, winPos.y , 0, __modelview, __projection, __viewport, &nearPoint.x, &nearPoint.y, &nearPoint.z); //Retreiving position projected on far plan gluUnProject( winPos.x, winPos.y, 1, __modelview, __projection, __viewport, &farPoint.x, &farPoint.y, &farPoint.z); – Sivakumar Jun 24 '11 at 06:25
  • put your code into your original post above with formatting. It makes it a lot easier to read. To specify a line of code, put four spaces in front of it. – Davido Jun 24 '11 at 16:18
1

You would have to parse every object of your scene and check the possible collision of each one of them with the ray you computed thanks to gluUnProject.

Depending on whether you want to select a face or an object, you could test the collision of the ray with bounding volumes (e.g. bounding box) of your objects for efficiency purposes.

JimN
  • 713
  • 1
  • 6
  • 8