Exemple #1
0
        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);
        }
Exemple #2
0
        public void Attach(EmbreeGeometry geometry, uint id)
        {
            EmbreeAPI.rtcAttachGeometryByID(Handle, geometry.Handle, id);
            m_device.CheckError("AddGeometry");

            m_geometries.Add(id, geometry);
        }
        /// <summary>
        /// Creates a raytracing device
        /// </summary>
        /// <param name="theradCount">0 uses all thread</param>
        public Device(int threadCount = 0)
        {
            ThreadCount = threadCount;

            var config = String.Format("threads={0}", threadCount);

            Handle = EmbreeAPI.rtcNewDevice(config);
        }
Exemple #4
0
        public uint Attach(EmbreeGeometry geometry)
        {
            var id = EmbreeAPI.rtcAttachGeometry(Handle, geometry.Handle);

            m_device.CheckError("AddGeometry");

            m_geometries.Add(id, geometry);

            return(id);
        }
        /// <summary>
        /// Checks and reports if there is a DeviceError.
        /// </summary>
        /// <returns>true if there was an error</returns>
        public bool CheckError(string msg)
        {
            var err = EmbreeAPI.rtcGetDeviceError(Handle);

            if (err != RTCDeviceError.None)
            {
                Report.Warn("[Embree] {0}: {1}", err, msg);
                return(true);
            }
            return(false);
        }
Exemple #6
0
        public Scene(Device device, RTCBuildQuality quality, bool dyamic)
        {
            m_device = device;
            Handle   = EmbreeAPI.rtcNewScene(device.Handle);
            var flags = RTCSceneFlags.Robust | RTCSceneFlags.ContextFilterFunction;

            if (dyamic)
            {
                flags |= RTCSceneFlags.Dynamic;
            }
            EmbreeAPI.rtcSetSceneFlags(Handle, flags);
            EmbreeAPI.rtcSetSceneBuildQuality(Handle, quality);
        }
Exemple #7
0
        public EmbreeBuffer(Device device, Array data)
        {
            var elementType = data.GetType().GetElementType();

            m_size = (ulong)Marshal.SizeOf(elementType) * (ulong)data.Length;

            Handle    = EmbreeAPI.rtcNewBuffer(device.Handle, m_size);
            m_dataPtr = EmbreeAPI.rtcGetBufferData(Handle);

            Upload(data);

            device.CheckError("CreateBuffer");
        }
Exemple #8
0
        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);
        }
Exemple #9
0
        /// <summary>
        /// Single geometry instance
        /// </summary>
        public GeometryInstance(Device device, EmbreeGeometry geometry, Affine3f transform)
            : base(device, RTCGeometryType.Instance)
        {
            m_scene = EmbreeAPI.rtcNewScene(device.Handle);
            EmbreeAPI.rtcSetSceneFlags(m_scene, RTCSceneFlags.Robust | RTCSceneFlags.ContextFilterFunction);
            EmbreeAPI.rtcSetSceneBuildQuality(m_scene, RTCBuildQuality.High);
            EmbreeAPI.rtcAttachGeometry(m_scene, geometry.Handle);
            EmbreeAPI.rtcCommitScene(m_scene);

            EmbreeAPI.rtcSetGeometryInstancedScene(Handle, m_scene);
            EmbreeAPI.rtcSetGeometryTimeStepCount(Handle, 1);
            Transform = transform;

            Commit();
        }
        public TriangleGeometry(Device device, V3f[] vertices, int[] triangleIndices, RTCBuildQuality quality)
            : base(device, RTCGeometryType.Triangle, quality)
        {
            m_vertices = new EmbreeBuffer(device, vertices);
            m_indices  = new EmbreeBuffer(device, triangleIndices);

            // triangle index buffer needs to be UINT3
            EmbreeAPI.rtcSetGeometryBuffer(Handle, RTCBufferType.Index, 0, RTCFormat.UINT3, m_indices.Handle, 0, sizeof(int) * 3, (ulong)(triangleIndices.Length / 3));
            // triangle vertex needs to be FLOAT3
            EmbreeAPI.rtcSetGeometryBuffer(Handle, RTCBufferType.Vertex, 0, RTCFormat.FLOAT3, m_vertices.Handle, 0, sizeof(float) * 3, (ulong)vertices.Length);

            Commit();

            device.CheckError("Create TriangleGeometry");
        }
        /// <summary>
        /// Create TriangleGeometry from buffers. Indices expected to be int32 and vertices to be V3f.
        /// </summary>
        public TriangleGeometry(Device device, EmbreeBuffer vertexBuffer, int vertexOffset, int vertexCount, EmbreeBuffer indexBuffer, int indexOffset, int triangleCount, RTCBuildQuality quality)
            : base(device, RTCGeometryType.Triangle, quality)
        {
            EmbreeAPI.rtcRetainBuffer(vertexBuffer.Handle);
            EmbreeAPI.rtcRetainBuffer(indexBuffer.Handle);
            m_vertices = vertexBuffer;
            m_indices  = indexBuffer;

            // triangle index buffer needs to be UINT3
            EmbreeAPI.rtcSetGeometryBuffer(Handle, RTCBufferType.Index, 0, RTCFormat.UINT3, m_indices.Handle, (ulong)indexOffset * sizeof(int), sizeof(int) * 3, (ulong)triangleCount);
            // triangle vertex needs to be FLOAT3
            EmbreeAPI.rtcSetGeometryBuffer(Handle, RTCBufferType.Vertex, 0, RTCFormat.FLOAT3, m_vertices.Handle, (ulong)vertexOffset, sizeof(float) * 3, (ulong)vertexCount);

            Commit();

            device.CheckError("Create TriangleGeometry");
        }
Exemple #12
0
 public void Commit()
 {
     EmbreeAPI.rtcCommitScene(Handle);
 }
Exemple #13
0
 public void Dispose()
 {
     EmbreeAPI.rtcReleaseBuffer(Handle);
     Handle = IntPtr.Zero;
 }
 public virtual void Dispose()
 {
     EmbreeAPI.rtcReleaseGeometry(Handle);
     Handle = IntPtr.Zero;
 }
 public void Commit()
 {
     EmbreeAPI.rtcCommitGeometry(Handle);
 }
Exemple #16
0
 public override void Dispose()
 {
     EmbreeAPI.rtcReleaseScene(m_scene);
     m_scene = IntPtr.Zero;
     base.Dispose();
 }
 protected EmbreeGeometry(Device device, RTCGeometryType type, RTCBuildQuality quality)
 {
     Handle = EmbreeAPI.rtcNewGeometry(device.Handle, type);
     EmbreeAPI.rtcSetGeometryBuildQuality(Handle, quality);
 }
 public void Dispose()
 {
     EmbreeAPI.rtcReleaseDevice(Handle);
     Handle = IntPtr.Zero;
 }
 protected EmbreeGeometry(Device device, RTCGeometryType type)
 {
     Handle = EmbreeAPI.rtcNewGeometry(device.Handle, type);
 }