//******************************************************************************
///
/// @file core/material/texture.h
///
/// Declarations related to textures.
///
/// @copyright
/// @parblock
///
/// UberPOV Raytracer version 1.37.
/// Portions Copyright 2013-2016 Christoph Lipka.
///
/// UberPOV 1.37 is an experimental unofficial branch of POV-Ray 3.7, and is
/// subject to the same licensing terms and conditions.
///
/// ----------------------------------------------------------------------------
///
/// Persistence of Vision Ray Tracer ('POV-Ray') version 3.7.
/// Copyright 1991-2016 Persistence of Vision Raytracer Pty. Ltd.
///
/// POV-Ray is free software: you can redistribute it and/or modify
/// it under the terms of the GNU Affero General Public License as
/// published by the Free Software Foundation, either version 3 of the
/// License, or (at your option) any later version.
///
/// POV-Ray is distributed in the hope that it will be useful,
/// but WITHOUT ANY WARRANTY; without even the implied warranty of
/// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
/// GNU Affero General Public License for more details.
///
/// You should have received a copy of the GNU Affero General Public License
/// along with this program.  If not, see <http://www.gnu.org/licenses/>.
///
/// ----------------------------------------------------------------------------
///
/// POV-Ray is based on the popular DKB raytracer version 2.12.
/// DKBTrace was originally written by David K. Buck.
/// DKBTrace Ver 2.0-2.12 were written by David K. Buck & Aaron A. Collins.
///
/// @endparblock
///
//******************************************************************************

#ifndef POVRAY_CORE_TEXTURE_H
#define POVRAY_CORE_TEXTURE_H

// Module config header file must be the first file included within POV-Ray unit header files
#include "core/configcore.h"

#include "core/coretypes.h"
#include "core/material/blendmap.h"
#include "core/support/simplevector.h"

namespace pov
{

typedef struct Turb_Struct TURB;
struct GenericTurbulenceWarp;

/*****************************************************************************
* Global preprocessor defines
******************************************************************************/

/*
 * Macro to create random number in the [0; 1] range.
 */

#define FLOOR(x)  ((x) >= 0.0 ? floor(x) : (0.0 - floor(0.0 - (x)) - 1.0))

// Hash1dRTableIndex assumed values in the range 0..8191
#define Hash1dRTableIndex(a,b)   \
    ((hashTable[(int)(a) ^ (b)] & 0xFF) * 2)

// Hash2d assumed values in the range 0..8191
#define Hash2d(a,b)   \
    hashTable[(int)(hashTable[(int)(a)] ^ (b))]

#define Hash3d(a,b,c) \
    hashTable[(int)(hashTable[(int)(hashTable[(int)((a) & 0xfff)] ^ ((b) & 0xfff))] ^ ((c) & 0xfff))]

const int NOISE_MINX = -10000;
const int NOISE_MINY = NOISE_MINX;
const int NOISE_MINZ = NOISE_MINX;


/*****************************************************************************
* Global typedefs
******************************************************************************/

struct WeightedTexture
{
    COLC weight;
    TEXTURE *texture;

    WeightedTexture(COLC w, TEXTURE *t) :
        weight(w), texture(t) { }
};

typedef FixedSimpleVector<WeightedTexture, WEIGHTEDTEXTURE_VECTOR_SIZE> WeightedTextureVector;

typedef TexturePtr  TextureBlendMapData;

/// Texture blend map.
class TextureBlendMap : public BlendMap<TextureBlendMapData>
{
    public:

        TextureBlendMap();
        ~TextureBlendMap();
};

typedef BlendMapEntry<TextureBlendMapData>          TextureBlendMapEntry;
typedef shared_ptr<TextureBlendMap>                 TextureBlendMapPtr;
typedef shared_ptr<const TextureBlendMap>           TextureBlendMapConstPtr;

struct Texture_Struct : public Pattern_Struct
{
    TextureBlendMapPtr Blend_Map;
    int References;
    TEXTURE *Next;
    PIGMENT *Pigment;
    TNORMAL *Tnormal;
    FINISH *Finish;
    vector<TEXTURE*> Materials; // used for BITMAP_PATTERN (and only there)
};

struct Finish_Struct
{
    SNGL Diffuse, DiffuseBack, Brilliance, BrillianceOut, DiffuseAlbedoAdjust, DiffuseAlbedoAdjustRad;

    /// How much of the diffuse contribution should be computed using the Lommel-Seeliger model.
    ///
    /// By default, POV-Ray uses a Lambertian-based diffuse reflectivity model. While this model is useful for
    /// comparatively smooth surfaces, rough surfaces are better simulated using other models, one of the simplest
    /// being the Lommel-Seeliger model. In astronomy, a weighted average of the Lambertian and Lommel-Seeliger models
    /// is frequently used.
    SNGL LommelSeeligerWeight;

