Question

I'm trying to learn GLSL for Open GL ES 2.0. I've looked around at tutorials but there's clearly something missing in the explanations because it makes absolutely no sense. Take the code below. This is ripped right from the official OpenGL ES 2.0 reference card.

This is supposed to be the vertex shader.

uniform mat4 mvp_matrix; // model-view-projection matrix
uniform mat3 normal_matrix; // normal matrix
uniform vec3 ec_light_dir; // light direction in eye coords
attribute vec4 a_vertex; // vertex position
attribute vec3 a_normal; // vertex normal
attribute vec2 a_texcoord; // texture coordinates
varying float v_diffuse;
varying vec2 v_texcoord;

void main(void)
{
   // put vertex normal into eye coords
   vec3 ec_normal = normalize(normal_matrix * a_normal);
   // emit diffuse scale factor, texcoord, and position
   v_diffuse = max(dot(ec_light_dir, ec_normal), 0.0);
   v_texcoord = a_texcoord;
   gl_Position = mvp_matrix * a_vertex;
}

Problem 1: Nothing is initialized. There's 8 variables/objects listed at the start and not one is ever initialized. So we've got 8 null variables/objects.

Problem 2: The variables/objects that are initialized are initialized using the previous 8 null variables/objects. Great, so now everything is null...

Problem 3: Nothing is passed out of the function. How this can shade anything is beyond me, it takes nothing in and passes nothing out. The only thing it touches that could have an effect is gl_Position which is apparently some kind of special output variable. But it only depends on mvp_matrix and a_vertex so what's all the other stuff for?

And it isn't just this example, it's all the one's I've found so far. I don't understand how declaring a bunch of variables, not using most of them, and then passing/assigning null objects all over the place can do anything.

Was it helpful?

Solution

you are assuming that a shader behaves the same as regular c code, but this is not the case. vertex shaders have two types of input: attributes and uniforms, and one type of output: varyings. that is what is being defined at the top of the shader code. attributes are per vertex data, they are different for every vertex. uniforms are the same across all the vertexes. initialising the attributes and uniforms is done elsewhere, in your main rendering method in the application. here, there will be lots of calls like:

// set up the position attribute
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, vertices);

the outputs to the shader are the varyings v_diffuse and v_texcoord. the values for these outputs are interpolated across all the rendered fragment shaders, for use in lighting the fragments.

the gl_Position variable is a special output that sets the position of the vertex.

for this example to make sense, you also need to look at the main loop doing the rendering and the fragment shader, they are all tied together.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top