opengl selection. three selection methods color coding (opengl) selection mode (opengl) selection...

19
OpenGL Selection

Upload: britney-robinson

Post on 05-Jan-2016

290 views

Category:

Documents


1 download

TRANSCRIPT

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

OpenGL Selection

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

Three Selection Methods

• Color coding (OpenGL)

• Selection mode (OpenGL)

• Selection ray (generic)

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

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)

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

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

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

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.

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

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(…);

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

An ExampleglRenderMode(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: 1glLoadName(2);//Draw B, B has a name: 2

int hits=glRenderMode(GL_RENDER);//The selections are stored in the buffer

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

In the Selection Buffer

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

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.

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

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.

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

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)

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

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)

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

Basic Idea• Two questions:• How to get this ray• How to do the intersection test

Image Plane

Center of Projection

An image(640*640)

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

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:

w ⋅xw⋅yw⋅zw

⎢⎢⎢⎢

⎥⎥⎥⎥

=M

rxryrz1

⎢⎢⎢⎢

⎥⎥⎥⎥

x=mouse_x×2/screen_width-1y=1-mouse_y×2/screen_height

• We know:

u ⋅rxu⋅ryu⋅rzu

⎢⎢⎢⎢

⎥⎥⎥⎥

=M−1

xyz1

⎢⎢⎢⎢

⎥⎥⎥⎥

Any point on the ray Any vector

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

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:

wp ⋅pxwp ⋅pywp ⋅pzwp

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

=M−1

xy−11

⎢⎢⎢⎢

⎥⎥⎥⎥

wq ⋅qxwq ⋅qywq ⋅qzwq

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

=M−1

xy11

⎢⎢⎢⎢

⎥⎥⎥⎥

rx

ry

rz

1

⎢⎢⎢⎢

⎥⎥⎥⎥

=s

pxpypz1

⎢⎢⎢⎢

⎥⎥⎥⎥

+ (1−s)

qxqyqz1

⎢⎢⎢⎢

⎥⎥⎥⎥

px

py

pz

1

⎢⎢⎢⎢

⎥⎥⎥⎥

=M eye2world

pxpypz1

⎢⎢⎢⎢

⎥⎥⎥⎥

qx

qy

qz

1

⎢⎢⎢⎢

⎥⎥⎥⎥

=M eye2world

qxqyqz1

⎢⎢⎢⎢

⎥⎥⎥⎥

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

To Find The Intersection

• The ray-sphere intersection

r

C

P

Q

R =Ps+Q(1−s), R−C 2 =r2

spx + (1−s)qx −cx( )2+

spy + (1−s)qy −cy( )2+

spz + (1−s)qz −cz( )2=r2

s is the unknown. Solve it!The smallest s (s>0) gives the earliest intersection.

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

To Find The Intersection• The ray-triangle intersection

Q

N =(V1−V0 )×(V2−V0 )(V1−V0 )×(V2−V0 )

V0

V2

V1

P

N

Let N be the triangle normal:

R =Ps+Q(1−s), R−V0( )⋅N=0

R

This is a linear equation. Solve s and find R.

b1 =AV0RV2AV0V1V2

, b2 =AV0V1RAV0V1V2

,

b0 =1−b1 −b2 , Aijk = j−i( )×(k−i)( )⋅Nfor

R is in the triangle, if barycentric Coordinates 0≤b0, b1, b2≤1.

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

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.

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

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.