Ejemplo n.º 1
0
        private OptixBuffer CreateBuffer(float[] data)
        {
            BufferDesc spdBuffDesc = new BufferDesc
            {
                Width  = (ulong)data.Length,
                Format = Format.Float,
                //ElemSize = 0,
                Type = BufferType.Input
            };
            var x = new OptixBuffer(OptixContext, spdBuffDesc);

            x.SetData(data);
            return(x);
        }
Ejemplo n.º 2
0
        protected virtual void SaveImage(string fileName, OptixBuffer outputBuffer = null)
        {
            if (string.IsNullOrWhiteSpace(fileName))
            {
                return;
            }

            var buffer       = outputBuffer ?? OutputBuffer;
            var bufferStream = buffer.Map();
            var data         = new Vector4[bufferStream.Length / 16];
            var index        = 0;

            while (bufferStream.CanRead && index <= data.Length - 1)
            {
                data[index++] = bufferStream.Read <Vector4>();
            }
            buffer.Unmap();
            //ImageFactory.SaveExr(fileName, Width, Height, data);
            var bmp = new System.DrawingCore.Bitmap(Width, Height);

            //var rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
            //var bmpData = bmp.LockBits(rect, ImageLockMode.ReadWrite, bmp.PixelFormat);
            for (var y = 0; y < Height; y++)
            {
                for (var x = 0; x < Width; x++)
                {
                    var c = data[x + y * Width];
                    bmp.SetPixel(x, y, ConvertColor(ref c));
                }
            }
            var stats = GetStats();//string.Format("Tone map:{0}; Shadow rays {1};Max depth {2}", ToneMapping, maxShadowRays, maxDepth);

            if (!string.IsNullOrWhiteSpace(stats))
            {
                ApplyWaterMark(10, Height - 8 - /*stats.Length */ 8, bmp, new[] { stats });
            }

            bmp.Save(ImageSaveDir + Path.GetFileNameWithoutExtension(fileName) + ".png");

            Trace.WriteLine(fileName + " saved.");
            bmp.Dispose();
        }
Ejemplo n.º 3
0
        private void CreateLights()
        {
            var light = new ParallelogramLight
            {
                corner = new Vector3(2.8096f, 17.1165f, 2.3659f),
                v1     = new Vector3(-5.0f, 0.0f, 0.0f),
                v2     = new Vector3(0.0f, 0.0f, 5.0f)
            };

            light.normal   = Vector3.Normalize(Vector3.Cross(light.v1, light.v2));
            light.emission = new Vector3(15.0f, 15.0f, 5.0f);

            var desc = new BufferDesc {
                Width = 1u, Format = Format.User, Type = BufferType.Input, ElemSize = (uint)Marshal.SizeOf(typeof(ParallelogramLight))
            };
            var lightBuffer = new OptixBuffer(OptixContext, desc);
            var stream      = lightBuffer.Map();

            stream.Write(light);
            lightBuffer.Unmap();

            OptixContext["plights"].Set(lightBuffer);
        }
