Jump to content
  • Advertisement
Sign in to follow this  
BenS1

OpenGL Rendering Normals (For Debugging) using the Geometry Shader

This topic is 2160 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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. Edited by Tordin

Share this post


Link to post
Share on other sites
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. Edited by Nik02

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
Colors can show discontinuities very effectively, whereas with lines it could actually be more difficult.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!