Struct cgl_rs::graphics::Shader

source ·
pub struct Shader { /* private fields */ }
Expand description

A shader object

Implementations§

source§

impl Shader

source

pub fn from_source( vertex_shader_source: &str, fragment_shader_source: &str ) -> Result<Shader, String>

Creates a new shader object from the provided vertex and fragment shader source code.

Arguments
  • vertex_shader_source - The source code for the vertex shader.
  • fragment_shader_source - The source code for the fragment shader.
Returns

Returns a Result containing the newly created Shader object if successful, or a String containing an error message if unsuccessful.

Example
let vertex_shader_source = r#"#version 430 core

    layout (location = 0) in vec3 aPos;

    void main()
    {
        gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
    }
"#;

let fragment_shader_source = r#"#version 430 core

    out vec4 FragColor;

    void main()
    {
        FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);
    }
"#;

cgl_rs::init();
let mut window = cgl_rs::Window::new("Hello World", 800, 600).unwrap();
cgl_rs::graphics::init();
{
   let shader = cgl_rs::graphics::Shader::from_source(vertex_shader_source, fragment_shader_source).unwrap();
   // Do stuff with the shader
}
cgl_rs::graphics::shutdown();
window.destroy();
cgl_rs::shutdown();
source

pub fn from_files( vertex_shader_path: &str, fragment_shader_path: &str ) -> Result<Shader, String>

Creates a new shader object from the provided vertex and fragment shader source files.

Arguments
  • vertex_shader_path - The path to the file containing the source code for the vertex shader.
  • fragment_shader_path - The path to the file containing the source code for the fragment shader.
Returns

Returns a Result containing the newly created Shader object if successful, or a String containing an error message if unsuccessful.

Example
cgl_rs::init();
let mut window = cgl_rs::Window::new("Hello World", 800, 600).unwrap();
cgl_rs::graphics::init();
{
   let shader = cgl_rs::graphics::Shader::from_files("path/to/vertex_shader.glsl", "path/to/fragment_shader.glsl").unwrap();
   // Do stuff with the shader
}
cgl_rs::graphics::shutdown();
window.destroy();
cgl_rs::shutdown();
source

pub fn compute_from_source( compute_shader_source: &str ) -> Result<Shader, String>

Creates a new compute shader object from the provided compute shader source code.

Arguments
  • compute_shader_source - The source code for the compute shader.
Returns

Returns a Result containing the newly created Shader object if successful, or a String containing an error message if unsuccessful.

