Creating a Program
Prev: Creating a Shader | Next: Setup Example |
The following figure shows the necessary steps to get a shader program ready and going.
The first step is creating an object which will act as a program container. The function available for this purpose returns a handle for the container.
The syntax for this function, in OpenGL 2.0 syntax is as follows:
GLuint glCreateProgram(void);
And using the ARB extension is:
GLhandleARB glCreateProgramObjectARB(void);
You can create as many programs as you want. Once rendering, you can switch from program to program, and even go back to fixed functionality during a single frame. For instance you may want to draw a teapot with refraction and reflection shaders, while having a cube map displayed for background using OpenGL’s fixed functionality.
The next step involves attaching the shaders created in the previous subsection to the program you’ve just created. The shaders do not need to be compiled at this time; they don’t even have to have source code. All that is required to attach a shader to a program is the shader container.
To attach a shader to a program use the OpenGL 2.0 function:
void glAttachShader(GLuint program, GLuint shader);
Parameters:
- program – the handler to the program.
- shader – the handler to the shader you want to attach.
And using the ARB extension is:
void glAttachObjectARB(GLhandleARB program, GLhandleARB shader);
Parameters:
- program – the handler to the program.
- shader – the handler to the shader you want to attach.
If you have a pair vertex/fragment of shaders you’ll need to attach both to the program. You can have many shaders of the same type (vertex or fragment) attached to the same program, just like a C program can have many modules. For each type of shader there can only be one shader with a main function, also as in C.
You can attach a shader to multiple programs, for instance if you plan to use the same vertex shader in several programs.
The final step is to link the program. In order to carry out this step the shaders must be compiled as described in the previous subsection.
The syntax for the link function, in OpenGL 2.0, is as follows:
void glLinkProgram(GLuint program);
Parameters:
- program – the handler to the program.
The syntax for the link function, using the ARB extensions, is:
void glLinkProgramARB(GLhandleARB program);
Parameters:
- program – the handler to the program.
After the link operation the shader’s source can be modified, and the shaders recompiled without affecting the program.
As shown in the figure above, after linking the program, there is a function to actually load and use the program, (ARB extension) glUseProgramObjectARB, or (OpenGL 2.0) glUseProgram. Each program is assigned an handler, and you can have as many programs linked and ready to use as you want (and your hardware allows).
The syntax for this function is as follows (OpenGL 2.0 notation):
void glUseProgram(GLuint prog);
Parameters:
- prog – the handler to the program you want to use, or zero to return to fixed functionality
The syntax using the ARB extensions is as follows:
void glUseProgramObjectARB(GLhandleARB prog);
Parameters:
- prog – the handler to the program you want to use, or zero to return to fixed functionality
If a program is in use, and it is linked again, it will automatically be placed in use again, so in this case you don’t need to call this function again. If the parameter is zero then the fixed functionality is activated.
Prev: Creating a Shader | Next: Setup Example |
2 Responses to “Creating a Program”
Leave a Reply Cancel reply
This site uses Akismet to reduce spam. Learn how your comment data is processed.
I don’t know if I missed it but I’m curious what the difference between the regular and arb extension. Also what would the benefits of one be over the other.
ARB is pre-standard, considered legacy now.
the “regular” is standard OpenGL 2.0 and above.
The difference is in the syntax and also in some minor behavioral changes/fixes. ARB may be supported on even the most ancient graphic cards, but every single one that is now on the market supports OpenGL 2.0 anyway.
ARB is no longer maintained and the performance may fall with newer and newer drivers.