shader basic questions

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;

}

[/code]

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.

Isn’t mat_modelproj transform the vertex to clip space and I’ve the eyevector calculated in the same space ?

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)

Thank you for the confirmation. My shader is just looked strange so I want to confirm this basic stuff.

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.

Hmm, you might try doing the calculation in world space, with:

In the vertex shader (l_positionW is a float3 on a texcoord register)

l_positionW = mul(trans_model_to_world, vtx_position).xyz;

In the fragment shader

float3 eyevector = normalize(l_positionW - wspos_view.xyz);

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 .

Sound complicated ! Thank you for you advice and I will try out later.