    /// @name Minnaert diffuse model parameters.
    ///
    /// By default, POV-Ray uses a Lambertian-based diffuse reflectivity model. While this model is useful for
    /// solid material surfaces, some surfaces like textiles are better simulated using other models, one being the
    /// Minnaert model.
    ///
    /// @{

    /// Exponent of the Minnaert diffuse model.
    ///
    /// This is the value often denoted as @f$ m @f$ or @f$ k @f$ in the Minnaert BRDF formula. Note that the
    /// effective exponent in such formulae is @f$ m-1 @$f.
    ///
    SNGL MinnaertExponent;

    /// @}

    /// @name Oren-Nayar diffuse model parameters.
    ///
    /// By default, POV-Ray uses a Lambertian-based diffuse reflectivity model. While this model is useful for
    /// comparatively smooth surfaces, rough surfaces are better simulated using other models, one of which is the
    /// (simplified) Oren-Nayar model, being a superset of the Lambertian model and governed by the following
    /// parameters.
    ///
    /// @{

    /// Factor A of the simplified Oren-Nayar model, defaulting to 1.0 for the Lambertian model.
    SNGL OrenNayarA;

    /// Factor B of the simplified Oren-Nayar model, defaulting to 0.0 for the Lambertian model.
    SNGL OrenNayarB;

    /// @}

    SNGL Specular, Roughness;
    SNGL Phong, Phong_Size;
    SNGL Irid, Irid_Film_Thickness, Irid_Turb;
    SNGL Temp_Caustics, Temp_IOR, Temp_Dispersion, Temp_Refract, Reflect_Exp, Reflect_Blur;
    unsigned int Reflect_Count;
    SNGL Crand, Metallic;
    AttenuatingColour Ambient;
    LightColour Emission;
    AttenuatingColour Reflection_Max, Reflection_Min;
    PseudoColour SubsurfaceTranslucency, SubsurfaceAnisotropy;
    //PseudoColour SigmaPrimeS, SigmaA;
    SNGL Reflection_Falloff;  // Added by MBP 8/27/98
    bool Reflection_Fresnel;
    bool Fresnel;
    SNGL Reflect_Metallic;  // MBP
    int Conserve_Energy;    // added by NK Dec 19 1999
    bool UseSubsurface;     // whether to use subsurface light transport

    void SetOrenNayarSigma(double sigma)
    {
        SetOrenNayarSigmaSqr(sigma*sigma);
    }
    void SetOrenNayarSigmaSqr(double sigmaSqr)
    {
        OrenNayarA = 1.0 - 0.50 * sigmaSqr / (sigmaSqr + 0.57);
        OrenNayarB =       0.45 * sigmaSqr / (sigmaSqr + 0.09);
    }
};


/*****************************************************************************
* Global variables
******************************************************************************/

#ifdef DYNAMIC_HASHTABLE
extern unsigned short *hashTable;
#else
extern ALIGN16 unsigned short hashTable[8192];
#endif


/*****************************************************************************
* Global functions
******************************************************************************/

void Initialize_Noise (void);
void Initialize_Waves(vector<double>& waveFrequencies, vector<Vector3d>& waveSources, unsigned int numberOfWaves);
void Free_Noise_Tables (void);

DBL SolidNoise(const Vector3d& P);

#if defined(TRY_OPTIMIZED_NOISE)
extern DBL (*Noise) (const Vector3d& EPoint, int noise_generator);
extern void (*DNoise) (Vector3d& result, const Vector3d& EPoint);
void Initialise_NoiseDispatch();
#else
INLINE_NOISE DBL Noise (const Vector3d& EPoint, int noise_generator);
INLINE_NOISE void DNoise (Vector3d& result, const Vector3d& EPoint);
#endif

DBL Turbulence (const Vector3d& EPoint, const GenericTurbulenceWarp* Turb, int noise_generator);
void DTurbulence (Vector3d& result, const Vector3d& EPoint, const GenericTurbulenceWarp* Turb);
DBL cycloidal (DBL value);
DBL Triangle_Wave (DBL value);
void Transform_Textures (TEXTURE *Textures, const TRANSFORM *Trans);
void Destroy_Textures (TEXTURE *Textures);
void Post_Textures (TEXTURE *Textures);
FINISH *Create_Finish (void);
FINISH *Copy_Finish (const FINISH *Old);
TEXTURE *Copy_Texture_Pointer (TEXTURE *Texture);
TEXTURE *Copy_Textures (TEXTURE *Textures);
TEXTURE *Create_Texture (void);
int Test_Opacity (const TEXTURE *Texture);

}

#endif // POVRAY_CORE_TEXTURE_H
