tutorials/viewer_stream/viewer_stream_device.cpp
Vec3fa ambientOcclusionShading(int x, int y, RTCRay& ray)
{
  RTCRay rays[AMBIENT_OCCLUSION_SAMPLES];

  Vec3fa Ng = normalize(ray.Ng);
  if (dot(ray.dir,Ng) > 0.0f) Ng = neg(Ng);

  Vec3fa col = Vec3fa(min(1.0f,0.3f+0.8f*abs(dot(Ng,normalize(ray.dir)))));

  /* calculate hit point */
  float intensity = 0;
  Vec3fa hitPos = ray.org + ray.tfar * ray.dir;

  RandomSampler sampler;
  RandomSampler_init(sampler,x,y,0);

  /* enable only valid rays */
  for (int i=0; i<AMBIENT_OCCLUSION_SAMPLES; i++)
  {
    /* sample random direction */
    Vec2f s = RandomSampler_get2D(sampler);
    Sample3f dir;
    dir.v = cosineSampleHemisphere(s);
    dir.pdf = cosineSampleHemispherePDF(dir.v);
    dir.v = frame(Ng) * dir.v;

    /* initialize shadow ray */
    RTCRay& shadow = rays[i];
    shadow.org = hitPos;
    shadow.dir = dir.v;
    bool mask = 1; { // invalidate inactive rays
      shadow.tnear = mask ? 0.001f       : (float)(pos_inf);
      shadow.tfar  = mask ? (float)(inf) : (float)(neg_inf);
    }
    shadow.geomID = RTC_INVALID_GEOMETRY_ID;
    shadow.primID = RTC_INVALID_GEOMETRY_ID;
    shadow.mask = -1;
    shadow.time = 0;    // FIXME: invalidate inactive rays
  }

  RTCIntersectContext context;
  context.flags = RAYN_FLAGS;

  /* trace occlusion rays */
#if USE_INTERFACE == 0
  rtcOccluded1M(g_scene,&context,rays,AMBIENT_OCCLUSION_SAMPLES,sizeof(RTCRay));
#elif USE_INTERFACE == 1
  for (size_t i=0; i<AMBIENT_OCCLUSION_SAMPLES; i++)
    rtcOccluded(g_scene,rays[i]);
#else
  for (size_t i=0; i<AMBIENT_OCCLUSION_SAMPLES; i++)
    rtcOccluded1M(g_scene,&context,&rays[i],1,sizeof(RTCRay));
#endif

  /* accumulate illumination */
  for (int i=0; i<AMBIENT_OCCLUSION_SAMPLES; i++) {
    if (rays[i].geomID == RTC_INVALID_GEOMETRY_ID)
      intensity += 1.0f;
  }

  /* shade pixel */
  return col * (intensity/AMBIENT_OCCLUSION_SAMPLES);
}
/* renders a single pixel casting with ambient occlusion */
renders a single pixel casting with ambient occlusion
renders a single pixel casting with ambient occlusion
Vec3fa ambientOcclusionShading(int x, int y, RTCRay& ray)
{
  RTCRay rays[AMBIENT_OCCLUSION_SAMPLES];
  Vec3fa Ng = normalize(ray.Ng);
  if (dot(ray.dir,Ng) > 0.0f) Ng = neg(Ng);
  Vec3fa col = Vec3fa(min(1.0f,0.3f+0.8f*abs(dot(Ng,normalize(ray.dir)))));
  float intensity = 0;
  Vec3fa hitPos = ray.org + ray.tfar * ray.dir;
  RandomSampler sampler;
  RandomSampler_init(sampler,x,y,0);
  for (int i=0; i<AMBIENT_OCCLUSION_SAMPLES; i++)
  {
    Vec2f s = RandomSampler_get2D(sampler);
    Sample3f dir;
    dir.v = cosineSampleHemisphere(s);
    dir.pdf = cosineSampleHemispherePDF(dir.v);
    dir.v = frame(Ng) * dir.v;
    RTCRay& shadow = rays[i];
    shadow.org = hitPos;
    shadow.dir = dir.v;
    bool mask = 1; { 
      shadow.tnear = mask ? 0.001f       : (float)(pos_inf);
      shadow.tfar  = mask ? (float)(inf) : (float)(neg_inf);
    }
    shadow.geomID = RTC_INVALID_GEOMETRY_ID;
    shadow.primID = RTC_INVALID_GEOMETRY_ID;
    shadow.mask = -1;
    shadow.time = 0;    
  }
  RTCIntersectContext context;
  context.flags = RAYN_FLAGS;
#if USE_INTERFACE == 0
  rtcOccluded1M(g_scene,&context,rays,AMBIENT_OCCLUSION_SAMPLES,sizeof(RTCRay));
#elif USE_INTERFACE == 1
  for (size_t i=0; i<AMBIENT_OCCLUSION_SAMPLES; i++)
    rtcOccluded(g_scene,rays[i]);
#else
  for (size_t i=0; i<AMBIENT_OCCLUSION_SAMPLES; i++)
    rtcOccluded1M(g_scene,&context,&rays[i],1,sizeof(RTCRay));
#endif
  for (int i=0; i<AMBIENT_OCCLUSION_SAMPLES; i++) {
    if (rays[i].geomID == RTC_INVALID_GEOMETRY_ID)
      intensity += 1.0f;
  }
  return col * (intensity/AMBIENT_OCCLUSION_SAMPLES);
}
Vec3fa ambientOcclusionShading ( int x , int y , RTCRay & ray ) { RTCRay rays [ AMBIENT_OCCLUSION_SAMPLES ] ; Vec3fa Ng = normalize ( ray . Ng ) ; if ( dot ( ray . dir , Ng ) > 0.0f ) Ng = neg ( Ng ) ; Vec3fa col = Vec3fa ( min ( 1.0f , 0.3f + 0.8f * abs ( dot ( Ng , normalize ( ray . dir ) ) ) ) ) ; float intensity = 0 ; Vec3fa hitPos = ray . org + ray . tfar * ray . dir ; RandomSampler sampler ; RandomSampler_init ( sampler , x , y , 0 ) ; for ( int i = 0 ; i < AMBIENT_OCCLUSION_SAMPLES ; i ++ ) { Vec2f s = RandomSampler_get2D ( sampler ) ; Sample3f dir ; dir . v = cosineSampleHemisphere ( s ) ; dir . pdf = cosineSampleHemispherePDF ( dir . v ) ; dir . v = frame ( Ng ) * dir . v ; RTCRay & shadow = rays [ i ] ; shadow . org = hitPos ; shadow . dir = dir . v ; bool mask = 1 ; { shadow . tnear = mask ? 0.001f : ( float ) ( pos_inf ) ; shadow . tfar = mask ? ( float ) ( inf ) : ( float ) ( neg_inf ) ; } shadow . geomID = RTC_INVALID_GEOMETRY_ID ; shadow . primID = RTC_INVALID_GEOMETRY_ID ; shadow . mask = -1 ; shadow . time = 0 ; } RTCIntersectContext context ; context . flags = RAYN_FLAGS ; #if USE_INTERFACE == 0 rtcOccluded1M ( g_scene , & context , rays , AMBIENT_OCCLUSION_SAMPLES , sizeof ( RTCRay ) ) ; #elif USE_INTERFACE == 1 for ( size_t i = 0 ; i < AMBIENT_OCCLUSION_SAMPLES ; i ++ ) rtcOccluded ( g_scene , rays [ i ] ) ; #else for ( size_t i = 0 ; i < AMBIENT_OCCLUSION_SAMPLES ; i ++ ) rtcOccluded1M ( g_scene , & context , & rays [ i ] , 1 , sizeof ( RTCRay ) ) ; #endif for ( int i = 0 ; i < AMBIENT_OCCLUSION_SAMPLES ; i ++ ) { if ( rays [ i ] . geomID == RTC_INVALID_GEOMETRY_ID ) intensity += 1.0f ; } return col * ( intensity / AMBIENT_OCCLUSION_SAMPLES ) ; }
renders a single pixel casting with ambient occlusion
renders a single pixel casting with ambient occlusion
/* calculate hit point */ /* enable only valid rays */ /* sample random direction */ /* initialize shadow ray */ // invalidate inactive rays // FIXME: invalidate inactive rays /* trace occlusion rays */ /* accumulate illumination */ /* shade pixel */