Exemplo n.º 1
0
        public void Show()
        {
            GL.ClearColor(0, 0, 0, 1);
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

            GL.ActiveTexture(TextureUnit.Texture0);
            GL.BindTexture(TextureTarget.Texture2D, averageTexture);

            LayerHelper.DrawQuad();
            GL.BindTexture(TextureTarget.Texture2D, 0);
        }
Exemplo n.º 2
0
        public void PostDraw()
        {
            // target texture
            GL.FramebufferTexture2D(FramebufferTarget.FramebufferExt,
                                    FramebufferAttachment.ColorAttachment0Ext, TextureTarget.Texture2D,
                                    updatedAverageTexture, 0);

            //LayerHelper.CheckFbo();

            GL.ClearColor(0, 0, 0, 1);
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

            // source textures
            GL.ActiveTexture(TextureUnit.Texture0);
            GL.BindTexture(TextureTarget.Texture2D, currentFrameTexture);
            GL.ActiveTexture(TextureUnit.Texture1);
            GL.BindTexture(TextureTarget.Texture2D, averageTexture);

            GL.UseProgram(accumShaderProgram);

            GL.Uniform1(GL.GetUniformLocation(accumShaderProgram, "currentFrameTexture"), 0); // TextureUnit.Texture0
            GL.Uniform1(GL.GetUniformLocation(accumShaderProgram, "averageTexture"), 1);      // TextureUnit.Texture1
            float frameWeight = 1 / (float)(iteration + 1);

            GL.Uniform1(GL.GetUniformLocation(accumShaderProgram, "frameWeight"), frameWeight);

            // accumulate the current frame
            // - read current frame and average textures
            // - make linear interpolation thereof inside a fragment shader
            // - render the result into updated average texture
            LayerHelper.DrawQuad();

            GL.UseProgram(0);

            GL.ActiveTexture(TextureUnit.Texture1);
            GL.BindTexture(TextureTarget.Texture2D, 0);
            GL.ActiveTexture(TextureUnit.Texture0);
            GL.BindTexture(TextureTarget.Texture2D, 0);

            //swap the two average textures
            uint tmp = updatedAverageTexture;

            updatedAverageTexture = averageTexture;
            averageTexture        = tmp;

            iteration++;
        }
Exemplo n.º 3
0
        public void Draw()
        {
            Debug.Assert(DepthPeeler != null);

            GL.ClearColor(0f, 0f, 0f, 1f);
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

            GL.UseProgram(shaderProgram);
            GL.Uniform1(GL.GetUniformLocation(shaderProgram, "layerTexture"), 0);
            GL.Uniform1(GL.GetUniformLocation(shaderProgram, "layerIndex"), GetLayerIndex());

            GL.ActiveTexture(TextureUnit.Texture0);
            GL.BindTexture(TextureTarget.Texture2DArray, GetTexture());
            LayerHelper.DrawQuad();
            GL.BindTexture(TextureTarget.Texture2DArray, 0);

            GL.UseProgram(0);
        }
