|
KGLLib
|
#include <program.h>
Public Member Functions | |
| Program () | |
| Program (const QList< Shader * > &shaders) | |
| Program (const QString &vertexshaderfile, const QString &fragmentshaderfile) | |
| virtual | ~Program () |
| void | addShader (Shader *shader) |
| void | addShaders (const QList< Shader * > &shaders) |
| virtual bool | link () |
| bool | isValid () const |
| char * | linkLog () const |
| virtual void | bind () const |
| virtual void | unbind () const |
| int | uniformLocation (const QString &name) |
| int | uniformLocation (const char *name) |
| int | attributeLocation (const QString &name) |
| int | attributeLocation (const char *name) |
| void | invalidateLocations () |
| bool | setUniform (const char *name, float value) |
| bool | setUniform (const char *name, Eigen::Vector2f value) |
| bool | setUniform (const char *name, Eigen::Vector3f value) |
| bool | setUniform (const char *name, Eigen::Vector4f value) |
| bool | setUniform (const char *name, int value) |
| GLuint | glId () const |
Protected Member Functions | |
| void | init () |
Protected Attributes | |
| GLuint | mGLId |
| bool | mValid |
| char * | mLinkLog |
| QHash< QString, int > * | mUniformLocations |
| QHash< QString, int > * | mAttributeLocations |
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.
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 }
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();
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
| KGLLib::Program::Program | ( | ) |
Creates empty Program. You will need to add some shaders and link the program before you can use it.
| KGLLib::Program::Program | ( | const QList< Shader * > & | shaders | ) |
Creates Program, adds given list of shaders and links the program. If linking succeeded, the program is ready to be used.
References addShaders(), and link().
| KGLLib::Program::Program | ( | const QString & | vertexshaderfile, |
| const QString & | fragmentshaderfile | ||
| ) |
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.
References addShader(), and link().
| KGLLib::Program::~Program | ( | ) | [virtual] |
Deletes this program and frees all resources.
References glId().
| void KGLLib::Program::addShader | ( | Shader * | shader | ) |
| void KGLLib::Program::addShaders | ( | const QList< Shader * > & | shaders | ) |
Adds all shaders in the given list to this program.
References addShader().
Referenced by Program().
| void KGLLib::Program::bind | ( | ) | const [virtual] |
Binds the program so that it will be used for anything that is rendered after the bind() call.
References KGLLib::renderer.
Referenced by KGLLib::Mesh::bind().
| GLuint KGLLib::Program::glId | ( | ) | const [inline] |
Referenced by addShader(), link(), and ~Program().
| bool KGLLib::Program::isValid | ( | ) | const [inline] |
| bool KGLLib::Program::link | ( | ) | [virtual] |
| 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.
| 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.
| 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.
| 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.
| 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.
| void KGLLib::Program::unbind | ( | ) | const [virtual] |
Unbind the program. Anything rendered after unbind() call will be rendered using the fixed-function pipeline. Note that if you want to change the currently used program, you needn't call unbind() before bind()ing the next program.
References KGLLib::renderer.
Referenced by KGLLib::Mesh::unbind().
1.7.4