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
use gl;
use gl::types::*;
pub trait Pixel {
type Encoding;
type RawEncoding;
fn pixel_format() -> PixelFormat;
}
pub trait ColorPixel: Pixel {}
pub trait DepthPixel: Pixel {}
pub trait RenderablePixel: Pixel {}
#[derive(Clone, Copy, Debug)]
pub struct PixelFormat {
pub encoding: Type,
pub format: Format
}
#[derive(Clone, Copy, Debug)]
pub enum Type {
Integral,
Unsigned,
Floating
}
#[derive(Clone, Copy, Debug)]
pub enum Format {
R(u8),
RG(u8, u8),
RGB(u8, u8, u8),
RGBA(u8, u8, u8, u8),
Depth(u8)
}
pub fn is_color_pixel(f: PixelFormat) -> bool {
match f.format {
Format::Depth(_) => false,
_ => true
}
}
pub fn is_depth_pixel(f: PixelFormat) -> bool {
!is_color_pixel(f)
}
#[derive(Clone, Copy, Debug)]
pub struct RGB8UI;
impl Pixel for RGB8UI {
type Encoding = (u8, u8, u8);
type RawEncoding = u8;
fn pixel_format() -> PixelFormat {
PixelFormat {
encoding: Type::Unsigned,
format: Format::RGB(8, 8, 8)
}
}
}
impl ColorPixel for RGB8UI {}
impl RenderablePixel for RGB8UI {}
#[derive(Clone, Copy, Debug)]
pub struct RGBA8UI;
impl Pixel for RGBA8UI {
type Encoding = (u8, u8, u8, u8);
type RawEncoding = u8;
fn pixel_format() -> PixelFormat {
PixelFormat {
encoding: Type::Unsigned,
format: Format::RGBA(8, 8, 8, 8)
}
}
}
impl ColorPixel for RGBA8UI {}
impl RenderablePixel for RGBA8UI {}
#[derive(Clone, Copy, Debug)]
pub struct R32F;
impl Pixel for R32F {
type Encoding = f32;
type RawEncoding = f32;
fn pixel_format() -> PixelFormat {
PixelFormat {
encoding: Type::Floating,
format: Format::R(32)
}
}
}
impl ColorPixel for R32F {}
impl RenderablePixel for R32F {}
#[derive(Clone, Copy, Debug)]
pub struct RGB32F;
impl Pixel for RGB32F {
type Encoding = (f32, f32, f32);
type RawEncoding = f32;
fn pixel_format() -> PixelFormat {
PixelFormat {
encoding: Type::Floating,
format: Format::RGB(32, 32, 32)
}
}
}
impl ColorPixel for RGB32F {}
impl RenderablePixel for RGB32F {}
#[derive(Clone, Copy, Debug)]
pub struct RGBA32F;
impl Pixel for RGBA32F {
type Encoding = (f32, f32, f32, f32);
type RawEncoding = f32;
fn pixel_format() -> PixelFormat {
PixelFormat {
encoding: Type::Floating,
format: Format::RGBA(32, 32, 32, 32)
}
}
}
impl ColorPixel for RGBA32F {}
impl RenderablePixel for RGBA32F {}
#[derive(Clone, Copy, Debug)]
pub struct Depth32F;
impl Pixel for Depth32F {
type Encoding = f32;
type RawEncoding = f32;
fn pixel_format() -> PixelFormat {
PixelFormat {
encoding: Type::Floating,
format: Format::Depth(32)
}
}
}
impl DepthPixel for Depth32F {}
pub fn opengl_pixel_format(pf: PixelFormat) -> Option<(GLenum, GLenum, GLenum)> {
match (pf.format, pf.encoding) {
(Format::R(32), Type::Floating) => Some((gl::RED, gl::R32F, gl::FLOAT)),
(Format::RGB(8, 8, 8), Type::Unsigned) => Some((gl::RGB_INTEGER, gl::RGB8UI, gl::UNSIGNED_BYTE)),
(Format::RGBA(8, 8, 8, 8), Type::Unsigned) => Some((gl::RGBA_INTEGER, gl::RGBA8UI, gl::UNSIGNED_BYTE)),
(Format::RGB(32, 32, 32), Type::Floating) => Some((gl::RGB, gl::RGB32F, gl::FLOAT)),
(Format::RGBA(32, 32, 32, 32), Type::Floating) => Some((gl::RGBA, gl::RGBA32F, gl::FLOAT)),
(Format::Depth(32), Type::Floating) => Some((gl::DEPTH_COMPONENT, gl::DEPTH_COMPONENT32F, gl::FLOAT)),
_ => panic!("unsupported pixel format {:?}", pf)
}
}
pub fn pixel_components(pf: PixelFormat) -> usize {
match pf.format {
Format::RGB(_, _, _) => 3,
Format::RGBA(_, _, _, _) => 4,
Format::Depth(_) => 1,
_ => panic!("unsupported pixel format")
}
}