summaryrefslogtreecommitdiff
path: root/opengl-bindings/src/misc.rs
blob: 3a3335d731e822559b1d00d9237a490501c163be (plain)
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
use bitflags::bitflags;

use crate::CurrentContextWithFns;
use crate::data_types::{Dimens, Vec2};

/// 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 get_viewport(
    current_context: &CurrentContextWithFns<'_>,
) -> (Vec2<u32>, Dimens<u32>)
{
    let mut values = [0i32; 4];

    unsafe {
        current_context
            .fns()
            .GetIntegerv(crate::sys::VIEWPORT, values.as_mut_ptr());
    }

    let [x, y, width, height] = values;

    let pos = Vec2::<u32> {
        x: x.try_into().expect("Negative viewport x coordinate"),
        y: y.try_into().expect("Negative viewport y coordinate"),
    };

    let size = Dimens::<u32> {
        width: width.try_into().expect("Negative viewport width"),
        height: height.try_into().expect("Negative viewport height"),
    };

    (pos, size)
}

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())
}

/// Defines a rectangle, called the scissor box, in window coordinates.
pub fn define_scissor_box(
    current_context: &CurrentContextWithFns<'_>,
    lower_left_corner_pos: Vec2<u16>,
    size: Dimens<u16>,
)
{
    let lower_left_corner_pos = Vec2::<crate::sys::types::GLint> {
        x: lower_left_corner_pos.x.into(),
        y: lower_left_corner_pos.y.into(),
    };

    let size = Dimens::<crate::sys::types::GLsizei> {
        width: size.width.into(),
        height: size.height.into(),
    };

    unsafe {
        current_context.fns().Scissor(
            lower_left_corner_pos.x,
            lower_left_corner_pos.y,
            size.width,
            size.height,
        );
    }
}

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, Clone, Copy)]
#[repr(u32)]
#[non_exhaustive]
pub enum Capability
{
    DepthTest = crate::sys::DEPTH_TEST,
    ScissorTest = crate::sys::SCISSOR_TEST,
    MultiSample = crate::sys::MULTISAMPLE,
    DebugOutput = crate::sys::DEBUG_OUTPUT,
    DebugOutputSynchronous = crate::sys::DEBUG_OUTPUT_SYNCHRONOUS,
    Blend = crate::sys::BLEND,
    CullFace = crate::sys::CULL_FACE,
}

#[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
    },
}