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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
use std::mem;
use std::fmt;
use std::error;
use gl;
pub struct Color {
pub r: f32,
pub g: f32,
pub b: f32,
pub a: f32
}
impl Color {
pub fn rgba(r: f32, g: f32, b: f32, a: f32) -> Self {
Color { r: r, g: g, b: b, a: a}
}
}
#[derive(Clone, Copy)]
pub struct Viewport {
pub x: u32,
pub y: u32,
pub width: u32,
pub height: u32
}
impl Viewport {
pub fn new(x: u32, y: u32, width: u32, height: u32) -> Self {
Viewport {
x: x,
y: y,
width: width,
height: height
}
}
}
pub trait GLObject {
type Id;
unsafe fn from_raw(id: Self::Id) -> Self;
fn id(&self) -> Self::Id;
fn into_raw(self) -> Self::Id
where Self: Sized
{
let id = self.id();
mem::forget(self);
id
}
}
bitflags! {
pub flags BufferBits: ::gl::types::GLbitfield {
const COLOR_BUFFER_BIT = ::gl::COLOR_BUFFER_BIT,
const DEPTH_BUFFER_BIT = ::gl::DEPTH_BUFFER_BIT,
const STENCIL_BUFFER_BIT = ::gl::STENCIL_BUFFER_BIT
}
}
gl_enum! {
pub gl_enum Capability {
pub const Blend as BLEND
= gl::BLEND,
pub const CullFace as CULL_FACE
= gl::CULL_FACE,
pub const DepthTest as DEPTH_TEST
= gl::DEPTH_TEST,
pub const Dither as DITHER
= gl::DITHER,
pub const PolygonOffsetFill as POLYGON_OFFSET_FILL
= gl::POLYGON_OFFSET_FILL,
pub const SampleAlphaToCoverage as SAMPLE_ALPHA_TO_COVERAGE =
gl::SAMPLE_ALPHA_TO_COVERAGE,
pub const SampleCoverage as SAMPLE_COVERAGE =
gl::SAMPLE_COVERAGE,
pub const ScisscorTest as SCISSCOR_TEST =
gl::SCISSOR_TEST,
pub const StencilTest as STENCIL_TEST =
gl::STENCIL_TEST
}
}
#[derive(Debug)]
pub enum GLError {
InvalidEnum,
InvalidValue,
InvalidOperation,
InvalidFramebufferOperation,
OutOfMemory,
FramebufferError(GLFramebufferError),
Message(String)
}
impl fmt::Display for GLError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
GLError::InvalidEnum => {
write!(f, "Invalid enum")
},
GLError::InvalidValue => {
write!(f, "Invalid value")
},
GLError::InvalidOperation => {
write!(f, "Invalid operation")
},
GLError::InvalidFramebufferOperation => {
write!(f, "Invalid framebuffer operation")
},
GLError::OutOfMemory => {
write!(f, "Out of memory")
},
GLError::FramebufferError(ref e) => {
write!(f, "{:?}", e)
},
GLError::Message(ref s) => {
write!(f, "{}", s)
}
}
}
}
impl error::Error for GLError {
fn description(&self) -> &str {
match *self {
GLError::InvalidEnum => "Invalid enum variant in OpenGL call.",
GLError::InvalidValue => "Out-of-range value in OpenGL call.",
GLError::InvalidOperation => "The specified OpenGL operation is not allowed in the current state.",
GLError::InvalidFramebufferOperation => "OpenGL command tried to read or write to an incomplete framebuffer.",
GLError::OutOfMemory => "There is not enough memory left to execute the specified OpenGL command.",
GLError::FramebufferError(ref e) => {
error::Error::description(e)
},
GLError::Message(ref s) => &s
}
}
fn cause(&self) -> Option<&error::Error> {
match *self {
GLError::FramebufferError(ref e) => { Some(e) },
_ => { None }
}
}
}
#[derive(Debug)]
pub enum GLFramebufferError {
IncompleteAttachment,
IncompleteDimensions,
IncompleteMissingAttachment,
Unsupported
}
impl fmt::Display for GLFramebufferError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
GLFramebufferError::IncompleteAttachment => {
write!(f, "Incomplete attachment")
},
GLFramebufferError::IncompleteDimensions => {
write!(f, "Incomplete dimensions")
},
GLFramebufferError::IncompleteMissingAttachment => {
write!(f, "Missing attachments")
},
GLFramebufferError::Unsupported => {
write!(f, "Unsupported")
}
}
}
}
impl error::Error for GLFramebufferError {
fn description(&self) -> &str {
match *self {
GLFramebufferError::IncompleteAttachment => "One or more framebuffer attachments are not complete",
GLFramebufferError::IncompleteDimensions => "Not all images attached to the framebuffer have the same width and height",
GLFramebufferError::IncompleteMissingAttachment => "The framebuffer has no images attached",
GLFramebufferError::Unsupported => "The framebuffer contains an unsupported combination of attachments",
}
}
}
impl From<GLFramebufferError> for GLError {
fn from(e: GLFramebufferError) -> GLError {
GLError::FramebufferError(e)
}
}
gl_enum! {
pub gl_enum DrawingMode {
pub const Points as POINTS = gl::POINTS,
pub const LineStrip as LINE_STRIP = gl::LINE_STRIP,
pub const LineLoop as LINE_LOOP = gl::LINE_LOOP,
pub const Lines as LINES = gl::LINES,
pub const TriangleStrip as TRIANGLE_STRIP = gl::TRIANGLE_STRIP,
pub const TriangleFan as TRIANGLE_FAN = gl::TRIANGLE_FAN,
pub const Triangles as TRIANGLES = gl::TRIANGLES
}
}
gl_enum! {
pub gl_enum DataType {
pub const Byte as BYTE = gl::BYTE,
pub const UnsignedByte as UNSIGNED_BYTE = gl::UNSIGNED_BYTE,
pub const Short as SHORT = gl::SHORT,
pub const UnsignedShort as UNSIGNED_SHORT = gl::UNSIGNED_SHORT,
pub const Fixed as FIXED = gl::FIXED,
pub const Float as FLOAT = gl::FLOAT
}
}