Class GLShader¶
Defined in File glutil.h
Page Contents
Class Documentation¶
-
class
GLShader
¶ Helper class for compiling and linking OpenGL shaders and uploading associated vertex and index buffers from Eigen matrices.
Public Functions
-
GLShader
()¶ Create an unitialized OpenGL shader.
-
bool
init
(const std::string &name, const std::string &vertex_str, const std::string &fragment_str, const std::string &geometry_str = "")¶ Initialize the shader using the specified source strings.
- Parameters
name
: The name this shader will be registered as.vertex_str
: The source of the vertex shader as a string.fragment_str
: The source of the fragment shader as a string.geometry_str
: The source of the geometry shader as a string. The default value is the empty string, which indicates no geometry shader will be used.
-
bool
initFromFiles
(const std::string &name, const std::string &vertex_fname, const std::string &fragment_fname, const std::string &geometry_fname = "")¶ Initialize the shader using the specified files on disk.
- Parameters
name
: The name this shader will be registered as.vertex_fname
: The path to the file containing the source of the fragment shader.fragment_fname
: The path to the file containing the source of the vertex shader.geometry_fname
: The path to the file containing the source of the geometry shader. The default value is the empty string, which indicates no geometry shader will be used.
-
const std::string &
name
() const¶ Return the name of the shader.
-
void
define
(const std::string &key, const std::string &value)¶ Set a preprocessor definition. Custom preprocessor definitions must be added before initializing the shader (e.g., via initFromFiles). See also: mDefinitions.
-
void
bind
()¶ Select this shader for subsequent draw calls. Simply executes
glUseProgram
with mProgramShader, andglBindVertexArray
with mVertexArrayObject.
-
void
free
()¶ Release underlying OpenGL objects.
-
GLint
attrib
(const std::string &name, bool warn = true) const¶ Return the handle of a named shader attribute (-1 if it does not exist)
-
GLint
uniform
(const std::string &name, bool warn = true) const¶ Return the handle of a uniform attribute (-1 if it does not exist)
-
template<typename
Matrix
>
voiduploadAttrib
(const std::string &name, const Matrix &M, int version = -1)¶ Upload an Eigen matrix as a vertex buffer object (refreshing it as needed)
-
template<typename
Matrix
>
voiddownloadAttrib
(const std::string &name, Matrix &M)¶ Download a vertex buffer object into an Eigen matrix.
-
template<typename
Matrix
>
voiduploadIndices
(const Matrix &M, int version = -1)¶ Upload an index buffer.
-
void
invalidateAttribs
()¶ Invalidate the version numbers associated with attribute data.
-
void
freeAttrib
(const std::string &name)¶ Completely free an existing attribute buffer.
-
bool
hasAttrib
(const std::string &name) const¶ Check if an attribute was registered a given name.
Create a symbolic link to an attribute of another GLShader. This avoids duplicating unnecessary data.
-
int
attribVersion
(const std::string &name) const¶ Return the version number of a given attribute.
-
void
resetAttribVersion
(const std::string &name)¶ Reset the version number of a given attribute.
-
void
drawArray
(int type, uint32_t offset, uint32_t count)¶ Draw a sequence of primitives.
-
void
drawIndexed
(int type, uint32_t offset, uint32_t count)¶ Draw a sequence of primitives using a previously uploaded index buffer.
-
template<typename
T
>
voidsetUniform
(const std::string &name, const Eigen::Matrix<T, 4, 4> &mat, bool warn = true)¶ Initialize a uniform parameter with a 4x4 matrix (float)
-
template<typename
T
>
voidsetUniform
(const std::string &name, const Eigen::Transform<T, 3, 3> &affine, bool warn = true)¶ Initialize a uniform parameter with a 3x3 affine transform (float)
-
template<typename
T
>
voidsetUniform
(const std::string &name, const Eigen::Matrix<T, 3, 3> &mat, bool warn = true)¶ Initialize a uniform parameter with a 3x3 matrix (float)
-
template<typename
T
>
voidsetUniform
(const std::string &name, const Eigen::Transform<T, 2, 2> &affine, bool warn = true)¶ Initialize a uniform parameter with a 2x2 affine transform (float)
-
void
setUniform
(const std::string &name, bool value, bool warn = true)¶ Initialize a uniform parameter with a boolean value.
-
template<typename T, typename std::enable_if< detail::type_traits< T >::integral==1, int >::type = 0>void nanogui::GLShader::setUniform(const std::string & name, T value, bool warn = true)
Initialize a uniform parameter with an integer value.
-
template<typename T, typename std::enable_if< detail::type_traits< T >::integral==0, int >::type = 0>void nanogui::GLShader::setUniform(const std::string & name, T value, bool warn = true)
Initialize a uniform parameter with a floating point value.
-
template<typename T, typename std::enable_if< detail::type_traits< T >::integral==1, int >::type = 0>void nanogui::GLShader::setUniform(const std::string & name, const Eigen::Matrix< T, 2, 1 > & v, bool warn = true)
Initialize a uniform parameter with a 2D vector (int)
-
template<typename T, typename std::enable_if< detail::type_traits< T >::integral==0, int >::type = 0>void nanogui::GLShader::setUniform(const std::string & name, const Eigen::Matrix< T, 2, 1 > & v, bool warn = true)
Initialize a uniform parameter with a 2D vector (float)
-
template<typename T, typename std::enable_if< detail::type_traits< T >::integral==1, int >::type = 0>void nanogui::GLShader::setUniform(const std::string & name, const Eigen::Matrix< T, 3, 1 > & v, bool warn = true)
Initialize a uniform parameter with a 3D vector (int)
-
template<typename T, typename std::enable_if< detail::type_traits< T >::integral==0, int >::type = 0>void nanogui::GLShader::setUniform(const std::string & name, const Eigen::Matrix< T, 3, 1 > & v, bool warn = true)
Initialize a uniform parameter with a 3D vector (float)
-
template<typename T, typename std::enable_if< detail::type_traits< T >::integral==1, int >::type = 0>void nanogui::GLShader::setUniform(const std::string & name, const Eigen::Matrix< T, 4, 1 > & v, bool warn = true)
Initialize a uniform parameter with a 4D vector (int)
-
template<typename T, typename std::enable_if< detail::type_traits< T >::integral==0, int >::type = 0>void nanogui::GLShader::setUniform(const std::string & name, const Eigen::Matrix< T, 4, 1 > & v, bool warn = true)
Initialize a uniform parameter with a 4D vector (float)
-
void
setUniform
(const std::string &name, const GLUniformBuffer &buf, bool warn = true)¶ Initialize a uniform buffer with a uniform buffer object.
-
size_t
bufferSize
() const¶ Return the size of all registered buffers in bytes.
-
const Buffer &
attribBuffer
(const std::string &name)¶ (Advanced) Returns a reference to the specified GLShader::Buffer.
Danger
Extreme caution must be exercised when using this method. The user is discouraged from explicitly storing the reference returned, as it can change, become deprecated, or no longer reside in
mBufferObjects
.There are generally very few use cases that justify using this method directly. For example, if you need the version of a buffer, call
attribVersion
. If you want to share data betweenGLShader
objects, callshareAttrib
.One example use case for this method is sharing data between different GPU pipelines such as CUDA or OpenCL. When sharing data, you typically need to map pointers between the API’s. The returned buffer’s
Buffer::id
is theGLuint
you will want to map to the other API.In short, only use this method if you absolutely need to.
- Return
A reference to the current buffer associated with
name
. Should not be explicitly stored.- Parameters
name
: The name of the desired attribute.
- Exceptions
std::runtime_error
: Ifname
is not found.
-
void
uploadAttrib
(const std::string &name, size_t size, int dim, uint32_t compSize, GLuint glType, bool integral, const void *data, int version = -1)¶
-
void
downloadAttrib
(const std::string &name, size_t size, int dim, uint32_t compSize, GLuint glType, void *data)¶
Protected Attributes
-
GLuint
mGeometryShader
¶ The geometry shader (if requested) of this GLShader (as returned by
glCreateShader
).
-
GLuint
mProgramShader
¶ The OpenGL program (as returned by
glCreateProgram
).
-
GLuint
mVertexArrayObject
¶ The vertex array associated with this GLShader (as returned by
glGenVertexArrays
).
-
std::map<std::string, Buffer>
mBufferObjects
¶ The map of string names to buffer objects representing the various attributes that have been uploaded using uploadAttrib.
-
std::map<std::string, std::string>
mDefinitions
¶ The map of preprocessor names to values (if any have been created). If a definition was added seeking to create
#define WIDTH 256
, the key would be"WIDTH"
and the value would be"256"
. These definitions will be included automatically in the string that gets compiled for the vertex, geometry, and fragment shader code.
-
struct
Buffer
¶ A wrapper struct for maintaining various aspects of items being managed by OpenGL. Buffers are created when GLShader::uploadAttrib is called.
Public Members
-
GLuint
id
¶ The identifier used with OpenGL.
-
GLuint
glType
¶ The OpenGL type of this buffer.
-
GLuint
dim
¶ The dimension of this buffer (typically the row width).
-
GLuint
compSize
¶ The size (in bytes) of an individual element in this buffer.
-
GLuint
size
¶ The total number of elements represented by this buffer.
-
int
version
¶ The current version if this buffer.
-
GLuint
-