GLSL

Started by
4 comments, last by rafciok_6 10 years, 1 month ago

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.

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.

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

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.

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

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

Thanks. That exactly what I asked for.

This topic is closed to new replies.

Advertisement