KGLLib
KGLLib::Program Class Reference
#include <program.h>
Detailed Description
Program class.Program is a GPU-executed program that is ready to be used for manipulating geometry and colors. Program can encapsulate vertex and fragment shaders or just one of them. If only either vertex or fragment shader is used, then traditional fixed-function pipeline is used for the other stage.
Creating Program objects
The easiest way to create usable Program object is to pass filenames of the vertex- and fragment shader to the Program constructor. This automatically creates temporary Shader objects and then combines them into a Program:Program* prog = new Program("myshader.vert", "myshader.frag"); if (!prog->isValid()) { // Error: program failed to load }
It is also possible to add more than one fragment or vertex shader (e.g. you can have some common functions in one shader and the main() function in other) or specify only vertex or only fragment shader. In this case you will have to first create the Shader objects yourself and then add them to a program:
// Create Shader objects VertexShader vertexShader("myshader.vert"); FragmentShader fragmentShader("myshader.frag"); // common.frag could contain common functions shared by different shaders FragmentShader commonShader("common.frag"); // Make sure all shaders were successfully loaded if (!vertexShader.isValid() || !fragmentShader.isValid() || !commonShader.isValid()) { // handle the error here return; } // Create Program object Program* prog = new Program(); // Add shader objects to program prog->addShader(&vertexShader); prog->addShader(&fragmentShader); prog->addShader(&commonShader); // Finally link them together prog->link(); // And make sure everything succeeded if (!prog->isValid()) { // handle the error }
Binding
To use the program, you need to first bind() it, then render your geometry and finally unbind() it. Note that it's more convenient to use the Mesh class which takes care of the binding and unbinding automatically.prog->bind(); // Everything rendered now will use the bound program renderObjects(); // Finally unbind the program prog->unbind();
Uniform variables
For communication between GPU Program and your main application, uniform variables can be used. They can be written from your main program and read in shader code. Note that modifying values of uniform variables may be expensive, thus you should do it only when really necessary.// The program has to be bound before setUniform() can be used prog->bind(); // Set variable "objectScale" to 2.0f prog->setUniform("objectScale", 2.0f); // Unbind the program prog->unbind();
The corresponding section in the shader code would look like this:
uniform float objectScale; // objectScale can now be used as any other variable
Definition at line 120 of file program.h.
Public Member Functions | |
| void | addShader (Shader *shader) |
| void | addShaders (const QList< Shader * > &shaders) |
| int | attributeLocation (const char *name) |
| int | attributeLocation (const QString &name) |
| virtual void | bind () const |
| GLuint | glId () const |
| void | invalidateLocations () |
| bool | isValid () const |
| virtual bool | link () |
| char * | linkLog () const |
| Program (const QString &vertexshaderfile, const QString &fragmentshaderfile) | |
| Program (const QList< Shader * > &shaders) | |
| Program () | |
| bool | setUniform (const char *name, int value) |
| bool | setUniform (const char *name, Eigen::Vector4f value) |
| bool | setUniform (const char *name, Eigen::Vector3f value) |
| bool | setUniform (const char *name, Eigen::Vector2f value) |
| bool | setUniform (const char *name, float value) |
| virtual void | unbind () const |
| int | uniformLocation (const char *name) |
| int | uniformLocation (const QString &name) |
| virtual | ~Program () |
Protected Member Functions | |
| void | init () |
Protected Attributes | |
| QHash< QString, int > * | mAttributeLocations |
| GLuint | mGLId |
| char * | mLinkLog |
| QHash< QString, int > * | mUniformLocations |
| bool | mValid |
Constructor & Destructor Documentation
| KGLLib::Program::Program | ( | ) |
Creates empty Program.
You will need to add some shaders and link the program before you can use it.
Definition at line 31 of file program.cpp.
Creates Program, adds given list of shaders and links the program.
If linking succeeded, the program is ready to be used.
Definition at line 36 of file program.cpp.
Loads vertex and fragment shaders from given files, adds them and links the program.
If everything succeeded, then the program is ready to be used.
Definition at line 43 of file program.cpp.
| KGLLib::Program::~Program | ( | ) | [virtual] |
Member Function Documentation
| void KGLLib::Program::addShader | ( | Shader * | shader | ) |
| int KGLLib::Program::attributeLocation | ( | const char * | name | ) |
Definition at line 150 of file program.cpp.
| int KGLLib::Program::attributeLocation | ( | const QString & | name | ) |
Definition at line 145 of file program.cpp.
| void KGLLib::Program::bind | ( | ) | const [virtual] |
Binds the program so that it will be used for anything that is rendered after the bind() call.
- See also:
- unbind()
Definition at line 118 of file program.cpp.
| GLuint KGLLib::Program::glId | ( | ) | const [inline] |
| void KGLLib::Program::init | ( | ) | [protected] |
Definition at line 66 of file program.cpp.
| void KGLLib::Program::invalidateLocations | ( | ) |
Definition at line 162 of file program.cpp.
| bool KGLLib::Program::isValid | ( | ) | const [inline] |
| bool KGLLib::Program::link | ( | ) | [virtual] |
Tries to link the shader.
If it succeeds, then the program is ready to be used. If there are errors, they should be visible in the linkLog.
- Returns:
- whether linking succeeded.
Definition at line 92 of file program.cpp.
| char* KGLLib::Program::linkLog | ( | ) | const [inline] |
| bool KGLLib::Program::setUniform | ( | const char * | name, | |
| int | value | |||
| ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Definition at line 204 of file program.cpp.
| bool KGLLib::Program::setUniform | ( | const char * | name, | |
| Eigen::Vector4f | value | |||
| ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Definition at line 195 of file program.cpp.
| bool KGLLib::Program::setUniform | ( | const char * | name, | |
| Eigen::Vector3f | value | |||
| ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Definition at line 186 of file program.cpp.
| bool KGLLib::Program::setUniform | ( | const char * | name, | |
| Eigen::Vector2f | value | |||
| ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Definition at line 177 of file program.cpp.
| bool KGLLib::Program::setUniform | ( | const char * | name, | |
| float | value | |||
| ) |
Sets the uniform with the given name to the given value and returns true.
If there is no uniform with such name then false is returned.
Note that the program has to be bound before this method can be used.
- See also:
- bind()
Definition at line 168 of file program.cpp.
| void KGLLib::Program::unbind | ( | ) | const [virtual] |
| int KGLLib::Program::uniformLocation | ( | const char * | name | ) |
Definition at line 133 of file program.cpp.
| int KGLLib::Program::uniformLocation | ( | const QString & | name | ) |
Definition at line 128 of file program.cpp.
Member Data Documentation
QHash<QString, int>* KGLLib::Program::mAttributeLocations [protected] |
GLuint KGLLib::Program::mGLId [protected] |
char* KGLLib::Program::mLinkLog [protected] |
QHash<QString, int>* KGLLib::Program::mUniformLocations [protected] |
bool KGLLib::Program::mValid [protected] |
The documentation for this class was generated from the following files:
KDE 4.2 API Reference