LinuxDevCenter.com
oreilly.comSafari Books Online.Conferences.

advertisement


OpenAL Development: The Programmer's View
Pages: 1, 2, 3

An example: coding an object for movement and sound, part 2

The xldemo program illustrates the basic use of OpenAL. A scene is created that includes two stationary objects (houses), two moving objects with fixed paths (a car and a van), and one freely moving object (the listener, i.e., you). The Linux version of the xldemo program is built from the Linux-specific sources in openal/demos/XLDEMO/linux and the shared sources in the openal/demos/XLDEMO/COMMON directory. Let's take a closer look into those sources to watch OpenAL at work.



Note: Code comments preceded by "tsao" are by Joseph I. Valenzuela. The author is especially indebted to Mr. Valenzuela for his assistance with this section.

In the Linux directory, xlmain.cpp sets up OpenGL/OpenAL and starts the main program loop. Looking into the code, we first see some standard includes (such as stdlib.h and sys/time.h) declared along with these files:

#include "GL/glut.h"  
  // sets up OpenGL 
#include "../COMMON/common.h"
  // defines the AVEnvironment class
#include "../COMMON/audioenv.h"
  // defines the audio environment class

The AVEnvironment class includes items such as window height and width, camera/listener angles and positions, and the named objects in the virtual environment (Sky, Ground, CarList, etc). It also references functions such as PlaceCamera and ChangeView (both found in xlmain.cpp). The audio environment class contains functions for loading and playing sound files, starting and stopping playback, and managing the positions of Sources and Listener.

Both common.h and audioenv.h include xlheader.h, which in turn calls the OpenAL headers -- view this section of code here.

Now let's look at audioenv.cpp in the openal/demos/XLDEMO/COMMON directory. View that code here.

These code fragments include most of the OpenAL calls used in the xldemo program. As you can see, quite a lot gets done by not a lot of code. The OpenAL API functions take care of the gritty details, freeing the programmer from considerable tedious calculations. Furthermore, both OpenGL and OpenAL share the same mechanisms for determining the camera/listener position, velocity, angle, etc., all of which get updated simultaneously for visuals and audio, but I leave that investigation to the motivated reader. More examples of interesting OpenAL code can be found in the sources to the Chromium BSU game and the excellent tutorial by Fotis Hadginikos.

Dave Phillips maintains the Linux Music & Sound Applications Web site and has been a performing musician for more than 30 years.


Discuss this article in the O'Reilly Network Linux Forum.

Return to the Linux DevCenter.

 




Linux Online Certification

Linux/Unix System Administration Certificate Series
Linux/Unix System Administration Certificate Series — This course series targets both beginning and intermediate Linux/Unix users who want to acquire advanced system administration skills, and to back those skills up with a Certificate from the University of Illinois Office of Continuing Education.

Enroll today!


Linux Resources
  • Linux Online
  • The Linux FAQ
  • linux.java.net
  • Linux Kernel Archives
  • Kernel Traffic
  • DistroWatch.com


  • Sponsored by: