Graphics Reference
In-Depth Information
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
add handlers that forward left-button events to the interactor, if it's not null.
void Window1_MouseRightButtonDown(object sender, MouseEvent e)
// Check to see if the user clicked on cube1.
// If so, create a sphere around it.
ModelVisual3D hit = GetHitTestResult(e.GetPosition(mainViewport));
if (hit == m_cube1)
if (interactor == null)
interactor = new Interactor(m_cube1, mainViewport, this);
else
endInteraction();
// if there's already an interactor, delegate to it.
else if (interactor != null)
interactor.Cleanup();
interactor = null;
The interactor, just as in the photo-manipulation example, keeps track of the
manipulated object ( controlled ) and the transformation for that object at the start
of the manipulation. We also note the viewport from which the object is seen
(which allows us to transform mouse clicks into rays from the eye). Initializing the
interaction consists of recording the initial transformation on the controlled object,
and creating a transparent sphere, centered at the object center. The corresponding
cleanup procedure removes the sphere.
1
2
3
4
5
6
7
8
9
private void initializeInteraction()
initialTransform = controlled.Transform;
find bounds for selected object,
locate center and place a sphere there
viewport3D.Children.Add(sphere);
public void Cleanup()
viewport3D.Children.Remove(sphere);
initialTransform = null;
When the user left-clicks on the sphere, we record the current transformation
associated to the controlled object and the location of the click. Just as in the
photo-manipulation program, we record this position in the coordinate system of
the parent of the controlled object. We also record that we are in the midst of a
drag operation, and when the left button is released, we reset the drag status.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public void mouseLeftButtonDown(System.Windows.Input.MouseButtonEventArgs e)
ModelVisual3D hit = GetHitTestResult(e.GetPosition(viewport3D));
if (hit != sphere)
return
else if (!inDrag)
startPoint = spherePointFromMousePosition(e.GetPosition(viewport3D));
initialTransform = controlled.Transform;
inDrag = true;
public void mouseLeftButtonUp(System.Windows.Input.MouseButtonEventArgs e)
inDrag = false;
private Point3D spherePointFromMousePosition(Point mousePoint)
form a ray from the eye through the mousePoint
if it hits the sphere
return the hit point.
else // ray misses sphere
return closest point to ray on the sphere
 
Search WWH ::




Custom Search