Slide 1

Slide 1 text

13. Object selection Tatsuya Yatagawa

Slide 2

Slide 2 text

Object selection in OpenGL ◼ Using selection buffer ◼ This is available in legacy OpenGL (without shader), but it is rather complicated and unintuitive. ◼ Write labels to off-screen buffer ◼ If shader program is available, it is more intuitive. ◼ Draw a label (i.e., an index to identify the object) on screen. ◼ It’s easy! You can do it just by not calling “glfwSwapBuffers.” ◼ If target buffer is off-screen buffer, it does not affect visible objects! Computer Graphics Course @Waseda University 2

Slide 3

Slide 3 text

Workflow to object selection ◼ Workflow 1. Detect mouse click (by mouse event callback). 2. Write object index as pixel color (to off-screen buffer). 3. Pick up pixel values using glReadPixels. 4. Check pixel value and identify selected object. Computer Graphics Course @Waseda University 3

Slide 4

Slide 4 text

Write selection label as pixel color ◼ Pass selection label as uniform variable Computer Graphics Course @Waseda University Set “selectID” as positive index if in selection mode. Otherwise set -1. 4

Slide 5

Slide 5 text

Process in shader programs ◼ Vertex shader ◼ Nothing is changed. Perform coordinate transformation. ◼ Fragment shader Computer Graphics Course @Waseda University Transform “selectID” to floating point number in [0, 1] when ID is positive. 5 Note: float outputs are automatically converted to UINT8 by multiplying 255.

Slide 6

Slide 6 text

Pick up pixel values ◼ glReadPixels ◼ Parameters ◼ 1st: X coordinate of upper left pixel of buffer area to be read. ◼ 2nd: Y coordinate of upper left pixel of buffer area to be read. ◼ 3rd: Width of buffer area to be read. ◼ 4th: Height of buffer area to be read. ◼ 5th: Color format of buffer data (typically GL_RGBA). ◼ 6th: Scalar type of buffer data (typically GL_UNSIGNED_BYTE). ◼ 7th: Pointer to data array in which the read data is stored. Computer Graphics Course @Waseda University 6

Slide 7

Slide 7 text

Pick up pixel value(s) ◼ Straightforward approach ◼ Read entire buffer, and pick up its value at clicked pixel. Problem ◼ Data transfer between RAM and GPU memory is time consuming. ◼ Reading entire buffer is not efficient. Computer Graphics Course @Waseda University Be careful that Y coordinate is inversed! 7

Slide 8

Slide 8 text

Pick up pixel value(s) ◼ More efficient approach ◼ Only read value at clicked pixel! Computer Graphics Course @Waseda University 8

Slide 9

Slide 9 text

Whole code for object selection Computer Graphics Course @Waseda University 9

Slide 10

Slide 10 text

Result ◼ Object index is printed when each cube is clicked Computer Graphics Course @Waseda University 10

Slide 11

Slide 11 text

Exercise 13-1 ◼ Select cube faces rather than whole cube. Then, highlight only the selected face. When non-cube region is clicked, highlight entire cube. ◼ You should draw only a single cube (do not have to draw multiple cubes as in the example). Computer Graphics Course @Waseda University 11