How to chain different type of objects for a render call #291
-
Is it possible to have a number of objects rendered, if the number of objects is not known at compile-time? Essentially I want the user to be able to load in a series of 3D coordinates, and then the application to display them as basic objects (spheres, cubes, etc), but I cannot find a good way of storing these objects to pass to the let mut marker_objects = Vec::new();
for marker in marker_locations.iter(){
let mut user_object = Gm::new(
Mesh::new(&context, &CpuMesh::sphere(16)),
PhysicalMaterial::new_transparent(
&context,
&CpuMaterial {
albedo: Color {
r: 0,
g: 255,
b: 0,
a: 200,
},
..Default::default()
},
),
);
user_object.set_transformation(Mat4::from_translation(vec3(
*marker.XMetres,
*marker.YMetres,
*marker.ZMetres
));
marker_objects.push(user_object);
}
\\ ... \\
window.render_loop(move |mut frame_input: FrameInput| {
camera.set_viewport(frame_input.viewport);
control.handle_events(&mut camera, &mut frame_input.events);
frame_input
.screen()
.clear(ClearState::color_and_depth(0.8, 0.8, 0.8, 1.0, 1.0))
.render(
&camera,
marker_objects.iter()
.chain(&axes), //! Can't do this, so how do I chain the axis (which is a different type of object)?
&[&light0, &light1],
);
FrameOutput::default()
}); In the shapes example project, there is an `ObjectIterator' thatit commented out, but I can't fund any reference to it in the main code or the documentation: //let i: ObjectIterator = (&axes as &dyn Object).into(); Any suggestions on how to dynamically create objects at runtime and add them to the render? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I understand your confusion! As you say So you might ask why something like Hope it makes sense 🙂 Also
was a mistake, an experiment that I forgot to remove, but now it is 💪 |
Beta Was this translation helpful? Give feedback.
I understand your confusion! As you say
Axes
andGm
are different types, however they both implement theObject
trait and an iterator overObject
s is exactly what is needed as the input to the render functions. Unfortunately, Rust does not automatically convert a&Gm
or&Axes
to a&dyn Object
even in this case where it's obviously what is needed. Therefore we need to do that manually so instead ofmarker_objects.iter().chain(&axes)
it should bemarker_objects.iter().map(|o| o as &dyn Object).chain(&axes)
. I hope Rust will implement an automatic conversion in the future, but this is how it works right now.So you might ask why something like
marker_objects[0].into_iter().chain(&marker_obje…