OpenGL::Sandbox::Shader - Wrapper object for OpenGL shader


version 0.120


OpenGL Shaders allow custom code to be loaded onto the graphics hardware and run in parallel and asynchronous to the host application.

Each shader has an ID, and once compiled (or loaded as pre-compiled binaries) they can be attached to Programs and used as a rendering pipeline. This class wraps a single shader ID, providing methods to conveniently load, compile, attach, detach, and destroy the associated shader within OpenGL.

Note that this implementation currently requires at least OpenGL version (TODO), or it will throw an exception as soon as you try to use the shaders.



Human-readable name of this shader (not GL's integer "name")


Path from which shader code will be loaded. If not set, the shader will not load anything automatically.


Optional - supply source code directly rather than loading from "filename".


Type of shader, i.e. GL_FRAGMENT_SHADER, GL_VERTEX_SHADER, ...

If you don't set this before lazy-building "shader_id", it will attempt to guess from the filename, and if it can't guess it will throw an exception.


A method name or coderef of your choice for lazy-loading (and compiling) the code. If not set, the loader is determined from the "filename" and if that is not set, nothing gets loaded on creation of the shader_id.

Gets executed as $shader->$loader($filename).


Boolean; whether the shader is loaded and compiled, via this API. (it won't know about changes you make via your own OpenGL calls)


The OpenGL integer "name" of this shader. This is a lazy-built attribute, and will call glCreateShader the first time you access it. Use has_id to find out whether this has happened yet.


True if the id attribute is defined.




Load shader source code into OpenGL. This does not happen when the object is first constructed, in case the OpenGL context hasn't been initialized yet. It automatically happens when you use a program pipeline that is attached to the shader.

Calls $self->loader->($self, $self->filename). "shader_id" will be a valid shader id after this (assuming the loader doesn't die). The default loader also compiles the shader, and throws an exception if compilation fails.

Returns $self for convenient chaining.


Michael Conrad <>


This software is copyright (c) 2019 by Michael Conrad.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.