1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
use std::mem; use gl; use gl::types::*; use super::{Blending, CullFace, Depth, DrawMode, Error, IndexKind}; #[inline] pub fn gl_set_defaults() { unsafe { gl::FrontFace(gl::CCW); if gl::PixelStorei::is_loaded() { gl::PixelStorei(gl::UNPACK_ALIGNMENT, 1); } } gl_set_viewport(0, 0, 1, 1); gl_set_clear_depth(1.0); gl_set_clear_stencil(0); gl_set_depth_write(true); gl_set_depth_range(0.0, 1.0); gl_set_line_width(1.0); gl_set_blending(Blending::Default); gl_set_cull_face(CullFace::Back); gl_set_depth_func(Depth::LessThanOrEqual); gl_set_clear_color(&[0.0, 0.0, 0.0, 1.0]); gl_clear(true, true, true); } #[inline(always)] pub fn gl_get_error() -> Error { unsafe { gl::GetError().into() } } #[inline(always)] pub fn gl_set_viewport(x: usize, y: usize, width: usize, height: usize) { unsafe { gl::Viewport(x as GLint, y as GLint, width as GLsizei, height as GLsizei); } } #[inline(always)] pub fn gl_set_clear_depth(clear_depth: f64) { unsafe { if gl::ClearDepthf::is_loaded() { gl::ClearDepthf(clear_depth as f32); } else if gl::ClearDepth::is_loaded() { gl::ClearDepth(clear_depth); } } } #[inline(always)] pub fn gl_set_clear_stencil(clear_stencil: isize) { unsafe { gl::ClearStencil(clear_stencil as GLint); } } #[inline(always)] pub fn gl_set_depth_write(depth_write: bool) { unsafe { gl::DepthMask(if depth_write { gl::TRUE } else { gl::FALSE }); } } #[inline(always)] pub fn gl_set_depth_range(near: f64, far: f64) { unsafe { if gl::DepthRangef::is_loaded() { gl::DepthRangef(near as f32, far as f32); } else if gl::DepthRange::is_loaded() { gl::DepthRange(near, far); } } } #[inline(always)] pub fn gl_set_line_width(line_width: f32) { unsafe { gl::LineWidth(line_width as GLfloat); } } #[inline] pub fn gl_set_blending(blending: Blending) { match &blending { &Blending::Additive => unsafe { gl::Enable(gl::BLEND); gl::BlendEquation(gl::FUNC_ADD); gl::BlendFunc(gl::SRC_ALPHA, gl::ONE); }, &Blending::Subtractive => unsafe { gl::Enable(gl::BLEND); gl::BlendEquation(gl::FUNC_ADD); gl::BlendFunc(gl::ZERO, gl::ONE_MINUS_SRC_COLOR); }, &Blending::Multiply => unsafe { gl::Enable(gl::BLEND); gl::BlendEquation(gl::FUNC_ADD); gl::BlendFunc(gl::ZERO, gl::SRC_COLOR); }, &Blending::Default => unsafe { gl::Enable(gl::BLEND); gl::BlendEquationSeparate(gl::FUNC_ADD, gl::FUNC_ADD); gl::BlendFuncSeparate( gl::SRC_ALPHA, gl::ONE_MINUS_SRC_ALPHA, gl::ONE, gl::ONE_MINUS_SRC_ALPHA, ); }, &Blending::None => unsafe { gl::Disable(gl::BLEND); }, } } #[inline] pub fn gl_set_cull_face(cull_face: CullFace) { match &cull_face { &CullFace::Back => unsafe { gl::Enable(gl::CULL_FACE); gl::CullFace(gl::BACK); }, &CullFace::Front => unsafe { gl::Enable(gl::CULL_FACE); gl::CullFace(gl::FRONT); }, &CullFace::FrontAndBack => unsafe { gl::Enable(gl::CULL_FACE); gl::CullFace(gl::FRONT_AND_BACK); }, &CullFace::None => unsafe { gl::Disable(gl::CULL_FACE); }, } } #[inline] pub fn gl_set_depth_func(depth_func: Depth) { match &depth_func { &Depth::Never => unsafe { gl::Enable(gl::DEPTH_TEST); gl::DepthFunc(gl::NEVER); }, &Depth::LessThan => unsafe { gl::Enable(gl::DEPTH_TEST); gl::DepthFunc(gl::LESS); }, &Depth::Equal => unsafe { gl::Enable(gl::DEPTH_TEST); gl::DepthFunc(gl::EQUAL); }, &Depth::LessThanOrEqual => unsafe { gl::Enable(gl::DEPTH_TEST); gl::DepthFunc(gl::LEQUAL); }, &Depth::GreaterThan => unsafe { gl::Enable(gl::DEPTH_TEST); gl::DepthFunc(gl::GREATER); }, &Depth::NotEqual => unsafe { gl::Enable(gl::DEPTH_TEST); gl::DepthFunc(gl::NOTEQUAL); }, &Depth::GreaterThanOrEqual => unsafe { gl::Enable(gl::DEPTH_TEST); gl::DepthFunc(gl::GEQUAL); }, &Depth::Always => unsafe { gl::Enable(gl::DEPTH_TEST); gl::DepthFunc(gl::ALWAYS); }, &Depth::None => unsafe { gl::Disable(gl::DEPTH_TEST); }, } } #[inline(always)] pub fn gl_set_clear_color(color: &[f32; 4]) { unsafe { gl::ClearColor(color[0], color[1], color[2], color[3]); } } #[inline] pub fn gl_clear(color: bool, depth: bool, stencil: bool) { let mut bits: GLbitfield = 0; if color { bits = bits | gl::COLOR_BUFFER_BIT; } if depth { bits = bits | gl::DEPTH_BUFFER_BIT; } if stencil { bits = bits | gl::STENCIL_BUFFER_BIT; } gl_clear_bits(bits); } #[inline(always)] pub fn gl_clear_bits(bits: GLbitfield) { unsafe { gl::Clear(bits); } } #[inline] pub fn gl_draw_arrays(mode: DrawMode, first: usize, count: usize) { unsafe { gl::DrawArrays(mode.into(), first as GLint, count as GLsizei); } } #[inline] pub fn gl_draw_elements(mode: DrawMode, count: usize, kind: IndexKind, offset: GLint) { unsafe { gl::DrawElements( mode.into(), count as GLint, kind.into(), mem::transmute(offset as usize), ); } }