3-D Visualization

Example -- Moving the Camera Through a Scene

A fly-through is an effect created by moving the camera through three-dimensional space, giving the impression that you are flying along with the camera as if in an aircraft. You can fly through regions of a scene that might be otherwise obscured by objects in the scene or you can fly by a scene by keeping the camera focused on a particular point.

To accomplish these effects you move the camera along a particular path, the x-axis for example, in a series of steps. To produce a fly-through, move both the camera position and the camera target at the same time.

The following example makes use of the fly-though effect to view the interior of an isosurface drawn within a volume defined by a vector field of wind velocities. This data represents air currents over North America.

See `coneplot` for a fixed visualization of the same data.

Summary of Techniques

This example employs a number of visualization techniques. It uses

• Isosurfaces and cone plots to illustrate the flow through the volume
• Lighting to illuminate the isosurface and cones in the volume
• Stream lines to define a path for the camera through the volume
• Coordinated motion of the camera position, camera target, and light

Graphing the Volume Data

The first step is to draw the isosurface and plot the air flow using cone plots.

See `isosurface`, `isonormals`, `reducepatch`, and `coneplot` for information on using these commands.

Setting the data aspect ratio (`daspect`) to `[1,1,1]` before drawing the cone plot enables MATLAB to calculate the size of the cones correctly for the final view.

• ```load wind
wind_speed = sqrt(u.^2 + v.^2 + w.^2);

hpatch = patch(isosurface(x,y,z,wind_speed,35));
isonormals(x,y,z,wind_speed,hpatch)
set(hpatch,'FaceColor','red','EdgeColor','none');

[f vt] = reducepatch(isosurface(x,y,z,wind_speed,45),0.05);
daspect([1,1,1]);
hcone = coneplot(x,y,z,u,v,w,vt(:,1),vt(:,2),vt(:,3),2);
set(hcone,'FaceColor','blue','EdgeColor','none');
```

Setting Up the View

You need to define viewing parameters to ensure the scene is displayed correctly:

• Selecting a perspective projection provides the perception of depth as the camera passes through the interior of the isosurface (`camproj`).
• Setting the camera view angle to a fixed value prevents MATLAB from automatically adjusting the angle to encompass the entire scene as well as zooming in the desired amount (`camva`).
• ```camproj perspective
camva(25)
```

Specifying the Light Source

Positioning the light source at the camera location and modifying the reflectance characteristics of the isosurface and cones enhances the realism of the scene:

• Creating a light source at the camera position provides a "headlight" that moves along with the camera through the isosurface interior (`camlight`).
• Setting the reflection properties of the isosurface gives the appearance of a dark interior (`AmbientStrength` set to 0.1) with highly reflective material (`SpecularStrength` and `DiffuseStrength` set to 1).
• Setting the `SpecularStrength` of the cones to 1 makes them highly reflective.
• ```hlight = camlight('headlight');
set(hpatch,'AmbientStrength',.1,...
'SpecularStrength',1,...
'DiffuseStrength',1);
set(hcone,'SpecularStrength',1);
set(gcf,'Color','k')
```

Selecting a Renderer

Because this example uses lighting, MATLAB must use either `zbuffer` or, if available, `OpenGL` renderer settings. The `OpenGL` renderer is likely to be much faster displaying the animation; however, you need to use `gouraud` lighting with OpenGL, which is not as smooth as phong lighting, which you can use with the `zbuffer` renderer. The two choices are

• ```lighting gouraud
set(gcf,'Renderer','OpenGL')
```

or for `zbuffer`

• ```lighting phong
set(gcf,'Renderer','zbuffer')
```

Defining the Camera Path as a Stream Line

Stream lines indicate the direction of flow in the vector field. This example uses the x-, y-, and z-coordinate data of a single stream line to map a path through the volume. The camera is then moved along this path. The tasks include

• Create a stream line starting at the point x = 80, y = 30, z = 11.
• Get the x-, y-, and z-coordinate data of the stream line.
• Delete the stream line (note that you could also use `stream3` to calculate the stream line data without actually drawing the stream line).
• ```hsline = streamline(x,y,z,u,v,w,80,30,11);
xd = get(hsline,'XData');
yd = get(hsline,'YData');
zd = get(hsline,'ZData');
delete(hsline)
```

Implementing the Fly-Through

To create a fly-through, move the camera position and camera target along the same path. In this example, the camera target is placed five elements further along the x-axis than the camera. A small value is added to the camera target x position to prevent the position of the camera and target from becoming the same point if the condition `xd(n) = xd(n+5)` should occur:

• Update the camera position and camera target so that they both move along the coordinates of the stream line.
• Move the light along with the camera.
• Call `drawnow` to display the results of each move.
• ```for i=1:length(xd)-50
campos([xd(i),yd(i),zd(i)])
camtarget([xd(i+5)+min(xd)/100,yd(i),zd(i)])
These snapshots illustrate the view at values of `i` equal to `10`, `110`, and `185`.