directxhlsllightingdirectx-10specular

How do I send information to an HLSL effect in DirectX 10?


I'd like to send my view vector to an ID3D10Effect variable in order to calculate specular lighting. How do I send a vector or even just scalar values to the HLSL from the running DirectX program? I want to do something like

render() {
   //do transformations
   D3DXMatrix view = camera->getViewMatrix();
   basicEffect.setVariable(viewVector, view);
   //render stuff
}

Solution

  • In your effect, you should have something like:

    cbuffer {
        float4x4 viewMatrix;
    }
    

    Then in your render function, before binding the effect:

    D3DXMatrix view = camera->getViewMatrix();
    basicEffect->GetVariableByName("viewMatrix")->AsMatrix()->SetMatrix((float*) &view);
    

    As with most effect attribute handles, I would suggest 'caching' the pointer to the variable. Storing the matrix variable in another pointer outside of your render loop, like:

    ID3D10EffectMatrixVariable* vmViewMatrix = basicEffect->GetVariableByName("viewMatrix")->AsMatrix();
    

    And then setting the variable turns into:

    vmViewMatrix->SetMatrix((float*) &view);