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
#![deny(missing_docs)]
extern crate piston;
extern crate vecmath;
extern crate wavefront_obj;
extern crate image;
#[cfg(any(feature = "dx12", feature = "metal", feature = "vulkan"))]
mod rendy_backend;
#[cfg(not(any(feature = "dx12", feature = "metal", feature = "vulkan")))]
mod gl_backend;
#[cfg(any(feature = "dx12", feature = "metal", feature = "vulkan"))]
pub use rendy_backend::Scene;
#[cfg(not(any(feature = "dx12", feature = "metal", feature = "vulkan")))]
pub use gl_backend::Scene;
use std::path::Path;
use std::io;
use vecmath::*;
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Command {
UseProgram(Program),
SetModelViewProjection(Matrix4Uniform),
SetModel(Matrix4Uniform),
SetView(Matrix4Uniform),
SetTexture(Texture),
SetF32(F32Uniform, f32),
SetVector2(Vector2Uniform, Vector2<f32>),
SetVector3(Vector3Uniform, Vector3<f32>),
SetMatrix4(Matrix4Uniform, Matrix4<f32>),
EnableFrameBufferSRGB,
DisableFrameBufferSRGB,
EnableBlend,
DisableBlend,
EnableCullFace,
DisableCullFace,
CullFaceFront,
CullFaceBack,
CullFaceFrontAndBack,
DrawTriangles(VertexArray, usize),
DrawTriangleStrip(VertexArray, usize),
DrawLines(VertexArray, usize),
DrawPoints(VertexArray, usize),
Translate(Vector3<f32>),
TranslateGlobal(Vector3<f32>),
Scale(Vector3<f32>),
RotateXDeg(f32),
RotateYDeg(f32),
RotateZDeg(f32),
RotateAxisDeg(Vector3<f32>, f32),
PushTransform,
PopTransform,
Draw(CommandList),
}
#[derive(Debug)]
pub struct FrameGraph {
command_lists: Vec<Vec<Command>>,
}
impl FrameGraph {
pub fn new() -> FrameGraph {
FrameGraph {
command_lists: vec![]
}
}
pub fn command_list(&mut self, commands: Vec<Command>) -> CommandList {
let id = self.command_lists.len();
self.command_lists.push(commands);
CommandList(id)
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct VertexShader(usize);
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct FragmentShader(usize);
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct Program(usize);
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct Matrix4Uniform(usize);
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct Vector2Uniform(usize);
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct Vector3Uniform(usize);
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct F32Uniform(usize);
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct VertexArray(usize);
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct ColorBuffer(usize, usize);
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct VertexBuffer3(usize, usize);
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct VertexBuffer2(usize, usize);
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct UVBuffer(usize, usize);
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct NormalBuffer(usize, usize);
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct CommandList(usize);
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct Texture(usize);
impl ColorBuffer {
pub fn len(&self) -> usize {self.1}
}
impl VertexBuffer3 {
pub fn len(&self) -> usize {self.1}
}
impl VertexBuffer2 {
pub fn len(&self) -> usize {self.1}
}
pub struct ObjMesh {
pub vertices: Vec<f32>,
pub uvs: Vec<f32>,
pub normals: Vec<f32>,
}
impl ObjMesh {
pub fn load<P: AsRef<Path>>(path: P) -> Result<ObjMesh, io::Error> {
use std::fs::File;
use std::io::Read;
let mut obj_file = File::open(path)?;
let mut data = String::new();
obj_file.read_to_string(&mut data)?;
let obj_set = wavefront_obj::obj::parse(data).unwrap();
let obj = &obj_set.objects[0];
let temp_vertices = {
let mut res = vec![];
for v in &obj.vertices {
res.push(v.x as f32);
res.push(v.y as f32);
res.push(v.z as f32);
}
res
};
let temp_uvs = {
let mut res = vec![];
for uv in &obj.tex_vertices {
res.push(uv.u as f32);
res.push(1.0 - uv.v as f32);
}
res
};
let temp_normals = {
let mut res = vec![];
for normal in &obj.normals {
res.push(normal.x as gl::types::GLfloat);
res.push(normal.y as gl::types::GLfloat);
res.push(normal.z as gl::types::GLfloat);
}
res
};
let mut vertices = vec![];
let mut uvs = vec![];
let mut normals = vec![];
for geom in &obj.geometry {
for shape in &geom.shapes {
use wavefront_obj::obj::Primitive;
if let Primitive::Triangle(
(a_v, Some(a_uv), Some(a_n)),
(b_v, Some(b_uv), Some(b_n)),
(c_v, Some(c_uv), Some(c_n))
) = shape.primitive {
vertices.push(temp_vertices[a_v * 3 + 0]);
vertices.push(temp_vertices[a_v * 3 + 1]);
vertices.push(temp_vertices[a_v * 3 + 2]);
vertices.push(temp_vertices[b_v * 3 + 0]);
vertices.push(temp_vertices[b_v * 3 + 1]);
vertices.push(temp_vertices[b_v * 3 + 2]);
vertices.push(temp_vertices[c_v * 3 + 0]);
vertices.push(temp_vertices[c_v * 3 + 1]);
vertices.push(temp_vertices[c_v * 3 + 2]);
uvs.push(temp_uvs[a_uv * 2 + 0]);
uvs.push(temp_uvs[a_uv * 2 + 1]);
uvs.push(temp_uvs[b_uv * 2 + 0]);
uvs.push(temp_uvs[b_uv * 2 + 1]);
uvs.push(temp_uvs[c_uv * 2 + 0]);
uvs.push(temp_uvs[c_uv * 2 + 1]);
normals.push(temp_normals[a_n * 3 + 0]);
normals.push(temp_normals[a_n * 3 + 1]);
normals.push(temp_normals[a_n * 3 + 2]);
normals.push(temp_normals[b_n * 3 + 0]);
normals.push(temp_normals[b_n * 3 + 1]);
normals.push(temp_normals[b_n * 3 + 2]);
normals.push(temp_normals[c_n * 3 + 0]);
normals.push(temp_normals[c_n * 3 + 1]);
normals.push(temp_normals[c_n * 3 + 2]);
}
}
}
Ok(ObjMesh {
vertices,
uvs,
normals
})
}
}
#[derive(Clone)]
pub struct SceneSettings {
clear_depth_buffer: bool,
clear_enable_depth_test: bool,
}
impl SceneSettings {
pub fn new() -> SceneSettings {
SceneSettings {
clear_depth_buffer: true,
clear_enable_depth_test: true,
}
}
pub fn clear_depth_buffer(mut self, val: bool) -> Self {
self.clear_depth_buffer = val;
self
}
pub fn clear_enable_depth_test(mut self, val: bool) -> Self {
self.clear_enable_depth_test = val;
self
}
}
impl Default for SceneSettings {
fn default() -> Self {SceneSettings::new()}
}
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
}