@@ -332,6 +332,7 @@ SimpleRaytracer::parse_scene_xml(const std::string& scenefile)
332332 if (fov_attr)
333333 fov = OIIO ::Strutil::from_string<float >(fov_attr.value ());
334334
335+ m_fov = fov;
335336 camera.lookat (eye, dir, up, fov);
336337 } else if (strcmp (node.name (), " Sphere" ) == 0 ) {
337338 // load sphere
@@ -506,6 +507,60 @@ SimpleRaytracer::parse_scene_xml(const std::string& scenefile)
506507 if (scene.num_prims () == 0 )
507508 errhandler ().severefmt (" No primitives in scene" );
508509 camera.finalize ();
510+ prepare_camera_spaces ();
511+ }
512+
513+
514+
515+ void
516+ SimpleRaytracer::prepare_camera_spaces ()
517+ {
518+ Vec3 right = camera.cx .normalized ();
519+ Vec3 up = camera.cy .normalized ();
520+
521+ Matrix44 camera_to_world = {right.x , right.y , right.z , 0 ,
522+ up.x , up.y , up.z , 0 ,
523+ camera.dir .x , camera.dir .y , camera.dir .z , 0 ,
524+ camera.eye .x , camera.eye .y , camera.eye .z , 1 };
525+
526+ name_transform (" camera" , camera_to_world );
527+
528+ // Seems never used once moved to named transforms
529+ m_world_to_camera = camera_to_world.inverse ();
530+ Matrix44 M = m_world_to_camera;
531+ float depthrange = (double )m_yon-(double )m_hither;
532+ if (m_projection == RS ::Hashes::perspective) {
533+ float tanhalffov = tanf (0 .5f * m_fov * M_PI /180.0 );
534+ Matrix44 camera_to_screen (1 /tanhalffov, 0 , 0 , 0 ,
535+ 0 , 1 /tanhalffov, 0 , 0 ,
536+ 0 , 0 , m_yon/depthrange, 1 ,
537+ 0 , 0 , -m_yon*m_hither/depthrange, 0 );
538+ M = M * camera_to_screen;
539+ } else {
540+ Matrix44 camera_to_screen (1 , 0 , 0 , 0 ,
541+ 0 , 1 , 0 , 0 ,
542+ 0 , 0 , 1 /depthrange, 0 ,
543+ 0 , 0 , -m_hither/depthrange, 1 );
544+ M = M * camera_to_screen;
545+ }
546+ name_transform (" screen" , M.inverse () );
547+
548+ float aspect = (float )camera.yres / (float )camera.xres ;
549+ float screenleft = -1.0 , screenwidth = 2.0 ;
550+ float screenbottom = -1.0 *aspect, screenheight = 2.0 *aspect;
551+ Matrix44 screen_to_ndc (1 /screenwidth, 0 , 0 , 0 ,
552+ 0 , 1 /screenheight, 0 , 0 ,
553+ 0 , 0 , 1 , 0 ,
554+ -screenleft/screenwidth, -screenbottom/screenheight, 0 , 1 );
555+ M = M * screen_to_ndc;
556+ name_transform (" NDC" , M.inverse () );
557+
558+ Matrix44 ndc_to_raster (camera.xres , 0 , 0 , 0 ,
559+ 0 , camera.yres , 0 , 0 ,
560+ 0 , 0 , 1 , 0 ,
561+ 0 , 0 , 0 , 1 );
562+ M = M * ndc_to_raster;
563+ name_transform (" raster" , M.inverse () );
509564}
510565
511566
@@ -577,48 +632,6 @@ bool
577632SimpleRaytracer::get_inverse_matrix (ShaderGlobals* /* sg*/ , Matrix44& result,
578633 ustringhash to, float /* time*/ )
579634{
580- if (to == OSL ::Hashes::camera || to == OSL ::Hashes::screen
581- || to == OSL ::Hashes::NDC || to == RS ::Hashes::raster) {
582- // clang-format off
583- Matrix44 M = m_world_to_camera;
584- if (to == OSL ::Hashes::screen || to == OSL ::Hashes::NDC || to == RS ::Hashes::raster) {
585- float depthrange = (double )m_yon-(double )m_hither;
586- if (m_projection == RS ::Hashes::perspective) {
587- float tanhalffov = tanf (0 .5f * m_fov * M_PI /180.0 );
588- Matrix44 camera_to_screen (1 /tanhalffov, 0 , 0 , 0 ,
589- 0 , 1 /tanhalffov, 0 , 0 ,
590- 0 , 0 , m_yon/depthrange, 1 ,
591- 0 , 0 , -m_yon*m_hither/depthrange, 0 );
592- M = M * camera_to_screen;
593- } else {
594- Matrix44 camera_to_screen (1 , 0 , 0 , 0 ,
595- 0 , 1 , 0 , 0 ,
596- 0 , 0 , 1 /depthrange, 0 ,
597- 0 , 0 , -m_hither/depthrange, 1 );
598- M = M * camera_to_screen;
599- }
600- if (to == OSL ::Hashes::NDC || to == RS ::Hashes::raster) {
601- float screenleft = -1.0 , screenwidth = 2.0 ;
602- float screenbottom = -1.0 , screenheight = 2.0 ;
603- Matrix44 screen_to_ndc (1 /screenwidth, 0 , 0 , 0 ,
604- 0 , 1 /screenheight, 0 , 0 ,
605- 0 , 0 , 1 , 0 ,
606- -screenleft/screenwidth, -screenbottom/screenheight, 0 , 1 );
607- M = M * screen_to_ndc;
608- if (to == RS ::Hashes::raster) {
609- Matrix44 ndc_to_raster (camera.xres , 0 , 0 , 0 ,
610- 0 , camera.yres , 0 , 0 ,
611- 0 , 0 , 1 , 0 ,
612- 0 , 0 , 0 , 1 );
613- M = M * ndc_to_raster;
614- }
615- }
616- }
617- // clang-format on
618- result = M;
619- return true ;
620- }
621-
622635 TransformMap::const_iterator found = m_named_xforms.find (to);
623636 if (found != m_named_xforms.end ()) {
624637 result = *(found->second );
0 commit comments