class methods
- ofShader()
- ~ofShader()
- begin()
- beginTransformFeedback()
- bindAttribute()
- bindDefaults()
- bindUniformBlock()
- dispatchCompute()
- end()
- endTransformFeedback()
- getAttributeLocation()
- getGeometryMaxOutputCount()
- getProgram()
- getShader()
- getShaderSource()
- getUniformBlockBinding()
- getUniformBlockIndex()
- getUniformLocation()
- isLoaded()
- linkProgram()
- load()
- loadCompute()
- operator!=()
- operator=()
- operator==()
- printActiveAttributes()
- printActiveUniformBlocks()
- printActiveUniforms()
- setAttribute1d()
- setAttribute1f()
- setAttribute1fv()
- setAttribute1s()
- setAttribute2d()
- setAttribute2f()
- setAttribute2fv()
- setAttribute2s()
- setAttribute3d()
- setAttribute3f()
- setAttribute3fv()
- setAttribute3s()
- setAttribute4d()
- setAttribute4f()
- setAttribute4fv()
- setAttribute4s()
- setGeometryInputType()
- setGeometryOutputCount()
- setGeometryOutputType()
- setUniform1f()
- setUniform1fv()
- setUniform1i()
- setUniform1iv()
- setUniform2f()
- setUniform2fv()
- setUniform2i()
- setUniform2iv()
- setUniform3f()
- setUniform3fv()
- setUniform3i()
- setUniform3iv()
- setUniform4f()
- setUniform4fv()
- setUniform4i()
- setUniform4iv()
- setUniformMatrix3f()
- setUniformMatrix4f()
- setUniformTexture()
- setUniforms()
- setup()
- setupShaderFromFile()
- setupShaderFromSource()
- unload()
Graphics Language Shading Language (GLSL) can be used in oF by using the ofShader object. Shading happens in two distinct steps: the vertex shader creates values for each vertex in the model, and the fragment shader creates values for each pixel in the rendered object. To define a shader, create a .frag file for the fragment shader and a .vert file for the vertex shader. A vertex shader has attributes about a location in space or vertex, which means not only the actual coordinates of that location but also its color, how any textures should be mapped onto it, and how the vertices are modified in the operation. A vertex shader can change the positions of each vertex, the number of lighting computations per vertex, and the color that will be applied to each vertex. A geometry shader can generate new graphics primitives like points, lines, and triangles, from those primitives that were sent to the graphics card from the CPU. This means that you could get a point and turn it into a triangle or even a bunch of triangles, or get a line and turn it into a rectangle, or do real-time extrusion. They are very powerful and can be quite tricky to get right, but they're becoming more popular. The fragment shader is somewhat misleadingly named because what it really allows you to do is to change values assigned to each pixel. The vertex shader operates on the vertices, and the fragment shader operates on the pixels. By the time the fragment shader gets information passed into it by the graphics card, the color of a particular pixel has already been computed and in the fragment shader can be combined with an element like a lighting effecting, a fog effect, or a blur among many other options. The usual end result of this stage per fragment is a color value and a depth for the fragment.
begin()
void ofShader::begin()
After you call begin() everything that you draw, vertexes and textures, in your of application have the effects of the shader applied to them.
beginTransformFeedback(...)
void ofShader::beginTransformFeedback(GLenum mode, const ofShader::TransformFeedbackBaseBinding &binding)
beginTransformFeedback(...)
void ofShader::beginTransformFeedback(GLenum mode, const ofShader::TransformFeedbackRangeBinding &binding)
beginTransformFeedback(...)
void ofShader::beginTransformFeedback(GLenum mode, const vector< TransformFeedbackBaseBinding > &binding)
beginTransformFeedback(...)
void ofShader::beginTransformFeedback(GLenum mode, const vector< TransformFeedbackRangeBinding > &binding)
bindAttribute(...)
void ofShader::bindAttribute(GLuint location, const string &name)
bindAttribute
is used for faster access to attributes. The idea is to set attributes by location (an integer) instead of querying attribute locations multiple times by name. This method is used internally by OF in ofShader::bindDefaults()
to set the default locations of the position
, color
, normal
and texcoord
attributes.
bindDefaults()
bool ofShader::bindDefaults()
Binds default uniforms and attributes, only useful for fixed pipeline simulation under programmable renderer. Has to be called before linkProgram().
See setupShaderFromSource() example.
end()
void ofShader::end()
After you call end() any drawing, vertexes and textures, do not have the effect of the shader applied to them.
endTransformFeedback(...)
void ofShader::endTransformFeedback(const ofShader::TransformFeedbackBaseBinding &binding)
endTransformFeedback(...)
void ofShader::endTransformFeedback(const ofShader::TransformFeedbackRangeBinding &binding)
endTransformFeedback(...)
void ofShader::endTransformFeedback(const vector< TransformFeedbackBaseBinding > &binding)
endTransformFeedback(...)
void ofShader::endTransformFeedback(const vector< TransformFeedbackRangeBinding > &binding)
getGeometryMaxOutputCount()
int ofShader::getGeometryMaxOutputCount()
returns maximum number of supported vertices for your graphics card
getProgram()
GLuint ofShader::getProgram()
This returns the GLuint for the actual shader object that is active on the graphics card. This is more of an advanced usage method, but can be helpful if you need to do something that the ofShader doesn't support currently. This is all the shaders: vertex, geom, and frag.
getShader(...)
GLuint ofShader::getShader(GLenum type)
This returns the GLuint for the actual shader object that is active on the graphics card. This is more of an advanced usage method, but can be helpful if you need to do something that the ofShader doesn't support currently. This returns only one of the shaders. You can pass GL_VERTEX_SHADER, GL_GEOMETRY_SHADER_EXT, GL_FRAGMENT_SHADER
getShaderSource(...)
string ofShader::getShaderSource(GLenum type)
Documentation from code comments
@brief returns the shader source as it was passed to the GLSL compiler
Parameters:
type (GL_VERTEX_SHADER | GL_FRAGMENT_SHADER | GL_GEOMETRY_SHADER_EXT) the shader source you'd like to inspect.
linkProgram()
bool ofShader::linkProgram()
Links program with all compiled shaders. This is more of an advanced use method, as this is done automatically for you.
load(...)
bool ofShader::load(const filesystem::path &shaderName)
This assumes that your vertex and fragment shaders have the same name, i.e. "dof.vert" and "dof.frag" and loads them using just the name of the shader:
shader.load("dof"); // assumes the shaders are in /data
load(...)
bool ofShader::load(const filesystem::path &vertName, const filesystem::path &fragName, const filesystem::path &geomName)
Here you can load shaders with whatever names you choose. The geometry shader is optional, but the vertex and fragment shaders aren't.
shader.load("dof.vert", "dof.frag"); // assumes the shaders are in /data
printActiveAttributes()
void ofShader::printActiveAttributes()
This prints out all the active attributes to the console.
printActiveUniforms()
void ofShader::printActiveUniforms()
This prints out all the active uniforms to the console.
setAttribute1d(...)
void ofShader::setAttribute1d(GLint location, double v1)
Set one double attribute on the shader. Attributes are different than uniforms in that you can pass an attribute to each vertex or fragment that is being shaded.
setAttribute1f(...)
void ofShader::setAttribute1f(GLint location, float v1)
Set one float attributes on the shader. Attributes are different than uniforms in that you can pass an attribute to each vertex or fragment that is being shaded.
setAttribute1fv(...)
void ofShader::setAttribute1fv(const string &name, const float *v, GLsizei stride)
setAttribute1s(...)
void ofShader::setAttribute1s(GLint location, short v1)
Set a short attribute, a short int, on the shader. Attributes are different than uniforms in that you can pass an attribute to each vertex or fragment that is being shaded.
setAttribute2d(...)
void ofShader::setAttribute2d(GLint location, double v1, double v2)
Set two double attribute on the shader. Attributes are different than uniforms in that you can pass an attribute to each vertex or fragment that is being shaded.
setAttribute2f(...)
void ofShader::setAttribute2f(GLint location, float v1, float v2)
Set two float attributes on the shader. Attributes are different than uniforms in that you can pass an attribute to each vertex or fragment that is being shaded.
setAttribute2fv(...)
void ofShader::setAttribute2fv(const string &name, const float *v, GLsizei stride)
setAttribute2s(...)
void ofShader::setAttribute2s(GLint location, short v1, short v2)
Set two short attributes, a short int, on the shader. Attributes are different than uniforms in that you can pass an attribute to each vertex or fragment that is being shaded.
setAttribute3d(...)
void ofShader::setAttribute3d(GLint location, double v1, double v2, double v3)
Set three double attribute on the shader. Attributes are different than uniforms in that you can pass an attribute to each vertex or fragment that is being shaded.
setAttribute3f(...)
void ofShader::setAttribute3f(GLint location, float v1, float v2, float v3)
Set three float attributes on the shader. Attributes are different than uniforms in that you can pass an attribute to each vertex or fragment that is being shaded.
setAttribute3fv(...)
void ofShader::setAttribute3fv(const string &name, const float *v, GLsizei stride)
setAttribute3s(...)
void ofShader::setAttribute3s(GLint location, short v1, short v2, short v3)
Set three short attributes, a short int, on the shader. Attributes are different than uniforms in that you can pass an attribute to each vertex or fragment that is being shaded.
setAttribute4d(...)
void ofShader::setAttribute4d(GLint location, double v1, double v2, double v3, double v4)
Set four double attribute on the shader. Attributes are different than uniforms in that you can pass an attribute to each vertex or fragment that is being shaded.
setAttribute4f(...)
void ofShader::setAttribute4f(GLint location, float v1, float v2, float v3, float v4)
Set four float attributes on the shader. Attributes are different than uniforms in that you can pass an attribute to each vertex or fragment that is being shaded.
setAttribute4fv(...)
void ofShader::setAttribute4fv(const string &name, const float *v, GLsizei stride)
setAttribute4s(...)
void ofShader::setAttribute4s(GLint location, short v1, short v2, short v3, short v4)
Set four short attributes, a short int, on the shader. Attributes are different than uniforms in that you can pass an attribute to each vertex or fragment that is being shaded.
setGeometryInputType(...)
void ofShader::setGeometryInputType(GLenum type)
You have to call this before linking the program with geometry shaders. Possible types are GL_POINTS, GL_LINES, GL_LINES_ADJACENCY_EXT, GL_TRIANGLES, GL_TRIANGLES_ADJACENCY_EXT
setGeometryOutputCount(...)
void ofShader::setGeometryOutputCount(int count)
You have to call this before linking the program with geometry shaders to set the number of output vertices, For quads, this should be 4, for points 1. Triangle strips can use up to the maximum number. You can check the maximum number supported on your graphics cards with getGeometryMaxOutputCount()
setGeometryOutputType(...)
void ofShader::setGeometryOutputType(GLenum type)
You have to call this before linking the program with geometry shaders. type: GL_POINTS, GL_LINE_STRIP or GL_TRIANGLE_STRIP
setUniform1f(...)
void ofShader::setUniform1f(const string &name, float v1)
set a float uniform on the shader
setUniform2fv(...)
void ofShader::setUniform2fv(const string &name, const float *v, int count=1)
This allows you to set multiple vec2 uniforms.
vec2 v[2];
setUniform2iv(...)
void ofShader::setUniform2iv(const string &name, const int *v, int count=1)
set an array of uniform values on the shader using int[2] value. On the shader this looks like:
ivec2 iv[2];
setUniform3f(...)
void ofShader::setUniform3f(const string &name, float v1, float v2, float v3)
set a vec3 uniform on the shader
setUniform3fv(...)
void ofShader::setUniform3fv(const string &name, const float *v, int count=1)
This allows you to set multiple vec3 uniforms.
vec3 v[2];
setUniform3i(...)
void ofShader::setUniform3i(const string &name, int v1, int v2, int v3)
uniform ivec3 texture;
setUniform3iv(...)
void ofShader::setUniform3iv(const string &name, const int *v, int count=1)
set an array of uniform values on the shader using int[2] value. On the shader this looks like:
ivec3 iv[2];
setUniform4f(...)
void ofShader::setUniform4f(const string &name, float v1, float v2, float v3, float v4)
set a vec4 uniform on the shader
vec4 fv;
setUniform4fv(...)
void ofShader::setUniform4fv(const string &name, const float *v, int count=1)
This allows you to set multiple vec4 uniforms.
vec4 v[2];
setUniform4iv(...)
void ofShader::setUniform4iv(const string &name, const int *v, int count=1)
set an array of uniform values on the shader using int[2] value. On the shader this looks like:
ivec4 iv[2];
setUniformMatrix3f(...)
void ofShader::setUniformMatrix3f(const string &name, const glm::mat3 &m, int count=1)
setUniformMatrix4f(...)
void ofShader::setUniformMatrix4f(const string &name, const glm::mat4 &m, int count=1)
setUniformTexture(...)
void ofShader::setUniformTexture(const string &name, const ofBaseHasTexture &img, int textureLocation)
set a texture reference On your shader it should look like this:
uniform sampler2DRect texture;
setUniformTexture(...)
void ofShader::setUniformTexture(const string &name, const ofTexture &img, int textureLocation)
setUniformTexture(...)
void ofShader::setUniformTexture(const string &name, int textureTarget, GLint textureID, int textureLocation)
setupShaderFromFile(...)
bool ofShader::setupShaderFromFile(GLenum type, const filesystem::path &filename)
This are more of advanced use function and doesn't need.
setupShaderFromSource(...)
bool ofShader::setupShaderFromSource(GLenum type, string source, string sourceDirectoryPath)
This method create and compile a shader from source. Working with OpenGL 3 or OpenGL ES 2.0, remember to call bindDefaults() before linkProgram()
Example of a simple shader to display a texture, using the ofGLProgrammableRenderer:
stringstream vertexSrc;
vertexSrc << "#version 150\n";
vertexSrc << "uniform mat4 modelViewProjectionMatrix;\n";
vertexSrc << "in vec4 position;\n";
vertexSrc << "in vec2 texcoord;\n";
vertexSrc << "out vec2 texCoordVarying;\n";
vertexSrc << "void main(void){\n";
vertexSrc << "\tgl_Position=modelViewProjectionMatrix*position;\n";
vertexSrc << "\ttexCoordVarying = texcoord;\n";
vertexSrc << "}\n";
stringstream fragmentSrc;
fragmentSrc << "#version 150\n";
fragmentSrc << "uniform sampler2DRect tex0;\n";
fragmentSrc << "in vec2 texCoordVarying;\n";
fragmentSrc << "out vec4 outputColor;\n";
fragmentSrc << "uniform vec2 direction;\n";
fragmentSrc << "void main(void) {\n";
fragmentSrc << "\toutputColor = texture(tex0, texCoordVarying);\n";
fragmentSrc << "}\n";
shader.setupShaderFromSource( GL_VERTEX_SHADER, vertexSrc.str() );
shader.setupShaderFromSource( GL_FRAGMENT_SHADER, fragmentSrc.str() );
shader.bindDefaults();
shader.linkProgram();
unload()
void ofShader::unload()
This unload the shader, which means that it will not be active on the graphics card any longer.
Last updated 星期二, 19 十一月 2024 17:25:27 UTC - 2537ee49f6d46d5fe98e408849448314fd1f180e
If you have any doubt about the usage of this module you can ask in the forum.
If you want to contribute better documentation or start documenting this section you can do so here
If you find anything wrong with this docs you can report any error by opening an issue