![]() |
Stellarium 0.12.4
|
GLSL shader program. More...
#include <StelGLSLShader.hpp>
Public Member Functions | |
virtual | ~StelGLSLShader () |
Destroy the shader program. More... | |
virtual bool | addVertexShader (const QString &source)=0 |
Add a vertex shader from source, compiling it in the process. More... | |
virtual bool | addFragmentShader (const QString &source)=0 |
Add a fragment shader from source, compiling it in the process. More... | |
virtual bool | addVertexShader (const QString &name, const QString &source)=0 |
Add a named (optional) vertex shader. More... | |
virtual bool | hasVertexShader (const QString &name) const =0 |
Has a named vertex shader with specified name been added? More... | |
virtual void | enableVertexShader (const QString &name)=0 |
Enable previously added named vertex shader. More... | |
virtual void | disableVertexShader (const QString &name)=0 |
Disable previously added named vertex shader. More... | |
virtual bool | build ()=0 |
Build the shader program. More... | |
virtual void | unlock ()=0 |
Unlock the shader program for modifications. More... | |
virtual QString | log () const =0 |
Return a string containing the error log of the shader. More... | |
virtual void | bind ()=0 |
Bind the shader, using it for following draw calls. More... | |
virtual void | release ()=0 |
Release a bound shader after use. More... | |
template<class T > | |
void | setUniformValue (const char *const name, T value) |
Set value of a uniform shader variable. More... | |
virtual void | useUnprojectedPositionAttribute ()=0 |
Does this shader need the unprojected position attribute? More... | |
Protected Member Functions | |
template<class T > | |
void | setUniformValue_ (const char *const name, T value) |
"Default" overload of setUniformValue() implementation. More... | |
virtual void | setUniformValue_ (const char *const name, const bool value)=0 |
setUniformValue() implementation for type bool. More... | |
virtual void | setUniformValue_ (const char *const name, const int value)=0 |
setUniformValue() implementation for type int. More... | |
virtual void | setUniformValue_ (const char *const name, const float value)=0 |
setUniformValue() implementation for type float. More... | |
virtual void | setUniformValue_ (const char *const name, const Vec2f value)=0 |
setUniformValue() implementation for type Vec2f. More... | |
virtual void | setUniformValue_ (const char *const name, const Vec3f &value)=0 |
setUniformValue() implementation for type Vec3f. More... | |
virtual void | setUniformValue_ (const char *const name, const Vec4f &value)=0 |
setUniformValue() implementation for type Vec4f. More... | |
virtual void | setUniformValue_ (const char *const name, const Mat4f &value)=0 |
setUniformValue() implementation for type Mat4f. More... | |
GLSL shader program.
Can be used with Renderer backends that support GLSL, such as the StelQGL2Renderer.
Shader programs are created by StelRenderer. After creation, sources of used vertex and fragment shaders have to be added, and then the shader must be built using the build() member function. After that, the shader can be bound to be used for drawing. It can also be modified by calling the unlock() member function and adding more shaders, and even by disabling or reenabling them. Disabling/reenabling is only supported for vertex shaders, but fragment shader functions can be added when needed.
There is currently no geometry shader support, but it can be added when needed.
Uniform variables are set through the setUniformValue() member function. Only most commonly used data types are supported, but it is easy to add support for more types if needed. Some uniform variables are specified internally and need to be declared in the shader:
Vertex attributes can't be set through the API - they are set internally from vertex buffer during drawing.
Any shader that uses StelProjector projection (that is, any shader except of those working with 2D data) must declare and call the project() function to project the vertex position. If vertex projection is done on the CPU, this is a dummy function that simply returns its argument. If it's done on the GPU, it's done in this function.
The project() function must be declared (but not defined), as follows:
Usage of the project() function:
Attribute variable names match vertex attribute interpretations as follows:
If useUnprojectedPositionAttribute() is called, another attribute is added:
Shader program building can fail. After a failure, the shader is invalid and should not be used (should be destroyed).
Shader creation example:
Shader usage example:
Definition at line 145 of file StelGLSLShader.hpp.
|
inlinevirtual |
Destroy the shader program.
StelGLSLShader must be destroyed by the user before its Renderer is destroyed.
Definition at line 151 of file StelGLSLShader.hpp.
|
pure virtual |
Add a fragment shader from source, compiling it in the process.
This operation can fail. In case of failure, use log() to find out the cause.
The shader must be unlocked when this is called.
source | Source code of the shader. |
Implemented in StelQGLGLSLShader.
|
pure virtual |
Add a vertex shader from source, compiling it in the process.
This operation can fail. In case of failure, use log() to find out the cause.
The shader must be unlocked when this is called.
source | Source code of the shader. |
Implemented in StelQGLGLSLShader.
|
pure virtual |
Add a named (optional) vertex shader.
Named vertex shaders can be disabled and reenabled. (Unnamed shaders are always enabled.) This allows to dynamically exchange implementations of functions, making shaders modular. GLSL projection is an example of this.
The shader must be unlocked when this is called.
name | Name of the shader. |
source | Source code of the shader. |
Implemented in StelQGLGLSLShader.
|
pure virtual |
Bind the shader, using it for following draw calls.
This must be called before setting uniforms. The shader must be built when this is called.
Note that the shader must be released after any draw calls using the shader to allow default shaders to return to use.
Implemented in StelQGLGLSLShader.
|
pure virtual |
Build the shader program.
This must be called before the shader can be bound.
This operation can fail. In case of failure, use log() to find out the cause. If build() fails, the shader should be assumed to no longer be usable and be destroyed.
Implemented in StelQGLGLSLShader.
|
pure virtual |
Disable previously added named vertex shader.
The shader must be unlocked when this is called.
name | Name of the shader to disable. |
Implemented in StelQGLGLSLShader.
|
pure virtual |
Enable previously added named vertex shader.
The shader must be unlocked when this is called.
name | Name of the shader to enable. |
Implemented in StelQGLGLSLShader.
|
pure virtual |
Has a named vertex shader with specified name been added?
Implemented in StelQGLGLSLShader.
|
pure virtual |
Return a string containing the error log of the shader.
If the shader is succesfully built, it will contain a success message. It might also contain GLSL code warnings.
Implemented in StelQGLGLSLShader.
|
pure virtual |
Release a bound shader after use.
This must be called after using the shader so that the StelRenderer backend can go back to using default shaders.
It also must be called before bind()-ing another StelGLSLShader.
Implemented in StelQGLGLSLShader.
|
inline |
Set value of a uniform shader variable.
A uniform variable has the same value for each drawn vertex/pixel. This can be used for things such as a transformation matrix, global vertex color and so on.
setUniformValue() supports these data types (GLSL type in parentheses):
bool(bool), int(int), float(float), Vec2f(vec2), Vec3f(vec3), Vec4f(vec4), Mat4f(mat4)
The shader must be bound when this is called.
name | Name of the uniform variable. Must match variable name in at least one of the used vertex/fragment shaders. For efficiency reasons, the name is not guaranteed to be copied within the shader backend. Therefore, the name string must exist until a call to release(). (This is easiest to achieve by simply using string literals.) |
value | Value to set the variable to. |
Definition at line 275 of file StelGLSLShader.hpp.
|
inlineprotected |
"Default" overload of setUniformValue() implementation.
This is called for unsupported uniform types, and results in an error.
Definition at line 295 of file StelGLSLShader.hpp.
|
protectedpure virtual |
setUniformValue() implementation for type bool.
Implemented in StelQGLGLSLShader.
|
protectedpure virtual |
setUniformValue() implementation for type int.
Implemented in StelQGLGLSLShader.
|
protectedpure virtual |
setUniformValue() implementation for type float.
Implemented in StelQGLGLSLShader.
|
protectedpure virtual |
setUniformValue() implementation for type Vec2f.
Implemented in StelQGLGLSLShader.
|
protectedpure virtual |
setUniformValue() implementation for type Vec3f.
Implemented in StelQGLGLSLShader.
|
protectedpure virtual |
setUniformValue() implementation for type Vec4f.
Implemented in StelQGLGLSLShader.
|
protectedpure virtual |
setUniformValue() implementation for type Mat4f.
Implemented in StelQGLGLSLShader.
|
pure virtual |
Unlock the shader program for modifications.
Allows to, add, enable, disable vertex/fragment shaders.
This can be called even if the shader is bound (which is used for last-moment modifications, like GLSL projection in renderer backend), but it must be rebuilt before drawing and releasing.
Implemented in StelQGLGLSLShader.
|
pure virtual |
Does this shader need the unprojected position attribute?
If called, the shader will have to declare another vertex attribute, unprojectedVertex, for vertex position before StelProjector projection. Useful e.g. for lighting.
Implemented in StelQGLGLSLShader.