KGLLib
Public Member Functions | Protected Member Functions | Protected Attributes
KGLLib::Program Class Reference

Program class. More...

#include <program.h>

List of all members.

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

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
See also:
Shader, Mesh

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.

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().


Member Function Documentation

void KGLLib::Program::addShader ( Shader shader)

Adds given shader to this program.

References glId().

Referenced by addShaders(), and Program().

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.

See also:
unbind()

References KGLLib::renderer.

Referenced by KGLLib::Mesh::bind().

GLuint KGLLib::Program::glId ( ) const [inline]
Returns:
OpenGL id (aka handle) of this program.

Referenced by addShader(), link(), and ~Program().

bool KGLLib::Program::isValid ( ) const [inline]

Returns true if this program can be used for rendering, false otherwise.

Invalid programs can be result of syntax errors in the shader code. Program which hasn't been linked yet is also invalid.

See also:
link(), linkLog()
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.

References glId().

Referenced by Program().

char* KGLLib::Program::linkLog ( ) const [inline]
Returns:
Link log of the program or null if there was none or the program hasn't been linked yet. Note that Program keeps ownership of the returned string, so you mustn't delete it. TODO: maybe return QString?
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.

See also:
bind()
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.

See also:
bind()

References KGLLib::renderer.

Referenced by KGLLib::Mesh::unbind().


The documentation for this class was generated from the following files: