GLSL tips and where to get started


ever since opengl is out there that I’ve struggled to get the “Shader” thing right. My main issue is not knowing the prebuilt varibles (like gl_LightSource) and such.

So my question is basically if someone could give me some pointers and advice on where to start looking for how to code a simple shader that could implement something like:

  • multiple light sources (per-pixel light) at the same time,
  • multiple textures (with normal mapping),
  • alpha channel,
  • and fog

I’ve tried to do one myself but without any success so far.

I’m currently using “OpenGL version 3.2.0”.
I really appreciate anyone that could help.

My best regards,

What specifically is causing you trouble?

If you just want a simple, standalone test program that compiles, links, and renders with some simple shaders, we could point you to some or post one. What’s your dev environment?

As far as some shader tutorial sources, there are a bunch out there. But consider these. You can sift through the book example code as well:

  1. OpenGL Programming Guide (Example code)
  2. OpenGL 4 Shading Language Cookbook (Example Code)
  3. OpenGL Shading Language (For example code, search for “”]

I hesitate a little to mention #3 above because it’s 10 years old and doesn’t make use of the latest GLSL/GL techniques. However, it does have some solid tutorials with source code describing how to accomplish specific tasks using shaders (including those that you mention above). All of them do really, though the last 2 focus more on the shader code.

Well basically, I cant get to work just a simple Shader with multiple lightsources. Struggling a lot mainly because I cant seem to grasp the problem or debug a faulty program. So basically I’ve came across a lot of examples around the web but I learned thar each GL version has its own variables and deprecated functions that lead me to struggle a lot more.

Secondary Issue is the fact that I don’t know the data types such as Mat2, Mat3 and so on.

Java (LWJGL) win 8.1 64x, with GL 3.2
Well, I only could set to work a simples shader where everything gets green/blue/red, basically setting gl_FragColor = vec4(1.0,2.0,0.0,1.0);

more than that, I just get weird and funky results.

But if I could just write a simple shader with lighting (multiple sources) using multiple textures (base one, and secondary ones as modulate effects, basically multiplying each pixel) I would be more than satisfied, at least having a pointer so I could manipulate the shader afterwards to my own needs. To start I would like to have something without Uniform Parameters.

That is also one of my main concerns, if what I’m aiming for could be limited by the fact that newer techniques could be either easier or better (performance-wise).

and thanks for the examples, the first one though is C#, so I think I would need to adapt it, but that has always been my main issue “adapt” and while not having any kind of debug info (in design time) I struggle to get to the problems in hand.

For a reference of types, functions and variables, refer to the GLSL specification:
(HTML) (PDF) (Previous versions)

But the specification is designed for completeness, not legibility. Realistically, you’re going to need to read at least one tutorial to make any sense of it.

Ok, well you probably know this. But walk before you can run. Start simple. Debug that. Then build on that.

Put multiple light sources on the shelf for now. Get a single light source working (directional first, then point). The above books (especially the last two) will just flat tell you how. And there’s learning value in getting their examples to work, or just taking their working examples and “tweaking them” in different ways to see how things are wired together.

However, if any of: “N dot L” lighting, vectors, matrices (e.g. 3x3, 4x4, etc.), modeling and viewing transforms, object space, world space, or eye space are greek to you, you really should save yourself time and frustration, pick up a good book (see above) and do some reading. If something is confusing, ask questions here. There are a number of folks that read these forums that would be happy to answer specific questions.

I wouldn’t worry about using the absolute latest techniques when you’re learning. The GL driver implementation you’re using is only GL 3.2 anyway. You first need to get the high level picture in your head as to what the pieces are and how they all fit together. At this stage, just pick a GLSL version to target and stick with it. They’re not really that different. Since your implementation currently supports GL 3.2, then I’d just pick GLSL 1.5 (the corresponding GLSL version) and be done with it.

Thanks guys. I’ll be here posting more then.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.