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
|
use bitflags::bitflags;
use crate::data_types::{Dimens, Vec2};
use crate::CurrentContextWithFns;
/// Sets the viewport.
///
/// The `u32` values in `position` and `size` must fit in `i32`s.
///
/// # Errors
/// Returns `Err` if any value in `position` or `size` does not fit into a `i32`.
pub fn set_viewport(
current_context: &CurrentContextWithFns<'_>,
position: &Vec2<u32>,
size: &Dimens<u32>,
) -> Result<(), SetViewportError>
{
let position = Vec2::<crate::sys::types::GLint> {
x: position.x.try_into().map_err(|_| {
SetViewportError::PositionXValueTooLarge {
value: position.x,
max_value: crate::sys::types::GLint::MAX as u32,
}
})?,
y: position.y.try_into().map_err(|_| {
SetViewportError::PositionYValueTooLarge {
value: position.y,
max_value: crate::sys::types::GLint::MAX as u32,
}
})?,
};
let size = Dimens::<crate::sys::types::GLsizei> {
width: size.width.try_into().map_err(|_| {
SetViewportError::SizeWidthValueTooLarge {
value: size.width,
max_value: crate::sys::types::GLsizei::MAX as u32,
}
})?,
height: size.height.try_into().map_err(|_| {
SetViewportError::SizeHeightValueTooLarge {
value: size.height,
max_value: crate::sys::types::GLsizei::MAX as u32,
}
})?,
};
unsafe {
current_context
.fns()
.Viewport(position.x, position.y, size.width, size.height);
}
Ok(())
}
pub fn clear_buffers(current_context: &CurrentContextWithFns<'_>, mask: BufferClearMask)
{
unsafe {
current_context.fns().Clear(mask.bits());
}
}
pub fn set_polygon_mode(
current_context: &CurrentContextWithFns<'_>,
face: impl Into<PolygonModeFace>,
mode: impl Into<PolygonMode>,
)
{
unsafe {
current_context
.fns()
.PolygonMode(face.into() as u32, mode.into() as u32);
}
}
pub fn enable(current_context: &CurrentContextWithFns<'_>, capacity: Capability)
{
unsafe {
current_context.fns().Enable(capacity as u32);
}
}
pub fn disable(current_context: &CurrentContextWithFns<'_>, capability: Capability)
{
unsafe {
current_context.fns().Disable(capability as u32);
}
}
pub fn set_enabled(
current_context: &CurrentContextWithFns<'_>,
capability: Capability,
enabled: bool,
)
{
if enabled {
enable(current_context, capability);
} else {
disable(current_context, capability);
}
}
#[must_use]
pub fn get_context_flags(current_context: &CurrentContextWithFns<'_>) -> ContextFlags
{
let mut context_flags = crate::sys::types::GLint::default();
unsafe {
current_context
.fns()
.GetIntegerv(crate::sys::CONTEXT_FLAGS, &raw mut context_flags);
}
ContextFlags::from_bits_truncate(context_flags.cast_unsigned())
}
bitflags! {
#[derive(Debug, Clone, Copy)]
pub struct BufferClearMask: u32 {
const COLOR = crate::sys::COLOR_BUFFER_BIT;
const DEPTH = crate::sys::DEPTH_BUFFER_BIT;
const STENCIL = crate::sys::STENCIL_BUFFER_BIT;
}
}
#[derive(Debug)]
#[repr(u32)]
pub enum Capability
{
DepthTest = crate::sys::DEPTH_TEST,
MultiSample = crate::sys::MULTISAMPLE,
DebugOutput = crate::sys::DEBUG_OUTPUT,
DebugOutputSynchronous = crate::sys::DEBUG_OUTPUT_SYNCHRONOUS,
}
#[derive(Debug)]
#[repr(u32)]
pub enum PolygonMode
{
Point = crate::sys::POINT,
Line = crate::sys::LINE,
Fill = crate::sys::FILL,
}
#[derive(Debug)]
#[repr(u32)]
pub enum PolygonModeFace
{
Front = crate::sys::FRONT,
Back = crate::sys::BACK,
FrontAndBack = crate::sys::FRONT_AND_BACK,
}
bitflags! {
#[derive(Debug, Clone, Copy)]
pub struct ContextFlags: u32 {
const FORWARD_COMPATIBLE = crate::sys::CONTEXT_FLAG_FORWARD_COMPATIBLE_BIT;
const DEBUG = crate::sys::CONTEXT_FLAG_DEBUG_BIT;
const ROBUST_ACCESS = crate::sys::CONTEXT_FLAG_ROBUST_ACCESS_BIT;
}
}
#[derive(Debug, thiserror::Error)]
pub enum SetViewportError
{
#[error("Position X value ({value}) is too large. Must be < {max_value}")]
PositionXValueTooLarge
{
value: u32, max_value: u32
},
#[error("Position Y value ({value}) is too large. Must be < {max_value}")]
PositionYValueTooLarge
{
value: u32, max_value: u32
},
#[error("Size width value ({value}) is too large. Must be < {max_value}")]
SizeWidthValueTooLarge
{
value: u32, max_value: u32
},
#[error("Size height value ({value}) is too large. Must be < {max_value}")]
SizeHeightValueTooLarge
{
value: u32, max_value: u32
},
}
|