Microsoft DirectX 8.1 (Visual Basic) |
To determine if Microsoft® Direct3D® supports vertex tweening, check for the D3DVTXPCAPS_TWEENING flag in the VertexProcessingCaps member of the D3DCAPS8 structure. The following code example uses the Direct3DDevice8.GetDeviceCaps method to determine if tweening is supported.
' ' This example assumes that d3dDevice ' is a valid Direct3DDevice object. ' Dim d3dCaps As D3DCAPS8 d3dDevice.GetDeviceCaps d3dCaps If 0 <> (d3dCaps.VertexProcessingCaps & D3DVTXPCAPS_TWEENING) Then 'Vertex tweening is supported. End If
To use vector tweening, you must first set up a custom vertex type that uses a second normal or a second position. The following code example shows a sample declaration that includes both a second point and a second position.
Private Type TEX_VERTEX Position As D3DVECTOR Normal As D3DVECTOR Position2 As D3DVECTOR Normal2 As D3DVECTOR ' Create a vertex buffer with the type TEX_VERTEX.
The next step is to set the current declaration. The code example below shows how to do this.
Dim decl(5) As Long decl(0) = D3DVSD_STREAM(0) decl(1) = D3DVSD_REG( D3DVSDE_POSITION, D3DVSDT_FLOAT3 ) 'Position 1 decl(2) = D3DVSD_REG( D3DVSDE_NORMAL, D3DVSDT_FLOAT3 ) 'Normal 1 decl(3) = D3DVSD_REG( D3DVSDE_POSITION2, D3DVSDT_FLOAT3) 'Position 2 decl(4) = D3DVSD_REG( D3DVSDE_NORMAL2, D3DVSDT_FLOAT3 ) 'Normal 2 decl(5) = D3DVSD_END()
For more information on creating a custom vertex type and a vertex buffer, see Creating a Vertex Buffer.
Notes When vertex tweening is enabled, a second position or a second normal must be present in the current declaration.
The code example below shows how to implement fixed-function vertex tweening after a vertex type and declaration are set.
' Variables used for this example Dim handle As Long Dim TweenFactor As Float TweenFactor = 0.3
The first step is to use the Direct3DDevice8.CreateVertexShader method to create a vertex shader, as shown in the code example below.
Call d3dDevice.CreateVertexShader(decl, ByVal 0, handle, 0)
The first parameter accepted by CreateVertexShader is a vector shader declaration. This example uses the declaration declared above. The second parameter accepts a vertex shader function array. This example does not use a vertex shader function, so this parameter is set to ByVal 0. The third parameter accepts a vertex shader handle representing the returned vertex. The fourth parameter specifies the usage controls for the vertex shader. You can use the D3DUSAGE_SOFTWAREPROCESSING flag to indicate that the vertex shader should use software vertex processing. This example sets this parameter to zero to indicate that vertex processing should be done in hardware.
The next step is to set the current vertex shader by calling the Direct3DDevice8.SetVertexShader method as shown in the code example below.
Call d3dDevice.SetVertexShader(handle)
SetVertexShader accepts a handle to a vertex shader. The value for this parameter can be a handle returned by CreateVertexShader or an FVF code. This example uses the handle returned from CreateVertexShader called in the last step.
The next step is to set the current render state to enable vertex tweening, set the tween factor, and set the stream source. The code example below uses Direct3DDevice8.SetRenderState and Direct3DDevice8.SetStreamSource to accomplish this.
Call d3dDevice.SetRenderState(D3DRS_VERTEXBLEND, D3DVBF_TWEENING) Call d3dDevice.SetRenderState(D3DRS_TWEENFACTOR, TweenFactor) Call d3dDevice.SetStreamSource(0 ,vb, 12*Len(vb(0))
If you have more than one stream specified for the current vertex shader, you need to call SetStreamSource for each stream that will be used for the tweening effect. At this point, vertex tweening is enabled. A call to any rendering function automatically has vertex tweening applied to it.