/* task that renders a single screen tile */ Vec3fa renderPixelStandard(float x, float y, const Vec3fa& vx, const Vec3fa& vy, const Vec3fa& vz, const Vec3fa& p) { /* initialize ray */ RTCRay ray; ray.org = p; ray.dir = normalize(x*vx + y*vy + vz); ray.tnear = 0.0f; ray.tfar = inf; ray.geomID = RTC_INVALID_GEOMETRY_ID; ray.primID = RTC_INVALID_GEOMETRY_ID; ray.mask = -1; ray.time = 0; /* intersect ray with scene */ rtcIntersect(g_scene,ray); /* shade pixels */ Vec3fa color = Vec3fa(0.0f); if (ray.geomID != RTC_INVALID_GEOMETRY_ID) { Vec3fa diffuse = ray.geomID != 0 ? Vec3fa(0.9f,0.6f,0.5f) : Vec3fa(0.8f,0.0f,0.0f); color = color + diffuse*0.5f; Vec3fa lightDir = normalize(Vec3fa(-1,-1,-1)); Vec3fa Ng = normalize(ray.Ng); #if ENABLE_SMOOTH_NORMALS Vec3fa P = ray.org + ray.tfar*ray.dir; if (ray.geomID > 0) { Vec3fa dPdu,dPdv; int geomID = ray.geomID; { rtcInterpolate(g_scene,geomID,ray.primID,ray.u,ray.v,RTC_VERTEX_BUFFER,nullptr,&dPdu.x,&dPdv.x,3); } Ng = normalize(cross(dPdv,dPdu)); dPdu = dPdu + Ng*displacement_du(P,dPdu); dPdv = dPdv + Ng*displacement_dv(P,dPdv); Ng = normalize(cross(dPdv,dPdu)); } #endif /* initialize shadow ray */ RTCRay shadow; shadow.org = ray.org + ray.tfar*ray.dir; shadow.dir = neg(lightDir); shadow.tnear = 0.001f; shadow.tfar = inf; shadow.geomID = RTC_INVALID_GEOMETRY_ID; shadow.primID = RTC_INVALID_GEOMETRY_ID; shadow.mask = -1; shadow.time = 0; /* trace shadow ray */ rtcOccluded(g_scene,shadow); /* add light contribution */ if (shadow.geomID == RTC_INVALID_GEOMETRY_ID) color = color + diffuse*clamp(-(dot(lightDir,Ng)),0.0f,1.0f); } return color; }
/* task that renders a single screen tile */ Vec3fa renderPixelStandard(float x, float y, const ISPCCamera& camera) { /* initialize ray */ RTCRay ray; ray.org = Vec3fa(camera.xfm.p); ray.dir = Vec3fa(normalize(x*camera.xfm.l.vx + y*camera.xfm.l.vy + camera.xfm.l.vz)); ray.tnear = 0.0f; ray.tfar = inf; ray.geomID = RTC_INVALID_GEOMETRY_ID; ray.primID = RTC_INVALID_GEOMETRY_ID; ray.mask = -1; ray.time = 0; /* intersect ray with scene */ rtcIntersect(g_scene,ray); /* shade pixels */ Vec3fa color = Vec3fa(0.0f); if (ray.geomID != RTC_INVALID_GEOMETRY_ID) { /* interpolate diffuse color */ Vec3fa diffuse = Vec3fa(1.0f,0.0f,0.0f); if (ray.geomID > 0) { unsigned int geomID = ray.geomID; { rtcInterpolate(g_scene,geomID,ray.primID,ray.u,ray.v,RTC_USER_VERTEX_BUFFER0,&diffuse.x,nullptr,nullptr,3); } diffuse = 0.5f*diffuse; } /* calculate smooth shading normal */ Vec3fa Ng = normalize(ray.Ng); color = color + diffuse*0.5f; Vec3fa lightDir = normalize(Vec3fa(-1,-1,-1)); /* initialize shadow ray */ RTCRay shadow; shadow.org = ray.org + ray.tfar*ray.dir; shadow.dir = neg(lightDir); shadow.tnear = 0.001f; shadow.tfar = inf; shadow.geomID = 1; shadow.primID = 0; shadow.mask = -1; shadow.time = 0; /* trace shadow ray */ rtcOccluded(g_scene,shadow); /* add light contribution */ if (shadow.geomID) { Vec3fa r = normalize(reflect(ray.dir,Ng)); float s = pow(clamp(dot(r,lightDir),0.0f,1.0f),10.0f); float d = clamp(-dot(lightDir,Ng),0.0f,1.0f); color = color + diffuse*d + 0.5f*Vec3fa(s); } } return color; }
/* task that renders a single screen tile */ Vec3fa renderPixelStandard(float x, float y, const ISPCCamera& camera, RayStats& stats) { /* initialize sampler */ RandomSampler sampler; RandomSampler_init(sampler, (int)x, (int)y, 0); /* initialize ray */ RTCRay ray; ray.org = Vec3fa(camera.xfm.p); ray.dir = Vec3fa(normalize(x*camera.xfm.l.vx + y*camera.xfm.l.vy + camera.xfm.l.vz)); ray.tnear = 0.0f; ray.tfar = inf; ray.geomID = RTC_INVALID_GEOMETRY_ID; ray.primID = RTC_INVALID_GEOMETRY_ID; ray.mask = -1; ray.time = RandomSampler_get1D(sampler); /* intersect ray with scene */ RTCIntersectContext context; context.flags = g_iflags_coherent; rtcIntersect1Ex(g_scene,&context,ray); RayStats_addRay(stats); /* shade background black */ if (ray.geomID == RTC_INVALID_GEOMETRY_ID) { return Vec3fa(0.0f); } /* shade all rays that hit something */ Vec3fa color = Vec3fa(0.5f); /* compute differential geometry */ DifferentialGeometry dg; dg.geomID = ray.geomID; dg.primID = ray.primID; dg.u = ray.u; dg.v = ray.v; dg.P = ray.org+ray.tfar*ray.dir; dg.Ng = ray.Ng; dg.Ns = ray.Ng; if (g_use_smooth_normals) if (ray.geomID != RTC_INVALID_GEOMETRY_ID) // FIXME: workaround for ISPC bug, location reached with empty execution mask { Vec3fa dPdu,dPdv; unsigned int geomID = ray.geomID; { rtcInterpolate(g_scene,geomID,ray.primID,ray.u,ray.v,RTC_VERTEX_BUFFER0,nullptr,&dPdu.x,&dPdv.x,3); } dg.Ns = cross(dPdv,dPdu); } int materialID = postIntersect(ray,dg); dg.Ng = face_forward(ray.dir,normalize(dg.Ng)); dg.Ns = face_forward(ray.dir,normalize(dg.Ns)); /* shade */ if (g_ispc_scene->materials[materialID]->type == MATERIAL_OBJ) { ISPCOBJMaterial* material = (ISPCOBJMaterial*) g_ispc_scene->materials[materialID]; color = Vec3fa(material->Kd); } return color*dot(neg(ray.dir),dg.Ns); }