示例#1
0
        private void LoadEnvMap()
        {
            if (mTutorial < 5)
            {
                return;
            }

            string texturePath = Path.GetFullPath(EnvMapPath);

            Bitmap image = new Bitmap(Image.FromFile(texturePath));

            if (image == null)
            {
                return;
            }

            var imgData = image.LockBits(new System.DrawingCore.Rectangle(0, 0, image.Width, image.Height),
                                         ImageLockMode.ReadWrite, image.PixelFormat);


            BufferDesc bufferDesc = new BufferDesc()
            {
                Width = (uint)image.Width, Height = (uint)image.Height, Format = Format.UByte4, Type = BufferType.Input
            };
            Buffer textureBuffer = new Buffer(OptixContext, bufferDesc);

            int stride      = imgData.Stride;
            int numChannels = 4;

            unsafe
            {
                byte *src = (byte *)imgData.Scan0.ToPointer();

                BufferStream stream = textureBuffer.Map();
                for (int h = 0; h < image.Height; h++)
                {
                    for (int w = 0; w < image.Width; w++)
                    {
                        UByte4 color = new UByte4(src[(image.Height - h - 1) * stride + w * numChannels + 2],
                                                  src[(image.Height - h - 1) * stride + w * numChannels + 1],
                                                  src[(image.Height - h - 1) * stride + w * numChannels + 0],
                                                  255);

                        stream.Write <UByte4>(color);
                    }
                }
                textureBuffer.Unmap();
            }
            image.UnlockBits(imgData);

            TextureSampler texture = new TextureSampler(OptixContext, TextureSamplerDesc.GetDefault(WrapMode.Repeat));

            texture.SetBuffer(textureBuffer);

            OptixContext["envmap"].Set(texture);
        }
示例#2
0
        protected override void Initialize()
        {
            string rayGenPath = shaderPath;

            /*-----------------------------------------------
             * Create the Optix context
             *-----------------------------------------------*/
            OptixContext = new Context();
            OptixContext.RayTypeCount    = 2;
            OptixContext.EntryPointCount = 1;
            OptixContext.SetStackSize(4096);

            /* Create the ray-generation and exception programs
             *-----------------------------------------------*/
            var rayGen    = new OptixProgram(OptixContext, rayGenPath, mTutorial < 11 ? "pinhole_camera" : "env_camera");
            var exception = new OptixProgram(OptixContext, rayGenPath, "exception");
            var miss      = new OptixProgram(OptixContext, rayGenPath, mTutorial < 5 ? "miss" : "envmap_miss");

            OptixContext["bg_color"].Set(100 / 255.0f, 149 / 255.0f, 237 / 255.0f);
            OptixContext["bad_color"].Set(1.0f, 0.0f, 0.0f);

            OptixContext.SetRayGenerationProgram(0, rayGen);
            OptixContext.SetExceptionProgram(0, exception);
            OptixContext.SetRayMissProgram(0, miss);

            /*-----------------------------------------------
             * Create lights
             *-----------------------------------------------*/
            BasicLight[] lights = new BasicLight[1];
            lights[0].Position    = new Vector3(-5.0f, 60.0f, -16.0f);
            lights[0].Color       = new Vector3(1.0f, 1.0f, 1.0f);
            lights[0].CastsShadow = 1;

            BufferDesc desc = new BufferDesc()
            {
                Width    = (uint)lights.Length,
                Format   = Format.User,
                Type     = BufferType.Input,
                ElemSize = (uint)Marshal.SizeOf(typeof(BasicLight))
            };
            Buffer lightsBuffer = new Buffer(OptixContext, desc);

            lightsBuffer.SetData <BasicLight>(lights);

            OptixContext["lights"].Set(lightsBuffer);

            /*-----------------------------------------------
             * Create noise texture
             *-----------------------------------------------*/
            if (mTutorial >= 8)
            {
                uint noiseTexDim = 64;
                desc = new BufferDesc()
                {
                    Width = noiseTexDim, Height = noiseTexDim, Depth = noiseTexDim, Format = Format.Float, Type = BufferType.Input
                };
                Buffer noiseBuffer = new Buffer(OptixContext, desc);

                Random       rand   = new Random();
                BufferStream stream = noiseBuffer.Map();
                for (int i = 0; i < noiseTexDim * noiseTexDim * noiseTexDim; i++)
                {
                    stream.Write <float>((float)rand.NextDouble());
                }
                noiseBuffer.Unmap();

                TextureSampler noiseTex = new TextureSampler(OptixContext, TextureSamplerDesc.GetDefault(WrapMode.Repeat));
                noiseTex.SetBuffer(noiseBuffer);

                OptixContext["noise_texture"].Set(noiseTex);
            }

            /*-----------------------------------------------
             * Load enivronment map texture
             *-----------------------------------------------*/
            LoadEnvMap();

            /*-----------------------------------------------
             * Load the geometry
             *-----------------------------------------------*/
            CreateGeometry();

            /*-----------------------------------------------
             * Create the output buffer
             *-----------------------------------------------*/
            CreateOutputBuffer(Format.UByte4);
            OptixContext["output_buffer"].Set(OutputBuffer);

            /*-----------------------------------------------
             * Finally compile the optix context, and build the accel tree
             *-----------------------------------------------*/
            SetCamera();

            OptixContext["max_depth"].Set(100);
            OptixContext["radiance_ray_type"].Set(0u);
            OptixContext["shadow_ray_type"].Set(1u);
            OptixContext["frame_number"].Set(0u);
            OptixContext["scene_epsilon"].Set(.001f);
            OptixContext["importance_cutoff"].Set(0.01f);
            OptixContext["ambient_light_color"].Set(0.31f, 0.33f, 0.28f);

            OptixContext.Compile();
            OptixContext.BuildAccelTree();

            //very loose calculation of number of rays
            float numSecondaryRays = 0;

            if (mTutorial >= 9)
            {
                numSecondaryRays = 2.5f; //only convex hull casts refraction rays
            }
            else if (mTutorial >= 8)
            {
                numSecondaryRays = 2;
            }
            else if (mTutorial >= 4)
            {
                numSecondaryRays = 1.5f; //only the floor casts reflection rays, so give aproximate
            }
            else if (mTutorial >= 3)
            {
                numSecondaryRays = 1;
            }

            RaysTracedPerFrame = (int)(Width * Height * (numSecondaryRays + 1));
        }