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
|
use crate::opengl::shader::{
Error as ShaderError,
Kind as ShaderKind,
Program as ShaderProgram,
Shader,
};
use crate::renderer::Renderable;
use crate::texture::Texture;
use crate::transform::Transform;
use crate::vector::Vec3;
use crate::vertex::Vertex;
const FRAG_SHADER_COLOR: &str = include_str!("../fragment-color.glsl");
const FRAG_SHADER_TEXTURE: &str = include_str!("../fragment-texture.glsl");
#[derive(Debug)]
pub struct Object
{
id: Id,
renderable: Renderable,
transform: Transform,
texture: Option<Texture>,
}
impl Object
{
/// Returns the object ID.
#[must_use]
pub fn id(&self) -> Id
{
self.id
}
#[must_use]
pub fn position(&self) -> &Vec3<f32>
{
self.transform.translation()
}
pub fn translate(&mut self, translation: Vec3<f32>)
{
self.transform.set_translation(translation);
}
pub fn scale(&mut self, scaling: Vec3<f32>)
{
self.transform.set_scaling(scaling);
}
#[must_use]
pub fn texture(&self) -> Option<&Texture>
{
self.texture.as_ref()
}
pub(crate) fn renderable(&self) -> &Renderable
{
&self.renderable
}
pub(crate) fn transform(&self) -> &Transform
{
&self.transform
}
}
/// Object builder.
#[derive(Debug, Default)]
pub struct Builder
{
vertices: Vec<Vertex>,
indices: Option<Vec<u32>>,
texture: Option<Texture>,
}
impl Builder
{
/// Returns a new [`Object`] builder.
#[must_use]
pub fn new() -> Self
{
Self::default()
}
#[must_use]
pub fn vertices(mut self, vertices: impl IntoIterator<Item = Vertex>) -> Self
{
self.vertices = vertices.into_iter().collect();
self
}
#[must_use]
pub fn indices(mut self, indices: impl IntoIterator<Item = u32>) -> Self
{
self.indices = Some(indices.into_iter().collect());
self
}
#[must_use]
pub fn texture(mut self, texture: Texture) -> Self
{
self.texture = Some(texture);
self
}
/// Builds a new [`Object`].
///
/// # Errors
/// Will return `Err` if:
/// - Creating shaders fails
/// - Linking the shader program fails
pub fn build(self, id: Id) -> Result<Object, Error>
{
let vertex_shader =
Self::create_shader(ShaderKind::Vertex, include_str!("../vertex.glsl"))
.map_err(Error::CreateVertexShaderFailed)?;
let fragment_shader = Self::create_shader(
ShaderKind::Fragment,
if self.texture.is_some() {
FRAG_SHADER_TEXTURE
} else {
FRAG_SHADER_COLOR
},
)
.map_err(Error::CreateFragmentShaderFailed)?;
let shader_program = ShaderProgram::new();
shader_program.attach(&vertex_shader);
shader_program.attach(&fragment_shader);
shader_program
.link()
.map_err(Error::LinkShaderProgramFailed)?;
let renderable =
Renderable::new(shader_program, &self.vertices, self.indices.as_deref());
let transform = Transform::new();
Ok(Object {
id,
renderable,
transform,
texture: self.texture,
})
}
fn create_shader(kind: ShaderKind, source: &str) -> Result<Shader, ShaderError>
{
let shader = Shader::new(kind);
shader.set_source(source)?;
shader.compile()?;
Ok(shader)
}
}
/// Object error
#[derive(Debug, thiserror::Error)]
pub enum Error
{
#[error("Failed to create vertex shader")]
CreateVertexShaderFailed(#[source] ShaderError),
#[error("Failed to create fragment shader")]
CreateFragmentShaderFailed(#[source] ShaderError),
#[error("Failed to link shader program")]
LinkShaderProgramFailed(#[source] ShaderError),
}
/// Object ID.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Id
{
id: u16,
}
impl Id
{
/// Returns a new object ID.
#[must_use]
pub fn new(id: u16) -> Self
{
Self { id }
}
}
|