Exemplo n.º 4
0
        public void Draw()
        {
            Debug.Assert(NBuffers != null);

            GL.ClearColor(0f, 0f, 0f, 1f);
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

            GL.UseProgram(shaderProgram);
            GL.Uniform1(GL.GetUniformLocation(shaderProgram, "nBufferLayerTexture"), 0);
            GL.Uniform2(GL.GetUniformLocation(shaderProgram, "colorMask"), colorMasks[selectedColorMask]);
            GL.Uniform1(GL.GetUniformLocation(shaderProgram, "layer"), selectedLayer);

            GL.ActiveTexture(TextureUnit.Texture0);
            GL.BindTexture(TextureTarget.Texture2DArray, NBuffers.NBuffersTextures);

            LayerHelper.DrawQuad();

            GL.BindTexture(TextureTarget.Texture2D, 0);

            GL.UseProgram(0);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Packs four single-channel depth images into one four-channel image.
        /// </summary>
        private void PackDepthImages()
        {
            // for visualization
            //GL.Ext.BindFramebuffer(FramebufferTarget.FramebufferExt, 0);

            GL.ClearColor(0f, 0f, 0f, 1f);
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

            GL.Ext.FramebufferTextureLayer(
                FramebufferTarget.FramebufferExt, FramebufferAttachment.ColorAttachment0Ext,
                packedDepthTextures, 0, 0);

            var result = GL.Ext.CheckFramebufferStatus(FramebufferTarget.FramebufferExt);

            if (result != FramebufferErrorCode.FramebufferCompleteExt)
            {
                throw new ApplicationException(string.Format("Bad FBO: {0}", result));
            }

            GL.UseProgram(packingShaderProgram);

            GL.ActiveTexture(TextureUnit.Texture0);
            GL.BindTexture(TextureTarget.Texture2DArray, depthTextures);
            GL.Uniform1(GL.GetUniformLocation(packingShaderProgram, "depthTexture"), 0);

            LayerHelper.DrawQuad();

            GL.UseProgram(0);

            GL.Ext.BindFramebuffer(FramebufferTarget.FramebufferExt, 0);

            GL.ActiveTexture(TextureUnit.Texture0);

            //GL.BindTexture(TextureTarget.Texture2DArray, packedDepthTextures);
            //GL.Ext.GenerateMipmap(GenerateMipmapTarget.Texture2DArray);

            GL.BindTexture(TextureTarget.Texture2D, 0);
        }
Exemplo n.º 6
0
        protected override void DrawSingleFrame(int iteration, Scene scene, Navigation navigation)
        {
            Debug.Assert(DepthPeeler != null);

            Camera camera = navigation.Camera;

            ComputeSensorTransform(camera);

            GL.ClearColor(0f, 0f, 0f, 1f);
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

            // bind color and depth textures
            GL.ActiveTexture(TextureUnit.Texture0);
            int lensSampleTex = IncrementalModeEnabled
                ? incrementalLensSampleTexture
                : previewLensSampleTexture;

            GL.BindTexture(TextureTarget.Texture3D, lensSampleTex);
            //GL.ActiveTexture(TextureUnit.Texture1);
            //GL.BindTexture(TextureTarget.Texture1D, pixelSampleTexture);
            GL.ActiveTexture(TextureUnit.Texture2);
            GL.BindTexture(TextureTarget.Texture2DArray, DepthPeeler.PackedDepthTextures);
            GL.ActiveTexture(TextureUnit.Texture3);
            GL.BindTexture(TextureTarget.Texture2DArray, DepthPeeler.ColorTextures);
            GL.ActiveTexture(TextureUnit.Texture4);
            GL.BindTexture(TextureTarget.Texture2DArray, NBuffers.NBuffersTextures);

            // enable IBRT shader
            GL.UseProgram(shaderProgram);

            // set shader parameters (textures, lens model, ...)
            GL.Uniform1(GL.GetUniformLocation(shaderProgram, "lensSamplesTexture"), 0);
            //GL.Uniform1(GL.GetUniformLocation(shaderProgram, "pixelSamplesTexture"), 1);
            GL.Uniform1(GL.GetUniformLocation(shaderProgram, "packedDepthTexture"), 2);
            GL.Uniform1(GL.GetUniformLocation(shaderProgram, "colorTexture"), 3);
            GL.Uniform1(GL.GetUniformLocation(shaderProgram, "nBuffersTexture"), 4);

            GL.Uniform2(GL.GetUniformLocation(shaderProgram, "nBuffersSize"), NBuffers.Size);

            GL.Uniform2(GL.GetUniformLocation(shaderProgram, "sensorSize"), camera.SensorSize);
            //GL.Uniform1(GL.GetUniformLocation(shaderProgram, "sensorZ"), camera.SensorZ);
            GL.Uniform3(GL.GetUniformLocation(shaderProgram, "sensorShift"), camera.SensorShift3);
            GL.Uniform1(GL.GetUniformLocation(shaderProgram, "near"), camera.Near);
            GL.Uniform1(GL.GetUniformLocation(shaderProgram, "far"), camera.Far);
            GL.Uniform1(GL.GetUniformLocation(shaderProgram, "lensFocalLength"), camera.Lens.FocalLength);
            GL.Uniform1(GL.GetUniformLocation(shaderProgram, "lensApertureRadius"), camera.Lens.ApertureRadius);
            //Matrix4 perspective = camera.Perspective;
            //GL.UniformMatrix4(GL.GetUniformLocation(shaderProgram, "perspective"), false, ref perspective);
            GL.Uniform4(GL.GetUniformLocation(shaderProgram, "frustumBounds"), camera.FrustumBounds);

            // jittering
            GL.Uniform2(GL.GetUniformLocation(shaderProgram, "screenSize"), new Vector2(Width, Height));
            GL.Uniform2(GL.GetUniformLocation(shaderProgram, "screenSizeInv"), new Vector2(1.0f / Width, 1.0f / Height));
            GL.Uniform1(GL.GetUniformLocation(shaderProgram, "sampleCount"), (IncrementalModeEnabled ? SampleCount : PreviewSampleCount));
            GL.Uniform1(GL.GetUniformLocation(shaderProgram, "sampleCountInv"), 1.0f / (IncrementalModeEnabled ? SampleCount : PreviewSampleCount));
            GL.Uniform1(GL.GetUniformLocation(shaderProgram, "sampleIndexOffset"), IncrementalModeEnabled ? SampleCount * iteration : 0);
            GL.Uniform1(GL.GetUniformLocation(shaderProgram, "totalSampleCount"), IncrementalModeEnabled ? MaxTotalSampleCount : PreviewSampleCount);
            //GL.Uniform1(GL.GetUniformLocation(shaderProgram, "sampleCount"), SampleCount);
            //GL.Uniform1(GL.GetUniformLocation(shaderProgram, "sampleCountInv"), 1.0f / SampleCount);
            //GL.Uniform1(GL.GetUniformLocation(shaderProgram, "sampleIndexOffset"), SampleCount * iteration);
            //GL.Uniform1(GL.GetUniformLocation(shaderProgram, "totalSampleCount"), TotalSampleCount);

            GL.Uniform2(GL.GetUniformLocation(shaderProgram, "cameraShift"), camera.LensShift);
            GL.UniformMatrix3(GL.GetUniformLocation(shaderProgram, "sensorTransform"), 1, false, sensorTransform3x3);

            // draw the quad
            LayerHelper.DrawQuad();

            // disable shader
            GL.UseProgram(0);

            // unbind textures
            GL.ActiveTexture(TextureUnit.Texture4);
            GL.BindTexture(TextureTarget.Texture2D, 0);
            GL.ActiveTexture(TextureUnit.Texture3);
            GL.BindTexture(TextureTarget.Texture2D, 0);
            GL.ActiveTexture(TextureUnit.Texture2);
            GL.BindTexture(TextureTarget.Texture2D, 0);
            GL.ActiveTexture(TextureUnit.Texture1);
            GL.BindTexture(TextureTarget.Texture2D, 0);
            GL.ActiveTexture(TextureUnit.Texture0);
            GL.BindTexture(TextureTarget.Texture2D, 0);
        }
Exemplo n.º 7
0
        public void CreateNBuffers(DepthPeeler peeler)
        {
            // NOTE: we are only considering the first 4 depth layers packed into 1 image

            // - create the level 0 N-buffer from the original packedDepthTexture
            //   - copy it at 1/4 resolution (using mip-mapping??)
            // - create the rest of N-buffer level from the previous levels
            //   - for each level i in [1; LevelCount]:
            //     - attach level i-1 as the source texture
            //     - attach level i as the render target
            //     - render a quad

            // Different shaders are used for the first level and the rest of levels.
            // Since we are creating single set of N-buffers for four depth images
            // in parallel in the first level we must find the extrema even within
            // the vector components.

            //for (int i = 0; i < LayerCount; i++)
            //{
            //    GL.BindTexture(TextureTarget.Texture2D, nBuffersTextures[i]);
            //    GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
            //}

            GL.PushAttrib(AttribMask.ViewportBit);
            GL.Viewport(0, 0, nbuffersWidth, nbuffersHeight);

            GL.Ext.BindFramebuffer(FramebufferTarget.FramebufferExt, fboHandle);

            GL.DepthFunc(DepthFunction.Always);

            // the first level is taken directly from the original packed depth texture (without offsets)
            GL.UseProgram(level0shaderProgram);

            GL.ActiveTexture(TextureUnit.Texture0);
            GL.BindTexture(TextureTarget.Texture2DArray, peeler.PackedDepthTextures);

            GL.Uniform1(GL.GetUniformLocation(level0shaderProgram, "packedDepthTexture"), 0);
            GL.Uniform1(GL.GetUniformLocation(level0shaderProgram, "layer"), 0);

            GL.Ext.FramebufferTextureLayer(
                FramebufferTarget.FramebufferExt, FramebufferAttachment.ColorAttachment0Ext,
                nBuffersTextureArray, 0, 0);     // layer 0

            LayerHelper.DrawQuad();

            // -- DEBUG --

            //GL.UseProgram(0);

            //GL.ActiveTexture(TextureUnit.Texture0);
            //GL.BindTexture(TextureTarget.Texture2D, prevMinTexture);
            //// copy the source texture from the frame buffer to the destination texture
            //GL.CopyTexSubImage2D(TextureTarget.Texture2D, 0, 0, 0, 0, 0, nbuffersWidth, nbuffersHeight);

            //GL.UseProgram(shaderProgram);

            ////GL.Ext.BindFramebuffer(FramebufferTarget.FramebufferExt, 0);

            //Vector3 offset = new Vector3(1.0f / nbuffersWidth, 1.0f / nbuffersHeight, 0);

            //GL.Uniform1(GL.GetUniformLocation(shaderProgram, "prevLevelMinTexture"), 0);
            //GL.Uniform1(GL.GetUniformLocation(shaderProgram, "prevLevelMaxTexture"), 1);
            //GL.Uniform1(GL.GetUniformLocation(shaderProgram, "prevLevel"), 0);
            //GL.Uniform3(GL.GetUniformLocation(shaderProgram, "offset"), offset);

            //GL.ActiveTexture(TextureUnit.Texture1);
            //GL.BindTexture(TextureTarget.Texture2DArray, nBuffersTextureArray);

            //GL.ActiveTexture(TextureUnit.Texture0);
            //GL.BindTexture(TextureTarget.Texture2D, prevMinTexture);

            //GL.Ext.FramebufferTextureLayer(
            //    FramebufferTarget.FramebufferExt, FramebufferAttachment.ColorAttachment0Ext,
            //    nBuffersTextureArray, 0, 1);

            //LayerHelper.DrawQuad();

            // -- DEBUG --


            // the following levels are constructed from the previous ones (with offsets)

            GL.UseProgram(shaderProgram);

            // (x, y, 0)
            // (1,1,0) converted from [width; height] to [0.0; 1.0]^2 texture coordinates
            Vector3 offset = new Vector3(1.0f / nbuffersWidth, 1.0f / nbuffersHeight, 0);

            GL.Uniform1(GL.GetUniformLocation(shaderProgram, "prevLevelTexture"), 0);

            GL.ActiveTexture(TextureUnit.Texture0);
            GL.BindTexture(TextureTarget.Texture2DArray, nBuffersTextureArray);

            GL.TexParameter(TextureTarget.Texture2DArray, TextureParameterName.TextureBorderColor, new float[] { 1, 0, 0, 0 });

            for (int i = 1; i < LayerCount; i++)
            {
                GL.Uniform3(GL.GetUniformLocation(shaderProgram, "offset"), offset);
                GL.Uniform1(GL.GetUniformLocation(shaderProgram, "prevLevel"), i - 1);

                GL.Ext.FramebufferTextureLayer(
                    FramebufferTarget.FramebufferExt, FramebufferAttachment.ColorAttachment0Ext,
                    nBuffersTextureArray, 0, i);

                LayerHelper.DrawQuad();

                offset *= 2.0f;
            }

            GL.TexParameter(TextureTarget.Texture2DArray, TextureParameterName.TextureBorderColor, new float[] { 0, 1, 0, 0 });

            GL.DepthFunc(DepthFunction.Less);

            GL.UseProgram(0);

            GL.ActiveTexture(TextureUnit.Texture1);
            GL.BindTexture(TextureTarget.Texture2D, 0);
            GL.ActiveTexture(TextureUnit.Texture0);
            GL.BindTexture(TextureTarget.Texture2D, 0);

            GL.Ext.BindFramebuffer(FramebufferTarget.FramebufferExt, 0);

            GL.PopAttrib(); // ViewportBit

            //for (int i = 0; i < LayerCount; i++)
            //{
            //    GL.BindTexture(TextureTarget.Texture2D, nBuffersTextures[i]);
            //    GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.LinearMipmapLinear);
            //    GL.Ext.GenerateMipmap(GenerateMipmapTarget.Texture2D);
            //}
            //GL.BindTexture(TextureTarget.Texture2D, 0);
        }