Toshihiro Shimizu 890ddd
/*------------------------------------------------------------
Toshihiro Shimizu 890ddd
Iwa_PNPerspectiveFx
Toshihiro Shimizu 890ddd
PerlinNoise/SimplexNoiseパターンを生成、透視投影する
Toshihiro Shimizu 890ddd
------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "iwa_pnperspectivefx.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "trop.h"
Toshihiro Shimizu 890ddd
#include "tparamuiconcept.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "iwa_fresnel.h"
Toshihiro Shimizu 890ddd
#include "iwa_simplexnoise.h"
Toshihiro Shimizu 890ddd
#include "iwa_noise1234.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <vector></vector>
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
namespace {
Toshihiro Shimizu 890ddd
#ifndef M_PI
Toshihiro Shimizu 890ddd
const double M_PI = 3.1415926535897932384626433832795;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/* 内積を返す */
Shinya Kitaoka 120a6e
inline float dot(float3 a, float3 b) {
Shinya Kitaoka 120a6e
  return a.x * b.x + a.y * b.y + a.z * b.z;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/* 外積を返す */
Shinya Kitaoka 120a6e
inline float3 cross(float3 a, float3 b) {
Shinya Kitaoka 120a6e
  float3 ret = {a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z,
Shinya Kitaoka 120a6e
                a.x * b.y - a.y * b.x};
Shinya Kitaoka 120a6e
  return ret;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/* 正規化する */
Shinya Kitaoka 120a6e
inline float3 normalize(float3 v) {
Shinya Kitaoka 120a6e
  float length = sqrtf(v.x * v.x + v.y * v.y + v.z * v.z);
Shinya Kitaoka 120a6e
  float3 ret   = {v.x / length, v.y / length, v.z / length};
Shinya Kitaoka 120a6e
  return ret;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------
Toshihiro Shimizu 890ddd
 出力結果をChannel値に変換して格納
Toshihiro Shimizu 890ddd
------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <typename pixel="" raster,="" typename=""></typename>
Shinya Kitaoka 120a6e
void Iwa_PNPerspectiveFx::setOutputRaster(float4 *srcMem, const RASTER dstRas,
Shinya Kitaoka 120a6e
                                          TDimensionI dim, int drawLevel,
Shinya Kitaoka 120a6e
                                          const bool alp_rend_sw) {
Shinya Kitaoka 120a6e
  typename PIXEL::Channel halfChan =
Shinya Kitaoka 120a6e
      (typename PIXEL::Channel)(PIXEL::maxChannelValue / 2);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (alp_rend_sw)
Shinya Kitaoka 120a6e
    dstRas->fill(PIXEL(halfChan, halfChan, halfChan, halfChan));
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    dstRas->fill(PIXEL(halfChan, halfChan, halfChan));
Shinya Kitaoka 120a6e
  float4 *chan_p = srcMem;
Shinya Kitaoka 120a6e
  for (int j = 0; j < drawLevel; j++) {
Shinya Kitaoka 120a6e
    PIXEL *pix = dstRas->pixels(j);
Shinya Kitaoka 120a6e
    for (int i = 0; i < dstRas->getLx(); i++, chan_p++, pix++) {
Shinya Kitaoka 120a6e
      float val;
Shinya Kitaoka 120a6e
      val    = (*chan_p).x * (float)PIXEL::maxChannelValue + 0.5f;
Shinya Kitaoka 120a6e
      pix->r = (typename PIXEL::Channel)((val > (float)PIXEL::maxChannelValue)
Shinya Kitaoka 120a6e
                                             ? (float)PIXEL::maxChannelValue
Shinya Kitaoka 120a6e
                                             : val);
Shinya Kitaoka 120a6e
      val    = (*chan_p).y * (float)PIXEL::maxChannelValue + 0.5f;
Shinya Kitaoka 120a6e
      pix->g = (typename PIXEL::Channel)((val > (float)PIXEL::maxChannelValue)
Shinya Kitaoka 120a6e
                                             ? (float)PIXEL::maxChannelValue
Shinya Kitaoka 120a6e
                                             : val);
Shinya Kitaoka 120a6e
      val    = (*chan_p).z * (float)PIXEL::maxChannelValue + 0.5f;
Shinya Kitaoka 120a6e
      pix->b = (typename PIXEL::Channel)((val > (float)PIXEL::maxChannelValue)
Shinya Kitaoka 120a6e
                                             ? (float)PIXEL::maxChannelValue
Shinya Kitaoka 120a6e
                                             : val);
Shinya Kitaoka 120a6e
      val    = (*chan_p).w * (float)PIXEL::maxChannelValue + 0.5f;
Shinya Kitaoka 120a6e
      pix->m = (typename PIXEL::Channel)((val > (float)PIXEL::maxChannelValue)
Shinya Kitaoka 120a6e
                                             ? (float)PIXEL::maxChannelValue
Shinya Kitaoka 120a6e
                                             : val);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------
Toshihiro Shimizu 890ddd
 PerlinNoiseのパラメータを取得
Toshihiro Shimizu 890ddd
------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
void Iwa_PNPerspectiveFx::getPNParameters(TTile &tile, double frame,
Shinya Kitaoka 120a6e
                                          const TRenderSettings &settings,
Shinya Kitaoka 120a6e
                                          PN_Params ¶ms,
Shinya Kitaoka 120a6e
                                          TDimensionI &dimOut) {
Shinya Kitaoka 120a6e
  /*  動作パラメータを得る */
Shinya Kitaoka 120a6e
  params.renderMode = m_renderMode->getValue();
Shinya Kitaoka 120a6e
  params.noiseType  = m_noiseType->getValue();
Shinya Kitaoka 120a6e
  params.size       = (float)m_size->getValue(frame);
Shinya Kitaoka 120a6e
  /* SimplexNoiseの密度感をそろえるための係数をかける */
Shinya Kitaoka 120a6e
  if (params.noiseType == 1) params.size *= 1.41421356f;
Shinya Kitaoka 120a6e
  params.octaves = m_octaves->getValue() + 1;
Shinya Kitaoka 120a6e
  params.offset  = float2{(float)m_offset->getValue(frame).x,
Shinya Kitaoka 120a6e
                         (float)m_offset->getValue(frame).y};
Shinya Kitaoka 120a6e
  params.p_intensity = (float)m_persistance_intensity->getValue(frame);
Shinya Kitaoka 120a6e
  params.p_size      = (float)m_persistance_size->getValue(frame);
Shinya Kitaoka 120a6e
  params.p_offset    = (float)m_persistance_offset->getValue(frame);
Shinya Kitaoka 120a6e
  TPointD _eyeLevel  = m_eyeLevel->getValue(frame);
Shinya Kitaoka 120a6e
  params.eyeLevel    = float2{(float)_eyeLevel.x, (float)_eyeLevel.y};
Shinya Kitaoka 120a6e
  params.alp_rend_sw = m_alpha_rendering->getValue();
Shinya Kitaoka 120a6e
  params.waveHeight  = (float)m_waveHeight->getValue(frame);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  const float fov = (float)m_fov->getValue(frame);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TAffine aff        = settings.m_affine;
Shinya Kitaoka 120a6e
  const double scale = 1.0 / sqrt(fabs(aff.det()));
Shinya Kitaoka 120a6e
  TAffine aff_pn     = TScale(scale) * TTranslation(tile.m_pos);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  params.a11 = aff_pn.a11;
Shinya Kitaoka 120a6e
  params.a12 = aff_pn.a12;
Shinya Kitaoka 120a6e
  params.a13 = aff_pn.a13;
Shinya Kitaoka 120a6e
  params.a21 = aff_pn.a21;
Shinya Kitaoka 120a6e
  params.a22 = aff_pn.a22;
Shinya Kitaoka 120a6e
  params.a23 = aff_pn.a23;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  params.time        = (float)m_evolution->getValue(frame) * 0.05;
Shinya Kitaoka 120a6e
  params.p_evolution = (float)m_persistance_evolution->getValue(frame);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TPointD eyePoint =
Shinya Kitaoka 120a6e
      aff * _eyeLevel - (tile.m_pos + tile.getRaster()->getCenterD());
Shinya Kitaoka 120a6e
  const float eyeHeight = (float)eyePoint.y;
Shinya Kitaoka 120a6e
  /* 描画範囲の下からの距離 */
Shinya Kitaoka 120a6e
  params.drawLevel = (int)((float)dimOut.ly / 2.0f + eyeHeight);
Shinya Kitaoka 120a6e
  if (params.drawLevel > dimOut.ly) params.drawLevel = dimOut.ly;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  //------------------------------------------------------------
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  /* カメラたて方向のmmサイズの半分の寸法 */
Shinya Kitaoka 120a6e
  int camHeight = settings.m_cameraBox.getLy();
Shinya Kitaoka 120a6e
  TPointD vec_p0p1((double)camHeight * aff_pn.a12,
Shinya Kitaoka 120a6e
                   (double)camHeight * aff_pn.a22);
Shinya Kitaoka 120a6e
  params.fy_2 = sqrtf(vec_p0p1.x * vec_p0p1.x + vec_p0p1.y * vec_p0p1.y) / 2.0f;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  float fov_radian_2 = (fov / 2.0f) * float(M_PI_180);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  /* カメラから投影面への距離 */
Shinya Kitaoka 120a6e
  float D = params.fy_2 / tanf(fov_radian_2);
Shinya Kitaoka 120a6e
  /* カメラから、投影面上の水平線への距離 */
Shinya Kitaoka 120a6e
  params.A = sqrtf(params.eyeLevel.y * params.eyeLevel.y + D * D);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  /* カメラ位置から下枠へのベクトルと、水平線のなす角度 */
Shinya Kitaoka 120a6e
  float theta = fov_radian_2 + asinf(params.eyeLevel.y / params.A);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  float M = params.fy_2 / sinf(fov_radian_2);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  params.cam_pos = float3{0.0f, -M * cosf(theta), M * sinf(theta)};
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  /*ベースとなるフレネル反射率を求める*/
Shinya Kitaoka 120a6e
  params.base_fresnel_ref = 0.0f;
Shinya Kitaoka 120a6e
  float phi               = 90.0f - theta * 180.0f / M_PI;
Shinya Kitaoka 120a6e
  if (phi >= 0.0f && phi < 90.0f) {
Shinya Kitaoka 120a6e
    int index   = (int)phi;
Shinya Kitaoka 120a6e
    float ratio = phi - (float)index;
Shinya Kitaoka 120a6e
    params.base_fresnel_ref =
Shinya Kitaoka 120a6e
        fresnel[index] * (1.0f - ratio) + fresnel[index + 1] * ratio;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  /*強度の正規化のため、合計値を算出*/
Shinya Kitaoka 120a6e
  float intensity = 2.0f; /* -1 ~ 1 */
Shinya Kitaoka 120a6e
  params.int_sum  = 0.0f;
Shinya Kitaoka 120a6e
  for (int o = 0; o < params.octaves; o++) {
Shinya Kitaoka 120a6e
    params.int_sum += intensity;
Shinya Kitaoka 120a6e
    intensity *= params.p_intensity;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Iwa_PNPerspectiveFx::Iwa_PNPerspectiveFx()
Shinya Kitaoka 120a6e
    : m_renderMode(new TIntEnumParam(0, "Noise"))
Shinya Kitaoka 120a6e
    , m_noiseType(new TIntEnumParam(0, "Perlin Noise"))
Shinya Kitaoka 120a6e
    , m_size(10.0)
Shinya Kitaoka 120a6e
    , m_evolution(0.0)
Shinya Kitaoka 120a6e
    , m_octaves(new TIntEnumParam(0, "1"))
Shinya Kitaoka 120a6e
    , m_offset(TPointD(0, 0))
Shinya Kitaoka 120a6e
    , m_persistance_intensity(0.5)
Shinya Kitaoka 120a6e
    , m_persistance_size(0.5)
Shinya Kitaoka 120a6e
    , m_persistance_evolution(0.5)
Shinya Kitaoka 120a6e
    , m_persistance_offset(0.5)
Shinya Kitaoka 120a6e
    , m_fov(30)
Shinya Kitaoka 120a6e
    , m_eyeLevel(TPointD(0, 0))
Shinya Kitaoka 120a6e
    , m_alpha_rendering(true)
Shinya Kitaoka 120a6e
    , m_waveHeight(10.0) {
Shinya Kitaoka 120a6e
  bindParam(this, "renderMode", m_renderMode);
Shinya Kitaoka 120a6e
  bindParam(this, "noiseType", m_noiseType);
Shinya Kitaoka 120a6e
  bindParam(this, "size", m_size);
Shinya Kitaoka 120a6e
  bindParam(this, "evolution", m_evolution);
Shinya Kitaoka 120a6e
  bindParam(this, "octaves", m_octaves);
Shinya Kitaoka 120a6e
  bindParam(this, "offset", m_offset);
Shinya Kitaoka 120a6e
  bindParam(this, "persistance_intensity", m_persistance_intensity);
Shinya Kitaoka 120a6e
  bindParam(this, "persistance_size", m_persistance_size);
Shinya Kitaoka 120a6e
  bindParam(this, "persistance_evolution", m_persistance_evolution);
Shinya Kitaoka 120a6e
  bindParam(this, "persistance_offset", m_persistance_offset);
Shinya Kitaoka 120a6e
  bindParam(this, "fov", m_fov);
Shinya Kitaoka 120a6e
  bindParam(this, "eyeLevel", m_eyeLevel);
Shinya Kitaoka 120a6e
  bindParam(this, "alpha_rendering", m_alpha_rendering);
Shinya Kitaoka 120a6e
  bindParam(this, "waveHeight", m_waveHeight);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m_noiseType->addItem(1, "Simplex Noise");
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m_renderMode->addItem(1, "Noise (no resampled)");
Shinya Kitaoka 120a6e
  m_renderMode->addItem(2, "Warp HV offset");
Shinya Kitaoka 120a6e
  m_renderMode->addItem(4, "Warp HV offset 2");
Shinya Kitaoka 120a6e
  m_renderMode->addItem(3, "Fresnel reflectivity");
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m_size->setMeasureName("fxLength");
Shinya Kitaoka 120a6e
  m_size->setValueRange(0.0, 1000.0);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m_octaves->addItem(1, "2");
Shinya Kitaoka 120a6e
  m_octaves->addItem(2, "3");
Shinya Kitaoka 120a6e
  m_octaves->addItem(3, "4");
Shinya Kitaoka 120a6e
  m_octaves->addItem(4, "5");
Shinya Kitaoka 120a6e
  m_octaves->addItem(5, "6");
Shinya Kitaoka 120a6e
  m_octaves->addItem(6, "7");
Shinya Kitaoka 120a6e
  m_octaves->addItem(7, "8");
Shinya Kitaoka 120a6e
  m_octaves->addItem(8, "9");
Shinya Kitaoka 120a6e
  m_octaves->addItem(9, "10");
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m_persistance_intensity->setValueRange(0.1, 2.0);
Shinya Kitaoka 120a6e
  m_persistance_size->setValueRange(0.1, 2.0);
Shinya Kitaoka 120a6e
  m_persistance_evolution->setValueRange(0.1, 2.0);
Shinya Kitaoka 120a6e
  m_persistance_offset->setValueRange(0.1, 2.0);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m_fov->setValueRange(10, 90);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m_eyeLevel->getX()->setMeasureName("fxLength");
Shinya Kitaoka 120a6e
  m_eyeLevel->getY()->setMeasureName("fxLength");
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m_waveHeight->setMeasureName("fxLength");
Shinya Kitaoka 120a6e
  m_waveHeight->setValueRange(1.0, 100.0);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
bool Iwa_PNPerspectiveFx::doGetBBox(double frame, TRectD &bBox,
Shinya Kitaoka 120a6e
                                    const TRenderSettings &info) {
Shinya Kitaoka 120a6e
  bBox = TConsts::infiniteRectD;
Shinya Kitaoka 120a6e
  return true;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
bool Iwa_PNPerspectiveFx::canHandle(const TRenderSettings &info, double frame) {
Shinya Kitaoka 120a6e
  return false;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void Iwa_PNPerspectiveFx::doCompute(TTile &tile, double frame,
Shinya Kitaoka 120a6e
                                    const TRenderSettings &settings) {
Shinya Kitaoka 120a6e
  /* サポートしていないPixelタイプはエラーを投げる */
Shinya Kitaoka 120a6e
  if (!((TRaster32P)tile.getRaster()) && !((TRaster64P)tile.getRaster())) {
Shinya Kitaoka 120a6e
    throw TRopException("unsupported input pixel type");
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TDimensionI dimOut(tile.getRaster()->getLx(), tile.getRaster()->getLy());
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  /* PerinNoiseのパラメータ */
Shinya Kitaoka 120a6e
  PN_Params pnParams;
Shinya Kitaoka 120a6e
  getPNParameters(tile, frame, settings, pnParams, dimOut);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  /* 水平線が画面より下のときreturn */
Shinya Kitaoka 120a6e
  if (pnParams.drawLevel < 0) {
Shinya Kitaoka 120a6e
    tile.getRaster()->clear();
Shinya Kitaoka 120a6e
    return;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  const float evolution   = (float)m_evolution->getValue(frame);
Shinya Kitaoka 120a6e
  const float p_evolution = (float)m_persistance_evolution->getValue(frame);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  float4 *out_host;
Shinya Kitaoka 120a6e
  /* ホストのメモリ確保 */
Shinya Kitaoka 120a6e
  TRasterGR8P out_host_ras(sizeof(float4) * dimOut.lx, pnParams.drawLevel);
Shinya Kitaoka 120a6e
  out_host_ras->lock();
Shinya Kitaoka 120a6e
  out_host = (float4 *)out_host_ras->getRawData();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  doCompute_CPU(tile, frame, settings, out_host, dimOut, pnParams);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  /* 出力結果をChannel値に変換して格納 */
Shinya Kitaoka 120a6e
  tile.getRaster()->clear();
Shinya Kitaoka 120a6e
  TRaster32P outRas32 = (TRaster32P)tile.getRaster();
Shinya Kitaoka 120a6e
  TRaster64P outRas64 = (TRaster64P)tile.getRaster();
Shinya Kitaoka 120a6e
  if (outRas32)
Shinya Kitaoka 120a6e
    setOutputRaster<traster32p, tpixel32="">(</traster32p,>
Shinya Kitaoka 120a6e
        out_host, outRas32, dimOut, pnParams.drawLevel, pnParams.alp_rend_sw);
Shinya Kitaoka 120a6e
  else if (outRas64)
Shinya Kitaoka 120a6e
    setOutputRaster<traster64p, tpixel64="">(</traster64p,>
Shinya Kitaoka 120a6e
        out_host, outRas64, dimOut, pnParams.drawLevel, pnParams.alp_rend_sw);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  out_host_ras->unlock();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Shinya Kitaoka 120a6e
void Iwa_PNPerspectiveFx::doCompute_CPU(TTile &tile, double frame,
Shinya Kitaoka 120a6e
                                        const TRenderSettings &settings,
Shinya Kitaoka 120a6e
                                        float4 *out_host, TDimensionI &dimOut,
Shinya Kitaoka 120a6e
                                        PN_Params &pnParams) {
Shinya Kitaoka 120a6e
  /* モードで分ける */
Shinya Kitaoka 120a6e
  if (pnParams.renderMode == 0 || pnParams.renderMode == 1) {
Shinya Kitaoka 120a6e
    calcPerinNoise_CPU(out_host, dimOut, pnParams,
Shinya Kitaoka 120a6e
                       (bool)(pnParams.renderMode == 0));
Shinya Kitaoka 120a6e
  } else if (pnParams.renderMode == 2 || pnParams.renderMode == 3 ||
Shinya Kitaoka 120a6e
             pnParams.renderMode == 4) {
Shinya Kitaoka 120a6e
    calcPNNormal_CPU(out_host, dimOut, pnParams);
Shinya Kitaoka 120a6e
    if (pnParams.renderMode == 4) {
Shinya Kitaoka 120a6e
      calcPNNormal_CPU(out_host, dimOut, pnParams, true);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------
Toshihiro Shimizu 890ddd
 通常のノイズのCPU計算
Toshihiro Shimizu 890ddd
------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
void Iwa_PNPerspectiveFx::calcPerinNoise_CPU(float4 *out_host,
Shinya Kitaoka 120a6e
                                             TDimensionI &dimOut, PN_Params &p,
Shinya Kitaoka 120a6e
                                             bool doResample) {
Shinya Kitaoka 120a6e
  int reso = (doResample) ? 10 : 1;
Shinya Kitaoka 120a6e
  /* 結果を収めるイテレータ */
Shinya Kitaoka 120a6e
  float4 *out_p = out_host;
Shinya Kitaoka 120a6e
  /* 各ピクセルについて */
Shinya Kitaoka 120a6e
  for (int yy = 0; yy < p.drawLevel; yy++) {
Shinya Kitaoka 120a6e
    for (int xx = 0; xx < dimOut.lx; xx++, out_p++) {
Shinya Kitaoka 120a6e
      float val_sum = 0.0f;
Shinya Kitaoka 120a6e
      int count     = 0;
Shinya Kitaoka 120a6e
      /* 各リサンプル点について */
Shinya Kitaoka 120a6e
      for (int tt = 0; tt < reso; tt++) {
Shinya Kitaoka 120a6e
        for (int ss = 0; ss < reso; ss++) {
Shinya Kitaoka 120a6e
          float2 tmpPixPos = {
Shinya Kitaoka 120a6e
              (float)xx - 0.5f + ((float)ss + 0.5f) / (float)reso,
Shinya Kitaoka 120a6e
              (float)yy - 0.5f + ((float)tt + 0.5f) / (float)reso};
Shinya Kitaoka 120a6e
          float2 screenPos = {
Shinya Kitaoka 120a6e
              tmpPixPos.x * p.a11 + tmpPixPos.y * p.a12 + p.a13,
Shinya Kitaoka 120a6e
              tmpPixPos.x * p.a21 + tmpPixPos.y * p.a22 + p.a23};
Shinya Kitaoka 120a6e
          /* ② Perlin Noise 平面上の座標を計算する */
Shinya Kitaoka 120a6e
          float2 noisePos;
Shinya Kitaoka 120a6e
          noisePos.x = -(p.eyeLevel.y + p.fy_2) * (screenPos.x - p.eyeLevel.x) /
Shinya Kitaoka 120a6e
                           (screenPos.y - p.eyeLevel.y) +
Shinya Kitaoka 120a6e
                       p.eyeLevel.x;
Shinya Kitaoka 120a6e
          noisePos.y =
Shinya Kitaoka 120a6e
              (p.fy_2 + screenPos.y) * p.A / (p.eyeLevel.y - screenPos.y);
Shinya Kitaoka 120a6e
          float tmpVal           = 0.5f;
Shinya Kitaoka 120a6e
          float currentSize      = p.size;
Shinya Kitaoka 120a6e
          float2 currentOffset   = p.offset;
Shinya Kitaoka 120a6e
          float currentIntensity = 1.0f;
Shinya Kitaoka 120a6e
          // float2* basis_p = basis;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
          float currentEvolution = p.time;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
          /* ノイズを各世代足しこむ */
Shinya Kitaoka 120a6e
          for (int o = 0; o < p.octaves; o++) {
Shinya Kitaoka 120a6e
            float2 currentNoisePos = {
Shinya Kitaoka 120a6e
                (noisePos.x - currentOffset.x) / currentSize,
Shinya Kitaoka 120a6e
                (noisePos.y - currentOffset.y) / currentSize};
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
            if (p.noiseType == 0) {
Shinya Kitaoka 120a6e
              tmpVal += currentIntensity *
Shinya Kitaoka 120a6e
                        Noise1234::noise(currentNoisePos.x, currentNoisePos.y,
Shinya Kitaoka 120a6e
                                         currentEvolution) /
Shinya Kitaoka 120a6e
                        p.int_sum;
Shinya Kitaoka 120a6e
            } else {
Shinya Kitaoka 120a6e
              tmpVal +=
Shinya Kitaoka 120a6e
                  currentIntensity *
Shinya Kitaoka 120a6e
                  SimplexNoise::noise(currentNoisePos.x, currentNoisePos.y,
Shinya Kitaoka 120a6e
                                      currentEvolution) /
Shinya Kitaoka 120a6e
                  p.int_sum;
Shinya Kitaoka 120a6e
            }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
            currentSize *= p.p_size;
Shinya Kitaoka 120a6e
            currentOffset.x *= p.p_offset;
Shinya Kitaoka 120a6e
            currentOffset.y *= p.p_offset;
Shinya Kitaoka 120a6e
            currentIntensity *= p.p_intensity;
Shinya Kitaoka 120a6e
            currentEvolution *= p.p_evolution;
Shinya Kitaoka 120a6e
          }
Shinya Kitaoka 120a6e
          val_sum += tmpVal;
Shinya Kitaoka 120a6e
          count += 1;
Shinya Kitaoka 120a6e
        }
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      float val = val_sum / (float)count;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      /* クランプ */
Shinya Kitaoka 120a6e
      val = (val < 0.0f) ? 0.0f : ((val > 1.0f) ? 1.0f : val);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      (*out_p).x = val;
Shinya Kitaoka 120a6e
      (*out_p).y = val;
Shinya Kitaoka 120a6e
      (*out_p).z = val;
Shinya Kitaoka 120a6e
      (*out_p).w = (p.alp_rend_sw) ? val : 1.0f;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------
Toshihiro Shimizu 890ddd
 WarpHVモード、Fresnel反射モード
Toshihiro Shimizu 890ddd
------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
void Iwa_PNPerspectiveFx::calcPNNormal_CPU(float4 *out_host,
Shinya Kitaoka 120a6e
                                           TDimensionI &dimOut, PN_Params &p,
Shinya Kitaoka 120a6e
                                           bool isSubWave) {
Shinya Kitaoka 120a6e
  /* 結果を収めるイテレータ */
Shinya Kitaoka 120a6e
  float4 *out_p = out_host;
Shinya Kitaoka 120a6e
  /* 各ピクセルについて */
Shinya Kitaoka 120a6e
  for (int yy = 0; yy < p.drawLevel; yy++) {
Shinya Kitaoka 120a6e
    for (int xx = 0; xx < dimOut.lx; xx++, out_p++) {
Shinya Kitaoka 120a6e
      float2 screenPos = {(float)xx * p.a11 + (float)yy * p.a12 + p.a13,
Shinya Kitaoka 120a6e
                          (float)xx * p.a21 + (float)yy * p.a22 + p.a23};
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      /*  ② Perlin Noise 平面上の座標を計算する */
Shinya Kitaoka 120a6e
      float2 noisePos;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      noisePos.x = -(p.eyeLevel.y + p.fy_2) * (screenPos.x - p.eyeLevel.x) /
Shinya Kitaoka 120a6e
                       (screenPos.y - p.eyeLevel.y) +
Shinya Kitaoka 120a6e
                   p.eyeLevel.x;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      noisePos.y = (p.fy_2 + screenPos.y) * p.A / (p.eyeLevel.y - screenPos.y);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      float gradient[2]; /* 0 : よこ差分、1 : たて差分 */
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      float delta = 0.001f;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      /* 横、縦差分それぞれについて */
Shinya Kitaoka 120a6e
      for (int yokoTate = 0; yokoTate < 2; yokoTate++) {
Shinya Kitaoka 120a6e
        /* 勾配の初期化 */
Shinya Kitaoka 120a6e
        gradient[yokoTate] = 0.0f;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
        /* サンプリング位置のオフセットを求める */
Shinya Kitaoka 120a6e
        float2 kinbouNoisePos[2] = {
Shinya Kitaoka 120a6e
            float2{noisePos.x - ((yokoTate == 0) ? delta : 0.0f),
Shinya Kitaoka 120a6e
                   noisePos.y - ((yokoTate == 0) ? 0.0f : delta)},
Shinya Kitaoka 120a6e
            float2{noisePos.x + ((yokoTate == 0) ? delta : 0.0f),
Shinya Kitaoka 120a6e
                   noisePos.y + ((yokoTate == 0) ? 0.0f : delta)}};
Shinya Kitaoka 120a6e
        float currentSize      = p.size;
Shinya Kitaoka 120a6e
        float2 currentOffset   = p.offset;
Shinya Kitaoka 120a6e
        float currentIntensity = 1.0f;
Shinya Kitaoka 120a6e
        // float2* basis_p = basis;
Shinya Kitaoka 120a6e
        float currentEvolution = (isSubWave) ? p.time + 100.0f : p.time;
Shinya Kitaoka 120a6e
        /* 各世代について */
Shinya Kitaoka 120a6e
        for (int o = 0; o < p.octaves; o++, currentSize *= p.p_size,
Shinya Kitaoka 120a6e
                 currentOffset.x *= p.p_offset, currentOffset.y *= p.p_offset,
Shinya Kitaoka 120a6e
                 currentIntensity *= p.p_intensity) {
Shinya Kitaoka 120a6e
          /* プラス方向、マイナス方向それぞれオフセットしたノイズ座標を求める */
Shinya Kitaoka 120a6e
          float2 currentOffsetNoisePos[2];
Shinya Kitaoka 120a6e
          for (int mp = 0; mp < 2; mp++)
Shinya Kitaoka 120a6e
            currentOffsetNoisePos[mp] =
Shinya Kitaoka 120a6e
                float2{(kinbouNoisePos[mp].x - currentOffset.x) / currentSize,
Shinya Kitaoka 120a6e
                       (kinbouNoisePos[mp].y - currentOffset.y) / currentSize};
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
          /* ノイズの差分を積算していく */
Shinya Kitaoka 120a6e
          float noiseDiff;
Shinya Kitaoka 120a6e
          // Perlin Noise
Shinya Kitaoka 120a6e
          if (p.noiseType == 0) {
Shinya Kitaoka 120a6e
            noiseDiff =
Shinya Kitaoka 120a6e
                Noise1234::noise(currentOffsetNoisePos[1].x,
Shinya Kitaoka 120a6e
                                 currentOffsetNoisePos[1].y, currentEvolution) -
Shinya Kitaoka 120a6e
                Noise1234::noise(currentOffsetNoisePos[0].x,
Shinya Kitaoka 120a6e
                                 currentOffsetNoisePos[0].y, currentEvolution);
Shinya Kitaoka 120a6e
          } else {
Shinya Kitaoka 120a6e
            /* インデックスをチェック */
Shinya Kitaoka 120a6e
            /* まず、前後 */
Shinya Kitaoka 120a6e
            CellIds kinbouIds[2] = {
Shinya Kitaoka 120a6e
                SimplexNoise::getCellIds(currentOffsetNoisePos[0].x,
Shinya Kitaoka 120a6e
                                         currentOffsetNoisePos[0].y,
Shinya Kitaoka 120a6e
                                         currentEvolution),
Shinya Kitaoka 120a6e
                SimplexNoise::getCellIds(currentOffsetNoisePos[1].x,
Shinya Kitaoka 120a6e
                                         currentOffsetNoisePos[1].y,
Shinya Kitaoka 120a6e
                                         currentEvolution)};
Shinya Kitaoka 120a6e
            /* 同じセルに入っていたら、普通に差分を計算 */
Shinya Kitaoka 120a6e
            if (kinbouIds[0] == kinbouIds[1]) {
Shinya Kitaoka 120a6e
              noiseDiff = SimplexNoise::noise(currentOffsetNoisePos[1].x,
Shinya Kitaoka 120a6e
                                              currentOffsetNoisePos[1].y,
Shinya Kitaoka 120a6e
                                              currentEvolution) -
Shinya Kitaoka 120a6e
                          SimplexNoise::noise(currentOffsetNoisePos[0].x,
Shinya Kitaoka 120a6e
                                              currentOffsetNoisePos[0].y,
Shinya Kitaoka 120a6e
                                              currentEvolution);
Shinya Kitaoka 120a6e
            }
Shinya Kitaoka 120a6e
            /* 違うセルの場合、中心位置を用いる */
Shinya Kitaoka 120a6e
            else {
Shinya Kitaoka 120a6e
              float2 currentCenterNoisePos = {
Shinya Kitaoka 120a6e
                  (noisePos.x - currentOffset.x) / currentSize,
Shinya Kitaoka 120a6e
                  (noisePos.y - currentOffset.y) / currentSize};
Shinya Kitaoka 120a6e
              CellIds centerIds = SimplexNoise::getCellIds(
Shinya Kitaoka 120a6e
                  currentCenterNoisePos.x, currentCenterNoisePos.y,
Shinya Kitaoka 120a6e
                  currentEvolution);
Shinya Kitaoka 120a6e
              if (kinbouIds[0] == centerIds) {
Shinya Kitaoka 120a6e
                noiseDiff = SimplexNoise::noise(currentCenterNoisePos.x,
Shinya Kitaoka 120a6e
                                                currentCenterNoisePos.y,
Shinya Kitaoka 120a6e
                                                currentEvolution) -
Shinya Kitaoka 120a6e
                            SimplexNoise::noise(currentOffsetNoisePos[0].x,
Shinya Kitaoka 120a6e
                                                currentOffsetNoisePos[0].y,
Shinya Kitaoka 120a6e
                                                currentEvolution);
Shinya Kitaoka 120a6e
              } else  // if(kinbouIds[1] == centerIds)
Shinya Kitaoka 120a6e
              {
Shinya Kitaoka 120a6e
                noiseDiff = SimplexNoise::noise(currentOffsetNoisePos[1].x,
Shinya Kitaoka 120a6e
                                                currentOffsetNoisePos[1].y,
Shinya Kitaoka 120a6e
                                                currentEvolution) -
Shinya Kitaoka 120a6e
                            SimplexNoise::noise(currentCenterNoisePos.x,
Shinya Kitaoka 120a6e
                                                currentCenterNoisePos.y,
Shinya Kitaoka 120a6e
                                                currentEvolution);
Shinya Kitaoka 120a6e
              }
Shinya Kitaoka 120a6e
              /* 片端→中心の変位を使っているので、片端→片端に合わせて変位を2倍する
Shinya Kitaoka 120a6e
               */
Shinya Kitaoka 120a6e
              noiseDiff *= 2.0f;
Shinya Kitaoka 120a6e
            }
Shinya Kitaoka 120a6e
          }
Shinya Kitaoka 120a6e
          /* 差分に強度を乗算して足しこむ */
Shinya Kitaoka 120a6e
          gradient[yokoTate] += currentIntensity * noiseDiff / p.int_sum;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
          currentEvolution *= p.p_evolution;
Shinya Kitaoka 120a6e
        }
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      /* X方向、Y方向の近傍ベクトルを計算する */
Shinya Kitaoka 120a6e
      float3 vec_x  = {delta * 2, 0.0f, gradient[0] * p.waveHeight};
Shinya Kitaoka 120a6e
      float3 vec_y  = {0.0f, delta * 2, gradient[1] * p.waveHeight};
Shinya Kitaoka 120a6e
      float3 normal = normalize(cross(vec_x, vec_y));
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      /* カメラから平面へのベクトル */
Shinya Kitaoka 120a6e
      float3 cam_vec = {noisePos.x - p.cam_pos.x, noisePos.y - p.cam_pos.y,
Shinya Kitaoka 120a6e
                        -p.cam_pos.z};
Shinya Kitaoka 120a6e
      cam_vec = normalize(cam_vec);
Shinya Kitaoka 120a6e
      /* WarpHVの参照画像モード */
Shinya Kitaoka 120a6e
      if (p.renderMode == 2 || p.renderMode == 4) {
Shinya Kitaoka 120a6e
        /* 平面からの反射ベクトル */
Shinya Kitaoka 120a6e
        float alpha        = dot(normal, cam_vec);
Shinya Kitaoka 120a6e
        float3 reflect_cam = {
Shinya Kitaoka 120a6e
            2.0f * alpha * normal.x - cam_vec.x,
Shinya Kitaoka 120a6e
            2.0f * alpha * normal.y - cam_vec.y,
Shinya Kitaoka 120a6e
            2.0f * alpha * normal.z - cam_vec.z}; /* これの長さは1 */
Shinya Kitaoka 120a6e
        /* 完全に水平な面で反射した場合の反射ベクトル */
Shinya Kitaoka 120a6e
        float3 reflect_cam_mirror = {cam_vec.x, cam_vec.y, -cam_vec.z};
Shinya Kitaoka 120a6e
        /* 角度のずれを格納する */
Shinya Kitaoka 120a6e
        /*  -PI/2 ~ PI/2 */
Shinya Kitaoka 120a6e
        float angle_h = atanf(reflect_cam.x / reflect_cam.y) -
Shinya Kitaoka 120a6e
                        atanf(reflect_cam_mirror.x / reflect_cam_mirror.y);
Shinya Kitaoka 120a6e
        float angle_v = atanf(reflect_cam.z / reflect_cam.y) -
Shinya Kitaoka 120a6e
                        atanf(reflect_cam_mirror.z / reflect_cam_mirror.y);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
        /* 30°を最大とする */
Shinya Kitaoka 120a6e
        angle_h = 0.5f + angle_h / 0.5236f;
Shinya Kitaoka 120a6e
        angle_v = 0.5f - angle_v / 0.5236f;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
        /* クランプ */
Shinya Kitaoka 120a6e
        angle_h = (angle_h < 0.0f) ? 0.0f : ((angle_h > 1.0f) ? 1.0f : angle_h);
Shinya Kitaoka 120a6e
        angle_v = (angle_v < 0.0f) ? 0.0f : ((angle_v > 1.0f) ? 1.0f : angle_v);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
        if (p.renderMode == 2) {
Shinya Kitaoka 120a6e
          (*out_p).x = angle_h;
Shinya Kitaoka 120a6e
          (*out_p).y = angle_v;
Shinya Kitaoka 120a6e
          (*out_p).z = 0.0f;
Shinya Kitaoka 120a6e
          (*out_p).w = 1.0f;
Shinya Kitaoka 120a6e
        } else  //  p.renderMode == 4
Shinya Kitaoka 120a6e
        {
Shinya Kitaoka 120a6e
          if (!isSubWave) {
Shinya Kitaoka 120a6e
            (*out_p).y = angle_v;
Shinya Kitaoka 120a6e
            (*out_p).z = 0.0f;
Shinya Kitaoka 120a6e
            (*out_p).w = 1.0f;
Shinya Kitaoka 120a6e
          } else
Shinya Kitaoka 120a6e
            (*out_p).x = angle_v;
Shinya Kitaoka 120a6e
        }
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
      /* フレネル反射モード */
Shinya Kitaoka 120a6e
      else if (p.renderMode == 3) {
Shinya Kitaoka 120a6e
        cam_vec.x *= -1;
Shinya Kitaoka 120a6e
        cam_vec.y *= -1;
Shinya Kitaoka 120a6e
        cam_vec.z *= -1;
Shinya Kitaoka 120a6e
        float diffuse_angle = acosf(dot(normal, cam_vec)) * 180.0f / 3.14159f;
Shinya Kitaoka 120a6e
        float ref           = 0.0f;
Shinya Kitaoka 120a6e
        if (diffuse_angle >= 0.0f && diffuse_angle < 90.0f) {
Shinya Kitaoka 120a6e
          int index   = (int)diffuse_angle;
Shinya Kitaoka 120a6e
          float ratio = diffuse_angle - (float)index;
Shinya Kitaoka 120a6e
          float fresnel_ref =
Shinya Kitaoka 120a6e
              fresnel[index] * (1.0f - ratio) + fresnel[index + 1] * ratio;
Shinya Kitaoka 120a6e
          ref =
Shinya Kitaoka 120a6e
              (fresnel_ref - p.base_fresnel_ref) / (1.0f - p.base_fresnel_ref);
Shinya Kitaoka 120a6e
        } else if (diffuse_angle >= 90.0f)
Shinya Kitaoka 120a6e
          ref = 1.0f;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
        /* クランプ */
Shinya Kitaoka 120a6e
        ref        = (ref < 0.0f) ? 0.0f : ((ref > 1.0f) ? 1.0f : ref);
Shinya Kitaoka 120a6e
        (*out_p).x = ref;
Shinya Kitaoka 120a6e
        (*out_p).y = ref;
Shinya Kitaoka 120a6e
        (*out_p).z = ref;
Shinya Kitaoka 120a6e
        (*out_p).w = (p.alp_rend_sw) ? ref : 1.0f;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void Iwa_PNPerspectiveFx::getParamUIs(TParamUIConcept *&concepts, int &length) {
Shinya Kitaoka 120a6e
  concepts = new TParamUIConcept[length = 1];
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  concepts[0].m_type  = TParamUIConcept::POINT;
Shinya Kitaoka 120a6e
  concepts[0].m_label = "Eye Level";
Shinya Kitaoka 120a6e
  concepts[0].m_params.push_back(m_eyeLevel);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
FX_PLUGIN_IDENTIFIER(Iwa_PNPerspectiveFx, "iwa_PNPerspectiveFx");