views:

98

answers:

2

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
}
+1  A: 

Use GetVariableByName to get an interface to the named variable in the HLSL. Call AsVector (Note the documentation at this point is wrong. It returns a pointer!) on the returned interface to get a vector variable interface and then call SetFloatVector.

Goz
+1  A: 

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);
Chris Waters