public static Vector3[,,] LoadBRDF( FileInfo _BRDFFile )
{
Vector3[,,] Result = null;
try
{
using ( FileStream S = _BRDFFile.OpenRead() )
using ( BinaryReader Reader = new BinaryReader( S ) )
{
// Check coefficients count is the expected value
int DimX = Reader.ReadInt32();
int DimY = Reader.ReadInt32();
int DimZ = Reader.ReadInt32();
int CoeffsCount = DimX*DimY*DimZ;
if ( CoeffsCount != BRDF_SAMPLING_RES_THETA_H*BRDF_SAMPLING_RES_THETA_D*BRDF_SAMPLING_RES_PHI_D/2 )
throw new Exception( "The amount of coefficients stored in the file is not the expected value (i.e. " + CoeffsCount + "! (is it a BRDF file?)" );
// Allocate the R,G,B arrays
Result = new Vector3[DimX,DimY,DimZ];
int PitchThetaD = BRDF_SAMPLING_RES_PHI_D/2;
int PitchThetaH = PitchThetaD*BRDF_SAMPLING_RES_THETA_D;
// Read content
int[] NegativeValuesCount = new int[3] { 0, 0, 0 };
Vector3 MinValues = new Vector3() { x=double.MaxValue, y=double.MaxValue, z=double.MaxValue };
for ( int ComponentIndex=0; ComponentIndex < 3; ComponentIndex++ )
{
double Factor = 1.0;
if ( ComponentIndex == 0 )
Factor = BRDF_SCALE_RED;
else if ( ComponentIndex == 1 )
Factor = BRDF_SCALE_GREEN;
else
Factor = BRDF_SCALE_BLUE;
for ( int CoeffIndex=0; CoeffIndex < CoeffsCount; CoeffIndex++ )
{
int Temp = CoeffIndex;
int ThetaH = Temp / PitchThetaH;
Temp -= ThetaH * PitchThetaH;
int ThetaD = Temp / PitchThetaD;
Temp -= ThetaD * PitchThetaD;
int PhiD = Temp;
double Value = Factor * Reader.ReadDouble();
// Result[ThetaH,ThetaD,PhiD] = V = new Vector3();
Result[ThetaH,ThetaD,PhiD][ComponentIndex] = Value;
if ( Value < 0.0 )
NegativeValuesCount[ComponentIndex]++;
MinValues[ComponentIndex] = Math.Min( MinValues[ComponentIndex], Value );
}
}
}
}
catch ( Exception _e )
{ // Forward...
throw new Exception( "Failed to load source BRDF file: " + _e.Message );
}
return Result;
}