Ejemplo n.º 4
0
        protected override void CreateGeometry()
        {
            base.CreateGeometry();

            if (!GenerateGeometry)
            {
                return;
            }

            //create buffer descriptions
            var vDesc = new BufferDesc()
            {
                Width = (uint)Positions.Count, Format = Format.Float3, Type = BufferType.Input
            };
            var nDesc = new BufferDesc()
            {
                Width = (uint)Normals.Count, Format = Format.Float3, Type = BufferType.Input
            };
            var tcDesc = new BufferDesc()
            {
                Width = (uint)Texcoords.Count, Format = Format.Float2, Type = BufferType.Input
            };

            // Create the buffers to hold our geometry data
            var vBuffer  = new OptixBuffer(Context, vDesc);
            var nBuffer  = new OptixBuffer(Context, nDesc);
            var tcBuffer = new OptixBuffer(Context, tcDesc);

            vBuffer.SetData <Vector3>(Positions.ToArray());
            nBuffer.SetData <Vector3>(Normals.ToArray());
            tcBuffer.SetData <Vector2>(Texcoords.ToArray());

            List <GeometryInstance> instances = new List <GeometryInstance>();

            foreach (ObjGroup group in Groups)
            {
                //empty group
                if (group.VIndices.Count == 0 && group.NIndices.Count == 0 && group.TIndices.Count == 0)
                {
                    continue;
                }

                //ValidateGroup( group );

                var normalsUseVIndices = GenerateNormals && group.NIndices.Count == 0 && Normals.Count > 0;

                if (normalsUseVIndices)
                {
                    Debug.Assert(Normals.Count == Positions.Count);
                }

                var numNormIndices = normalsUseVIndices ? group.VIndices.Count : group.NIndices.Count;

                var viDesc = new BufferDesc {
                    Width = (uint)group.VIndices.Count, Format = Format.Int3, Type = BufferType.Input
                };
                var niDesc = new BufferDesc {
                    Width = (uint)numNormIndices, Format = Format.Int3, Type = BufferType.Input
                };
                var tiDesc = new BufferDesc {
                    Width = (uint)group.TIndices.Count, Format = Format.Int3, Type = BufferType.Input
                };

                var viBuffer = new OptixBuffer(Context, viDesc);
                var niBuffer = new OptixBuffer(Context, niDesc);
                var tiBuffer = new OptixBuffer(Context, tiDesc);

                viBuffer.SetData(group.VIndices.ToArray());
                //if normals weren't in the obj and we genereated them, use the vertex indices
                niBuffer.SetData(normalsUseVIndices ? group.VIndices.ToArray() : group.NIndices.ToArray());
                tiBuffer.SetData(group.TIndices.ToArray());

                //create a geometry node and set the buffers
                var geometry = new Geometry(Context);
                geometry.IntersectionProgram = new OptixProgram(Context, IntersecitonProgPath, IntersecitonProgName);
                geometry.BoundingBoxProgram  = new OptixProgram(Context, BoundingBoxProgPath, BoundingBoxProgName);
                geometry.PrimitiveCount      = (uint)group.VIndices.Count;

                geometry["vertex_buffer"].Set(vBuffer);
                geometry["normal_buffer"].Set(nBuffer);
                geometry["texcoord_buffer"].Set(tcBuffer);
                geometry["vindex_buffer"].Set(viBuffer);
                geometry["nindex_buffer"].Set(niBuffer);
                geometry["tindex_buffer"].Set(tiBuffer);

                //create a geometry instance
                GeometryInstance instance = new GeometryInstance(Context);
                instance.Geometry = geometry;
                instance.AddMaterial(_materialResolveFunc(group.mtrl) ?? DefaultMaterial);

                if (group.mtrl != null)
                {
                    ObjMaterial mtrl = mMtrls[group.mtrl];
                    instance["diffuse_color"].SetFloat3(ref mtrl.Kd);
                    instance["emission_color"].SetFloat3(ref mtrl.Ke);
                }
                else
                {
                    instance["diffuse_color"].Set(1.0f, 1.0f, 1.0f);
                }

                instances.Add(instance);
            }

            //create an acceleration structure for the geometry
            var accel = new Acceleration(Context, Builder, Traverser);

            if (Builder == AccelBuilder.Sbvh || Builder == AccelBuilder.TriangleKdTree)
            {
                accel.VertexBufferName = "vertex_buffer";
                accel.IndexBufferName  = "vindex_buffer";
            }

            //now attach the instance and accel to the geometry group
            GeoGroup.Acceleration = accel;
            GeoGroup.AddChildren(instances);
        }
Ejemplo n.º 5
0
 protected void CreateOutputBuffer(Format format)
 {
     this.OutputBuffer = CreateOutputBuffer(format, Width, Height);
 }