Example
let compute_shader_source = r#"#version 430 core
 
   layout (local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
 
   void main()
   {
      // Do stuff
   }
"#;
 
 
cgl_rs::init();
let mut window = cgl_rs::Window::new("Hello World", 800, 600).unwrap();
cgl_rs::graphics::init();
{
   let shader = cgl_rs::graphics::Shader::compute_from_source("path/to/compute_shader.glsl").unwrap();
   // Do stuff with the shader
}
cgl_rs::graphics::shutdown();
window.destroy();
cgl_rs::shutdown();
source

pub fn compute_from_files(compute_shader_path: &str) -> Result<Shader, String>

Creates a new compute shader object from the provided compute shader file.

Arguments
  • compute_shader_path - The path to the compute shader file.
Returns

Returns a Result containing the newly created Shader object if successful, or a String containing an error message if unsuccessful.

Example
cgl_rs::init();
let mut window = cgl_rs::Window::new("Hello World", 800, 600).unwrap();
cgl_rs::graphics::init();
{
   let shader = cgl_rs::graphics::Shader::compute_from_files("path/to/compute_shader.glsl").unwrap();
   // Do stuff with the shader
}
cgl_rs::graphics::shutdown();
window.destroy();
cgl_rs::shutdown();
source

pub fn create_dummy_compute() -> Result<Shader, String>

Creates a basic compute shader object with a simple compute shader.

Returns

Returns a Result containing the newly created Shader object if successful, or a String containing an error message if unsuccessful.

Example
cgl_rs::init();
let mut window = cgl_rs::Window::new("Hello World", 800, 600).unwrap();
cgl_rs::graphics::init();
{
   let shader = cgl_rs::graphics::Shader::create_dummy_compute().unwrap();
   // Do stuff with the shader
}
cgl_rs::graphics::shutdown();
window.destroy();
cgl_rs::shutdown();
source

pub fn create_dummy() -> Result<Shader, String>

Creates a basic shader object with a simple vertex and fragment shader.

Returns

Returns a Result containing the newly created Shader object if successful, or a String containing an error message if unsuccessful.

Example
cgl_rs::init();
let mut window = cgl_rs::Window::new("Hello World", 800, 600).unwrap();
cgl_rs::graphics::init();
{
   let shader = cgl_rs::graphics::Shader::create_dummy().unwrap();
   // Do stuff with the shader
}
cgl_rs::graphics::shutdown();
window.destroy();
cgl_rs::shutdown();
source

pub fn destroy(&mut self)

Destroys the shader object.

Remarks

This function destroys the shader object and frees any resources associated with it. If the shader object has already been destroyed, this function does nothing.

Example
cgl_rs::init();
let mut window = cgl_rs::Window::new("Hello World", 800, 600).unwrap();
cgl_rs::graphics::init();
{
   let mut shader = cgl_rs::graphics::Shader::create_dummy().unwrap();
   // Do stuff with the shader
   shader.destroy();
}
cgl_rs::graphics::shutdown();
window.destroy();
cgl_rs::shutdown();
source

pub fn get_uniform_location(&mut self, uniform_name: &str) -> i32

Retrieves the location of0 a uniform variable in the shader program.

Arguments
  • uniform_name - A string slice containing the name of the uniform variable.
Returns

Returns an i32 representing the location of the uniform variable in the shader program.

Remarks

This function caches the uniform location for future use, so it is more efficient to call this function once and store the result than to call it repeatedly.

Example
cgl_rs::init();
let mut window = cgl_rs::Window::new("Hello World", 800, 600).unwrap();
cgl_rs::graphics::init();
{
    let mut shader = cgl_rs::graphics::Shader::create_dummy().unwrap();
    let uniform_location = shader.get_uniform_location("uniform_name");
    // Do stuff with the uniform location
}
cgl_rs::graphics::shutdown();
window.destroy();
cgl_rs::shutdown();
source

pub fn set_vec2(&mut self, uniform_name: &str, value: &Vector2)

Sets the value of a uniform variable of type Vector2 in the shader program.

Arguments
  • uniform_name - A string slice containing the name of the uniform variable.
  • value - A reference to a Vector2 containing the value to set.
Example
cgl_rs::init();
let mut window = cgl_rs::Window::new("Hello World", 800, 600).unwrap();
cgl_rs::graphics::init();
{
    let mut shader = cgl_rs::graphics::Shader::create_dummy().unwrap();
    let vec2 = cgl_rs::math::Vector2::new(1.0, 2.0);
    shader.set_vec2("uniform_name", &vec2);
    // Do stuff with the uniform variable
}
cgl_rs::graphics::shutdown();
window.destroy();
cgl_rs::shutdown();
source

pub fn set_vec3(&mut self, uniform_name: &str, value: &Vector3)

Sets the value of a uniform variable of type Vector3 in the shader program.

Arguments
  • uniform_name - A string slice containing the name of the uniform variable.
  • value - A reference to a Vector3 containing the value to set.
Example
cgl_rs::init();
let mut window = cgl_rs::Window::new("Hello World", 800, 600).unwrap();
cgl_rs::graphics::init();
{
    let mut shader = cgl_rs::graphics::Shader::create_dummy().unwrap();
    let vec3 = cgl_rs::math::Vector3::new(1.0, 2.0, 3.0);
    shader.set_vec3("uniform_name", &vec3);
    // Do stuff with the uniform variable
}
cgl_rs::graphics::shutdown();
window.destroy();
cgl_rs::shutdown();
source

pub fn set_vec4(&mut self, uniform_name: &str, value: &Vector4)

Sets the value of a uniform variable of type Vector4 in the shader program.

Arguments
  • uniform_name - A string slice containing the name of the uniform variable.
  • value - A reference to a Vector4 containing the value to set.
Example
cgl_rs::init();
let mut window = cgl_rs::Window::new("Hello World", 800, 600).unwrap();
cgl_rs::graphics::init();
{
    let mut shader = cgl_rs::graphics::Shader::create_dummy().unwrap();
    let vec4 = cgl_rs::math::Vector4::new(1.0, 2.0, 3.0, 4.0);
    shader.set_vec4("uniform_name", &vec4);
    // Do stuff with the uniform variable
}
cgl_rs::graphics::shutdown();
window.destroy();
cgl_rs::shutdown();
source

pub fn set_mat4(&mut self, uniform_name: &str, value: &Matrix4x4)

Sets the value of a uniform variable of type Matrix4x4 in the shader program.

Arguments
  • uniform_name - A string slice containing the name of the uniform variable.
  • value - A reference to a Matrix4x4 containing the value to set.
Example
cgl_rs::init();
let mut window = cgl_rs::Window::new("Hello World", 800, 600).unwrap();
cgl_rs::graphics::init();
{
    let mut shader = cgl_rs::graphics::Shader::create_dummy().unwrap();
    let mat4 = cgl_rs::math::Matrix4x4::identity();
    shader.set_mat4("uniform_name", &mat4);
    // Do stuff with the uniform variable
}
cgl_rs::graphics::shutdown();
window.destroy();
cgl_rs::shutdown();
source

pub fn set_int(&mut self, uniform_name: &str, value: i32)

Sets the value of a uniform variable of type i32 in the shader program.

Arguments
  • uniform_name - A string slice containing the name of the uniform variable.
  • value - An i32 value containing the value to set.
Example
cgl_rs::init();
let mut window = cgl_rs::Window::new("Hello World", 800, 600).unwrap();
cgl_rs::graphics::init();
{
    let mut shader = cgl_rs::graphics::Shader::create_dummy().unwrap();
    shader.set_int("uniform_name", 42);
    // Do stuff with the uniform variable
}
cgl_rs::graphics::shutdown();
window.destroy();
cgl_rs::shutdown();
source

pub fn set_bool(&mut self, uniform_name: &str, value: bool)

Sets the value of a uniform variable of type bool in the shader program.

Arguments
  • uniform_name - A string slice containing the name of the uniform variable.
  • value - A boolean value containing the value to set.
Example
cgl_rs::init();
let mut window = cgl_rs::Window::new("Hello World", 800, 600).unwrap();
cgl_rs::graphics::init();
{
    let mut shader = cgl_rs::graphics::Shader::create_dummy().unwrap();
    shader.set_bool("uniform_name", true);
    // Do stuff with the uniform variable
}
cgl_rs::graphics::shutdown();
window.destroy();
cgl_rs::shutdown();
source

pub fn set_float(&mut self, uniform_name: &str, value: f32)

Sets the value of a uniform variable of type float in the shader program.

Arguments
  • uniform_name - A string slice containing the name of the uniform variable.
  • value - A f32 value containing the value to set.
Example
cgl_rs::init();
let mut window = cgl_rs::Window::new("Hello World", 800, 600).unwrap();
cgl_rs::graphics::init();
{
    let mut shader = cgl_rs::graphics::Shader::create_dummy().unwrap();
    shader.set_float("uniform_name", 3.14);
    // Do stuff with the uniform variable
}
cgl_rs::graphics::shutdown();
window.destroy();
cgl_rs::shutdown();
source

pub fn set_double(&mut self, uniform_name: &str, value: f64)

Sets the value of a uniform variable of type double in the shader program.

Arguments
  • uniform_name - A string slice containing the name of the uniform variable.
  • value - A f64 value containing the value to set.
Example
cgl_rs::init();
let mut window = cgl_rs::Window::new("Hello World", 800, 600).unwrap();
cgl_rs::graphics::init();
{
   let mut shader = cgl_rs::graphics::Shader::create_dummy().unwrap();
   shader.set_double("uniform_name", 3.14);
}
cgl_rs::graphics::shutdown();
window.destroy();
cgl_rs::shutdown();
source

pub fn set_vec2v(&mut self, uniform_name: &str, x: f32, y: f32)

Sets the value of a uniform variable of type vec2 in the shader program.

Arguments
  • uniform_name - A string slice containing the name of the uniform variable.
  • x - A f32 value containing the x component of the vector.
  • y - A f32 value containing the y component of the vector.
Example
cgl_rs::init();
let mut window = cgl_rs::Window::new("Hello World", 800, 600).unwrap();
cgl_rs::graphics::init();
{
    let mut shader = cgl_rs::graphics::Shader::create_dummy().unwrap();
    shader.set_vec2v("uniform_name", 1.0, 2.0);
    // Do stuff with the uniform variable
}
cgl_rs::graphics::shutdown();
window.destroy();
cgl_rs::shutdown();
source

pub fn set_vec3v(&mut self, uniform_name: &str, x: f32, y: f32, z: f32)

Sets the value of a uniform variable of type vec3 in the shader program.

Arguments
  • uniform_name - A string slice containing the name of the uniform variable.
  • x - A f32 value containing the x component of the vector.
  • y - A f32 value containing the y component of the vector.
  • z - A f32 value containing the z component of the vector.
Example
cgl_rs::init();
let mut window = cgl_rs::Window::new("Hello World", 800, 600).unwrap();
cgl_rs::graphics::init();
{
    let mut shader = cgl_rs::graphics::Shader::create_dummy().unwrap();
    shader.set_vec3v("uniform_name", 1.0, 2.0, 3.0);
    // Do stuff with the uniform variable
}
cgl_rs::graphics::shutdown();
window.destroy();
cgl_rs::shutdown();
source

pub fn set_vec4v(&mut self, uniform_name: &str, x: f32, y: f32, z: f32, w: f32)

Sets the value of a uniform variable of type vec4 in the shader program.

Arguments
  • uniform_name - A string slice containing the name of the uniform variable.
  • x - A f32 value containing the x component of the vector.
  • y - A f32 value containing the y component of the vector.
  • z - A f32 value containing the z component of the vector.
  • w - A f32 value containing the w component of the vector.
Example
cgl_rs::init();
let mut window = cgl_rs::Window::new("Hello World", 800, 600).unwrap();
cgl_rs::graphics::init();
{
    let mut shader = cgl_rs::graphics::Shader::create_dummy().unwrap();
    shader.set_vec4v("uniform_name", 1.0, 2.0, 3.0, 4.0);
    // Do stuff with the uniform variable
}
cgl_rs::graphics::shutdown();
window.destroy();
cgl_rs::shutdown();
source

pub fn set_ivec2v(&mut self, uniform_name: &str, x: i32, y: i32)

Sets the value of a uniform variable of type ivec2 in the shader program.

Arguments
  • uniform_name - A string slice containing the name of the uniform variable.
  • x - An i32 value containing the x component of the vector.
  • y - An i32 value containing the y component of the vector.
Example
cgl_rs::init();
let mut window = cgl_rs::Window::new("Hello World", 800, 600).unwrap();
cgl_rs::graphics::init();
{
    let mut shader = cgl_rs::graphics::Shader::create_dummy().unwrap();
    shader.set_ivec2v("uniform_name", 1, 2);
    // Do stuff with the uniform variable
}
cgl_rs::graphics::shutdown();
window.destroy();
cgl_rs::shutdown();
source

pub fn set_ivec3v(&mut self, uniform_name: &str, x: i32, y: i32, z: i32)

Sets the value of a uniform variable of type ivec3 in the shader program.

Arguments
  • uniform_name - A string slice containing the name of the uniform variable.
  • x - An i32 value containing the x component of the vector.
  • y - An i32 value containing the y component of the vector.
  • z - An i32 value containing the z component of the vector.
Example
cgl_rs::init();
let mut window = cgl_rs::Window::new("Hello World", 800, 600).unwrap();
cgl_rs::graphics::init();
{
    let mut shader = cgl_rs::graphics::Shader::create_dummy().unwrap();
    shader.set_ivec3v("uniform_name", 1, 2, 3);
    // Do stuff with the uniform variable
}
cgl_rs::graphics::shutdown();
window.destroy();
cgl_rs::shutdown();
source

pub fn set_ivec4v(&mut self, uniform_name: &str, x: i32, y: i32, z: i32, w: i32)

Sets the value of a uniform variable of type ivec4 in the shader program.

Arguments
  • uniform_name - A string slice containing the name of the uniform variable.
  • x - An i32 value containing the x component of the vector.
  • y - An i32 value containing the y component of the vector.
  • z - An i32 value containing the z component of the vector.
  • w - An i32 value containing the w component of the vector.
Example
cgl_rs::init();
let mut window = cgl_rs::Window::new("Hello World", 800, 600).unwrap();
cgl_rs::graphics::init();
{
    let mut shader = cgl_rs::graphics::Shader::create_dummy().unwrap();
    shader.set_ivec4v("uniform_name", 1, 2, 3, 4);
    // Do stuff with the uniform variable
}
cgl_rs::graphics::shutdown();
window.destroy();
cgl_rs::shutdown();
source

pub fn dispatch(&self, x: i32, y: i32, z: i32)

Dispatches a compute shader with the specified number of work groups.

Arguments
  • x - An i32 value containing the number of work groups in the x dimension.
  • y - An i32 value containing the number of work groups in the y dimension.
  • z - An i32 value containing the number of work groups in the z dimension.
Example
cgl_rs::init();
let mut window = cgl_rs::Window::new("Hello World", 800, 600).unwrap();
cgl_rs::graphics::init();
{
    let shader = cgl_rs::graphics::Shader::create_dummy_compute().unwrap();
    shader.dispatch(1, 1, 1);
    // Do stuff with the compute shader
}
cgl_rs::graphics::shutdown();
window.destroy();
cgl_rs::shutdown();

Trait Implementations§

source§

impl Clone for Shader

source§

fn clone(&self) -> Self

Clones the shader object.

NOTE: The new instance will have the same handle, has_been_destroyed flag. This means that the new instance will not be able to receive events nor will the internal window handle be destroyed when the new instance is dropped. The internal window handle will be destroyed when the original instance is dropped.

Example
cgl_rs::init();
let mut window =  cgl_rs::Window::new("Hello World", 600, 800).unwrap();
cgl_rs::graphics::init();
{
    let shader = cgl_rs::graphics::Shader::create_dummy().unwrap();
    let shader2 = shader.clone();
}
cgl_rs::graphics::shutdown();
window.destroy();
cgl_rs::shutdown();
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Shader

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Drop for Shader

source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl RefUnwindSafe for Shader

§

impl !Send for Shader

§

impl !Sync for Shader

§

impl Unpin for Shader

§

impl UnwindSafe for Shader

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.