use crate::graphics::Colour;
#[cfg(any(windows))]
use crate::windows::OpenGraphicsLibrary;
use core::mem::transmute;
const ZERO:u32=0;
const ONE:u32=1;
const SRC_COLOR:u32=0x0300;
const ONE_MINUS_SRC_COLOR:u32=0x0301;
const SRC_ALPHA:u32=0x0302;
const ONE_MINUS_SRC_ALPHA:u32=0x0303;
const DST_ALPHA:u32=0x0304;
const ONE_MINUS_DST_ALPHA:u32=0x0305;
const DST_COLOR:u32=0x0306;
const ONE_MINUS_DST_COLOR:u32=0x0307;
const SRC_ALPHA_SATURATE:u32=0x0308;
const CONSTANT_COLOR:u32=0x8001;
const ONE_MINUS_CONSTANT_COLOR:u32=0x8002;
const CONSTANT_ALPHA:u32=0x8003;
const ONE_MINUS_CONSTANT_ALPHA:u32=0x8004;
const SRC1_ALPHA:u32=0x8589;
const SRC1_COLOR:u32=0x88F9;
const ONE_MINUS_SRC1_COLOR:u32=0x88FA;
const ONE_MINUS_SRC1_ALPHA:u32=0x88FB;
const FUNC_ADD:u32=0x8006;
const FUNC_SUBTRACT:u32=0x800A;
const FUNC_REVERSE_SUBTRACT:u32=0x800B;
const MIN:u32=0x8007;
const MAX:u32=0x8008;
const BLEND_COLOR:u32=0x8005;
const BLEND_DST_ALPHA:u32=0x80CA;
const BLEND_DST_RGB:u32=0x80C8;
const BLEND_EQUATION:u32=0x8009;
const BLEND_EQUATION_ALPHA:u32=0x883D;
const BLEND_EQUATION_RGB:u32=0x8009;
const BLEND_SRC_ALPHA:u32=0x80CB;
const BLEND_SRC_RGB:u32=0x80C9;
#[repr(u32)]
pub enum BlendingEquation{
Addition=FUNC_ADD,
Subtraction=FUNC_SUBTRACT,
ReverseSubtraction=FUNC_REVERSE_SUBTRACT,
Minimum=MIN,
Maximum=MAX
}
#[repr(u32)]
#[derive(Clone,Copy,Debug)]
pub enum BlendingFunction{
Zero=ZERO,
One=ONE,
SourceColour=SRC_COLOR,
OneMinusSourceColour=ONE_MINUS_SRC_COLOR,
DestinationColour=DST_COLOR,
OneMinusDestinationColour=ONE_MINUS_DST_COLOR,
SourceAlpha=SRC_ALPHA,
OneMinusSourceAlpha=ONE_MINUS_SRC_ALPHA,
DestinationAlpha=DST_ALPHA,
OneMinusDestinationAlpha=ONE_MINUS_DST_ALPHA,
ConstantColour=CONSTANT_COLOR,
OneMinusConstantColour=ONE_MINUS_CONSTANT_COLOR,
ConstantAlpha=CONSTANT_ALPHA,
OneMinusConstantAlpha=ONE_MINUS_CONSTANT_ALPHA,
SourceAlphaSaturate=SRC_ALPHA_SATURATE,
}
pub struct Blend{
glBlendColor:usize,
glBlendFunc:usize,
glBlendFuncSeparate:usize,
glBlendEquation:usize,
glBlendEquationSeparate:usize,
}
impl Blend{
pub const fn new()->Blend{
Self{
glBlendColor:0,
glBlendFunc:0,
glBlendFuncSeparate:0,
glBlendEquation:0,
glBlendEquationSeparate:0,
}
}
#[cfg(any(windows))]
pub fn load(&mut self,library:&OpenGraphicsLibrary){
unsafe{
self.glBlendColor=transmute(library.get_proc_address("glBlendColor\0"));
self.glBlendFunc=transmute(library.get_proc_address("glBlendFunc\0"));
self.glBlendFuncSeparate=transmute(library.get_proc_address("glBlendFuncSeparate\0"));
self.glBlendEquation=transmute(library.get_proc_address("glBlendEquation\0"));
self.glBlendEquationSeparate=transmute(library.get_proc_address("glBlendEquationSeparate\0"));
}
}
}
impl Blend{
#[inline(always)]
pub fn enable(&self){
unsafe{
GLCore.enable(GLCapability::Blend)
}
}
#[inline(always)]
pub fn disable(&self){
unsafe{
GLCore.disable(GLCapability::Blend)
}
}
#[inline(always)]
pub fn is_enabled(&self)->bool{
unsafe{
GLCore.is_enabled(GLCapability::Blend)
}
}
}
impl Blend{
#[inline(always)]
pub fn set_blending_colour(&self,[red,greed,blue,alpha]:Colour){
unsafe{
transmute::<usize,fn(f32,f32,f32,f32)>(self.glBlendColor)(red,greed,blue,alpha)
}
}
#[inline(always)]
pub fn get_blending_colour(&self)->Colour{
unsafe{
let mut colour=[0f32;4];
GLCore.get_float_v(BLEND_COLOR,colour.get_unchecked_mut(0));
colour
}
}
#[inline(always)]
pub fn write_blending_colour(&self,colour:&mut Colour){
unsafe{
GLCore.get_float_v(BLEND_COLOR,colour.get_unchecked_mut(0))
}
}
}
impl Blend{
#[inline(always)]
pub fn set_function(&self,sourse_factor:BlendingFunction,destination_factor:BlendingFunction){
unsafe{
transmute::<usize,fn(BlendingFunction,BlendingFunction)>(self.glBlendFunc)(sourse_factor,destination_factor)
}
}
#[inline(always)]
pub fn set_function_separate(
&self,
sourse_factor_rgb:BlendingFunction,
destination_factor_rgb:BlendingFunction,
sourse_factor_alpha:BlendingFunction,
destination_factor_alpha:BlendingFunction,
){
unsafe{
transmute::<usize,fn(BlendingFunction,BlendingFunction,BlendingFunction,BlendingFunction)>(self.glBlendFuncSeparate)(
sourse_factor_rgb,
destination_factor_rgb,
sourse_factor_alpha,
destination_factor_alpha
)
}
}
#[inline(always)]
pub fn get_function_src_rgb(&self)->BlendingFunction{
unsafe{
let mut function=BlendingFunction::One;
GLCore.get_integer_v(BLEND_SRC_RGB,transmute(&mut function));
function
}
}
#[inline(always)]
pub fn write_function_src_rgb(&self,function:&mut BlendingFunction){
unsafe{
GLCore.get_integer_v(BLEND_SRC_RGB,transmute(function))
}
}
#[inline(always)]
pub fn get_function_src_alpha(&self)->BlendingFunction{
unsafe{
let mut function=BlendingFunction::One;
GLCore.get_integer_v(BLEND_SRC_ALPHA,transmute(&mut function));
function
}
}
#[inline(always)]
pub fn write_function_src_alpha(&self,function:&mut BlendingFunction){
unsafe{
GLCore.get_integer_v(BLEND_SRC_ALPHA,transmute(function))
}
}
#[inline(always)]
pub fn get_function_dst_rgb(&self)->BlendingFunction{
unsafe{
let mut function=BlendingFunction::Zero;
GLCore.get_integer_v(BLEND_DST_RGB,transmute(&mut function));
function
}
}
#[inline(always)]
pub fn write_function_dst_rgb(&self,function:&mut BlendingFunction){
unsafe{
GLCore.get_integer_v(BLEND_DST_RGB,transmute(function))
}
}
#[inline(always)]
pub fn get_function_dst_alpha(&self)->BlendingFunction{
unsafe{
let mut function=BlendingFunction::Zero;
GLCore.get_integer_v(BLEND_DST_ALPHA,transmute(&mut function));
function
}
}
#[inline(always)]
pub fn write_function_dst_alpha(&self,function:&mut BlendingFunction){
unsafe{
GLCore.get_integer_v(BLEND_DST_ALPHA,transmute(function))
}
}
}
impl Blend{
#[inline(always)]
pub fn set_equation(&self,equation:BlendingEquation){
unsafe{
transmute::<usize,fn(BlendingEquation)>(self.glBlendEquation)(equation)
}
}
#[inline(always)]
pub fn set_equation_separate(
&self,
equation_rgb:BlendingEquation,
equation_alpha:BlendingEquation
){
unsafe{
transmute::<usize,fn(BlendingEquation,BlendingEquation)>(self.glBlendEquationSeparate)(equation_rgb,equation_alpha)
}
}
#[inline(always)]
pub fn get_equation_rbg(&self)->BlendingEquation{
unsafe{
let mut equation=BlendingEquation::Addition;
GLCore.get_integer_v(BLEND_EQUATION_RGB,transmute(&mut equation));
equation
}
}
#[inline(always)]
pub fn write_equation_rbg(&self,equation:&mut BlendingEquation){
unsafe{
GLCore.get_integer_v(BLEND_EQUATION_RGB,transmute(equation))
}
}
#[inline(always)]
pub fn get_equation_alpha(&self)->BlendingEquation{
unsafe{
let mut equation=BlendingEquation::Addition;
GLCore.get_integer_v(BLEND_EQUATION_ALPHA,transmute(&mut equation));
equation
}
}
#[inline(always)]
pub fn write_equation_alpha(&self,equation:&mut BlendingEquation){
unsafe{
GLCore.get_integer_v(BLEND_EQUATION_ALPHA,transmute(equation));
}
}
}