-
-
Notifications
You must be signed in to change notification settings - Fork 58
/
mod.rs
200 lines (177 loc) · 7.18 KB
/
mod.rs
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
fn vertex(pos: [f32; 3]) -> glam::Vec3 {
glam::Vec3::from(pos)
}
fn create_mesh() -> rend3::types::Mesh {
let vertex_positions = [
// far side (0.0, 0.0, 1.0)
vertex([-1.0, -1.0, 1.0]),
vertex([1.0, -1.0, 1.0]),
vertex([1.0, 1.0, 1.0]),
vertex([-1.0, 1.0, 1.0]),
// near side (0.0, 0.0, -1.0)
vertex([-1.0, 1.0, -1.0]),
vertex([1.0, 1.0, -1.0]),
vertex([1.0, -1.0, -1.0]),
vertex([-1.0, -1.0, -1.0]),
// right side (1.0, 0.0, 0.0)
vertex([1.0, -1.0, -1.0]),
vertex([1.0, 1.0, -1.0]),
vertex([1.0, 1.0, 1.0]),
vertex([1.0, -1.0, 1.0]),
// left side (-1.0, 0.0, 0.0)
vertex([-1.0, -1.0, 1.0]),
vertex([-1.0, 1.0, 1.0]),
vertex([-1.0, 1.0, -1.0]),
vertex([-1.0, -1.0, -1.0]),
// top (0.0, 1.0, 0.0)
vertex([1.0, 1.0, -1.0]),
vertex([-1.0, 1.0, -1.0]),
vertex([-1.0, 1.0, 1.0]),
vertex([1.0, 1.0, 1.0]),
// bottom (0.0, -1.0, 0.0)
vertex([1.0, -1.0, 1.0]),
vertex([-1.0, -1.0, 1.0]),
vertex([-1.0, -1.0, -1.0]),
vertex([1.0, -1.0, -1.0]),
];
let index_data: &[u32] = &[
0, 1, 2, 2, 3, 0, // far
4, 5, 6, 6, 7, 4, // near
8, 9, 10, 10, 11, 8, // right
12, 13, 14, 14, 15, 12, // left
16, 17, 18, 18, 19, 16, // top
20, 21, 22, 22, 23, 20, // bottom
];
rend3::types::MeshBuilder::new(vertex_positions.to_vec(), rend3::types::Handedness::Left)
.with_indices(index_data.to_vec())
.build()
.unwrap()
}
const SAMPLE_COUNT: rend3::types::SampleCount = rend3::types::SampleCount::One;
#[derive(Default)]
pub struct CubeExample {
object_handle: Option<rend3::types::ObjectHandle>,
directional_light_handle: Option<rend3::types::DirectionalLightHandle>,
point_lights: Vec<rend3::types::PointLightHandle>,
}
impl rend3_framework::App for CubeExample {
const HANDEDNESS: rend3::types::Handedness = rend3::types::Handedness::Left;
fn sample_count(&self) -> rend3::types::SampleCount {
SAMPLE_COUNT
}
fn setup(&mut self, context: rend3_framework::SetupContext<'_>) {
// Create mesh and calculate smooth normals based on vertices
let mesh = create_mesh();
// Add mesh to renderer's world.
//
// All handles are refcounted, so we only need to hang onto the handle until we
// make an object.
let mesh_handle = context.renderer.add_mesh(mesh).unwrap();
// Add PBR material with all defaults except a single color.
let material = rend3_routine::pbr::PbrMaterial {
albedo: rend3_routine::pbr::AlbedoComponent::Value(glam::Vec4::new(0.5, 0.5, 0.5, 1.0)),
..rend3_routine::pbr::PbrMaterial::default()
};
let material_handle = context.renderer.add_material(material);
// Combine the mesh and the material with a location to give an object.
let object = rend3::types::Object {
mesh_kind: rend3::types::ObjectMeshKind::Static(mesh_handle),
material: material_handle,
transform: glam::Mat4::IDENTITY,
};
// Creating an object will hold onto both the mesh and the material
// even if they are deleted.
//
// We need to keep the object handle alive.
self.object_handle = Some(context.renderer.add_object(object));
let view_location = glam::Vec3::new(3.0, 3.0, -5.0);
let view = glam::Mat4::from_euler(glam::EulerRot::XYZ, -0.55, 0.5, 0.0);
let view = view * glam::Mat4::from_translation(-view_location);
// Set camera's location
context.renderer.set_camera_data(rend3::types::Camera {
projection: rend3::types::CameraProjection::Perspective { vfov: 60.0, near: 0.1 },
view,
});
// Create a single directional light
//
// We need to keep the directional light handle alive.
self.directional_light_handle = Some(context.renderer.add_directional_light(rend3::types::DirectionalLight {
color: glam::Vec3::ONE,
intensity: 1.0,
// Direction will be normalized
direction: glam::Vec3::new(-1.0, -4.0, 2.0),
distance: 400.0,
resolution: 2048,
}));
let lights = [
// position, color
(glam::vec3(0.1, 1.2, -1.5), glam::vec3(1.0, 0.0, 0.0)),
(glam::vec3(1.5, 1.2, -0.1), glam::vec3(0.0, 1.0, 0.0)),
];
for (position, color) in lights {
self.point_lights.push(context.renderer.add_point_light(rend3::types::PointLight {
position,
color,
radius: 2.0,
intensity: 4.0,
}));
}
}
fn handle_redraw(&mut self, context: rend3_framework::RedrawContext<'_, ()>) {
// Swap the instruction buffers so that our frame's changes can be processed.
context.renderer.swap_instruction_buffers();
// Evaluate our frame's world-change instructions
let mut eval_output = context.renderer.evaluate_instructions();
// Lock the routines
let pbr_routine = rend3_framework::lock(&context.routines.pbr);
let tonemapping_routine = rend3_framework::lock(&context.routines.tonemapping);
// Build a rendergraph
let mut graph = rend3::graph::RenderGraph::new();
// Import the surface texture into the render graph.
let frame_handle = graph.add_imported_render_target(
context.surface_texture,
0..1,
0..1,
rend3::graph::ViewportRect::from_size(context.resolution),
);
// Add the default rendergraph without a skybox
context.base_rendergraph.add_to_graph(
&mut graph,
rend3_routine::base::BaseRenderGraphInputs {
eval_output: &eval_output,
routines: rend3_routine::base::BaseRenderGraphRoutines {
pbr: &pbr_routine,
skybox: None,
tonemapping: &tonemapping_routine,
},
target: rend3_routine::base::OutputRenderTarget {
handle: frame_handle,
resolution: context.resolution,
samples: SAMPLE_COUNT,
},
},
rend3_routine::base::BaseRenderGraphSettings {
ambient_color: glam::Vec4::ZERO,
clear_color: glam::Vec4::new(0.10, 0.05, 0.10, 1.0), // Nice scene-referred purple
},
);
// Dispatch a render using the built up rendergraph!
graph.execute(context.renderer, &mut eval_output);
}
}
pub fn main() {
let app = CubeExample::default();
rend3_framework::start(app, winit::window::WindowBuilder::new().with_title("cube-example").with_maximized(true));
}
#[cfg(test)]
#[rend3_test::test_attr]
async fn test() {
crate::tests::test_app(crate::tests::TestConfiguration {
app: CubeExample::default(),
reference_path: "src/cube/screenshot.png",
size: glam::UVec2::new(1280, 720),
threshold_set: rend3_test::Threshold::Mean(0.01).into(),
})
.await
.unwrap();
}