Tuesday, March 27, 2012

Part 4: The Program

In this post, I will write the code to compile and link the shaders inside the program to be able to use it for drawing.

You will need 2 new variables and a struct for this part. One of the variables will be used to hold the program handle, which you will use to make subsequent calls to OpenGL that need it. The other variable, an array, will hold the information about your uniforms, their name and location.

Every time you draw, you will probably want to give your shaders a different ModelView transformation matrix, to make your object move for example. For that, you need its name and location, and this should only be queried once. Here is the struct and the two variables you need to add (Add them to your private interface):

Here's the code for compiling the shaders:

And here's the code for creating the program and linking the compiled shaders:

And finally, here's a piece of code to query the uniforms information

Now you can add these two lines of code where you left off in the ViewDidLoad method

[self createProgram];
[self getUniforms];

This is all for the program part, build and run the application. Nothing should have changed, but you shouldn't be getting any errors. You also have a fully operational program, as well as all the information you need to set the uniforms inside the shader every time you want to draw.


Alun said...

I saw a comment about missing Gists on these posts - I think there is another one missing here - the bit where it says "Here is the struct and the two variables you need to add (Add them to your private interface):" is empty!

Great tutorials BTW!

Gabriel Gohier-Roy said...

Thanks! I fixed it. I found out that they used to put the file name in the gist's url prior to a change they made internally, and they could still find it by its Id, even when I changed the file name on github. At some point though, they stopped providing the file name, and then all the gists that had their names changed had broken links. Ugh...