abstract class Prism::Shader::Program
- Prism::Shader::Program
- Reference
- Object
Overview
Represents a shader program.
This utilizes a ReferencePool(CompiledProgram)
in order to re-use shader programs.
Orphaned shaders will be garbage collected and their OpenGL resources released.
The key feature of this class is you can bind to uniforms by name. All of the complicated location binding is handled automatically.
Direct Known Subclasses
Defined in:
prism/shader/program.crConstructors
-
.new(file_name : String, &shader_reader : String -> String)
Creates a new shader from file_name.
Class Method Summary
-
.pool
Retrieves the
ReferencePool(CompiledProgram)
pool.
Instance Method Summary
- #finalize
-
#set_uniform(name : String, value : Array(Shader::UniformStruct))
Sets an array of custom uniforms.
-
#set_uniform(name : String, value : Shader::UniformStruct)
Sets a custom uniform.
-
#set_uniform(name : String, value : LibGL::Int)
Sets an integer uniform variable value
-
#set_uniform(name : String, value : LibGL::Float)
Sets a float uniform variable value
-
#set_uniform(name : String, value : Vector3f)
Sets a 3 dimensional float vector value to a uniform variable
-
#set_uniform(name : String, value : Vector2f)
Sets a 2 dimensional float vector value to a uniform variable
-
#set_uniform(name : String, value : Matrix4f)
Sets a 4 dimensional matrix float value to a uniform variable
-
#set_uniform(name : String, value : Bool)
Sets a boolean uniform variable value.
-
#set_uniform(name : String, value : Prism::Texture)
Sets a texture uniform variable value.
-
#start
Binds the program to OpenGL so it can run.
-
#stop
Unbinds the program from OpenGL so it won't run.
Macro Summary
-
uniform(name, type)
Generates a uniform property.
Constructor Detail
Creates a new shader from file_name.
The vertex and fragment shaders will be interpolated from file_name, therefore file_name should be extension-less.
Shader programs may include other files using the #include "file.ext"
statement.
These includes are resolved by shader_reader which receives the name of the included file.
NOTE file_name will be passed to shader_reader as well.
Example
Shader.new "forward-point" do |path|
File.read(File.join("my/shader/directory", path))
# Ends up reading:
# forward-point.vs
# lighting.glh <- embeded into forward-point.vs
# forward-point.fs
end
Class Method Detail
Retrieves the ReferencePool(CompiledProgram)
pool.
The pool allows you to re-use CompiledProgram
s, and will automatically manage references to each CompiledProgram
.
See ReferencePool
for more information.
Instance Method Detail
Sets an array of custom uniforms.
Sets a custom uniform. The value will be serialized and it's generated values stored in the uniform.
Sets a 3 dimensional float vector value to a uniform variable
Sets a 2 dimensional float vector value to a uniform variable
Sets a 4 dimensional matrix float value to a uniform variable
Sets a boolean uniform variable value. Booleans are represented as floats in GLSL
Binds the program to OpenGL so it can run. First we enable the program, then we bind values to all of the uniforms. Finally, we enable all of the attributes.
Macro Detail
Generates a uniform property.
You can define uniforms a few different ways. Below are two examples that bind to the projection_matrix uniform.
uniform projection_matrix, Matrix4f
uniform "projection_matrix", Matrix4f
Now on your Shader::Program
instance you can set the uniform
shader.projection_matrix = myMatrix
You can also use camel case for your uniform names. The generated methods will be underscored, but the uniform itself will remain camel case.
uniform projectionMatrix, Matrix4f
# later on...
shader.projection_matrix = myMatrix
# in the glsl code..
uniform vec4 projectionMatrix