Virtual World
Total Page:16
File Type:pdf, Size:1020Kb
PROGRAMMING Coin 3D Interactive 3D Worlds with Coin and Qt Virtual World Qt and Coin, an Open Inventor clone, design particularly simpli- fies programming in make the programming of interactive C++. Open Inventor itself uses OpenGL to 3D worlds a lot easier than OpenGL render graphics and thus inherits its supe- ever has. Speed of image rendering rior quality and speed. is not always the critical requirement. The toolkit is far more than just a library Quick and easy to follow program- that provides classes for creating 3D graphics. Open ming may produce better results. Inventor allows you to describe and store 3D scenes in BY STEPHAN SIEMEN so-called scene graphs, for exam- ple. The file format developed for this purpose was used as a template for VRML (Virtual Reality Modelling Language) in 1994. Open Inventor also or most people OpenGL, immedi- allows interaction with the scene. The ately spring to mind when it comes scene graphs can be used to identify the Fto programming 3D worlds. Often objects and thus define appropriate Open Inventor by SGI or the compatible reactions. Coin (see “Open Inventor and Coin”) are Unfortunately, Coin and SoQt are not The command binds the Coin and SoQt a better choice: OpenGL may render included with every Linux distribution libraries and uses the “-I$QTDIR/ graphics quickly, but it does involve (see the “Installation” insert). Both include” option to specify the location of complex and time-consuming program- packages contain exhaustive documen- the Qt header files. The results are ming. OpenGL’s structure and tation. shown in Figure 2. commands are closely oriented on graphics hardware. Compared to Open 3D Graphics with a GUI Discovering and Exploring Inventor, OpenGL is on the same level as The biggest difference between program- Virtual Worlds Assembler. ming with Open Inventor and Coin is the The “SoQtExaminerViewer” class (line The object oriented Open Inventor GUI integration. The following examples 31) provides a variety of functions that Toolkit was developed in 1991 by the use SoQt to interface with the Qt library, allow you to view various sections of the same SGI programmers that produced Motif variants are available from [7]. scene. The graphic follows mouse move- OpenGL. Open Inventor maps the func- With the exception of the SoQt compo- ments if you hold down the left mouse tionality of OpenGL to objects. The nents, all of these examples also run on button. The viewer provides alternative SGI’s Open Source Variant of Open views of the scene via the wheels and Coin Inventor; thus the names Coin and Open buttons on the window borders. Manufacturer: Systems in Motion Inventor are interchangeable in the fol- The three wheels are particularly inter- lowing sections. esting: two of them are in the bottom left License: LGPL (GPL for Version 2.0 planned) As befits a programming tutorial, our corner, and another is located on the Stable Version: Coin 1.0.3,SoQt 1.0.1 first example will be a Hello World pro- lower right. The right wheel, or dolly, Coin Professional: Commercial license (per gram. It initializes a viewer and displays influences the distance between the user developer and year 2000 US dollars),suit- a three dimensional text. The user can and the scene (zoom effect). The two able for proprietary projects view the text from any arbitrary position. wheels on the left rotate the scene about Multi platform support: Coin runs on Linux, The code is shown in Listing 1 and can its x or y axis. other Unix type systems and Windows; you be downloaded from [7]. The following need a C++ compiler and an OpenGL library command compiles the program: Discovery Tools GUI Bindings: Currently supports Qt,Gtk+, A total of seven buttons are located Motif and Windows; Coin can be used with- g++ HelloSoQt.cpp -o HelloSoQt U above the dolly wheel on the right win- out these bindings -lCoin -lSoQt -I$QTDIR/include dow margin: 72 March 2003 www.linux-magazine.com Coin 3D PROGRAMMING Application Motif SoQt SoXt Coin - Open Qt Inventor Qt-GL OpenGL Operating System Hardware Figure 1:The Coin library mediates between the application and OpenGL. Figure 2:The sample Hello SoQt program is shown in the viewer.The user SoQt is used to bind the Qt GUI toolkit while SoXt is an alternative for Motif can alter the viewing position • Arrow: Edit mode mode is automatically enabled on objects in a scene. The elements of the • Hand: Interactive mode launching the viewer and allows the user scene graph are nodes, which contain • House: Reset camera position to to modify the camera position and angle various functions. Each of these nodes default as required using the GUI wheels and the describes part of the image scene. •Blue House: Set a camera position mouse. Additional settings are available When calculating and rendering a (viewer angle) as the new default posi- in the pop-up menu (right click). scene, Open Inventor works its way tion methodically from the root to the leaves •Eye: View the whole scene The Scene Graph: Managing (the lowest nodes) of the scene graph, • Lamp: Set the focus for the zoomer 3D Objects left to right and top down. (dolly) Open Inventor uses scene graphs for the Special nodes are used to permit addi- •Box: Toggles various transformations. efficient management of 3D scenes (3D tional modifications, such as All of these interactions move the cam- worlds). The scene graph has a tree transformations and rotation. These era and not the scene itself. Interactive structure whose roots allow access to the modifications affect any subordinate Listing 1:“HelloSoQt.cpp” 01 // SoQt header files 24 02 #include <Inventor/Qt/SoQt.h> 25 // Create a text 03 #include <Inventor/Qt/viewers/SoQtExaminerViewer.h> 26 SoText3 *text3D = new SoText3(); 04 27 text3D->string.setValue("Hello SoQt"); 05 // Coin header files 28 root->addChild(text3D); 06 #include <Inventor/nodes/SoBaseColor.h> 29 07 #include <Inventor/nodes/SoText3.h> 30 // Create a viewer 08 #include <Inventor/nodes/SoSeparator.h> 31 SoQtExaminerViewer *b = new 09 SoQtExaminerViewer(window); 10 int main(int argc, char **argv) 32 b->setSceneGraph(root); 11 { 33 b->show(); 12 // Initialize SoQt library. 34 13 // The return value points to a Qt window 35 // Start the window 14 QWidget *window = SoQt::init("test"); 36 SoQt::show(window); 15 37 // Loop until exit. 16 // Create a "scene graph" 38 SoQt::mainLoop(); 17 SoSeparator *root = new SoSeparator; 39 18 root->ref(); 40 // Delete viewer and reference for scene 19 41 delete b; 20 // Set the RGB color. Yellow in this case 42 root->unref(); 21 SoBaseColor *color = new SoBaseColor; 43 22 color->rgb = SbColor(1, 1, 0); 44 return 0; 23 root->addChild(color); 45 } www.linux-magazine.com March 2003 73 PROGRAMMING Coin 3D available as PDF documents under [3] chair (enter the document name you require in the search box) In the case of Coin and its extensions seat front legs back legs back (such as SoQt) the documentation is included in the source code. You can use right right left left Doxygen to generate a HTML overview. seat color seat surface displacement 5rot back color se ation at back Inserting a New Node The following example inserts a new displacement displacement 2 displacement 3 displacement 4 node into a scene graph: leg leg 2 SoCone *cone = new SoCone; hcone->height.setValue(4); cone->parts.setValue("SIDES"); leg color post post 2 root->addChild(cone) Transformation Separator Appearance/Design Geometry This creates an object of the desired Figure 3:The scene graph divides the chair into its components:seat, front and hind legs as well as lean class (“SoCone” in this case) and defines angle.The left and right legs differ only by their position values for some fields. In our example nodes. At this point a similarity to the active operations or describe various Open Inventor and Coin OpenGL status machine becomes appar- transformations. In 1996 SGI handed over the development ent. A property, for example the color of of Open Inventor as of version 2.1 to TGS [2]. a 3D object (class “SoBaseColor”) Nodes: Shapes, Colors, This company develops and distributes the remains active until a new value is Materials and Light latest version (currently 3.1) commercially.In assigned to the property. Each node is described in a class. The August 2000 SGI decided to publish its own The programmer can organize the name of a node class starts with “So”, as Open Source Open Inventor version (2.1) for objects in the scene graph to manipulate in “SoMaterial” or “SoCone”. The “So” Linux [1]. the appearance and behavior of the gen- prefix is omitted when describing a The Norwegians,Systems in Motion [4], erated scene. scene in a file. Node names are VRML offer an implementation of their own,called The order of the effects is important. like in this case. Coin [5],for Windows,Linux and other Unix systems. Coin is currently available Replacing a rotation with a translation Each class comprises of fields that under the LGPL license (version 1.x), will lead to different generated results. characterize the properties of the node. although version 2.0 is due to be released Open Inventor offers a variety of node The SGI documentation details the fields under GPL. types that can define surfaces, describe available in each node type: the “Open materials or adjust the camera lighting. Inventor Nodes Quick Reference” and More Dynamism with Coin Other nodes provide interfaces for inter- “Open Inventor C++ Reference” are Coin was chosen for this article – its devel- opment promises more dynamism than Table 1 Shape Classes the SGI variant.