Presentation is loading. Please wait.

Presentation is loading. Please wait.

OpenGL Selection. Three Selection Methods Color coding (OpenGL) Selection mode (OpenGL) Selection ray (generic)

Similar presentations


Presentation on theme: "OpenGL Selection. Three Selection Methods Color coding (OpenGL) Selection mode (OpenGL) Selection ray (generic)"— Presentation transcript:

1 OpenGL Selection

2 Three Selection Methods Color coding (OpenGL) Selection mode (OpenGL) Selection ray (generic)

3 Method 1: Color Coding To make a selection at (x, y): Draw a color-coded image in the mouse callback function, Read the pixel value from the back frame buffer, The pixel color tells you the selection. (x, y)

4 Method 1: Color Coding To read the pixel value: Only the visible object will be selected The nearest object (if using the depth test) The latest object (otherwise) To avoid the color-coded image from display Remember to use double buffering Don’t swap the frame buffer glReadPixels(x, y, width, height, format, type, *data) glReadBuffer(GL_BACK) default

5 Method 2: Selection Mode The idea is similar to color coding. You will still need to pretend to draw something. However, No need to assign unique colors. No need to use double buffering. The selection can ignore the visibility and pick multiple objects. OpenGL can help you do it.

6 Method 2: Selection Mode To start the fake drawing process: To specify the selection region: To store the selections: To give each object a name: glRenderMode(GL_SELECT) gluPickMatrix(x, y, width, height, viewport) glGetIntegerv(GL_VIEWPORT, viewport) glSelectBuffer(buffer_length, buffer) glInitNames(); glPushName(…); glLoadName(…);

7 An Example glRenderMode(GL_SELECT); int selectBuffer[100]; glSelectBuffer(100, selectBuffer); glMatrixMode(GL_PROJECTION); glLoadIdentity(); int vp[4]; glGetIntegerv(GL_VIEWPORT, vp); gluPickMatrix(x, y, 5, 5, vp); gluPerspective(10, 1, 1, 100); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); //Change camera view glInitNames(); glPushName(0); glLoadName(1); //Draw A, A has a name: 1 glLoadName(2); //Draw B, B has a name: 2 int hits=glRenderMode(GL_RENDER); //The selections are stored in the buffer

8 In the Selection Buffer

9 Some Tips The fake drawing process is the same as the regular drawing process, except for those selection functions. An object can have multiple names. If you don’t reset the projection matrix in your display function, remember to restore it after you finish the selection projection (using glPushMatrix and glPopMatrix). When multiple objects are selected, you can use the depth information to know which one is visible.

10 Method 3: Selection Ray It is a generic method, not limited to OpenGL. It is a basic component in ray tracing (an advanced rendering algorithm). Relies on the projection matrix and ray-object intersection tests. So it needs some effort to implement.

11 Basic Idea A pixel on the image is actually a ray. Any point on this ray will be projected onto this pixel. Image Plane Center of Projection An image (640*640)

12 Basic Idea So to find the selection, we can just test the intersection between the ray and any object. Image Plane Center of Projection An image (640*640)

13 Basic Idea Two questions: How to get this ray How to do the intersection test Image Plane Center of Projection An image (640*640)

14 To Get the Selection Ray Given a projection matrix M, which can be obtained by glGetFloatv(GL_PROJECTION_MATRIX, M) Given a selection pixel ( x, y ), for -1<x, y<1, which can be obtained by: x =mouse_x×2/screen_width-1 y =1-mouse_y×2/screen_height We know: Any point on the rayAny vector

15 To Get the Selection Ray So we can find two points on the ray: They need be transformed from the eye to the world coordinate system (how?): Finally, the ray is:

16 To Find The Intersection The ray-sphere intersection r C P Q s is the unknown. Solve it! The smallest s ( s>0 ) gives the earliest intersection.

17 To Find The Intersection The ray-triangle intersection Q V0V0 V2V2 V1V1 P N Let N be the triangle normal: R This is a linear equation. Solve s and find R. for R is in the triangle, if barycentric Coordinates 0≤b 0, b 1, b 2 ≤1.

18 Method 3: Selection Ray It does not need to read the frame buffer. It is flexible, independent of the rendering system. Each primitive type needs a ray intersection algorithm. Needs multiple matrix computation steps, so it is relatively slow.

19 Method 3: Selection Ray When the scene contains multiple objects? Test each of them and find the nearest intersection. It is slow, how to make it faster? Use Bounding Volume Hierarchy.


Download ppt "OpenGL Selection. Three Selection Methods Color coding (OpenGL) Selection mode (OpenGL) Selection ray (generic)"

Similar presentations


Ads by Google