Picking in Open GL Yingcai Xiao Picking Identifying

  • Slides: 11
Download presentation
 Picking in Open. GL Yingcai Xiao

Picking in Open. GL Yingcai Xiao

Picking • • Identifying objects in 3 D Two approaches HW: ID-Buffer SW: Selection

Picking • • Identifying objects in 3 D Two approaches HW: ID-Buffer SW: Selection Mode

HW: ID-Buffer • Use a memory buffer of the same resolution as the frame

HW: ID-Buffer • Use a memory buffer of the same resolution as the frame buffer. • Assign each object an ID (name) • Update the ID buffer with the object ID whenever the frame buffer is being updated (after checking and updating the z-buffer) • Return the ID at the cursor location when the user clicks the left button

SW: Selection Mode • Same idea as the ID-buffer but without using the buffer.

SW: Selection Mode • Same idea as the ID-buffer but without using the buffer. • Switch render mode to the selection mode • Set a small picking area around the cursor (e. g. 5 x 5) as the viewport • “Render” each object after pushing its name on the stack • All objects in the small viewport are added into a hit-list. • The ID, zmin and zmax of each hit-object are stored into a predefined selection buffer not • Switch back to regular render mode • Process the hit-list (e. g. display the selected object with highlight)

Implement picking in Open. GL glu. Pick. Matrix(x, y, width, height, viewport); x, y

Implement picking in Open. GL glu. Pick. Matrix(x, y, width, height, viewport); x, y should be set as the mouse location: x. Mouse and y. Mouse width, height are the size of the picking box gl. Get. Integerv(GL_VIEWPORT, viewport); gl. Init. Names(); gl. Push. Name(ID);

Example 1: void pick(x. Pos, y. Pos) { gl. Select. Buffer(LENGTH, select. Buff); gl.

Example 1: void pick(x. Pos, y. Pos) { gl. Select. Buffer(LENGTH, select. Buff); gl. Render. Mode(GL_SELECTION); gl. Matrix. Mode(GL_PROJECTION); gl. Load. Identity(); glu. Pick. Matrix(x, y, 2, 2, viewport); glu. Perspective(fovy, aspect, near, far); Render. Scene(); hits = gl. Render. Mode(GL_RENDER); Cout << we pick << select. Buff[3] << object; }

Example 2: void Object. Manager: : select. Object(float x, float y) { GLuint buff[64]

Example 2: void Object. Manager: : select. Object(float x, float y) { GLuint buff[64] = {0}; GLint hits, view[4]; gl. Select. Buffer(64, buff); gl. Get. Integerv(GL_VIEWPORT, view); gl. Render. Mode(GL_SELECT); gl. Init. Names(); gl. Push. Name(0); gl. Matrix. Mode(GL_PROJECTION); gl. Push. Matrix(); gl. Load. Identity(); glu. Pick. Matrix(x, y, 0. 05 , view); glu. Ortho 2 D(0, 800, 0, 600);

Example 2: glut. Swap. Buffers(); draw. Objects(); gl. Matrix. Mode(GL_PROJECTION); gl. Pop. Matrix(); hits

Example 2: glut. Swap. Buffers(); draw. Objects(); gl. Matrix. Mode(GL_PROJECTION); gl. Pop. Matrix(); hits = gl. Render. Mode(GL_RENDER); list_hits(hits, buff); gl. Matrix. Mode(GL_MODELVIEW); }

Example 2: void Object. Manager: : draw. Objects() { for (list<Game. Object*>: : iterator

Example 2: void Object. Manager: : draw. Objects() { for (list<Game. Object*>: : iterator ii = Objects. begin(); ii != Objects. end(); ++ii) { gl. Load. Name((*ii)->get. Id()); gl. Push. Matrix(); (*ii)->draw. Object(); gl. Pop. Matrix(); } }

Example 2: void Object. Manager: : list_hits(GLint hits, GLuint *names) { for (int i

Example 2: void Object. Manager: : list_hits(GLint hits, GLuint *names) { for (int i = 0; i<hits; i++) { for (list<Game. Object*>: : iterator ii = Objects. begin(); ii != Objects. end(); ++ii) { if ((GLubyte)names[i * 4 + 3] == (*ii)->get. Id()) (*ii)->on. Mouse(); } }

Example 2: printf("%d hits: n", hits); for (i = 0; i < hits; i++)

Example 2: printf("%d hits: n", hits); for (i = 0; i < hits; i++) printf( "Number: %dn" "Min Z: %dn" "Max Z: %dn" "Name on stack: %dn", (GLubyte)names[i * 4], (GLubyte)names[i * 4 + 1], (GLubyte)names[i * 4 + 2], (GLubyte)names[i * 4 + 3] ); printf("n"); }