## Moving the Camera Through a Scene

### Summary of Techniques

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.

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

### Graph 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® software to calculate the size of the cones correctly for the final view.

```load wind wind_speed = sqrt(u.^2 + v.^2 + w.^2); figure p = patch(isosurface(x,y,z,wind_speed,35)); isonormals(x,y,z,wind_speed,p) p.FaceColor = [0.75,0.25,0.25]; p.EdgeColor = [0.6,0.4,0.4]; [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); hcone.FaceColor = 'blue'; hcone.EdgeColor = 'none'; ```

### Set 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) ```

### Specify 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'); p.AmbientStrength = 1; p.SpecularStrength = 1; p.DiffuseStrength = 1; hcone.SpecularStrength = 1; set(gcf,'Color','k') set(gca,'Color',[0,0,0.25])```

### Select the Lighting Method

Use `gouraud` lighting for smoother lighting:

```lighting gouraud ```

### Define 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 (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 = hsline.XData; yd = hsline.YData; zd = hsline.ZData; delete(hsline) ```

### Implement 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)-5 campos([xd(i),yd(i),zd(i)]) camtarget([xd(i+5)+min(xd)/500,yd(i),zd(i)]) camlight(hlight,'headlight') drawnow end ```

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