Time for action—playing with osgviewer
If you have chosen to download the sample data and configured environment variables by using the installer, it is time to load and display a sample model. Here we will reproduce the "Hello World" example of the first chapter. Developers who have trouble compiling the example code are suggested to look into the osgviewer
first.
- Start the
osgviewer
utility by issuing the following command:# osgviewer cessna.osg
- The displayed result will be nearly the same as our "Hello World" example and the installer's demo. Don't forget to change the view point with your mouse. Pressing the space bar at any time will return you to the initial view.
- Besides that,
osgviewer
provides lots of shortcuts to switch between different display modes and gather rendering performance information as well. To have a first-hand experience of them, press the Esc key to quit the currently runningosgviewer
, and type in:# osgviewer cow.osg
- Now you will see a cow with a beautiful reflection map:
- Press the W key to switch polygon modes, which include filled (by default), wireframe, and points.
- Press the T key to toggle textures on or off to activate or deactivate the reflection map.
- Press the I key to enable or disable lighting. Scenery loaded in
osgviewer
are lighted by default. - Press the F key to change between full-screen and windowed display.
- Press the S key repeatedly to display real-time rendering statistics, which are very useful for optimizing graphics. The following image illustrates the current frame rate and traversal time, which was displayed by pressing the S key twice:
What just happened?
We have already had an overview of the osgviewer
. The osgviewer
is a fundamental but feature-rich scene graph viewing tool, which is used for loading models and images, and fully observing them by taking advantage of a series of auxiliary functionalities.
To load a picture with osgviewer
, issue the following command:
# osgviewer --image picture_name.bmp
To learn more about the osgviewer
command-line arguments, start the shell prompt again without any arguments, and read the output text carefully:
# osgviewer
Please try more models and image files in different formats, and you will find that osgviewer
supports a great number of data formats, including but not limited to .3ds, .obj, .stl, .bmp
and .dds
. For the external data formats that OSG supports, you can find out more details in Chapter 10, Saving and Loading Files.
Pop quiz—dependencies of osgviewer
Copy the osgviewer
utility and the data file cessna.osg
(which is in the data
subfolder of the installation directory) to another computer on which OSG has never been installed. Do you think it can work properly this time? OSG-based applications depend heavily on related dynamic libraries. Could you find out which libraries are required by osgviewer
while reading cessna.osg?
Some dependent modules locator software may help a lot in finishing such work. For example, you may download the free Dependency Walker utility from http://www.dependencywalker.com/.
Have a go hero—playing with osgconv
Another tool named osgconv
is mainly used for converting between formats. To summarize the usage, osgconv
can import graphical contents in order to export to different file formats. It supports as many input formats as osgviewer
. Please note that whether a given format can be written depends on the plugin, which means that some formats can only be read while others can be read and written. We will discuss this later in Chapter 10, Saving and Loading Files. Now, you can try to convert a .osg
file to a .3ds
file by using the following command, and then open the new file with any 3D modeling software such as 3dsmax, on your own, if you like:
# osgconv cessna.osg cessna.3ds