• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

KGLLib

KGLLib::Program

KGLLib::Program Class Reference

#include <program.h>

List of all members.


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

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.

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.

Definition at line 36 of file program.cpp.

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.

Definition at line 43 of file program.cpp.

KGLLib::Program::~Program (  )  [virtual]

Deletes this program and frees all resources.

Definition at line 58 of file program.cpp.


Member Function Documentation

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

Adds given shader to this program.

Definition at line 75 of file program.cpp.

void KGLLib::Program::addShaders ( const QList< Shader * > &  shaders  ) 

Adds all shaders in the given list to this program.

Definition at line 85 of file program.cpp.

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]

Returns:
OpenGL id (aka handle) of this program.

Definition at line 235 of file program.h.

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]

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

Definition at line 170 of file program.h.

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]

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?

Definition at line 178 of file program.h.

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]

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

Definition at line 123 of file program.cpp.

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]

Definition at line 245 of file program.h.

GLuint KGLLib::Program::mGLId [protected]

Definition at line 241 of file program.h.

char* KGLLib::Program::mLinkLog [protected]

Definition at line 243 of file program.h.

QHash<QString, int>* KGLLib::Program::mUniformLocations [protected]

Definition at line 244 of file program.h.

bool KGLLib::Program::mValid [protected]

Definition at line 242 of file program.h.


The documentation for this class was generated from the following files:
  • program.h
  • program.cpp

KGLLib

Skip menu "KGLLib"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

API Reference

Skip menu "API Reference"
  • KGLLib
Generated for API Reference by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal