TweetFollow Us on Twitter

Volume Number:9
Issue Number:3
Column Tag:C Workshop

What are Voxels?

A hobbyist level overview of voxels, voxel space and Phong shading

By Geoffrey Clements, Chelmsford, Massachusetts

Note: Source code files accompanying article are located on MacTech CD-ROM or source code disks.

About the author

Geoffrey Clements can be reached on internet at


The medical industry is studying ways of viewing medical imagery on the computer. Doctors are using computer graphics to plan operations before going into the operating room. In fact, entire operations are dry run on the computer, before the doctor lifts a knife.

To create the data for a medical image, a radiologist will take a series of computer aided tomography (CAT) scan images at different depths. The images are lined up in order of depth to form a three dimensional representation of the object being scanned. Each data point in this three dimensional cube is called a voxel. The cube of voxels are called a voxel space. Think of a voxel as a measure of the density of matter at that point. You can also think of a voxel as a “volume pixel”.

In this article we’ll draw a sphere that appears three dimensional using the techniques used to draw medical imagery. I chose a sphere to make the problem simpler to understand. But first we’ll check out how a medical image is generated.

A viewing window defines the orientation of the voxel space, and what part of the voxel space is in view. This viewing window corresponds to the computer screen. The image is created by following a ray from the viewing window through the voxels and summing the contributions of the voxels the ray passes through. The ray is perpendicular to the viewing window. Figure 1 shows two voxels spaces. The first has the viewing window on the front face. The second has the viewing window on the top left front corner. The arrow is the ray followed through the voxel space.

Figure 1 Voxel Space and Viewing Windows

The contribution of each voxel is calculated from a transparency and a color. The transparency, sometimes called opacity, is calculated from the intensities associated with each voxel. Usually the intensity is just scaled to yield an opacity. In our example, inside the sphere is opaque, and outside the sphere is transparent. Figure 2 shows the voxel space we will be using and the sphere opacity.

Figure 2 Voxel Space

To characterize the viewing environment we’ll define several vectors. First a normalized vector pointing toward the viewing window. Next we define normal vectors at each voxel. Finally we need a normal vector pointing toward each light source.

The color of a particular pixel is calculated using the intensities of the voxels and these vectors defining the viewing environment. We’ll be using the Phong shading model to calculate the color.

Phong Shading

The Phong shading model is used in order to give objects in an image their three dimensional quality. We’ll go through the Phong shading equation by describing each piece. I use the words “shade” and “color” interchangeably. They mean the same thing. We start with:

Shade is the color of the voxel we are calculating. Ip is the intensity of a light source. ka is the ambient light reflection coefficient. The ambient light reflection coefficient, as you might expect, just sets an overall light level for the image. A gray disk is drawn if we use this equation to calculate the color.

Next, we add some depth cueing, which will start to give the image a 3D look.

kd1 and kd2 are depth cueing coefficients. We set kd2 = 1 in the example program. k is the distance from the viewing plane along the ray. This adds a small 3D effect. A gray disk is drawn that gets slightly lighter as we move from the edge of the sphere into the center.

Now we add the effect of diffuse and specular reflected light. (This step is a big jump, but these effects are what makes Phong shading so good.)

Figure 3 shows the difference between diffuse and specularly reflected light. Diffuse reflection is light reflected in all directions. This is caused by the roughness of the reflecting surface. Specular reflection, on the other hand, is light that is reflected in only one direction. Specular reflection is caused by the smoothness of the reflecting surface.

Figure 3 Reflected light

Here is the Phong shading equation with the effect of diffuse and specularly reflected light added.

d is the diffuse reflection coefficient, and s is the specular reflection coefficient. N is a normal vector to the voxel whose shade we are calculating, V is a vector pointing toward the viewer, L is a vector pointing at the light source and H is the normalized sum of V and L. N•L is the dot product of N and L.

We’ve come to the point where we can set our viewing window and define the vector space. Figure 4 shows the vector space. There are two L vectors shown in Figure 4. We’ll use two light sources to make the image a little more interesting. To add another light source we only need to add the effect of diffuse and specular reflection to the shade calculated above.

Figure 4 Vector space

The axes are slightly unusual, but the x and y axes correspond to the Macintosh coordinate system. N, H, L1, L2, and V are drawn to show their relative directions. This space corresponds to a viewer looking into the front face, (through the viewing window). The two light sources are pointing down from the top-left front corner of the cube, and into the right side. These vectors must be set before we start.

We can pre-calculate most of the shade equation before going into the loop that calculates the pixel color. This speeds up drawing the image.

Here are the definitions for V, L1, L2, H, and N.

r is the radius of the sphere, i, j, k are a point on the surface of the sphere. In our example, the center of the sphere is at (cv, cv, cv).

We can generate equations for N•L and N•H.

At this point you may be asking yourself, “How do you sum the contributions from the voxels?” Here’s how the example program does it. The indexes i and j step through the pixels on the viewing window. The index k corresponds to the ray going back into the voxels. As k varies, we check to see if point (i, j, k) is inside or outside the sphere. If it is outside the sphere, return a color of zero. If k gets to the back of the voxel space without hitting the sphere, draw a black pixel at (i, j) on the screen. If we hit the sphere, calculate a color for the sphere surface and draw that color at (i, j) on the screen, then go on to the next pixel. There is no need to process any farther inside the sphere, because there is no way the light can get there. Normally the effects of all voxels the ray passes through are used to calculate a color, but I have chosen to make the problem (and program) simpler to understand.

The Example Program

The code was developed under Think C. The MacTraps library is the only library that needs to be included. Turn on the Native floating-point format and Generate 68881 instruction switches in the Compiler settings screen if you have a floating point coprocessor. If not, just turn on the Native floating-point format switch. The program takes about a minute to generate a 128x128 pixel image on a Mac IIci with 68881 instructions on, and three minutes with it off.

The example program uses a standard Macintosh event loop shell and main routine. All of the interesting processing is done by DoColor() called by the Init() routine. Init() starts by initializing the the Macintosh managers and setting up the menus. If Color Quickdraw is not available, the program quietly exits. If Color Quickdraw is available, a window is opened and sized to our voxel data. The size of the volume and sphere are set with the defines:

/* size of the voxel data */
#define volSize  64
/* half the size of voxel data */
#define halfVolSize32
/* the radius of the sphere */
#define sphere_r 30
/* sphere_r*sphere_r */
#define volumeMag900.0
/* sqrt(3.0)*sphere_r */
#define sqrt3r   51.9615
/* sphere_r*sqrt(6+2*sqrt(3)) */
#define rsqrt6   92.2913

Notice that some of the constants for the N•L and N•H equation are defined. This speeds up processing. Because this program is calculation intensive, start with a small volume and increase it later when you have the effect you like.

A grayscale palette is loaded and attached to the window using SetPalette(). The palette is a 128 shade grayscale 'pltt' resource created in ResEdit. An offscreen drawing port is used because we only want to calculate the pixel colors once. The offscreen port is set up using the GWorld calls defined in Volume VI of “Inside Macintosh”.

This brings us to the drawing section. The indexes i and j cycle through all the pixels of the viewing window. The k index is the ray moving back through the voxels. All of the work of deciding the color of a pixel is done in the DoColor() routine. Once a non-zero color is returned we move on to the next pixel in the viewing plane. Inside DoColor() the CalcVolumeData() routine calculates whether or not i, j, k is inside the sphere or not. If it is, we calculate a shade. If not, return a RGBColor of zero.

Once the drawing to the offscreen port is done, we set the current port to the onscreen window and exit Init(). At this point the screen is still blank. When WaitNextEvent() receives an update event we use CopyBits() to copy the offscreen bit map onto the screen.

Stuff To Try

Use a small volume to start off. Start with 64x64x64 voxel set. The following are a couple of other sets of defines to try to get various sized spheres. Remember the bigger the voxel space the more time you have to get coffee while the program runs.

/* 1 */

#define volSize  128
#define halfVolSize64
#define sphere_r 60
#define volumeMag3600.0
#define sqrt3r   103.9230
#define rsqrt6   184.5827

#define volSize  256
#define halfVolSize128
#define sphere_r 120
#define volumeMag14400.0
#define sqrt3r   207.8461
#define rsqrt6   369.1654
The defines:

