1 //! Models versions of OpenGL 2 3 use glsl::GLSL; 4 use std::str::FromStr; 5 use std::fmt; 6 use std::error::Error; 7 8 use graphics_api_version::Version; 9 10 #[allow(non_camel_case_types)] 11 #[allow(missing_docs)] 12 #[derive(Copy, Clone, Debug, PartialEq, PartialOrd, Eq, Ord)] 13 pub enum OpenGL { 14 V2_0, 15 V2_1, 16 V3_0, 17 V3_1, 18 V3_2, 19 V3_3, 20 V4_0, 21 V4_1, 22 V4_2, 23 V4_3, 24 V4_4, 25 V4_5 26 } 27 28 impl Into<Version> for OpenGL { into(self) -> Version29 fn into(self) -> Version { 30 let (major, minor) = self.get_major_minor(); 31 Version::opengl(major as u32, minor as u32) 32 } 33 } 34 35 impl OpenGL { 36 /// Creates a new `OpenGL` version from graphics API version. from_api(val: Version) -> Option<OpenGL>37 pub fn from_api(val: Version) -> Option<OpenGL> { 38 if val.api == "OpenGL" { 39 Some(match (val.major, val.minor) { 40 (2, 0) => OpenGL::V2_0, 41 (2, 1) => OpenGL::V2_1, 42 (3, 0) => OpenGL::V3_0, 43 (3, 1) => OpenGL::V3_1, 44 (3, 2) => OpenGL::V3_2, 45 (3, 3) => OpenGL::V3_3, 46 (4, 0) => OpenGL::V4_0, 47 (4, 1) => OpenGL::V4_1, 48 (4, 2) => OpenGL::V4_2, 49 (4, 3) => OpenGL::V4_3, 50 (4, 4) => OpenGL::V4_4, 51 (4, 5) => OpenGL::V4_5, 52 (_, _) => return None, 53 }) 54 } else { 55 None 56 } 57 } 58 59 /// Gets the minor version of OpenGL. get_major_minor(&self) -> (isize, isize)60 pub fn get_major_minor(&self) -> (isize, isize) { 61 match *self { 62 OpenGL::V2_0 => (2, 0), 63 OpenGL::V2_1 => (2, 1), 64 OpenGL::V3_0 => (3, 0), 65 OpenGL::V3_1 => (3, 1), 66 OpenGL::V3_2 => (3, 2), 67 OpenGL::V3_3 => (3, 3), 68 OpenGL::V4_0 => (4, 0), 69 OpenGL::V4_1 => (4, 1), 70 OpenGL::V4_2 => (4, 2), 71 OpenGL::V4_3 => (4, 3), 72 OpenGL::V4_4 => (4, 4), 73 OpenGL::V4_5 => (4, 5) 74 } 75 } 76 77 /// Gets GLSL version associated with OpenGL. 78 #[allow(non_snake_case)] to_glsl(&self) -> GLSL79 pub fn to_glsl(&self) -> GLSL { 80 match *self { 81 OpenGL::V2_0 => GLSL::V1_10, 82 OpenGL::V2_1 => GLSL::V1_20, 83 OpenGL::V3_0 => GLSL::V1_30, 84 OpenGL::V3_1 => GLSL::V1_40, 85 OpenGL::V3_2 => GLSL::V1_50, 86 OpenGL::V3_3 => GLSL::V3_30, 87 OpenGL::V4_0 => GLSL::V4_00, 88 OpenGL::V4_1 => GLSL::V4_10, 89 OpenGL::V4_2 => GLSL::V4_20, 90 OpenGL::V4_3 => GLSL::V4_30, 91 OpenGL::V4_4 => GLSL::V4_40, 92 OpenGL::V4_5 => GLSL::V4_50 93 } 94 } 95 } 96 97 98 impl FromStr for OpenGL { 99 type Err = ParseOpenGLError; 100 from_str(s: &str) -> Result<Self, Self::Err>101 fn from_str(s: &str) -> Result<Self, Self::Err> { 102 match s { 103 "2.0" => Ok(OpenGL::V2_0), 104 "2.1" => Ok(OpenGL::V2_1), 105 "3.0" => Ok(OpenGL::V3_0), 106 "3.1" => Ok(OpenGL::V3_1), 107 "3.2" => Ok(OpenGL::V3_2), 108 "3.3" => Ok(OpenGL::V3_3), 109 "4.0" => Ok(OpenGL::V4_0), 110 "4.1" => Ok(OpenGL::V4_1), 111 "4.2" => Ok(OpenGL::V4_2), 112 "4.3" => Ok(OpenGL::V4_3), 113 "4.4" => Ok(OpenGL::V4_4), 114 "4.5" => Ok(OpenGL::V4_5), 115 error => Err(ParseOpenGLError{input: error.into()}), 116 } 117 } 118 } 119 120 121 /// Represents an error while trying to get `OpenGL` from `&str`. 122 #[derive(Debug)] 123 pub struct ParseOpenGLError{ 124 input: String 125 } 126 127 impl fmt::Display for ParseOpenGLError { fmt(&self, f: &mut fmt::Formatter) -> fmt::Result128 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 129 write!(f, "`{}` is not a valid OpenGL version", self.input) 130 } 131 } 132 133 impl Error for ParseOpenGLError { description(&self) -> &str134 fn description(&self) -> &str { 135 "Invalid OpenGL version" 136 } 137 } 138