static PixImage RenderSimple(Scene scene, int width, int height, Trafo3d view, Trafo3d proj, Dictionary <uint, EmbreeIndexedGeometry> geos) { var img = new PixImage <byte>(width, height, 4); var mtx = img.GetMatrix <C4b>(); var viewProj = view * proj; var invViewProj = viewProj.Backward; RTCFilterFunction filter = null; //unsafe //{ // filter = new RTCFilterFunction(ptr => // { // //((uint*)ptr->valid)[0] = 0; // }); //} for (int i = 0; i < 20; i++) { var sw = Stopwatch.StartNew(); Parallel.For(0, height, new ParallelOptions(), y => { for (int x = 0; x < width; x++) { var uv = (new V2d(x, y) + 0.5) / new V2d(width, height); var ray = GetCameraRay(uv, invViewProj); var color = GetColor(scene, ray, geos, filter); mtx[x, y] = color; } }); var rayCount = width * height; var raysPerSecond = rayCount / sw.Elapsed.TotalSeconds / 1e6; Report.Line("{0:0.00}MRay/s", raysPerSecond); } return(img); }
public bool Occluded(V3f rayOrigin, V3f rayDirection, float minT = 0.0f, float maxT = float.MaxValue, RTCFilterFunction filter = null) { // NOTE: rtcInitIntersectContext is an inline method -> do manually var ctx = new RTCIntersectContext() { instID = unchecked ((uint)-1), filter = filter != null?Marshal.GetFunctionPointerForDelegate(filter) : IntPtr.Zero, }; var rtRay = new RTCRay() { org = rayOrigin, dir = rayDirection, tnear = minT, tfar = maxT, flags = 0, // must be initialized with 0 time = 0, mask = 0, id = 0, }; unsafe { var ctxPtr = &ctx; var rayPtr = &rtRay; EmbreeAPI.rtcOccluded1(Handle, ctxPtr, rayPtr); } // tfar set to -inf if intersection is found return(rtRay.tfar == float.NegativeInfinity); }
public bool Intersect(V3f rayOrigin, V3f rayDirection, ref RayHit hit, float minT = 0.0f, float maxT = float.MaxValue, RTCFilterFunction filter = null) { // NOTE: rtcInitIntersectContext is an inline method -> do manually var ctx = new RTCIntersectContext() { instID = unchecked ((uint)-1), // need to be initialized with RTC_INVALID_GEOMETRY_ID filter = filter != null?Marshal.GetFunctionPointerForDelegate(filter) : IntPtr.Zero, }; var rayHit = new RTCRayHit() { ray = new RTCRay() { org = rayOrigin, dir = rayDirection, tnear = minT, tfar = maxT, flags = 0, // must be initialized with 0 time = 0, mask = 0, id = 0, }, hit = new RTCHit() { geomID = unchecked ((uint)-1), // must be initialized to RTC_INVALID_GEOMETRY_ID } }; unsafe { var ctxPt = &ctx; var rayHitPt = &rayHit; EmbreeAPI.rtcIntersect1(Handle, ctxPt, rayHitPt); } if (rayHit.hit.geomID != unchecked ((uint)-1)) { hit.T = rayHit.ray.tfar; hit.Coord = rayHit.hit.uv; hit.Normal = rayHit.hit.Ng; hit.PrimitiveId = rayHit.hit.primID; hit.GeometryId = rayHit.hit.geomID; hit.InstanceId = rayHit.hit.instID; return(true); } return(false); }
static C4b GetColor(Scene scene, Ray3d ray, Dictionary <uint, EmbreeIndexedGeometry> geos, RTCFilterFunction filter = null) { var hit = new RayHit(); if (scene.Intersect((V3f)ray.Origin, (V3f)ray.Direction, ref hit, 0, float.MaxValue, filter)) { if (geos.TryGetValue(hit.GeometryId, out var geo)) { var ca = (C4b[])geo.IndexedGeometry.IndexedAttributes[DefaultSemantic.Colors]; return(ca[0]); } } return(C4b.Black); }