protected override void Initialize() { base.Initialize(); var modelName = "sibenik.obj"; var modelPath = Path.GetFullPath(@"..\..\..\..\..\Assets\Models\" + modelName); OptixContext = new Context(); mTraversal = new TraversalEngine(OptixContext, QueryType.ClosestHit, RayFormat.OriginDirectionInterleaved, TriFormat.Mesh, TraversalOutput.Normal, InitOptions.None); mTraversal.SetCpuThreadCount(1); //mTraversal.NumCpuThreads = Context.CpuNumThreads; var model = new OptixOBJLoader(modelPath, OptixContext, null, null, n => null); //OBJLoader normally automatically creates Geometry, GeometryInstances, and GeometryGroups for Optix //but the Traversal API doesn't use that, so turn that off model.ParseMaterials = false; model.ParseNormals = false; model.GenerateNormals = false; model.GenerateGeometry = false; model.LoadContent(); SetCamera(model.BBox); //we copy the data here or else the GC would clean up a model.Vertices.ToArray() for example var verts = new Vector3[model.Positions.Count]; var tris = new Int3[model.Groups[0].VIndices.Count]; model.Positions.CopyTo(verts); model.Groups[0].VIndices.CopyTo(tris); var indexes = tris.SelectMany(c => new[] { c.X, c.Y, c.Z }).ToArray(); mTraversal.SetMesh(verts, indexes); int numRays = Width * Height; var rays = CreateRays(); mTraversal.SetRayData <TraversalEngine.Ray>(rays); mTraversal.Preprocess(); mDepths = new float[numRays]; mNormals = new Vector3[numRays]; RaysTracedPerFrame = numRays; Console.WriteLine("Accel data size " + mTraversal.AccelDataSize); using (var accData = mTraversal.AccelData) { var size = accData.Length; var buffer = new byte[size]; accData.ReadRange(buffer, 0, (int)size); var nonZeros = buffer.Count(b => b != 0); Console.WriteLine("Non zeros in acc data " + nonZeros); } }
protected override void Initialize() { base.Initialize(); var modelName = "sibenik.obj"; var modelPath = Path.GetFullPath(@"..\..\..\..\..\Assets\Models\" + modelName); mTraversal = new PrimeEngine(RayFormat.OriginDirectionMinMaxInterleaved, RayHitType.Closest); var model = new OBJLoader(modelPath); model.ParseMaterials = false; model.ParseNormals = false; model.GenerateNormals = false; model.GenerateGeometry = false; model.LoadContent(); SetCamera(model.BBox); var verts = new Vector3[model.Positions.Count]; var tris = new Int3[model.Groups[0].VIndices.Count]; model.Positions.CopyTo(verts); model.Groups[0].VIndices.CopyTo(tris); var indexes = tris.SelectMany(c => new[] { c.X, c.Y, c.Z }).ToArray(); mTraversal.SetMesh(verts, indexes); var numRays = Width * Height; rays = CreateRays(); mTraversal.SetRays(rays); mDepths = new float[numRays]; mNormals = new Vector3[numRays]; RaysTracedPerFrame = numRays; }
private static void IntersectionTest() { ICamera Camera; int Width = 320, Height = 200; Camera = new Camera(); Camera.Aspect = (float)Width / (float)Height; Camera.Fov = 30; Camera.RotationVel = 100.0f; Camera.TranslationVel = 500.0f; //sibenik camera position //Camera.LookAt(new Vector3(-19.5f, -10.3f, .8f), new Vector3(0.0f, -13.3f, .8f), Vector3.UnitY); PrimeEngine.Ray[] CreateRays() { var rays = new PrimeEngine.Ray[Width * Height]; for (int x = 0; x < Width; x++) { for (int y = 0; y < Height; y++) { Vector2 d = new Vector2(x, y) / new Vector2(Width, Height) * 2.0f - new Vector2(1.0f); PrimeEngine.Ray ray = new PrimeEngine.Ray { origin = Camera.Position, tmin = 1e-4f, dir = (d.X * Camera.Right + d.Y * Camera.Up + Camera.Look).NormalizedCopy(), tmax = 1e34f }; rays[y * Width + x] = ray; } } return(rays); } using (var engine = new PrimeEngine(RayFormat.OriginDirectionMinMaxInterleaved, RayHitType.Closest, RTPBufferType.CudaLinear, false)) { var modelName = "teapot.obj"; var modelPath = Path.GetFullPath(@"..\..\..\..\Assets\Models\" + modelName); var model = new OBJLoader(modelPath); model.ParseMaterials = false; model.ParseNormals = false; model.GenerateNormals = false; model.GenerateGeometry = false; model.LoadContent(); Camera.CenterOnBoundingBox(model.BBox); var verts = new Vector3[model.Positions.Count]; var tris = new Int3[model.Groups[0].VIndices.Count]; model.Positions.CopyTo(verts); model.Groups[0].VIndices.CopyTo(tris); var indexes = tris.SelectMany(c => new[] { Math.Abs(c.X), Math.Abs(c.Y), Math.Abs(c.Z) }).ToArray(); Console.WriteLine("Setting Mesh"); engine.SetMesh(verts, indexes); Console.WriteLine("Setting Rays"); engine.SetRays(CreateRays()); Console.WriteLine("Querying Prime"); var hits = engine.Query(); Console.WriteLine($"Successful hits {hits.Count(p => p.t < 1e34f && p.t > 1e-4f)}"); } }