Drawing in 3D in a Realistic 3D World the Observer Becomes an Actor Whose Actions Provoke Reactions That Leave Tracks in Virtual Reality
Total Page:16
File Type:pdf, Size:1020Kb
PROGRAMMING Coin 3D – Interaction Interactive 3D Worlds with Coin and Qt Drawing in 3D In a realistic 3D world the observer becomes an actor whose actions provoke reactions that leave tracks in virtual reality. Qt and Coin allow you to program animated and interactive 3D worlds quickly and easily.We take a look at how you can interact with your new 3D world and create new effects. BY STEPHAN SIEMEN scene. This illustration is right mouse button deletes all the dots based on an example from the scene. from Inventor Mentor ([3], Chapter 10, The Right Choice example 2), how- A program that needs to reflect a user’s ever, we have wishes interactively, needs to select and expanded on it modify individual objects in the scene and moved from graph. This example adds new dots to Motif to using dotCoordinates and tells the dots node to Qt and SoQt. draw the new dots. Figure 2 Nodes can be accessed and selected by shows the reference to their position in the graph. scene graph for the drawing Each group node provides a method program. It requires only four called getChild() for this purpose. Figure nodes, and the user can edit three of 2 shows how the program references them dynamically (only the lighting individual nodes via the root of the remains constant). Pressing the center scene graph. However, this simple mouse button rotates the camera about method is extremely error-prone: if three dimensional scene appears the source. dotCoor- far more realistic if it is animated dinates and dots are Aand the user can interact with it. the most interesting The previous article in our Coin series nodes: [2] introduced the concept of animation • dotCoordinates are with a plane circumnavigating a revolv- objects of the ing globe. However, the scene did not SoCoordinate3 class allow the user to interact with, or modify that expects an the scene, although it was possible to arbitrary number of use SoQtExaminerViewer to view the 3D coordinates. scene from different perspectives. • dots are objects of Our interactive example in this article the SoDotSet class is a simple drawing program that allows that draws all the the user to draw arbitrary dots in a 3D coordinates for the dotCoordinates Coin 3D Update node as a dot in a The new version of Coin was released 3D space. shortly before this article was printed. The user can add Version 2.0 contains the VRML extension as new dots by pressing mentioned in this article and can be the left mouse but- Figure 1:The default right-mouse-button menu gives many options, such downloaded from [5]. ton. Clicking the as rendering only a wireframe of the scene. 72 May 2003 www.linux-magazine.com Coin 3D – Interaction PROGRAMMING nodes are added to the scene graph later, the indices change. Also, larger scene root graphs tend to become unmanageable – in this case you might prefer to use the setName() method to assign a unique name to each node. A SoSearchAction class object can then be used to discover camera light dot coordinates dot and change the position of the object. root->getChild(0) root->getChild(1) root->getChild(3) The newDot() in Listing 1 adds a new root->getChild(2) dot to the scene. It expects to be passed a pointer to the rendering area and the Figure 2:The neat scene graph for a 3D drawing program.The four children of the root node can be coordinates of the dot. Lines 5 to 7 queried using the getChild() method discover the pointers for the third and fourth nodes in the scene graph starting at the root node. Finally, lines 10 and 12 »widthAngle« Line of vision modify the characteristics of the node. Point of intersection »p1« Camera »p0« Adding Dots to the Scene position When a user adds dots to a scene the program has to correlate the two-dimen- sional mouse position in the window to »heightAngle« the three-dimensional position in the »nearDistance« scene. Our example uses a simple approach placing all the dots on a plane »farDistance« that transects the origin. Figure 3 shows the geometry of the Figure 3: In Coin the observer looks at a 3D scene through a camera.The camera only shows a section of volume that SoPerspectiveCamera sees as the world that is limited by four margins and two depth layers (red) a section of the 3D scene. If the camera range is set to infinity, rendering can By default units of distance are relative • Lines 7 to 9 convert the mouse coordi- take a while. This is why the to one another in Open Inventor, nates from the X11 window to the nearDistance and farDistance parameters however, you can use the SoUnit node to rendering area units (value between 0 restrict the volume of the camera assign a specific unit of distance. and 1). The origin is bottom left, in viewfinder. In our example nearDistance contrast to top left for X11. is set to 1 and farDistance to 7. As the The Mouse in the Volume •Line 17 gets an SbViewVolume class distance between the camera and the Listing 2 maps the x and y mouse object that describes the visible origin is 4, the focus of the scene is coordinates on this volume. Imagine this section of the scene, thus providing a exactly in middle of nearDistance and as a ray that vertically transects the correct viewing perspective. farDistance. The focal point is traversed camera volume. Figure 3 shows this ray •Line 21 extends the mouse pointer, by the plane (highlighted in green) on in blue. The individual steps are as converting the dot into a line that which the user can draw dots. follows: extends vertically into the image. The end points are stored in p0 and p1. Listing 1: New Dots •The drawing layer is half-way through the image and transects the line 01 // Add new dots to the scene exactly at its mid-point. 02 void newDot(SoQtRenderArea *renderArea, const SbVec3f dot) This is only one of many approaches to 03 { transferring a mouse position to a scene. 04 // Pointer to other nodes in the graph More complex approaches map the 05 SoGroup *root = (SoGroup *) renderArea->getSceneGraph(); mouse pointer to three dimensional 06 SoCoordinate3 objects. *dotCoordinates = (SoCoordinate3 *) root Additional details are available in ->getChild(2); the online documentation for Coin 07 SoDotSet *dots = (SoDotSet *) root->getChild(3); [6] and Inventor Mentor [3], [4]. The 08 SoPickAction and SoPickedDot classes 09 // Add coordinates are particularly useful. 10 dotCoordinates->dot.set1Value(dotCoordinates->dot.getNum(), dot); 11 // Get the dot node to draw all the coordinates Engines, Sensors & Callbacks 12 dots->numDots. setValue(dotCoordinates->dot.getNum()); Open Inventor provides two techniques 13 } for programming animations: engines www.linux-magazine.com May 2003 73 PROGRAMMING Coin 3D – Interaction Figure 4a:The example program initially looks like a 2D drawing program. Figure 4b:When the user moves the camera the 3D capabilities of the pro- You use the left mouse button to draw arbitrary figures gram become apparent. New dots are shown on a new layer and sensors. Engines are objects that link In [2] we used two engines to rotate a this also means that sensors are external to node fields in the scene graph and globe. Sensors are objects that recognize to the scene graph and cannot be stored change their values. Changes are changes in the scene graph and react in the scene graph files. effected either by logical links or by time by referring to a callback function. In Our example uses a sensor to rotate functions. As the engines are part of the comparison to engines, sensors provide the camera in the scene. When the user scene graph they can be stored in files the programmer with more potential, as presses the center mouse button the along with their characteristics. they allow original code to be added. But code in Listing 3a applies the timer sen- sor, SoTimerSensor, to the camera. The Listing 2: Mouse based projections sensor refers to its callback function 01 // Uses the mouse position to calculate (Listing 3b) at regular intervals to gradu- 02 // a3Ddot in the scene ally rotate the camera. Other sensors, 03 void projection(SoQtRenderArea *renderArea, such as SoNodeSensor, react to changes 04 int mouseX, int mouseY, SbVec3f &interface) in monitored nodes and react by calling 05 { their own callback functions. 06 // Position of mouse in rendering area: One advantage that callbacks offer is 07 SbVec2s size =renderArea->getSize( the fact that they can do more than 08 float x = float(mouseX) / size[0]; modify the scene graph. A sensor can, 09 float y = float(size[1] - mouseY) / size[1]; for example, write to the terminal when- 10 ever a characteristic of a monitored 11 // Pointer to camera object changes. However, callbacks 12 SoGroup *root = (SoGroup *) renderArea->getSceneGraph(); should not contain too much code, as 13 SoCamera *camera = (SoCamera *) root->getChild(0); they may be called frequently, and this 14 could impact program speed. 15 // Volume visible to camera 16 SbViewVolume cameraVolume; Callback Parameters 17 cameraVolume = camera->getViewVolume(); In Coin, callbacks have two parameters. 18 The first is a pointer to a void type that 19 // Mouse line vertical to scene allows the programmer to pass a refer- 20 // The other endpoint is created by mirroring on the plane ence to an arbitrary object to the 21 SbVec3f p0, p1; function. Thus, Listing 3b expects a 22 cameraVolume.projectDotToLine(SbVec2f(x,y), p0, p1); SoCamera class object, Listing 3a having 23 first passed the reference to the sensor 24 // The center of the line is the position we are looking for constructor.