/* these constants define the Phong shading */
/* ambient reflection coefficient */
#define ambientReflCoef 0.1
/* depth cueing coefficient */
#define depthCueCoef 1.0
/* diffuse reflection coefficient */
#define diffReflCoef 2.0
/* specular reflection coefficient */
#define specReflCoef 3.0
/* first light source intensity */
#define light  0.6
/* second light source intensity */
#define light2   1.2
/* coefficient to approx highlight */
#define highlightCoef11

set the constants for the Phong shading. You can play around with these to change the shading effects in the displayed image. But be careful. The value of shade should fall between 0.0 and 1.0. If shade is greater than one, the color will roll over from white to black, and the image will appear with black blotches in the middle of an area that should be white.

A major performance improvement can be made by replacing the floating-point math with suitable integer arithmetic. Some improvement could be made by calling SetCPixel() from a pointer rather than leaving it to the trap dispatcher. Or, the code for the functions could be inserted into Init() to eliminate the overhead of the function calls.

Drawing in 3D is not hard; it just takes some math know-how and a good computer.


Computer Graphics: Principles and Practice, 2nd ed., by J. D. Foley, A. Van Dam, S. K. Feiner, and J. F. Hughes (Addison-Wessley, 1990)

Marc Levoy, “Display of Surfaces from Volume Data,” IEEE Computer Graphics and Applications, May 1988 pp. 29-37

Code Listing
#include <Palettes.h>
#include <SANE.h>
#include <QDOffscreen.h>

/* size of the voxel data */
#define volSize  128
/* half the size of voxel data */
#define halfVolSize64
/* the radius of the sphere */
#define sphere_r 60
/* sphere_r*sphere_r */
#define volumeMag3600.0
/* sqrt(3.0)*sphere_r */
#define sqrt3r   103.9230
/* sphere_r*sqrt(6+2*sqrt(3)) */
#define rsqrt6   184.5827

/* resource numbers for the window, palette and menus */
#define windowRscr 128
#define paletteRscr 128

#define appleID 128
#define appleM 1
#define appleAbout 1

#define fileID 129
#define fileM 2
#define fileQuit 1

#define editID 130
#define editM 3
#define editUndo 1
#define editCut 3
#define editCopy 4
#define editPaste 5
#define editClear 6

#define sleepTicks 30

#define aboutDialog 128

/* these constants define the Phong shading */
/* ambient reflection coefficient */
#define ambientReflCoef 0.1
/* depth cueing coefficient */
#define depthCueCoef 1.0
/* diffuse reflection coefficient */
#define diffReflCoef 5.0
/* specular reflection coefficient */
#define specReflCoef 5.0
/* first light source intensity */
#define light  1.0
/* coefficient to approx highlight */
#define highlightCoef30

char aChar;
WindowPtr currentWindow;
MenuHandle myMenus[editM+1];
Rect dragRect, growRect;
long newSize;
Boolean doneFlag;
EventRecord event;
WindowPtr whichWindow;
RGBColor pixColor;
short i, j, k;
PaletteHandle palH;
DialogPtr dPtr;
short doneDlg;
OSErr err;
SysEnvRec envRec;

