0% found this document useful (0 votes)
42 views19 pages

Opengl Selection Opengl Selection

The document describes 3 methods for OpenGL object selection: 1. Color coding assigns unique colors and reads pixels to determine selection. 2. Selection mode enters a fake drawing mode to store selections without colors. 3. Selection ray computes the ray from the mouse and tests intersections to select objects. This is independent of OpenGL but requires ray-object algorithms and matrix computations.

Uploaded by

sercan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
42 views19 pages

Opengl Selection Opengl Selection

The document describes 3 methods for OpenGL object selection: 1. Color coding assigns unique colors and reads pixels to determine selection. 2. Selection mode enters a fake drawing mode to store selections without colors. 3. Selection ray computes the ray from the mouse and tests intersections to select objects. This is independent of OpenGL but requires ray-object algorithms and matrix computations.

Uploaded by

sercan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 19

OpenGL Selection

OpenGLSelection
ThreeSelectionMethods

Colorcoding(OpenGL)
Color coding (OpenGL)

Selectionmode(OpenGL)
S l i d (O G )

Selectionray(generic)
Method1:ColorCoding
Tomakeaselectionat(x,y):
Drawacolorcodedimageinthemouse
D l d di i h
callbackfunction,
Readthepixelvaluefromthebackframe
R d h i l l f h b kf
buffer,
Thepixelcolortellsyoutheselection.
Th i l l ll h l i

(x,y)
Method1:ColorCoding
Toreadthepixelvalue:
glReadPixels(x,y,width,height,format,type,*data)

glReadBuffer(GL BACK)
glReadBuffer(GL_BACK)
default
Onlythevisibleobjectwillbeselected
y j
Thenearestobject(ifusingthedepthtest)
Thelatestobject(otherwise)
Toavoidthecolorcodedimagefromdisplay
Remembertousedoublebuffering
Dontswaptheframebuffer
Method2:SelectionMode

Theideaissimilartocolorcoding.Youwillstill
g
needtopretendtodrawsomething.

However,
Noneedtoassignuniquecolors.
Noneedtousedoublebuffering.
N dt d bl b ff i
Theselectioncanignorethevisibilityandpick
multiple objects
multipleobjects.
OpenGLcanhelpyoudoit.
Method2:SelectionMode
Tostartthefakedrawingprocess:
glRenderMode(GL SELECT)
glRenderMode(GL_SELECT)
Tospecifytheselectionregion:
gluPickMatrix(x y width height viewport)
gluPickMatrix(x,y,width,height,viewport)
glGetIntegerv(GL_VIEWPORT,viewport)
Tostoretheselections:
To store the selections:
glSelectBuffer(buffer_length,buffer)
Togiveeachobjectaname:
To give each object a name:
glInitNames();
glPushName( );
glPushName();
glLoadName();
AnExample
glRenderMode(GL_SELECT);

int selectBuffer[100];
glSelectBuffer(100,selectBuffer);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
int vp[4];
glGetIntegerv(GL VIEWPORT vp);
glGetIntegerv(GL_VIEWPORT,vp);
gluPickMatrix(x,y,5,5,vp);
gluPerspective(10,1,1,100);

glMatrixMode(GL_MODELVIEW);
g ( _ )
glLoadIdentity();
//Changecameraview

glInitNames();
glPushName(0);
lP hN (0)
glLoadName(1);
//DrawA,Ahasaname:1
glLoadName(2);
//Draw B B has a name: 2
//DrawB,Bhasaname:2

int hits=glRenderMode(GL_RENDER);
//Theselectionsarestoredinthebuffer
IntheSelectionBuffer
SomeTips
Thefakedrawingprocessisthesameasthe
regular drawing process except for those selection
regulardrawingprocess,exceptforthoseselection
functions.
Anobjectcanhavemultiplenames.
A bj t h lti l
Ifyoudontresettheprojectionmatrixinyour
di l f ti
displayfunction,remembertorestoreitafteryou
b t t it ft
finishtheselectionprojection(usingglPushMatrix
g p
andglPopMatrix).
Whenmultipleobjectsareselected,youcanuse
thedepthinformationtoknowwhichoneis
visible.
Method3:SelectionRay

