// loads shader code from a file
loads shader code from a file
loads shader code from a file
fn read_shader_code(
        shader_path: &Path,
        shader_type: u32
    ) -> Result<gl::types::GLuint, String> {
        // reads the file text
        let mut shader_file = File::open(shader_path).unwrap();
        // reads the shader file
        let mut shader_string = String::new();
        shader_file.read_to_string(&mut shader_string).unwrap();
        // creates a new source string
        let source = CString::new(shader_string).unwrap();
        // initialises the shader id
        let shader_id: u32;
        unsafe {
            // creates a shader of a particular type
            shader_id = gl::CreateShader(shader_type);
            // reads the shader source, attaching it to the shader with this id
            gl::ShaderSource(shader_id, 1, &source.as_ptr(), std::ptr::null());
            // compiles the shader
            gl::CompileShader(shader_id);
        }
        // checks if the shader compiled successfully
        let mut success: gl::types::GLint = 1;
        unsafe {
            gl::GetShaderiv(shader_id, gl::COMPILE_STATUS, &mut success);
        }
        // return an error if shader compilation was unsuccessful
        if success == 0 {
            let mut len: gl::types::GLint = 0;
            unsafe {
                gl::GetShaderiv(shader_id, gl::INFO_LOG_LENGTH, &mut len);
            }
            let error = ShaderProgram::create_whitespace_cstring_with_len(len as usize);
            unsafe {
                gl::GetShaderInfoLog(
                    shader_id,
                    len,
                    std::ptr::null_mut(),
                    error.as_ptr() as *mut gl::types::GLchar
                );
            }
            return Err(error.to_string_lossy().into_owned());
        }
        // returns the shader id if everything was okay
        return Ok(shader_id)
    }
fn read_shader_code ( shader_path : & Path , shader_type : u32 ) -> Result < gl :: types :: GLuint , String > { // reads the file text let mut shader_file = File :: open ( shader_path ) . unwrap ( ) ; // reads the shader file let mut shader_string = String :: new ( ) ; shader_file . read_to_string ( & mut shader_string ) . unwrap ( ) ; // creates a new source string let source = CString :: new ( shader_string ) . unwrap ( ) ; // initialises the shader id let shader_id : u32 ; unsafe { // creates a shader of a particular type shader_id = gl :: CreateShader ( shader_type ) ; // reads the shader source, attaching it to the shader with this id gl :: ShaderSource ( shader_id , 1 , & source . as_ptr ( ) , std :: ptr :: null ( ) ) ; // compiles the shader gl :: CompileShader ( shader_id ) ; } // checks if the shader compiled successfully let mut success : gl :: types :: GLint = 1 ; unsafe { gl :: GetShaderiv ( shader_id , gl :: COMPILE_STATUS , & mut success ) ; } // return an error if shader compilation was unsuccessful if success == 0 { let mut len : gl :: types :: GLint = 0 ; unsafe { gl :: GetShaderiv ( shader_id , gl :: INFO_LOG_LENGTH , & mut len ) ; } let error = ShaderProgram :: create_whitespace_cstring_with_len ( len as usize ) ; unsafe { gl :: GetShaderInfoLog ( shader_id , len , std :: ptr :: null_mut ( ) , error . as_ptr ( ) as * mut gl :: types :: GLchar ) ; } return Err ( error . to_string_lossy ( ) . into_owned ( ) ) ; } // returns the shader id if everything was okay return Ok ( shader_id ) }
loads shader code from a file
loads shader code from a file
shader_path
&Path
shader_type
u32