Jump to content

  • Log In with Google      Sign In   
  • Create Account


GLSL

  • You cannot reply to this topic
5 replies to this topic

#1 rafciok_6   Members   -  Reputation: 118

Like
1Likes
Like

Posted 25 February 2014 - 10:04 AM

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. 



Sponsor:

#2 BitMaster   Crossbones+   -  Reputation: 3001

Like
4Likes
Like

Posted 25 February 2014 - 12:00 PM

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.



#3 rafciok_6   Members   -  Reputation: 118

Like
0Likes
Like

Posted 25 February 2014 - 12:46 PM

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  



#4 dpadam450   Members   -  Reputation: 807

Like
0Likes
Like

Posted 25 February 2014 - 01:16 PM

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.



#5 3TATUK2   Members   -  Reputation: 638

Like
2Likes
Like

Posted 25 February 2014 - 07:15 PM

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. :)



#6 rafciok_6   Members   -  Reputation: 118

Like
0Likes
Like

Posted 26 February 2014 - 03:26 AM

Thanks. That exactly what I asked for. 







PARTNERS