Hello everybody,
I’m writing an ARB vertex program to implement simple per-vertex Lambert lighting. I’m calculating the light intensity in the vertex program (0.0…1.0). I first used it as a texture sample coordinate and noticed something wrong, so I reduced it to isolate the problem and added a fragment program to visually debug the values.
This is what I’ve got currently:
!!ARBvp1.0
PARAM mvp[4] = { state.matrix.mvp };
PARAM light_pos = program.env[0];
TEMP light_vec;
TEMP nlight_vec;
DP4 result.position.x, mvp[0], vertex.position;
DP4 result.position.y, mvp[1], vertex.position;
DP4 result.position.z, mvp[2], vertex.position;
DP4 result.position.w, mvp[3], vertex.position;
# Distance vector from light position to vertex position
SUB light_vec, light_pos, vertex.position;
# By-the-book normalization for light_vec
DP3 nlight_vec.w, light_vec, light_vec;
RSQ nlight_vec.w, nlight_vec.w;
MUL nlight_vec.xyz, nlight_vec.w, light_vec;
# Dot product to compute the intensity of light for the vertex
DP3 result.color.x, nlight_vec, vertex.normal;
DP3 result.texcoord.x, nlight_vec, vertex.normal;
END
As you can see I write the same value to both result.color.x and result.texcoord.x; this is for debug, see the fragment program:
!!ARBfp1.0
MOV result.color.x, fragment.texcoord.x;
MOV result.color.y, fragment.color.x;
MOV result.color.z, fragment.color.x;
END
At this point I would expect the primitives to be rendered in greyscale, because I’m passing what should be three identical values to the r, g and b components of the fragment.
Instead, the less lit fragments are bluish, indicating that the red coordinate (coming from the texcoord) is lower than the others. How is this possible? Both color and texcoord attributes in the vp are filled with the same value, the result of nlight_vec . vertex.normal. I feel like I’m missing something obvious, as it’s been almost 10 years since I last wrote a vertex program, but I couldn’t figure out what so far.
Thanks in advance!