Itisagenericmethod,notlimitedtoOpenGL.
It i i th d t li it d t O GL
It
Itisabasiccomponentinraytracing(an
is a basic component in ray tracing (an
advancedrenderingalgorithm).
Reliesontheprojectionmatrixandrayobject
intersectiontests.Soitneedssomeeffortto
implement.
BasicIdea
Apixelontheimageisactuallyaray.Anypoint
y p j p
onthisraywillbeprojectedontothispixel.

Animage
An image
(640*640)

Centerof
Projection

Image Plane
ImagePlane
BasicIdea
Sotofindtheselection,wecanjusttestthe
y y j
intersectionbetweentherayandanyobject.

Animage
An image
(640*640)

Centerof
Projection

Image Plane
ImagePlane
BasicIdea
Twoquestions:
Howtogetthisray
Howtodotheintersectiontest

Animage
An image
(640*640)

Centerof
Projection

Image Plane
ImagePlane
ToGettheSelectionRay
GivenaprojectionmatrixM,whichcanbeobtainedby
glGetFloatv(GL_PROJECTION_MATRIX,M)
Givenaselectionpixel(x, y),for1<x,y<1,whichcanbe
obtainedby:
x=mouse_x2/screen_width1
y=1mouse_y2/screen_height
Weknow:

w x rx u rx x
w y r u r y
M y y M 1
w z rz u rz z

w 1 u 1
Anypointontheray Anyvector
ToGettheSelectionRay
Sowecanfindtwopointsontheray:
w p px x wq qx x

w p p y M 1 y wq q y M 1 y
w p 1 w q 1
p z q z
w p 1 wq 1
Theyneedbetransformedfromtheeyetothe
worldcoordinatesystem(how?):
p x p q x q x x
p p q q
M
y y M
y y
pz eye2world
pz qz eye2world
qz

1
1 1
1
Finally,therayis:
y y
rx px qx
r p q
y s y (1 s) y
rz pz qz

1
1
1
ToFindTheIntersection
Theraysphereintersection
Q
2
R P s Q(1 s), R C r 2
r
spx (1 s)qx cx 2

C
sp
2
y (1 s)qy cy

sp z (1 s)qz cz r 2
2

P
s istheunknown.Solveit!
Th
Thesmallests ( >0) i
ll t (s>0)givestheearliestintersection.
th li t i t ti
ToFindTheIntersection
Theraytriangleintersection
(V1 V0 ) (V2 V0 )
Let N be the triangle normal: N
LetNbethetrianglenormal:
(V1 V0 ) (V2 V0 )
P
R P s Q(1 s), R V0 N 0
Thisisalinearequation. N
and find R
Solve s andfindR.
Solves V2

R
b1 AV0 RV2 AV0 V1V2 , b2 AV0 V1R AV0 V1V2 ,
b0 1 b1 b2 , for Aijk j i (k i) N V0
Q
V1

R isinthetriangle,ifbarycentric
is in the triangle if barycentric
Coordinates0b0, b1, b21.
Method3:SelectionRay

Itdoesnotneedtoreadtheframebuffer.
It does not need to read the frame buffer
Itisflexible,independentoftherenderingsystem.

Eachprimitivetypeneedsarayintersection
algorithm.
l ith
Needsmultiplematrixcomputationsteps,soitis
relativelyslow.
l ti l l
Method3:SelectionRay
Whenthescenecontainsmultipleobjects?
Testeachofthemandfindthenearest
T t h f th d fi d th t
intersection.
Itisslow,howtomakeitfaster?Use
BoundingVolumeHierarchy.

You might also like