Contribute to smukherjee2016/sayo_pbr_rs development by creating an account on GitHub.

However, I have hit a roadblock on how to properly do dynamic dispatch and polymorphism.

In camera/mod.rs, I have the base Trait that has two functions, new() and generate_camera_ray(), and a struct PinholeCamera that implements them. In src/lib.rs, I parse the scene file and look for the camera type, and I need to call the corresponding Camera. I did look up the trait objects section of the book, however, I cannot get my code to compile. That brings me to have several questions:

Is this the proper way to do dynamic dispatch? Most likely no, but a hint in the right direction would be highly appreciated.

In src/lib.rs, I have a match block that I use to construct the camera object, before storing it in SceneConfig. Is variable shadowing the idiomatic way to do this? (Since the camera object will not be living outside the match block…)

Looking at your code, your problem has nothing to do with trait objects and dynamic dispatch.

Rather than get fancy with the match statement, which initializes the camera exactly the same way in both branches, you can factor that out.
a. (You also made a new binding for camera, which shadowed the old binding, this meant that you didn’t initialize the camera in all cases which led to the error later on that camera was possibly uninitialized).

Then you can replace the match statement with an if, and print out the warning inside the if.

You will also have to clone scene_filename, because you are using later after you move it.

A little sleep later, I realized that I didn’t need to initialize the base pointer. For whatever reason, I was under the impression that whenever I declare a variable, I have to assign it as well… But in this case just doing,