Microsoft DirectX 8.1 (C++) |
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 IDirect3DDevice8::GetDeviceCaps method to determine if tweening is supported.
// // This example assumes that m_d3dDevice is // a valid pointer to a IDirect3DDevice8 interface. // D3DCAPS8 d3dCaps; m_d3dDevice->GetDeviceCaps( &d3dCaps ); if( 0 != (d3dCaps.VertexProcessingCaps & D3DVTXPCAPS_TWEENING) ) //Vertex tweening is supported.
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.
struct TEX_VERTEX { D3DVECTOR position; D3DVECTOR normal; D3DVECTOR position2; D3DVECTOR normal2; }; //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.
DWORD decl[] { D3DVSD_STREAM(0), D3DVSD_REG( D3DVSDE_POSITION, D3DVSDT_FLOAT3 ) // Position 1 D3DVSD_REG( D3DVSDE_NORMAL, D3DVSDT_FLOAT3 ) // Normal 1 D3DVSD_REG( D3DVSDE_POSITION2, D3DVSDT_FLOAT3) // Position 2 D3DVSD_REG( D3DVSDE_NORMAL2, D3DVSDT_FLOAT3 ) // Normal 2 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 DWORD handle; float TweenFactor = 0.3f;
The first step is to use the IDirect3DDevice8::CreateVertexShader method to create a vertex shader, as shown in the code example below.
m_d3dDevice->CreateVertexShader( decl, NULL, &handle, 0 );
The first parameter accepted by CreateVertexShader is a pointer to a vector shader declaration. This example uses the declaration declared above. The second parameter accepts a pointer to a vertex shader function array. This example does not use a vertex shader function, so this parameter is set to NULL. The third parameter accepts a pointer to 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 IDirect3DDevice8::SetVertexShader method as shown in the code example below.
m_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 IDirect3DDevice8::SetRenderState and IDirect3DDevice8::SetStreamSource to accomplish this.
m_d3dDevice->SetRenderState( D3DRS_VERTEXBLEND, D3DVBF_TWEENING ); m_d3dDevice->SetRenderState( D3DRS_TWEENFACTOR, *(DWORD*) &TweenFactor ); m_d3dDevice->SetStreamSource( 0 ,vb, 12*sizeof(float));
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.