shun-iwasawa 832993
#pragma once
shun-iwasawa 832993
shun-iwasawa 832993
#ifndef IWA_FLOORBUMPFX_H
shun-iwasawa 832993
#define IWA_FLOORBUMPFX_H
shun-iwasawa 832993
shun-iwasawa 832993
#include "tfxparam.h"
shun-iwasawa 832993
#include "stdfx.h"
shun-iwasawa 832993
#include "tparamset.h"
shun-iwasawa 832993
shun-iwasawa 832993
#include <qvector3d></qvector3d>
shun-iwasawa 832993
struct float4 {
shun-iwasawa 832993
  float x, y, z, w;
shun-iwasawa 832993
  float4 operator*(const float &v) const {
shun-iwasawa 832993
    return float4{x * v, y * v, z * v, w * v};
shun-iwasawa 832993
  }
shun-iwasawa 832993
  float4 &operator+=(const float4 &v) {
shun-iwasawa 832993
    x += v.x;
shun-iwasawa 832993
    y += v.y;
shun-iwasawa 832993
    z += v.z;
shun-iwasawa 832993
    w += v.w;
shun-iwasawa 832993
    return *this;
shun-iwasawa 832993
  }
shun-iwasawa 832993
};
shun-iwasawa 832993
shun-iwasawa 832993
class Iwa_FloorBumpFx final : public TStandardRasterFx {
shun-iwasawa 832993
  FX_PLUGIN_DECLARATION(Iwa_FloorBumpFx)
shun-iwasawa 832993
public:
shun-iwasawa 832993
  enum RenderMode {
shun-iwasawa 832993
    TextureMode = 0,
shun-iwasawa 832993
    DiffuseMode,
shun-iwasawa 832993
    SpecularMode,
shun-iwasawa 832993
    FresnelMode,
shun-iwasawa 832993
    RefractionMode,
shun-iwasawa 832993
    ReflectionMode
shun-iwasawa 832993
  };
shun-iwasawa 832993
shun-iwasawa 832993
  struct FloorBumpVars {
shun-iwasawa 832993
    double waveHeight;
shun-iwasawa 832993
    double displacement;
shun-iwasawa 832993
    int refHeight;
shun-iwasawa 832993
    TDimensionI outDim;
shun-iwasawa 832993
    TDimensionI resultDim;
shun-iwasawa 832993
    int margin;
shun-iwasawa 832993
    double precision;
shun-iwasawa 832993
    // add margins to all ends and multiply by precision value
shun-iwasawa 832993
    TDimensionI sourceDim;  // u
shun-iwasawa 832993
    // only add margins for height image
shun-iwasawa 832993
    TDimensionI refDim;  // u
shun-iwasawa 832993
shun-iwasawa 832993
    // collecting parameters
shun-iwasawa 832993
    double textureOffsetAmount;  // u
shun-iwasawa 832993
    double spread;               // u
shun-iwasawa 832993
    double camAltitude;
shun-iwasawa 832993
    int renderMode;       // u
shun-iwasawa 832993
    bool differenceMode;  // u
shun-iwasawa 832993
shun-iwasawa 832993
    // making pixels in gray128 to be zero level height
shun-iwasawa 832993
    // ( 128/255. IT'S NOT 0.5! )
shun-iwasawa 832993
    double zeroLevel;  // u
shun-iwasawa 832993
    double H;          // u
shun-iwasawa 832993
    double W;          // u
shun-iwasawa 832993
    // angle between the optical axis and the horizontal axis
shun-iwasawa 832993
    double angle_el;  // u
shun-iwasawa 832993
    // Y coordinate of the Eye position (P)
shun-iwasawa 832993
    double P_y;  // u
shun-iwasawa 832993
    // distance from the Eye (P) to the center of the projection plane (T)
shun-iwasawa 832993
    double d_PT;  // u
shun-iwasawa 832993
shun-iwasawa 832993
    // Z-Y position of the center of top edge of the projection plane (A)
shun-iwasawa 832993
    QPointF A;  // u
shun-iwasawa 832993
    // Z-Y position of the center of bottom edge of the projection plane (B)
shun-iwasawa 832993
    QPointF B;  // u
shun-iwasawa 832993
shun-iwasawa 832993
    // (C) is an intersection between the XZ plane and the line P->B
shun-iwasawa 832993
    double C_z;               // u
shun-iwasawa 832993
    QVector3D sunVec;         // u
shun-iwasawa 832993
    double base_fresnel_ref;  // u
shun-iwasawa 832993
    double depth, r_index;    // uu
shun-iwasawa 832993
    double distance;          // u
shun-iwasawa 832993
    QVector3D eyePos;         // u
shun-iwasawa 832993
  };
shun-iwasawa 832993
shun-iwasawa 832993
protected:
shun-iwasawa 832993
  TRasterFxPort m_heightRef;  // height reference image
shun-iwasawa 832993
  TRasterFxPort m_texture;    // texture image
shun-iwasawa 832993
  TRasterFxPort m_dispRef;    // displacement image
shun-iwasawa 832993
shun-iwasawa 832993
  TIntEnumParamP m_renderMode;
shun-iwasawa 832993
shun-iwasawa 832993
  TDoubleParamP m_fov;  // camera fov (degrees)
shun-iwasawa 832993
  TDoubleParamP
shun-iwasawa 832993
      m_cameraAltitude;  // height of the bottom edge of projection plane
shun-iwasawa 832993
shun-iwasawa 832993
  TDoubleParamP m_eyeLevel;   // height of the vanishing point
shun-iwasawa 832993
  TDoubleParamP m_drawLevel;  // upper rendering boundary
shun-iwasawa 832993
shun-iwasawa 832993
  TDoubleParamP m_waveHeight;  // height of waves to the both sides (i.e.
shun-iwasawa 832993
                               // amplitude becomes 2*waveHeight)
shun-iwasawa 832993
shun-iwasawa 832993
  TBoolParamP
shun-iwasawa 832993
      m_differenceMode;  // available in diffuse and fresnel mode,
shun-iwasawa 832993
                         // render brightness difference from unbumped state
shun-iwasawa 832993
shun-iwasawa 832993
  // Texture mode parameters
shun-iwasawa 832993
  TDoubleParamP m_textureOffsetAmount;  // amount of texture trailing along with
shun-iwasawa 832993
                                        // gradient of the bump
shun-iwasawa 832993
  TDoubleParamP
shun-iwasawa 832993
      m_textureOffsetSpread;  // adding "blur" to the gradient distribution
shun-iwasawa 832993
shun-iwasawa 832993
  TDoubleParamP m_sourcePrecision;  // to load the texture with higher dpi
shun-iwasawa 832993
  TDoubleParamP m_souceMargin;  // margins to be added to all edges for both the
shun-iwasawa 832993
                                // height reference and the texture images
shun-iwasawa 832993
shun-iwasawa 832993
  TDoubleParamP m_displacement;
shun-iwasawa 832993
shun-iwasawa 832993
  // Shading (Diffuse and Specular) modes parameters
shun-iwasawa 832993
  TDoubleParamP m_lightAzimuth;  // light is in front of camera with azimuth=0.
shun-iwasawa 832993
                                 // clockwise angle (degrees)
shun-iwasawa 832993
  TDoubleParamP m_lightElevation;  // (degrees)
shun-iwasawa 832993
shun-iwasawa 832993
  // Refraction mode parameters
shun-iwasawa 832993
  TDoubleParamP m_depth;  // water depth. the bottom will be placed at -depth
shun-iwasawa 832993
  TDoubleParamP
shun-iwasawa 832993
      m_refractiveIndex;  // refractive index of the medium under the surface
shun-iwasawa 832993
shun-iwasawa 832993
  // Reflection mode parameter
shun-iwasawa 832993
  TDoubleParamP m_distanceLevel;  // the distance of the reflected object
shun-iwasawa 832993
                                  // specified by the postion on the surface
shun-iwasawa 832993
shun-iwasawa 832993
  // convert output values (in float4) to channel value
shun-iwasawa 832993
  template <typename pixel="" raster,="" typename=""></typename>
shun-iwasawa 832993
  void setOutputRaster(float4 *srcMem, const RASTER dstRas, TDimensionI dim,
shun-iwasawa 832993
                       int drawLevel);
shun-iwasawa 832993
shun-iwasawa 832993
  // convert input tile's channel values to float4 values
shun-iwasawa 832993
  template <typename pixel="" raster,="" typename=""></typename>
shun-iwasawa 832993
  void setSourceRaster(const RASTER srcRas, float4 *srcMem, TDimensionI dim);
shun-iwasawa 832993
shun-iwasawa 832993
  void setRefRaster(const TRaster64P refRas, float *refMem, TDimensionI dim,
shun-iwasawa 832993
                    bool isRef);
shun-iwasawa 832993
shun-iwasawa 832993
  inline void initVars(FloorBumpVars &vars, TTile &tile,
shun-iwasawa 832993
                       const TRenderSettings &settings, double frame);
shun-iwasawa 832993
shun-iwasawa 832993
public:
shun-iwasawa 832993
  Iwa_FloorBumpFx();
shun-iwasawa 832993
  bool doGetBBox(double frame, TRectD &bBox,
shun-iwasawa 832993
                 const TRenderSettings &info) override;
shun-iwasawa 832993
  bool canHandle(const TRenderSettings &info, double frame) override;
shun-iwasawa 832993
  void doCompute(TTile &tile, double frame,
shun-iwasawa 832993
                 const TRenderSettings &rend_sets) override;
shun-iwasawa 832993
  void doCompute_CPU(TTile &tile, const double frame,
shun-iwasawa 832993
                     const TRenderSettings &settings, const FloorBumpVars &vars,
shun-iwasawa 832993
                     float4 *source_host, float *ref_host, float4 *result_host);
shun-iwasawa 832993
  void doCompute_with_Displacement(TTile &tile, const double frame,
shun-iwasawa 832993
                                   const TRenderSettings &settings,
shun-iwasawa 832993
                                   const FloorBumpVars &vars,
shun-iwasawa 832993
                                   float4 *source_host, float *ref_host,
shun-iwasawa 832993
                                   float *disp_host, float4 *result_host);
shun-iwasawa 832993
  void getParamUIs(TParamUIConcept *&concepts, int &length) override;
shun-iwasawa 832993
};
shun-iwasawa 832993
shun-iwasawa 832993
#endif