OpenSceneGraph 3.0 Beginner's Guide Create high-performance virtual reality applications with OpenSceneGraph, one of the best 3D graphics engines Rui Wang Xuelei Qian BIRMINGHAM - MUMBAI OpenSceneGraph 3.0 Beginner's Guide Copyright © 2010 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors, nor Packt Publishing and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. First published: December 2010 Production Reference: 1081210 Published by Packt Publishing Ltd. 32 Lincoln Road Olton Birmingham, B27 6PA, UK. ISBN 978-1-849512-82-4 www.packtpub.com Cover Image by Ed Maclean ([email protected]) Credits Authors Editorial Team Leader Rui Wang Akshara Aware Xuelei Qian Project Team Leader Lata Basantani Reviewers Jean-Sébastien Guay Project Coordinator Cedric Pinson Leena Purkait Acquisition Editor Proofreader Usha Iyer Dirk Manuel Development Editor Graphics Maitreya Bhakal Nilesh Mohite Technical Editors Production Coordinator Conrad Sardinha Adline Swetha Jesuthas Vanjeet D'souza Cover Work Indexers Adline Swetha Jesuthas Tejal Daruwale Hemangini Bari Monica Ajmera Mehta Foreword Scene graphs have been the foundation of real-time graphics applications for the last two decades, whether it is a 3D game on a phone or a professional flight simulator costing millions of pounds, a virtual reality application through to the latest 3D real-time visualization on television, scene graphs are there under the hood, quietly churning out high quality visuals. However, even powerful tools like scene graphs don't write world leading graphics applications by themselves, they still need developers with the skill and knowledge to make best use of them and the hardware that they run on. This expertise isn't something that you can gain by reading a few pages on the web—graphics hardware and software continues to evolve and you need to keep up with it... It's a journey of learning and exploration undertaken throughout your career. OpenSceneGraph itself is the world's leading scene graph API, and has been written by, and to fulfil the needs of, professional graphics application developers. It is written to be powerful and productive to use rather than cut down and easy to use. Your first encounter with OpenSceneGraph may well be daunting; it's a professional grade scene graph containing many hundreds of classes and modules. But with this sophistication comes the ability to write very powerful graphics applications quickly so it's well worth the effort in learning how to make best use of it. The authors of this book are users and contributors to the OpenSceneGraph software and its community. For me it's rewarding to see this open source project reach out across the world and inspire people, such as Rui Wang and Xuelei Qian, not only to use and contribute to the software, but also to write a book about it so that others can start their own journey into real-time graphics. With this book their aim has been to take you from your first steps through to being able to use advanced features of the OpenSceneGraph and the graphics hardware that it runs on. Learning new concepts and APIs can often be dry and awkward, but once you get your first applications on screen you'll glimpse the potential, and it won't be long before you are seeing complex worlds come life. As a real-time graphics geek myself, I can't think anything more rewarding than immersing yourself in 3D worlds that you help create. Some familiarity with linear algebra, such like 3D vectors, quaternion numbers and matrix transformations, is helpful, too. Robert Osfield. OpenSceneGraph Project Lead About the Authors Rui Wang is a software engineer at the Chinese Academy of Surveying and Mapping and the manager of osgChina, the largest OSG discussion website in China. He is one of the most active members of the official OSG community, who contributes to the serialization I/O, GPU-based particle functionalities, BVH and animated GIF plugins, and other fixes and improvements to the OSG project. He translated Paul Martz's OpenSceneGraph Quick Start Guide into Chinese in 2008, and wrote his own Chinese book OpenSceneGraph Design and Implementation in 2009, cooperating with Xuelei Qian. He is also a novel writer and a guitar lover. Xuelei Qian received his B.Sc. degree in Precision Instrument Engineering from Southeast University, Jiangsu, China, and his Ph.D. degree in applied graphic computing from the University of Derby, Derby, UK in 1998 and 2005, respectively. Upon completion of his Ph.D. degree, he worked as a postdoctoral research fellow in the Dept. of Precision Instrument and Mechanology at Tsinghua University and his current research interests include E-manufacturing, STEP-NC and intelligent CNC, and virtual reality engineering. Acknowledgement We'd like to first thank Don Burns and Robert Osfield for their creative efforts in giving birth to OpenSceneGraph, as well as thousands of members in the OSG core community, for their supports and contributions all the time. Thanks again to Robert Osfield, a pure open source enthusiast and father of a happy family, for his tremendous passion in leading the development the OSG project for so many years (since 1999). He also took time out of his busy schedule to write the foreword for this book. We must express our deep gratitude to Rakesh Shejwal, Usha Iyer, Leena Purkait, Priya Mukherji, and the entire Packt Publishing team for their talented work in producing yet another product, as well as Jean-Sébastien Guay and Cedric Pinson for reviewing the first drafts of the book and providing insightful feedback. We would like to acknowledge John F. Richardson and Marek Teichmann, who announced the book at the OpenSceneGraph BOF at SIGGRAPH 2010. We also offer special thanks to Zhanying Wei, Xuexia Chen, Shixing Yang, Peng Xiao, Qingliang Liu, Su Jiang, and a number of other people who contributed to the completion of this book in different ways. Finally, we owe the most sincere thanks to Paul Martz, who dedicates the first non-commercial book to OSG beginners all over the world and provides great help in supporting the publication of our past and current books. About the Reviewers Jean-Sébastien Guay is a software developer from Montréal, Quebec, Canada. After completing a Bachelor's Degree in Software Development and Software Engineering at UQAM, he began a Master's Degree in Computer Graphics at École Polytechnique, where he chose to use OpenSceneGraph for his Master's project. Motivated by the open nature of the project and wanting to contribute, he started learning its inner workings, fixing bugs, improving the Windows build system, and helping others with their problems on the osg- users mailing list. He has been in the top three posters each month ever since. But is that a good thing or just an indication that he talks too much? Since late 2007, he has worked for CM-Labs Simulations Inc. (http://www.vortexsim. com/), where he develops the Vortex physics toolkit and training simulators for various industries such as construction, subsea, and others. Being the company's dedicated graphics developer allows him to continue using and contributing to OpenSceneGraph. The best part is he gets paid for it, too! Doing so has helped improve his proficiency with C++ as well as allowed him to use other scene graphs such as Vega Prime and OpenSG, which lets him keep an open mind and always see the big picture. Jean-Sébastien has participated in several OpenSceneGraph user meetings, and he was a presenter at the OpenSceneGraph BOFs at Siggraph in 2008 and 2009. He is also a co-developer of the osgOcean nodekit, an ocean surface rendering add-on library for OpenSceneGraph, which is available at http://osgocean.googlecode.com/. He has also contributed to other open source projects, such as Bugzilla, Yafaray, and others. Jean-Sébastien currently lives in the suburbs of Montréal, with his lovely wife and their three young boys. His personal website can be found at http://whitestar02.webhop.org/. Cedric M. Pinson has twelve years of experience in 3D software. He has worked in the video game industry at Nemosoft and Mekensleep, before joining OutFlop, where he has served as the project leader for 3D client technology. He is a contributor to the OpenSceneGraph project and the author and maintainer of osgAnimation. He now does freelance work around OpenGL technologies such as OpenSceneGraph and WebGL. I would like to thank my friends, Loic Dachary for helping me with his advice, Jeremy Moles for the motivation and comments about OpenSceneGraph, Johan Euphrosine for his support, Olivier Lejade who offered me a place to work, and Amy Jones who helps in many ways.