Rect copyRect;
GWorldPtr wallyWorld;
GDHandle savedDevice;
CGrafPtr savedPort;
double PowerOfN (double x, short r) {
 double ans;
 ans = 1.0;
 while (r- > 0) ans *= x;
 return ans;

double fx, fy, fz;

short CalcVolumeData (short i, short j, short k) {
 long x, y, z;
 fx = -(double)(i - halfVolSize);
 fy = -(double)(j - halfVolSize);
 fz = -(double)(k - halfVolSize);
 if ((fx * fx + fy * fy + fz * fz) <= volumeMag) 
 return 1;
 else return 0;

void DoColor (short i, short j, short k,
 RGBColor *RGBVal) {
 double n_dot_h, n_dot_l;
 double n_dot_h2, n_dot_l2, shade;
 unsigned short color;
 if (CalcVolumeData (i, j, k)) {
 n_dot_l = (fx + fy + fz)/sqrt3r;
 n_dot_h = (fx + fy + 2.7321*fz)/rsqrt6;
 shade = light*ambientReflCoef+
 *PowerOfN (n_dot_h, highlightCoef)));

 /* second light source */
 n_dot_l2 = -fx/sphere_r;
 n_dot_h2 = (-fx + fz)/(1.4142*sphere_r);
 shade +=  light/((double)(k)+depthCueCoef)
 *PowerOfN (n_dot_h2, highlightCoef));

 color = (unsigned short)(shade * 65534.0);

RGBVal->red = color;
 RGBVal->green = color;
 RGBVal->blue = color;
 else {
 RGBVal->red = 0;
 RGBVal->green = 0;
 RGBVal->blue = 0;

void OpenWindow (void) {
 currentWindow =  (WindowPtr)GetNewCWindow(
 windowRscr, NULL, (Ptr)-1);
 SizeWindow(currentWindow, volSize + 25,
 volSize + 25, 1);
 SetWTitle(currentWindow, &”\pVol3D”);

void Init (void) {
 short i, j, k;

 InitFonts ();
 FlushEvents (everyEvent, 0);
 InitWindows ();
 InitMenus ();
 TEInit ();
 InitDialogs (NULL);

 myMenus[appleM] = GetMenu(appleID);
 AddResMenu(myMenus[appleM], ‘DRVR’);

 myMenus[fileM] = GetMenu(fileID);
 myMenus[editM] = GetMenu(editID);

 for (i=appleM;i<=editM;i++)
 InsertMenu(myMenus[i], 0);

 DrawMenuBar ();

 SetRect(&dragRect, 30, 20,
 screenBits.bounds.right - 10,
 screenBits.bounds.bottom - 30);
 SetRect(&growRect, 50, 50,
 screenBits.bounds.right - 20,
 screenBits.bounds.bottom - 50);

 doneFlag = 0;
 err = SysEnvirons(1, &envRec);
 if (!envRec.hasColorQD) doneFlag = 1;
 else {
 OpenWindow ();
 palH = GetNewPalette (paletteRscr);
 if (palH == NULL) {
 doneFlag = 1;
 else {
 SetPalette (currentWindow, palH, 1);
 /* set up the offscreen drawing port */
 GetGWorld (&savedPort, &savedDevice);
 SetRect (&copyRect, 0, 0, volSize-1,
 LocalToGlobal (&;
 LocalToGlobal (&copyRect.bottom);
 err = NewGWorld (&wallyWorld, 0, &copyRect,
 NULL, NULL, 0);
 GlobalToLocal (&;
 GlobalToLocal (&copyRect.bottom);

 if (err != noErr)
 doneFlag = 1;
 else {
 SetGWorld (wallyWorld, NULL);
 if (LockPixels (wallyWorld->portPixMap)) {
 /* draw off screen here */
 for (j=0;j<volSize;j++) {
 k = 0;
 do {
 DoColor(i, j, k, &pixColor);
 } while (( == 0)
 & (k < volSize));
 SetCPixel (i, j, &pixColor);
 UnlockPixels (wallyWorld->portPixMap);
 else doneFlag = 1;
/* the drawing is done set the current port back to the display window 
 SetGWorld (savedPort, savedDevice);

void DoAboutBox (void) {

 dPtr = GetNewDialog (aboutDialog, NULL,
 ModalDialog(NULL, &doneDlg);
 while (!doneDlg);

void CleanUp (void) {
 HideWindow (currentWindow);
 DisposeGWorld (wallyWorld);
 DisposePalette (palH);
 DisposeWindow (currentWindow);
 doneFlag = 1;

void DoCommand (long menuResult) {
 short menuID, menuItem;
 Str255 daName;
 short daErr;

 menuItem = LoWord (menuResult);
 menuID = HiWord (menuResult);

 switch (menuID) {
 case appleID: 
 if (menuItem == appleAbout) DoAboutBox ();
 else {
 GetItem(myMenus[appleM], menuItem, daName);
 daErr = OpenDeskAcc(daName);
 if (currentWindow)
 SetPort (currentWindow);
 case fileID: 
 switch (menuItem) { 
 case fileQuit: 
 CleanUp ();

void DoEvent (void) {

 switch (event.what) {
 case mouseDown: 
 switch (FindWindow(event.where, 
 &whichWindow)) {
 case inMenuBar: 
 case inSysWindow: 
 SystemClick(&event, whichWindow);
 case inDrag: 
 DragWindow(whichWindow, event.where,
 case inGrow: 
 newSize = GrowWindow(whichWindow,
 event.where, &growRect);
 SizeWindow(whichWindow, LoWord(newSize),
 HiWord(newSize), 1);
 case inGoAway: 
 if (TrackGoAway(whichWindow,
 event.where)) CleanUp ();
 } /* case findwindow (...) */
 case keyDown:
 case autoKey: 
 aChar = (char)(BitAnd (event.message,
 if (BitAnd (event.modifiers, cmdKey))
 case activateEvt: 
 if (BitAnd(event.modifiers, activeFlag))
 DisableItem(myMenus[editM], 0);
 else EnableItem(myMenus[editM], 0);

 case updateEvt: 
 InsetRect (&currentWindow->portRect, 8, 8);
 OffsetRect (&currentWindow->portRect,
 -8, -8);

 if (LockPixels (wallyWorld->portPixMap)) {
 &currentWindow->portBits, &copyRect,
 &currentWindow->portRect, srcCopy, NULL);
 UnlockPixels (wallyWorld->portPixMap);
 OffsetRect (&currentWindow->portRect, 8, 8);
 InsetRect (&currentWindow->portRect, -8, -8);

void main (void) {
 currentWindow = NULL;
 Init ();
 InitCursor ();

 do {
 if (WaitNextEvent (everyEvent, &event,
 sleepTicks, NULL)) DoEvent ();
 } while (!doneFlag);


Community Search:
MacTech Search:

Software Updates via MacUpdate

Planet Diver guide - How to survive long...
Planet Diver is an endless arcade game about diving through planets while dodging lava, killing bats, and collecting Starstuff. Here are some tips to help you go the distance. [Read more] | Read more »
KORG iDS-10 (Music)
KORG iDS-10 1.0.0 Device: iOS iPhone Category: Music Price: $9.99, Version: 1.0.0 (iTunes) Description: ** Debut Discount: 50% OFF! Sale Price US$9.99 (Regular price US$19.99). Other all Korg apps are also 50% OFF until Dec 28! **... | Read more »
World of Tanks Generals guide - Tips and...
World of Tanks Generals is a brand new card game by the developer behind the World of Tanks shooter franchise. It plays like a cross between chess and your typical card game. You have to keep in consideration where you place your tanks on the board... | Read more »
TruckSimulation 16 guide: How to succeed...
Remember those strangely enjoyable truck missions in Grand Theft Auto V whereit was a disturbing amount of fun to deliver cargo? TruckSimulation 16 is reminiscent of that, and has you play the role of a truck driver who has to deliver various... | Read more »
The best GIF making apps
Animated GIFs have exploded in popularity recently which is likely thanks to a combination of Tumblr, our shorter attention spans, and the simple fact they’re a lot of fun. [Read more] | Read more »
The best remote desktop apps for iOS
We've been sifting through the App Store to find the best ways to do computer tasks on a tablet. That gave us a thought - what if we could just do computer tasks from our tablets? Here's a list of the best remote desktop apps to help you use your... | Read more »
Warhammer 40,000: Freeblade guide - How...
Warhammer 40,000: Freebladejust launched in the App Store and it lets you live your childhood dream of blowing up and slashing a bunch of enemies as a massive, hulking Space Marine. It's not easy being a Space Marine though - and particularly if... | Read more »
Gopogo guide - How to bounce like the be...
Nitrome just launched a new game and, as to be expected, it's a lot of addictive fun. It's called Gopogo, and it challenges you to hoparound a bunch of platforms, avoiding enemies and picking up shiny stuff. It's not easy though - just like the... | Read more »
Sago Mini Superhero (Education)
Sago Mini Superhero 1.0 Device: iOS Universal Category: Education Price: $2.99, Version: 1.0 (iTunes) Description: KAPOW! Jack the rabbit bursts into the sky as the Sago Mini Superhero! Fly with Jack as he lifts impossible weights,... | Read more »
Star Wars: Galaxy of Heroes guide - How...
Star Wars: Galaxy of Heroes is all about collecting heroes, powering them up, and using them together to defeat your foes. It's pretty straightforward stuff for the most part, but increasing your characters' stats can be a bit confusing because it... | Read more »

Price Scanner via

World’s First USB-C Adapter For MacBook Suppo...
Innergie, a brand of Delta Electronics, has announced its official release of the world’s first USB-C adapter supporting four DC output voltages, the PowerGear USB-C 45. This true Type C adapter... Read more
13-inch and 11-inch MacBook Airs on sale for...
B&H Photo has 13″ and 11″ MacBook Airs on sale for up to $120 off MSRP as part of their Holiday sale including free shipping plus NY sales tax only: - 11″ 1.6GHz/128GB MacBook Air: $819 $90 off... Read more
13-inch MacBook Pros on sale for up to $150 o...
Take up to $150 off MSRP on the price of a new 13″ MacBook Pro at B&H Photo today as part of their Holiday sale. Shipping is free, and B&H charges NY tax only. These prices are currently the... Read more
13-inch 128GB MacBook Air now on sale for $79...
Best Buy has just lowered their price on the 2015 13″ 1.6GHz/128GB MacBook Air to $799.99 on their online store for Cyber Monday. Choose free shipping or free local store pickup (if available). Sale... Read more
Best Buy lowers 13-inch MacBook Pro prices, n...
Best Buy has lowered prices on select 13″ MacBook Pros this afternoon. Now save up to $200 off MSRP for Cyber Monday on the following models. Choose free shipping or free local store pickup (if... Read more
Cyber Monday: Apple MacBooks on sale for up t...
Apple resellers have MacBook Pros, MacBook Airs, and MacBooks on sale for up to $250 off MSRP for Cyber Monday 2015. The following is a roundup of the lowest prices available for new models from any... Read more
Cyber Monday: Apple Watch on sale for up to $...
B&H Photo has the Apple Watch on sale for Cyber Monday for $50-$100 off MSRP. Shipping is free, and B&H charges NY sales tax only: - Apple Watch Sport: $50 off - Apple Watch: $50-$100 off B... Read more
Cyber Monday: 15% off Apple products, and sto...
Use code CYBER15 on Cyber Monday only to take 15% on Apple products at Target, and store-wide. Choose free shipping or free local store pickup (if available). Sale prices for online orders only, in-... Read more
iPad Air 2 And iPad mini Among Top Five Black...
Adobe has released its 2015 online shopping data for Black Friday and Thanksgiving Day. The five best selling electronic products on Black Friday were Samsung 4K TVs, Apple iPad Air 2, Microsoft Xbox... Read more
All-in-one PC Shipments Projected To Drop Ove...
Digitimes’ Aaron Lee and Joseph Tsai report that all-in-one (AIO) PC shipments may drop a double-digit percentage on-year in 2015 due to weaker-than-expected demand, although second-largest AIO make... Read more

Jobs Board

*Apple* New Products Tester Needed - Apple (...
…we therefore look forward to put out products to quality test for durability. Apple leads the digital music revolution with its iPods and iTunes online store, continues Read more
Software Engineer, *Apple* Watch - Apple (U...
# Software Engineer, Apple Watch Job Number: 33362459 Santa Clara Valley, Califo ia, United States Posted: Jul. 28, 2015 Weekly Hours: 40.00 **Job Summary** Join the Read more
SW Engineer - *Apple* Music - Apple (United...
# SW Engineer - Apple Music Job Number: 40899104 San Francisco, Califo ia, United States Posted: Aug. 18, 2015 Weekly Hours: 40.00 **Job Summary** Join the Android Read more
Sr Software Engineer *Apple* Pay - Apple (U...
# Sr Software Engineer Apple Pay Job Number: 44003019 Santa Clara Valley, Califo ia, United States Posted: Nov. 13, 2015 Weekly Hours: 40.00 **Job Summary** Apple Read more
*Apple* Site Security Manager - Apple (Unite...
# Apple Site Security Manager Job Number: 42975010 Culver City, Califo ia, United States Posted: Oct. 2, 2015 Weekly Hours: 40.00 **Job Summary** The Apple Site Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.