Advertisement Jump to content
Sign in to follow this  
rafciok_6

OpenGL GLSL

This topic is 1789 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

Hello 

I am using GLSL 1.5 trying to calculate per vertex lighting, and my problem is I do not really know how to calculate the normal for every vertex. Should I do it in vertex shader or I should do it in main OpenGL program and pass this value into the shader. In previous version there was gl_Normal defined value which is deprecated in 1.5 version. I will be very happy if someone can explain me this.  

Thank you for help. 

Share this post


Link to post
Share on other sites
Advertisement

In general you will want to calculate the value in the application. For normal triangles the geometry shader could be used to calculate a normal (but that would only be equivalent to flat shading, so it's pretty useless in most cases). For other types of primitives (like GL_TRIANGLES_ADJACENCY) something more interesting would be possible. Usually however, normals are calculated by the modeler.

There are countless approaches to generate normal data though. Which is best will depend on how much energy you want to invest into the implementation and the kind of models you work with. A simple approach is assigning to each vertex the averaged normal of all faces it is part of.

Share this post


Link to post
Share on other sites

Thank you for answer. I think I need to read a little bit more about it. Maybe you have any relevant tutorial or useful links for this topic. Thank you  

Share this post


Link to post
Share on other sites

If gl_Normal is deprecated in the version you are using ( at least try compiling a shader with gl_Normal to see), then you just need to set it up as a generic attribute and give it the name gl_Normal.

Share this post


Link to post
Share on other sites

Per vertex normals don't have a single straightforward formula, like a simple face normal does.

 

This shows how to calculate a face normal: https://www.opengl.org/wiki/Calculating_a_Surface_Normal

 

Vertex normals are usually pseudo "art" information. Meaning, as mentioned before, they're done in the modeling application and saved into the model file itself. Because they can be manually manipulated for different effects, ie "harder" - "softer" ...

 

The more "automated" way is to take the weighted average of the triangles around a vertex.

 

In maya for example, you can supply an "angle" to shift they by, and other applications might have similar different options.

 

Even wavefront obj supports vertex normals so I'd say make sure your priorities are correct.

 

If your application doesn't even support loading a model format which contains the vertex normal information itself, I'd say worry about implementing that first before you worry about how good some simple template object looks.

 

First get something working. THEN make it good. :)

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!