What nobody ever told you about passing data to GLSL - 22/02/2012, 13:50

For some reason this was never made clear in ANY tutorial I ever read. For some reason it wasn't evident to me despite being like the very first thing you must do when trying to set up a simple program in OpenGL ES 2.0, or essentially, anything that has to do with GLSL.

In any case, here it goes..

When you want to pass data to shader programs, there are some rules:

1) There are no attributes in fragment shaders. Instead you must use varyings.
2) To set a value to these varying things, you can't pull them directly by using glGetAttribLocation. Instead you MUST use an attribute from WITHIN the vertex shader and pass that value along. E.g.

Declare in both fragment and vertex shader the following varying variable:

varying vec2 varyTextureCoords;

Then in vertex shader ONLY, declare the following attribute variable:

attribute vec2 attrTextureCoords;

In the vertex shader code, pass the value from the attribute to the varying variable:

varyTextureCoords = attrTextureCoords;

3) Finally, uniforms can be declared in both the vertex and fragment shader. But they are not as cool since they cannot change during the execution of the glsl program.

And here is a vanilla superbasic glsl code for myself useful for texturing in case I forget these rules.


< Back to blog

This site doesn't use cookies, does not log IPs and does not track you in any way.