28.1. Introduction

This chapter is for the experienced OpenGL programmer and is not intended as an introduction to OpenGL. Before you read this chapter, be sure to read at least Chapters 1 through 5 and Chapter 9 of this programming guide. You'll need a basic understanding of the Inventor database (Chapter 1, Overview through Chapter 4, Cameras and Lights), Inventor actions (Chapter 8, Applying Actions), and Inventor event handling (Chapter 10, Handling Events and Selection) before you begin combining features of OpenGL with Inventor.

The preferred way to combine use of OpenGL and Inventor is by subclassing. When you subclass, you create a new node that makes calls to OpenGL. This process, which is beyond the scope of this chapter and is described in detail in The Inventor Toolmaker, allows you to build on an existing node. Another advantage of subclassing is that your new class has access to Inventor reading and writing (callback nodes, described in this chapter, do not read and write detailed information to a file).

It is important to note that Inventor inherits state from OpenGL for rendering only. Additional Inventor features, such as picking, computing bounding boxes, and writing to a file, do not use OpenGL and are unaware of changes made directly to the OpenGL state variables. For example, it is possible to send a viewing matrix directly to OpenGL and then use Inventor to draw a scene without a camera. However, if you then try to pick an object, Inventor will not know what viewing transformation to use for picking, since it doesn't use OpenGL for picking.

You can combine Inventor with OpenGL in several ways. An easy way to add custom OpenGL rendering to a scene database is to add a callback node (SoCallback( C++ | Java | .NET ); see Example 28.1, “ Using a Callback Node). This node allows you to set a callback function that is invoked for each of the various actions that nodes perform (rendering, picking, bounding-box calculation). The SoCallback( C++ | Java | .NET ) node differs from the event callback node in that it provides callbacks for all scene operations rather than just for event handling.

A second way to combine Inventor with OpenGL is to create a GLX window, make OpenGL and Inventor calls, and then apply an SoGLRenderAction( C++ | Java | .NET ), as shown in Example 28.2, “ Using a GLX Window. For instance, you could create a GLX window, clear the background, do some initial rendering into the window, set up the viewing matrix, and then use Inventor to draw a scene by applying a GL render action to the scene graph. Or, you could use Inventor to set up the camera, lights, and materials, and then use OpenGL code to draw the scene. As long as you follow the general rules described in the following section on OpenGL state usage, you can mix OpenGL and Inventor rendering as you wish. (Note that this is an advanced feature, not for the faint of heart.)