Trait glitter::shader::ContextShaderExt [] [src]

pub trait ContextShaderExt: BaseContext {
    unsafe fn create_shader(
        &self,
        shader_type: ShaderType
    ) -> Result<Shader, ()> { ... }
fn shader_source(&self, shader: &mut Shader, source: &str) { ... }
fn compile_shader(&self, shader: &mut Shader) -> Result<(), GLError> { ... }
fn get_shader_info_log(&self, shader: &Shader) -> Option<String> { ... } }

An extension trait that includes shader-related OpenGL methods.

Provided Methods

Create a new, uninitialized shader.

Safety

Most OpenGL functions that take a shader expect the shader to have been setup and compiled already. Before using this shader, it must have its source set using gl.shader_source and compiled with gl.compile_shader. Instead, consider using one of the shader builder functions, which handle proper setup and compilation automatically.

Example

#[macro_use] extern crate glitter;
use glitter::prelude::*;

let gl = unsafe { glitter::Context::current_context() };
let vertex_source = r##"#version 100
    attribute vec4 position;

    void main() {
        gl_Position = position;
    }
"##;

let shader = unsafe {
    let mut shader = gl.create_shader(glitter::VERTEX_SHADER).unwrap();
    gl.shader_source(&mut shader, vertex_source);
    gl.compile_shader(&mut shader).unwrap();

    shader
};

See also

gl.build_shader, gl.build_vertex_shader, and gl.build_fragment_shader: A safe interface for creating and building shaders, which automatically handle compilation and error checking.

glCreateShader OpenGL docs

Set or replace a shader object's source. The shader should be recompiled after calling this function by using the gl.compile_shader function

See also

gl.create_shader: A function to create a new, uninitialized shader. Includes an example of how to use gl.shader_source

gl.build_shader, gl.build_vertex_shader, and gl.build_fragment_shader: The preferred way to create and compile a shader object, which skips the need to directly call gl.shader_source.

glShaderSource OpenGL docs

Compile the shader's associated source.

Panics

This function will panic if an OpenGL error occurs while trying to compile the shader (determined by calling gl.get_error). Note that this function does not panic if there was an error within the shader; this function should only panic if the shader is invalid or if shader compilation is unavailable with the current OpenGL context.

Failures

If a compilation error occurs, an Err value will be returned with the compilation error messages (as determined by gl.get_shader_info_log).

See also

gl.create_shader: A function to create a new, uninitialized shader. Includes an example of how to use gl.compile_shader

gl.build_shader, gl.build_vertex_shader, and gl.build_fragment_shader: The preferred way to create and compile a shader object, which skips the need to directly call gl.compile_shader.

glCompileShader OpenGL docs

Get the information log associated with a shader. This is used to get compilation errors, warnings, or other diagnostic information that may have occurred while trying to compile a shader. Returns None if no such diagnostic information was generated.

Example

#[macro_use] extern crate glitter;
use glitter::prelude::*;

let fragment_source = r##"#version 100
    void main() {
        float someValue = 1.0 * 2.0;
        gl_FragColor = vec4(1.0, 0.0, 0.0, 0.0);
    }
"##;

let gl = unsafe { glitter::Context::current_context() };
let shader = gl.build_fragment_shader(fragment_source).unwrap();
if let Some(info) = gl.get_shader_info_log(&shader) {
    println!("Shader compilation info: {}", info);
    // "Shader compilation info: unused variable `someValue` on line 3"
    // (or whatever other diagnostics your graphics device
    //  decides to output)
}

See also

glGetShaderInfoLog OpenGL docs

Implementors