Converting PBR metallic data to the realistic material

REDsdk provides a helper function to convert data from a PBR metallic workflow into its realistic material.

Here is how to use it:

// Create the images.
RED::Object *diffuse, *reflection, *anisotropy, *ior;
RC_TEST( iresmgr->CreateImage2D( diffuse, iresmgr->GetState() ) );
RC_TEST( iresmgr->CreateImage2D( reflection, iresmgr->GetState() ) );
RC_TEST( iresmgr->CreateImage2D( anisotropy, iresmgr->GetState() ) );
RC_TEST( iresmgr->CreateImage2D( ior, iresmgr->GetState() ) );

RED::Object *albedo, *roughness, *metalness, *normal;
RC_TEST( iresmgr->CreateImage2D( albedo, iresmgr->GetState() ) );
RC_TEST( iresmgr->CreateImage2D( roughness, iresmgr->GetState() ) );
RC_TEST( iresmgr->CreateImage2D( metalness, iresmgr->GetState() ) );
RC_TEST( iresmgr->CreateImage2D( normal, iresmgr->GetState() ) );

// Load the images. Use the local storage.
RC_TEST( RED::ImageTools::Load( albedo, "albedo.png", RED::FMT_RGB, true, true, RED::TGT_TEX_2D, iresmgr->GetState() ) );
RC_TEST( RED::ImageTools::Load( roughness, "roughness.png", RED::FMT_FLOAT, true, true, RED::TGT_TEX_2D, iresmgr->GetState() ) );
RC_TEST( RED::ImageTools::Load( metalness, "metalness.png", RED::FMT_FLOAT, true, true, RED::TGT_TEX_2D, iresmgr->GetState() ) );
RC_TEST( RED::ImageTools::Load( normal, "normal.png", RED::FMT_RGB, true, true, RED::TGT_TEX_2D, iresmgr->GetState() ) );

// Convert metallic workflow images into realistic material data.
RC_TEST( RED::ImageTools::ConvertMetallicToRealisticMaterial( diffuse, 2048, 2048,        // Returned diffuse
                                                              reflection, 2048, 2048,     // Returned reflection
                                                              anisotropy, 2048, 2048,     // Returned anisotropy
                                                              ior, 2048, 2048,            // Returned IOR
                                                              NULL, 0,                    // No environment map.
                                                              albedo, RED::Color::WHITE,  // PBR Albedo
                                                              roughness, 1.f,             // PBR roughness
                                                              metalness, 1.f,             // PBR metallic
                                                              NULL, 1.f,                  // No PBR ambient occlusion
                                                              NULL, 0.5f, false,          // No alpha mask
                                                              NULL,                       // No environment texture
                                                              resmgr, iresmgr->GetState() ) );

// The convert function works in local storage, don't forget RED::IImage2D::SetPixels.
RC_TEST( diffuse->As< RED::IImage2D >()->SetPixels( RED::TGT_TEX_2D, iresmgr->GetState() ) );
RC_TEST( reflection->As< RED::IImage2D >()->SetPixels( RED::TGT_TEX_2D, iresmgr->GetState() ) );
RC_TEST( anisotropy->As< RED::IImage2D >()->SetPixels( RED::TGT_TEX_2D, iresmgr->GetState() ) );
RC_TEST( ior->As< RED::IImage2D >()->SetPixels( RED::TGT_TEX_2D, iresmgr->GetState() ) );
RC_TEST( normal->As< RED::IImage2D >()->SetPixels( RED::TGT_TEX_2D, iresmgr->GetState() ) );

// Use the textures in the realistic material: diffuse, reflection, anisotropy and normal.
// Use the returned IOR and set Fresnel on.
RC_TEST( imaterial->SetupRealisticMaterial(
  false,                                                                // Double sided
  true,                                                                 // Fresnel
  RED::Color::RED, diffuse, RED::Matrix::IDENTITY, RED::MCL_TEX0,       // Diffusion
  RED::Color::WHITE, reflection, RED::Matrix::IDENTITY, RED::MCL_TEX0,  // Reflection
  RED::Color::WHITE, FLT_MAX,                                           // Reflection fog
  false, false, NULL, RED::Matrix::IDENTITY,                            // Environment
  RED::Color::BLACK, NULL, RED::Matrix::IDENTITY, RED::MCL_TEX0,        // Transmission
  0.0f, NULL, RED::Matrix::IDENTITY, RED::MCL_TEX0,                     // Transmission glossiness
  1.0f, ior, RED::Matrix::IDENTITY, RED::MCL_TEX0,                      // IOR
  RED::Color::WHITE, 1.0f,                                              // Transmission scattering
  false, false,                                                         // Caustics
  RED::Color::BLACK, anisotropy, RED::Matrix::IDENTITY, RED::MCL_TEX0,  // Reflection anisotropy
  0.0f, NULL, RED::Matrix::IDENTITY, RED::MCL_TEX0,                     // Reflection anisotropy orientation
  normal, RED::Matrix::IDENTITY, RED::MCL_TEX0, RED::MCL_USER0,         // Bump
  0.0f, 0.0f, NULL, RED::Matrix::IDENTITY, RED::MCL_TEX0,               // Displacement
  NULL, &RED::LayerSet::ALL_LAYERS,                                     // Layersets
  resmgr, iresmgr->GetState() ) );                                      // Engine stuff

In this sample, neither AO texture nor environment texture are used. They can be set to NULL.