TheNerd's picture

Noob Question - WHY GLSL

So, I've been trying to get shadowing and lighting to work in OpenTK for a bit, specifically using VSM. As I started to learn more and more about GLSL I found tutorials on lots of things:

Per-Pixel Lighting with GLSL
Texturing with GLSL
Materials with GLSL

My question is - OpenGL has Lighting, Texturing, and Materials all built into its core framework. Why would you want to do the same thing in a shader? Is it faster in a shader?

And I've read that Per-Pixel lighting in OpenGL is bad performance-wise, but what if you did per-Pixel lighting via a shader, would that improve performance?



Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
iliak's picture

All the functions you mentioned are now deprecated in OpenGL 3.x...

TheNerd's picture

Deprecated in OpenGL core (meaning they should be done in shader only), or GLSL (meaning they should be done in OpenGL Application code only) ?

Tal's picture

I'm new to GL/TK so I'm answer by DirectX knowledge.(OpenGL->GLSL - DirectX->HLSL)
The basic things that you learned to create via shaders are only the basic things(and the start code) that you can do with it.
Actually, you're right - there is no need to use shaders for thoose goals, and it better to use fixed-function vertex because it is supported in older GPU and it's faster(but it's negligible for FPS).
But there is a huge but!
You can do some realistic effects via shaders! As I said, this basic things(lighting, textures, ...) that you're learnig is important for the "shader starting code" because when you use shaders you're getting more professional and you can't use both shader and fixed function!
Now I know that you got bored, so now we have a little list that the things we can do with shaders and we can't do with the fixed functions:
realistic water
some shaddows techniques
soft shaddows
god rays
rayleigh scattering(real day-time sky lighting)
If I made a mistake on the list please forgive me.
So as you can see - you are only in the beginning of a wonderful way to shade the pixel how ever you want.

the Fiddler's picture

The above posters are correct.

The bottom line is this: modern 3d hardware does not contain a fixed-function pipeline anymore. Since ~2003 or so, *all* effects are implemented via shaders. For example, if you call GL.Enable(EnableCap.Lighting), your drivers enable a specific shader that supports per-vertex lighting.

A rough sketch of how things evolved:

  • OpenGL 1.x only contains a fixed-function pipeline. You can enable/disable pre-defined effects (e.g. texturing, lighting, fog), control their parameters but you cannot create your own effects directly.
  • OpenGL 2.x allows you to use the fixed-function pipeline (emulated via hidden shaders) as well as create your own shaders.
  • OpenGL 3.x does with those hidden shaders and requires that you explicitly create your own shaders.
TheNerd's picture

AHHHHH ok so this makes sense so...

With per-vertex lighting, I get CRAP results. Say I have a 100X100 plane. I'd have to tessellate the living crap out of it to get even remotely decent results for a simple effect (say like, a rounded spotlight to shine on it). That massive tessellation has a huge vertex count that goes along with it - isn't that a BAD thing for something as simple as a plane? Is there a work around for this?

I thought, well, I can create my own "spotlight" shader and implement that along with my VSM shadows.... But I can't seem to find a shader out there that doesn't use the old deprecated methods. Boo.

Tal's picture

Again, you're right - but you have to mess up your hands at the beginning...
So for a better result, just put the light calculation in the pixel-shader instead the vertex-shader! So simple.
This method is called per-pixel lighting(instead of per-vertex lighting).
If you don't know what the reason that per-vertex lighting is less realistic than per-pixel lighting, ask here.

TheNerd's picture

hmm Do you guys happen to know of any good examples of Per-pixel Lighting Shaders that are GLSL 3+ that I could take a look at?

In game engines like the Crytek engine (2.0) or Unreal (latest version) engine - what types of lighting technologies are they using?

Tal's picture

All the games use Per-pixel Lighting! If they would not - their graphics is worst! Maybe there is a few isolated games that do use it- but this is the reason that I never heard of them...
Here is the different:

I'm gonna let you guess what is per-vertexed and what is per-pixeled LOL
And this is on a sphere - in a plane it's even worse(there is a mathematical reason for this)
The page is taken from here.
The reason for the different is simple - You calculate only the light of the vertex, and the pixel light is calculated by some linear process(that I forgot its name...) that based by the vertexes lights data from the vertex shader.
In per-pixeled it's the idle calculation - every pixel light is calculeted individually.
Almost forgot - a tutorial how to use per-pixel lighting:

TheNerd's picture

ok I gotcha , and yeah I can see why you would want Per-Pixel via a shader.

but the commands like gl_LightSource[0] and the other gl_LightSource parameters have been deprecated since GLSL 1.3 (or maybe 1.5). Is there another way to make a Per-Pixel Lighting shader without relying on those deprecated commands?

sgsrules's picture
TheNerd wrote:

ok I gotcha , and yeah I can see why you would want Per-Pixel via a shader.

but the commands like gl_LightSource[0] and the other gl_LightSource parameters have been deprecated since GLSL 1.3 (or maybe 1.5). Is there another way to make a Per-Pixel Lighting shader without relying on those deprecated commands?

gl_LightSource[0] and all its parts are just built in uniform variables left over from earlier incarnations of opengl. They got rid off all that stuff so that you could create your own custom variables and structures. So instead of using gl_LightSource parameters you would create your own light structure that contains all you light parameters like position color etc.

It sounds like you're jumping the gun on a few things. If you're not really sure why you would want to use shaders or per pixel lighting you really shouldn't be trying to implement variance shadow maps lol. Small steps man :) Try reading a few books and get the basics down before you start jumping into advanced tutorials, if you don't understand how a specific shader or technique really works and you're just copying shader code and trying to shoehorn it into your engine you are not going to get good results (Trust me i've been down that road). check out the orange book it's a good starting resource along with the opengl superbible. Another really good reference book i absolutely love is Real-Time Rendering but it mostly deals with theory and technique instead of implementation.