|Microsoft DirectX 9.0|
The MotionBlur sample demonstrates a vertex shader that creates a motion blur effect by stretching some vertices along the direction of motion and fading their transparency.
Source: (SDK root)\Samples\C++\Direct3D\MotionBlur
Executable: (SDK root)\Samples\C++\Direct3D\Bin
The following table lists the keys that are implemented. You can use menu commands for the same controls.
|UP ARROW||Increase the motion blur trail length.|
|DOWN ARROW||Decrease the motion blur trail length.|
|ENTER||Starts and stops the scene.|
|SPACEBAR||Advances the scene by a small increment.|
|F1||Shows Help or available commands.|
|F2||Prompts the user to select a new rendering device or a new display mode.|
|ALT+ENTER||Toggles between full-screen and windowed modes.|
|ESC||Exits the application.|
The mouse is also used in this sample to control the viewing position.
The sample shows two spheres, both represented by the same ID3DXMesh object. The spheres move in an ellipse, and each sphere remembers the matrix for its current position and last-computed position. Each sphere is rendered once using the "normal" (ordinary) shader, and once using the motion-blur shader.
The motion-blur shader starts by transforming the vertex position (v0) through the last-frame world matrix (c0) and the current-frame world matrix (c4), and stores the results in r0 and r1. The normal vector (v1) is also transformed through the current-frame world matrix (c4) and the result is stored in r2.
r3 gets the motion delta vector from r0 to r1, and is normalized. Then r4.x gets set to the dot product of the vertex normal and the motion delta vector. So the closer the vertex normal is to the motion delta vector, the larger r4.x will be for that vertex. If r4.x is less than zero, the vertex normal points away from the motion delta vector. Only these vertices will be blurred.
In the next section, r4.y gets set to 1 if r4.x is less than zero (c20.y), or 0 if r4.x is zero or positive. So the motion blur will only be applied to vertices whose normal faces away from the direction of motion. If r4.y is zero, r1 is unchanged. If r4.y is 1, r1 will get -r3 (the motion direction) added to it.
Next r1 is transformed through the rotation and projection matrices, and the result goes into oPos.
Finally, the diffuse color is computed and stored in oD0, with oD0's alpha being set to 0 if r4.x (the dot product of the vertex normal and the direction of motion) is negative, or 1 otherwise. This makes the blur fade out as it gets further from the real object.
This sample uses the sample framework code that is shared with other samples in the Microsoft?DirectX® software development kit (SDK). You can find the sample framework headers and source code in (SDK root)\DXSDK\Samples\C++\Common\Include and (SDK root)\DXSDK\Samples\C++\Common\Src.