# GLSL Programming/Blender/Two-Sided Surfaces

This tutorial covers two-sided per-vertex lighting.
It's part of a series of tutorials about basic lighting in Blender. In this tutorial, we extend the tutorial on specular highlights to render two-sided surfaces. If you haven't read the tutorial on specular highlights, this would be a very good time to read it.

### Two-Sided Lighting

As shown by the figure to the left, it's sometimes useful to apply different colors to the two sides of a surface. In the tutorial on cutaways, we have seen how a fragment shader can use the built-in variable gl_FrontFacing to determine whether a fragment is part of a front-facing or a back-facing triangle. Can a vertex shader also determine whether it is part of a front-facing or a back-facing triangle? The answer is a clear: no! One reason is that the same vertex can be part of a front-facing and a back-facing triangle at the same time; thus, whatever decision is made in the vertex shader, it is potentially wrong for some triangles. If you want a simple rule to remember: “Fragments are either front-facing or back-facing. Vertices are bi.”
Thus, two-sided per-vertex lighting has to let the fragment shader determine, whether the front or the back material color should be applied. For example, with this fragment shader:
On the other hand, this means that the vertex shader has to compute the surface lighting twice: for a front face and for a back face. Fortunately, this is usually still less work than computing the surface lighting for each fragment.

### Vertex Shader Code

The vertex shader code for two-sided per-vertex lighting is a straightforward extension of the code in the tutorial on specular highlights. However, we have to deactivate back-face culling as described in the tutorial on transparency. Furthermore, the shader requires two sets of material parameters (front and back), which could be available as gl_FrontMaterial and gl_BackMaterial. With these two sets of parameters, the vertex shader could compute two colors, one for front faces and one for back faces, where the negated normal vector has to be used for the back faces. Both colors are handed to the fragment shader, which decides which color to apply. Unfortunately, there is apparently no way in Blender to specify different materials for the front and back faces. Instead, gl_FrontMaterial and gl_BackMaterial contain always the same data. Thus, in Blender you will have to introduce new uniforms or constants to specify different parameters for the front and the back material. In this tutorial, however, we just pretend that Blender would different parameters in gl_FrontMaterial and gl_BackMaterial. The vertex shader code is then:
The fragment shader code is described above.

### Summary

Congratulations, you made it to the end of this short tutorial with a long shader. We have seen:
* Why a vertex shader cannot distinguish between front-facing and back-facing vertices (because the same vertex might be part of a front-facing and a back-facing triangles).
* How to compute lighting for front faces and for back faces in the vertex shader.
* How to let the fragment shader decide which color to apply.

### Further Reading

If you still want to know more
* about the shader version for single-sided surfaces, you should read the tutorial on specular highlights.
* about front-facing and back-facing triangles in GLSL, you should read the tutorial on cutaways.
< GLSL Programming/Blender