0% found this document useful (0 votes)
16 views13 pages

Class6 Picking

Uploaded by

jakox65758
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views13 pages

Class6 Picking

Uploaded by

jakox65758
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Computer Graphics II

Picking
Object Picking

Mankyu Sung 1
Computer Graphics II

Object Picking
 For many cases, we should be able to change the positions of objects with mouse
 The framework I provided have functions for object picking. You must know how to
use them
 These functions are not related to Physical Simulation directly. However, it is
required for interaction.

Mankyu Sung 2
Computer Graphics II

Basic idea of Object Picking 2

 Object picking method press ctrl button


1
 Numbering all objects in a scene (1,2,…) and dragging
 Shoot a ray at the mouse position and get the number when
the ray intersects one of the objects

 Object positon change


 Just picking and dragging of mouse changes the object’s just drag
positon on the plane (x,z)
 With pressing ctrl button and dragging of mouse changes the
object’s position on y axis

Mankyu Sung 3
Computer Graphics II

step 1
 Numbering : in the draw(), provide a number for each object

if (!shadow)
glLoadName(1); (set the number 1)

 You must to give different numbers for different objects

for (unsigned int i = 0; i < m_movers.size(); i++) {


if (!shadow)
glLoadName(i+1);
m_movers[i]->draw(shadow);
}

Mankyu Sung 4
Computer Graphics II
step 2
 Call MyGlWindow::doPick(), when you click a left mouse button
 Go to MyGlWindow::handle()
 int MyGlWindow::handle(int e)
{
switch (e) {
case FL_SHOW:
show();
return 1;
case FL_PUSH: //mouse pushing event
{
m_pressedMouseButton = Fl::event_button(); //which button? 1(left), 2(middle), 3(rig
m_lastMouseX = Fl::event_x(); //mouse x pos
When you click m_lastMouseY = Fl::event_y(); //mouse y pos
}
damage(1);
return 1;
case FL_RELEASE: //mouse releasing event
When you release a button m_pressedMouseButton = -1;
damage(1);
return 1;
When you drag a mouse case FL_DRAG: // if the user drags the mouse
{
Mankyu Sung 5
Computer Graphics II

step2
 Call doPick() on GL_PUSH event
case FL_PUSH:
{
m_pressedMouseButton = Fl::event_button();
m_lastMouseX = Fl::event_x();
m_lastMouseY = Fl::event_y();

if (m_pressedMouseButton == 1) { Right mouse click -> 3


doPick(); Left mouse click -> 1
Declare int selected
if (selected >= 0) {
std::cout << "picked" << std::endl;
for storing
}
selected object number
damage(1);
return 1;
(initial value = -1)
};
break;
}

Mankyu Sung 6
Computer Graphics II

step3
 Modify doPick()
….
setProjection(false); set this value to false
GLuint buf[100];
glSelectBuffer(100, buf);
glRenderMode(GL_SELECT); Ex)
glInitNames(); 1. When there is only one object
glPushName(0); m_mover->draw(0);
Call draw() with (shadow = 0)
2. When there are several objects
//why shadow=0? Because object itself
//must be pickable (not its shadow) for(int i=0; i<movers.size(); i++)
{
movers[i]->draw(0);
int hits = glRenderMode(GL_RENDER); }
//note that each mover must have a different number

Mankyu Sung 7
Computer Graphics II

step3
 Modify doPick()

int hits = glRenderMode(GL_RENDER);


if (hits) {
// warning; this just grabs the first object hit - if there
// are multiple objects, you really want to pick the closest
// one - see the OpenGL manual
// remember: we load names that are one more than the index
selected = buf[3] - 1;
} Assign picked object number to
else {// nothing hit, nothing selected
selected = -1;
selected
}

Mankyu Sung 8
Computer Graphics II

step4
 Dragging Event processing : Choice between camera control and object moving
case FL_DRAG: // if the user drags the mouse
{

if (selected >= 0 && m_pressedMouseButton == 1) {// if we pick a some object

//When you pick up an object

damage(1);
}
else {

//Otherwise, change camera


damage(1);
}

Mankyu Sung 9
Computer Graphics II

Step 4
 When an object is picked
if (selected >= 0 && m_pressedMouseButton == 1) {

double r1x, r1y, r1z, r2x, r2y, r2z;


getMouseLine(r1x, r1y, r1z, r2x, r2y, r2z);
object’s current position (input)
double rx, ry, rz;
mousePoleGo(r1x, r1y, r1z, r2x, r2y, r2z, input
static_cast<double>(x coordinate of object),
static_cast<double>(y coordinate of object), MousePoleGo
static_cast<double>(z coordinate of object),
output rx, ry, rz,
(Fl::event_state() & FL_CTRL) != 0);
object’s new position (output)
set object’s the new positon (rx, ry, rz)

damage(1);
}

Mankyu Sung 10
Computer Graphics II

Step 4 Example
if (selected >= 0 && m_pressedMouseButton == 1) {

double r1x, r1y, r1z, r2x, r2y, r2z;


getMouseLine(r1x, r1y, r1z, r2x, r2y, r2z);

double rx, ry, rz;


mousePoleGo(r1x, r1y, r1z, r2x, r2y, r2z,
static_cast<double>(m_moverconnection->m_movers[selected]->m_particle->getPosition().x),
static_cast<double>(m_moverconnection->m_movers[selected]->m_particle->getPosition().y),
static_cast<double>(m_moverconnection->m_movers[selected]->m_particle->getPosition().z),
rx, ry, rz,
(Fl::event_state() & FL_CTRL) != 0);

m_moverconnection->m_movers[selected]->m_particle->setPosition(rx, ry, rz);

damage(1);
}

Mankyu Sung 11
Computer Graphics II

Programming : Apply picking to bouncing ball example

Stop simulation before


you pick an object

Mankyu Sung 12
Computer Graphics II

https://padlet.com/mksung89/picking

Mankyu Sung 13

You might also like