Rendering Normals (For Debugging) using the Geometry Shader

Started by
5 comments, last by BenS1 11 years, 4 months ago
Hi,

I've got an annoying lighting problem with my terrain engine and I'm not sure if the problem is with my calculation of the terrain surface normals or something else.

I've tried single stepping through the HLSL code but personally I find it a little difficult to visualize vectors when they are just presented as numbers, so I'd much rather actually see the vectors (Normals in this case).

I guess it should be quite trivial to create a Geometry shader that renders the normals... but rather than reinvent the wheel I thought I'd just check to see if anyone has such a shader that I could use? (HLSL SM 5 compatible)

I've tried a quick search but all I've found are OpenGL versions and no HLSL versions.

Anyway, if anyone has such a shader then great. If not then I'll write my own and share it on here for anyone else that may want to use it in future.

Thanks
Ben
Advertisement
have you tired render the value as a color? it´s more easy to think of when you got colors. Or atleast i belive so.
"There will be major features. none to be thought of yet"
I haven't seen a ready-made one, but the logic is indeed trivial. You just output a line (of uniform length) that starts from the vertex position and points to the direction of the normal.

While at it, you can also draw tangent and bitangent lines as well in the same shader. If you do, be sure to output meaningful colors to go with the lines.

If you want to visualize the face normals instead of vertex normals, just average the vertex normals for the current triangle (and take the centroid or average of the positions to find the "center" of the triangle) and normalize the result before streaming it out from the GS.

Tordin's suggestion is also good. Red would represent the range of -1...+1 in the x direction, green in the y... and you get the idea.

Niko Suni

Thanks both,

Yes I'll try the colour idea first, although it'll still be a little difficult to visualize that a triangle of a certain colour represents a normal is a certain direction, but it could still be very useful.

I'll go on to implementing the geometry shader version second and I'll post the code on here for anyone that wants it.

Thanks
Ben
Interesting... it took less than 30 seconds to implement the rendering of normals as a colour idea and it indeed shows that there's something "not normal with my normals!" :)

I must have a problem with my Sobel filter somewhere

Off to the debugger...
Colors can show discontinuities very effectively, whereas with lines it could actually be more difficult.

Niko Suni

Sure Nik02, I agree its great for some stuff, but it wouldn't be obvious for example if all my Normals were pointing in the opposite direction.

As it happens, in this case the colours did show quite clearly that something was not right, so I removed my Sobel filter based Normal calculator and replaced it with a simple Normal calculator (i.e. takes 3 points on the terrain, creates 2 vectors and then the Cross of those 2 vectors). All works perfectly now.

At some point I'll revisit the Sobel filter, but for thsi project I don't think I need it anyway.

Big thanks to your help... this lighting problem had been driving me crazy for days.

Thanks
Ben

This topic is closed to new replies.

Advertisement