View Full Version : How to get shading with out supplying normals using glDrawElements.

02-07-2014, 04:23 AM
:)I am drawing a mesh using glDrawElements.
Recently I heard that, using GL Shading language(GLSL) ,
shading is possible with out supplying normals.
Is it possible. !!!????
Please help me on this.
If possible, please provide some examplary code.
Thanks in advance.

02-07-2014, 05:46 AM
Normals are always needed in fragment shader, but you can compute them directly in GLSL (additional work done by shaders, for every vertex).
Search on Google how to compute them in the best way in which fits your case.

02-07-2014, 07:14 AM
This strongly depends on the data you are planning to use.

If you have a heightmap, the computation is easy: The shader must simply take additional heights next to the point the normal will be calculated for. Then, a cross-product gives a good approximation of how the actual normal should look like. The more points are taken into account for this calculation, the more accurate the normal is.

However, for arbitrary geometry this may not be possible. As far as I know, the fragment shader cannot know all points of the drawn polygon; only one point can be known (by using the "flat" keyword which leaves the coordinate un-interpolated).

But why not calculate the normals out of the shader once for all faces, and then simply pass them on to the shader to read and draw?

02-07-2014, 03:09 PM
One way is to use a geometry shader; input triangles with adjacency, output a single triangle, and calculate the normal from the vertex data. Adjacency on input is only necessary if you want to average normals for adjacent faces.

Of course that's going to be slower than just calculating normals in your asset creation pipeline and including them in the models you load and draw. But it can certainly be done.

02-28-2014, 04:08 AM
Thanks tdname, I will use GLSL.

Thanks all of you to help me on this.