CHAPTER 10: OpenGL ES 2, Shaders, and... (^315)
if (_program)
{
glDeleteProgram(_program);
_program = 0;
}
return NO;
}
uniforms[UNIFORM_MODELVIEWPROJECTION_MATRIX] = //6
glGetUniformLocation(_program, "modelViewProjectionMatrix");
uniforms[UNIFORM_NORMAL_MATRIX] =
glGetUniformLocation(_program, "normalMatrix");
if (vertShader) //7
{
glDetachShader(_program, vertShader);
glDeleteShader(vertShader);
}
if (fragShader)
{
glDetachShader(_program, fragShader);
glDeleteShader(fragShader);
}
return YES;
}
Here’s what’s happening:
Line 1 generates a program handle and creates an empty program
object. You keep this handle around and use it to specify which
program you want to use for a specific piece of geometry, because
you can have multiple programs and swap them back and forth as
needed.
Now the shaders are compiled in lines 2ff.
Lines 3f bind the specific shader to the new program object. Each
program object must have one vertex and one fragment shader.
In lines 4ff, we bind whatever attributes we want to the program. In the
actual vertex shader code, you can see attributes by those names
defined for use:
attribute vec4 position;
attribute vec3 normal;
The names can be nearly anything you want; there is nothing special
about the use of position or normal.
Line 5 links both shaders.
singke
(singke)
#1