I am in the middle of porting some shader programs to panda. It is also an exercise for me to learn more about shaders. I would like to know if there is good tricks/ways to debug a shader program (without paying an expensive debugging tools) ?
Secondly, I have a vertex shader that transform a vertex position, and I would like to get the eye vector to that new position. I have the code like that, can you advice if it is correct, or there is better way to write this ?
# Panda
np.setShaderInput("eyePosition", base.camera)
#
void vshader(
in uniform float4 cspos_eyePosition,
in float4 vtx_position : POSITION,
...
out ....)
{
float4 position = vtx_position;
// change the position
...
l_position = mul(mat_modelproj, position);
l_eyeVector = l_position - cspos_eyePosition;
}
I think you want to use the model space position, and not the clip space, as the vertex position is in model space, and doing the subtraction in two different spaces wouldn’t return the eye vector you’re looking for. If you need to transform it to another coordinate space, just mul by the appropriate trans_x_to_y[_of_z] matrix.
Oh, I didn’t see the line where you calculated l_position, and I assumes you were using vtx_position for some reason. It looks fine, then, and I think it should work well. (I’d recommend a check by another person with more shader experience, however)
For the record purpose, if I do it in clip space, it does not work as expected.
Finally I do it in panda side, calculated the relative eye position from the object in the world space, and in the shader to calculate the final eye position relative to the vertex position.
This is for a per-pixel eyevector. You could move the second line to the vertex shader if that’s where you need it. Then you’d just have that line start with float3 l_positionW. Remember to have a uniform float4 wspos_view .