Carna
Version 3.0.1
|
Specifies the shader and it's configuration that are to be used for rendering a Geometry node with a MeshRenderingStage. Custom implementations could also tweak the render state. More...
#include <Material.h>
Classes | |
class | ManagedInterface |
Represents an acquisition of video resources from a particular Material. This realizes the RAII idiom. More... | |
Public Member Functions | |
void | addParameter (ShaderUniformBase *uniform) |
Records uniform as shader parameter. This will be uploaded to the shader when the material is activated. More... | |
template<typename ParameterType > | |
void | setParameter (const std::string &name, const ParameterType &value) |
void | clearParameters () |
Removes all previously set parameters. | |
void | removeParameter (const std::string &name) |
Removes the parameter named name if it exists. | |
bool | hasParameter (const std::string &name) const |
Tells whether a paramter named name exists. | |
const ShaderUniformBase & | parameter (const std::string &name) const |
References the paramter named name. More... | |
virtual bool | controlsSameVideoResource (const GeometryFeature &other) const override |
Tells whether this instance maintains the same video resources like other. More... | |
virtual ManagedInterface * | acquireVideoResource () override |
Acquires the video resources from this GeometryFeature by returning new instance of a class derived from ManagedInterface, that realizes the RAII idiom. Refer to its documentation for details. May return nullptr if this GeometryFeature does not provide any video resources. | |
![]() | |
unsigned int | videoResourceAcquisitionsCount () const |
Tells current number of video resource acquisitions. | |
void | release () |
Denotes that this object is no longer required and may be deleted as soon as it is valid to delete it. More... | |
void | addTo (Geometry &sceneGraphNode, unsigned int role) |
Puts this geometry feature on the sceneGraphNode. More... | |
void | removeFrom (Geometry &sceneGraphNode) |
Removes this geometry feature from the sceneGraphNode. More... | |
Static Public Member Functions | |
static Material & | create (const std::string &shaderName) |
Instantiates. Call release when you do not need the object any longer. | |
Public Attributes | |
const std::string | shaderName |
Identifies this material's shader. | |
Protected Member Functions | |
Material (const std::string &shaderName) | |
Instantiates. | |
virtual | ~Material () |
Deletes. | |
![]() | |
GeometryFeature () | |
Instantiates. | |
virtual | ~GeometryFeature () |
Deletes and logs an error if video resources are leaked. | |
Friends | |
class | GeometryFeature |
Specifies the shader and it's configuration that are to be used for rendering a Geometry node with a MeshRenderingStage. Custom implementations could also tweak the render state.
The core part of a material is the shader, which requires a basic understanding of the rendering pipeline. A very good explanation can be found here: http://www.lighthouse3d.com/tutorials/glsl-core-tutorial/pipeline33/ Nevertheless, lets pass through the most important aspects of the pipeline. The pipeline consists of stages, that are distinguished by whether they are programmable or not. Programmable stages are called shaders.
Note that the depth test might be performed within stage 4 already, if stage 5 does not alter the precomputed fragment depth. This is usually faster, because less fragments need to be processed by stage 5.
As already mentioned in the section above, the core part of a material is its shader. A shader, actually the shader program, consists of a vertex shader and a fragment shader at least. It may also have a fragment shader, which is optional. All three of these are explained in the section above.
So the first step in creating a new material most likely will be the implementation of a vertex and a fragment shader.
A typical vertex shader looks like this:
Line 1 specifies the GLSL version that the compiler should use to build the code. Line 2 declares a variable, whose value will be set from code that runs on the CPU. The keyword uniform
reflects the fact the value will be the same for each processed vertex. Line 3 declares the vertex format. The main
function is invoked for each processed vertex. The example code above assumes that the vertices are stored in model space within the vertex buffer and transforms them to clipping coordinates.
Following uniform variables will be set by the Material
class automatically, if declared:
modelView
takes the model-view transform. This is the concatenation of the view transform and the world transform. It maps from model space to view space.projection
takes the projection matrix that maps from view space to clipping coordinates.modelViewProjection
is the concatenation of projection
and modelView
. It maps from model space to clipping coordinates.Lets take a look at an exemplary fragment shader:
Line 1 declares the GLSL version, just like it was did for vertex shaders. Vertex and fragment shaders, that are going to be used together as a single shader program must also use the same GLSL version. Line 2 declares a variable, whose value will be set when the shader is triggered. Note that the keyword uniform
reflects the same intuitive meaning as it did in the vertex shader example above. Line 3 declares a variable, that the fragment shader will write its output to. You won't need a different declaration than this, unless you are going to render to multiple color buffers at once, which is an advanced technique. Just like for vertex shaders, the main
function of the fragment shader will be executed once for each fragment. The shader code above colors the whole rendered object in a uniform color.
The Material
class acquires its shader objects from the ShaderManager, which ensures that each shader is built just once and not each time someone requests it. So everything one has to do is to ensure that the ShaderManager
will be able to locate the shader sources:
This needs to be done before the material first tries to acquire the shader, i.e. before the geometry node, the material is attached to, is rendered.
The suffixes .vert
and .frag
are important. You will be able to reference the shader from a Material
object by setting its shaderName
to myShader
.
Definition at line 174 of file Material.h.
void Carna::base::Material::addParameter | ( | ShaderUniformBase * | uniform | ) |
|
overridevirtual |
Tells whether this instance maintains the same video resources like other.
This implementation checks whether other also is a Material
and if yes, whether its shaderName is the same.
Implements Carna::base::GeometryFeature.
const ShaderUniformBase& Carna::base::Material::parameter | ( | const std::string & | name | ) | const |
References the paramter named name.
hasParameter(name) == true
void Carna::base::Material::setParameter | ( | const std::string & | name, |
const ParameterType & | 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 306 of file Material.h.
Documentation generated by Doxygen