Ejemplo n.º 1
0
        public void GUIUpdate()
        {
            _PostProcess._GammaFactor       = wooSlider3.GetSliderValue();
            _PostProcess._GammaContrast     = wooSlider4.GetSliderValue();
            _PostProcess._ToneFactor        = wooSlider1.GetSliderValue();
            _PostProcess._GaussianExposure  = wooSlider5.GetSliderValue();
            _PostProcess._GaussianSD        = wooSlider6.GetSliderValue();
            _PostProcess._PostProcessAmount = wooSlider7.GetSliderValue();

            _PostProcess.Initialise(_GL);
            _ShaderRenderer.SetPostProcess(_PostProcess);
        }
Ejemplo n.º 2
0
 public void SetPostProcess(PostProcess postProcess)
 {
     _PostProcess = postProcess;
     if (_GL != null)
     {
         _PostProcess.Initialise(_GL);
     }
 }
Ejemplo n.º 3
0
        private void InitialiseRenderer()
        {
            _RaytracerOptions                 = new RaytracerOptions();
            _RaytracerOptions._DoFEnabled     = false;
            _RaytracerOptions._ShadowsEnabled = true;
            _RaytracerOptions._Reflections    = 1;

            _Scene = new Scene();
            _Scene._FractalSettings._FractalIterations.Clear();
            _Scene._FractalSettings._FractalColours.Clear();
            _Scene._FractalSettings._RenderOptions._Background = 1;
            FractalGradient preview = new FractalGradient();

            preview._GradientSegments[0]._StartColour = _Material;
            preview._Multiplier = 0.0f;
            _Scene._FractalSettings._FractalColours.Add(preview);
            _Scene._Camera._Position = new Vector3(0, 1.5, 1.5);
            _Scene._Camera._Target   = new Vector3(0, 1, 0);

            _PostProcess = new PostProcess();
//            _PostProcess._ToneMappingMode = 3;
            _PostProcess.Initialise(_GL);

            //  Initialise the scene.
            string frag = "";

            _Scene.Compile(_RaytracerOptions, _Scene._FractalSettings._RenderOptions, ref frag);

            _ShaderRenderer = new ShaderRenderer();
            _ShaderRenderer.Compile(_GL, frag, 16);

            int width, height;

            width  = (int)openGlCtrl.ActualWidth;
            height = (int)openGlCtrl.ActualHeight;
            _ShaderRenderer.Initialise(_GL, width, height, _Scene._Camera.GetViewMatrix(), _Scene._Camera.GetPosition(), false);
            _ShaderRenderer.SetShaderVars(_Scene._Camera.GetViewMatrix(), _Scene._Camera.GetPosition(), _Scene._FractalSettings._RenderOptions.GetSunVec3(), _Scene._Camera, _Scene._FractalSettings);
            _ShaderRenderer.SetProgressive(_RaytracerOptions._Progressive);
            _ShaderRenderer.SetPostProcess(_PostProcess);
            _ShaderRenderer.Clean(_GL);
            _ShaderRenderer.Start();
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Initialises the Scene.
        /// </summary>
        /// <param name="gl">The OpenGL instance.</param>
        public void Initialise(OpenGL gl, int width, int height, mat4 viewMatrix, vec3 position, bool burnVariables)
        {
            Logger.Log("ShaderRenderer.Initialise Started");
            _GL = gl;
            if (_Initialised)
            {
                Destroy(gl);
                _Initialised = true;
            }

            _ViewMatrix       = viewMatrix;
            _Position         = position;
            _TargetWidth      = width;
            _TargetHeight     = height;
            _ProgressiveSteps = 1;
            _ProgressiveIndex = 0;
            _BurnVariables    = burnVariables;

            //  We're going to specify the attribute locations for the position and normal,
            //  so that we can force both shaders to explicitly have the same locations.
            const uint positionAttribute  = 0;
            var        attributeLocations = new Dictionary <uint, string>
            {
                { positionAttribute, "Position" }
            };

            Logger.Log("ShaderRenderer.Initialise Loading shaders from manifest");
            //  Create the raymarch shader
            shaderRayMarch = new ShaderProgram();
            if (_Program == null)
            {
                shaderRayMarch.Create(gl,
                                      ManifestResourceLoader.LoadTextFile(@"Shaders\RayMarchProgressive.vert"),
                                      ManifestResourceLoader.LoadTextFile(@"Shaders\RayMarch.frag"), attributeLocations);
            }
            else
            {
                if (_BurnVariables)
                {
                    ShaderVariables shaderVars = GetShaderVars();
                    shaderVars.BurnVariables(ref _Program);
                }
                _ShaderError = false;
                try
                {
                    shaderRayMarch.Create(gl,
                                          ManifestResourceLoader.LoadTextFile(@"Shaders\RayMarchProgressive.vert"),
                                          _Program, attributeLocations);
                }
                catch (ShaderCompilationException exception)
                {
                    _ShaderError = true;
                    MessageBox.Show(exception.Message + "\r\n" + exception.CompilerOutput);
                }
            }

            // Create the transfer shader
            string fragShader = @"
#version 130
in vec2 texCoord;
out vec4 FragColor;
uniform float mode; // 0=ramp, 1=exposure, 2=standard
uniform float toneFactor;
uniform float gammaFactor;
uniform float gammaContrast;
uniform sampler2D renderedTexture;

vec3 filmic(vec3 value)
{
float A=0.22;
float B=0.30;
float C=0.1;
float D=0.2;
float E=0.01;
float F=0.3;
return ((value*(A*value+C*B)+D*E)/(value*(A*value+B)+D*F)) - E/F;
}

void main()
{
 vec4 rgb = texture(renderedTexture, vec2((texCoord.x+1)*0.5, (texCoord.y+1)*0.5));
 FragColor=rgb;
// FragColor.rgb /= FragColor.a;
 
 // brightness/contrast
 float luminance = dot(FragColor.rgb, vec3(0.2126,0.7152,0.0722));
 float luminanceOut = gammaFactor * pow(luminance, gammaContrast);
 float multiplier = (max(0, luminance) * luminanceOut) / (luminance * luminance);
 FragColor.rgb *= multiplier;

if (mode>2.9 && mode<3.1)
{
 //filmic https://www.slideshare.net/ozlael/hable-john-uncharted2-hdr-lighting
 FragColor.rgb = filmic(FragColor.rgb)/filmic(vec3(toneFactor));
}
else if (mode>1.9 && mode<2.1)
{
 //reinhard https://imdoingitwrong.wordpress.com/2010/08/19/why-reinhard-desaturates-my-blacks-3/
 float nL = luminance * (1+luminance/(toneFactor*toneFactor)) / (1+luminance);
 FragColor.rgb *= nL;
}
else if (mode>0.9 && mode<1.1)
{
 //exposure originally Matt Fairclough
 FragColor.rgb = 1 - exp(-FragColor.rgb * toneFactor);
}
else
{
 FragColor.rgb /= toneFactor;
}
}
";

            shaderTransfer = new ShaderProgram();
            shaderTransfer.Create(gl,
                                  ManifestResourceLoader.LoadTextFile(@"Shaders\RayMarch.vert"),
                                  fragShader, attributeLocations);
            CheckForError(gl);

            fragShader  = @"
#version 130
in vec2 texCoord;
out vec4 FragColor;
void main()
{
FragColor=vec4(0,0,0,0);
}
";
            shaderClean = new ShaderProgram();
            shaderClean.Create(gl,
                               ManifestResourceLoader.LoadTextFile(@"Shaders\RayMarch.vert"),
                               fragShader, attributeLocations);
            CheckForError(gl);

            // Create the transfer shader
            string fragShaderIntTransfer = @"
#version 130
in vec2 texCoord;
out vec4 FragColor;
uniform sampler2D renderedTexture;

void main()
{
 vec4 rgb = texture(renderedTexture, vec2((texCoord.x+1)*0.5, (texCoord.y+1)*0.5));
 FragColor=rgb;
}
";

            shaderIntTransfer = new ShaderProgram();
            shaderIntTransfer.Create(gl,
                                     ManifestResourceLoader.LoadTextFile(@"Shaders\RayMarch.vert"),
                                     fragShaderIntTransfer, attributeLocations);
            CheckForError(gl);

            Logger.Log("ShaderRenderer.Initialise Loading random numbers");
            LoadRandomNumbers(gl);
            Logger.Log("ShaderRenderer.Initialise Finished loading random numbers");

            float[] viewport = new float[4];
            gl.GetFloat(OpenGL.GL_VIEWPORT, viewport);

            gl.GenFramebuffersEXT(2, _FrameBuffer);
            CheckForError(gl);

            gl.GenTextures(2, _RaytracerBuffer);
            CheckForError(gl);
            for (int i = 0; i < 2; i++)
            {
                gl.BindFramebufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, _FrameBuffer[i]);

                gl.BindTexture(OpenGL.GL_TEXTURE_2D, _RaytracerBuffer[i]);
                gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MAG_FILTER, OpenGL.GL_LINEAR);
                gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MIN_FILTER, OpenGL.GL_LINEAR);
                gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_WRAP_S, OpenGL.GL_CLAMP_TO_EDGE);
                gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_WRAP_T, OpenGL.GL_CLAMP_TO_EDGE);
                gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_GENERATE_MIPMAP_SGIS, OpenGL.GL_FALSE); // automatic mipmap
//                gl.TexImage2D(OpenGL.GL_TEXTURE_2D, 0, OpenGL.GL_RGBA, (int)viewport[2], (int)viewport[3], 0,
//                           OpenGL.GL_RGBA, OpenGL.GL_FLOAT, null);
                gl.TexImage2D(OpenGL.GL_TEXTURE_2D, 0, OpenGL.GL_RGBA32F, _TargetWidth, _TargetHeight, 0,
                              OpenGL.GL_RGBA, OpenGL.GL_FLOAT, null);
                CheckForError(gl);

                gl.FramebufferTexture2DEXT(OpenGL.GL_FRAMEBUFFER_EXT, OpenGL.GL_COLOR_ATTACHMENT0_EXT, OpenGL.GL_TEXTURE_2D, _RaytracerBuffer[i], 0);
                gl.FramebufferRenderbufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, OpenGL.GL_DEPTH_ATTACHMENT_EXT, OpenGL.GL_RENDERBUFFER_EXT, 0);
            }
            gl.BindTexture(OpenGL.GL_TEXTURE_2D, 0);

            gl.GenFramebuffersEXT(2, _EffectFrameBuffer);
            CheckForError(gl);

            gl.GenTextures(2, _EffectRaytracerBuffer);
            CheckForError(gl);
            for (int i = 0; i < 2; i++)
            {
                gl.BindFramebufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, _EffectFrameBuffer[i]);

                gl.BindTexture(OpenGL.GL_TEXTURE_2D, _EffectRaytracerBuffer[i]);
                gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MAG_FILTER, OpenGL.GL_LINEAR);
                gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MIN_FILTER, OpenGL.GL_LINEAR);
                gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_WRAP_S, OpenGL.GL_CLAMP_TO_EDGE);
                gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_WRAP_T, OpenGL.GL_CLAMP_TO_EDGE);
                gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_GENERATE_MIPMAP_SGIS, OpenGL.GL_FALSE); // automatic mipmap
                gl.TexImage2D(OpenGL.GL_TEXTURE_2D, 0, OpenGL.GL_RGBA32F, _TargetWidth, _TargetHeight, 0,
                              OpenGL.GL_RGBA, OpenGL.GL_FLOAT, null);
                CheckForError(gl);

                gl.FramebufferTexture2DEXT(OpenGL.GL_FRAMEBUFFER_EXT, OpenGL.GL_COLOR_ATTACHMENT0_EXT, OpenGL.GL_TEXTURE_2D, _EffectRaytracerBuffer[i], 0);
                gl.FramebufferRenderbufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, OpenGL.GL_DEPTH_ATTACHMENT_EXT, OpenGL.GL_RENDERBUFFER_EXT, 0);
            }
            gl.BindTexture(OpenGL.GL_TEXTURE_2D, 0);

            // and now initialise the integer framebuffer
            gl.GenFramebuffersEXT(1, _IntFrameBuffer);
            CheckForError(gl);
            gl.GenTextures(1, _PostprocessBuffer);
            CheckForError(gl);
            gl.BindFramebufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, _IntFrameBuffer[0]);
            gl.BindTexture(OpenGL.GL_TEXTURE_2D, _PostprocessBuffer[0]);
            gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MAG_FILTER, OpenGL.GL_LINEAR);
            gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MIN_FILTER, OpenGL.GL_LINEAR);
            gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_WRAP_S, OpenGL.GL_CLAMP_TO_EDGE);
            gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_WRAP_T, OpenGL.GL_CLAMP_TO_EDGE);
            gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_GENERATE_MIPMAP_SGIS, OpenGL.GL_FALSE); // automatic mipmap
            gl.TexImage2D(OpenGL.GL_TEXTURE_2D, 0, OpenGL.GL_RGBA, _TargetWidth, _TargetHeight, 0, OpenGL.GL_RGBA, OpenGL.GL_UNSIGNED_BYTE, null);
            CheckForError(gl);
            gl.FramebufferTexture2DEXT(OpenGL.GL_FRAMEBUFFER_EXT, OpenGL.GL_COLOR_ATTACHMENT0_EXT, OpenGL.GL_TEXTURE_2D, _PostprocessBuffer[0], 0);
            gl.FramebufferRenderbufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, OpenGL.GL_DEPTH_ATTACHMENT_EXT, OpenGL.GL_RENDERBUFFER_EXT, 0);
            gl.BindTexture(OpenGL.GL_TEXTURE_2D, 0);

            _PostProcess = new PostProcess();
            _PostProcess.Initialise(gl);

            gl.GenFramebuffersEXT(1, _DepthFrameBuffer);
            gl.GenTextures(1, _DepthCalcBuffer);

            gl.BindFramebufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, _DepthFrameBuffer[0]);

            gl.BindTexture(OpenGL.GL_TEXTURE_2D, _DepthCalcBuffer[0]);
            gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MAG_FILTER, OpenGL.GL_LINEAR);
            gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MIN_FILTER, OpenGL.GL_LINEAR_MIPMAP_LINEAR);
            gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_WRAP_S, OpenGL.GL_CLAMP_TO_EDGE);
            gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_WRAP_T, OpenGL.GL_CLAMP_TO_EDGE);
            gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_GENERATE_MIPMAP_SGIS, OpenGL.GL_TRUE);
            gl.TexImage2D(OpenGL.GL_TEXTURE_2D, 0, OpenGL.GL_RGBA32F, 1, 1, 0, OpenGL.GL_RGBA, OpenGL.GL_FLOAT, null);
            gl.FramebufferTexture2DEXT(OpenGL.GL_FRAMEBUFFER_EXT, OpenGL.GL_COLOR_ATTACHMENT0_EXT, OpenGL.GL_TEXTURE_2D, _DepthCalcBuffer[0], 0);
            gl.FramebufferRenderbufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, OpenGL.GL_DEPTH_ATTACHMENT_EXT, OpenGL.GL_RENDERBUFFER_EXT, 0);

            _Initialised = true;

            /*
             *  gl.GenRenderbuffersEXT(2, _RaytracerBuffer);
             *  gl.BindRenderbufferEXT(OpenGL.GL_RENDERBUFFER_EXT, _RaytracerBuffer[0]);
             *  gl.RenderbufferStorageEXT(OpenGL.GL_RENDERBUFFER_EXT, OpenGL.GL_RGBA32F, (int)viewport[2], (int)viewport[3]);
             *  gl.BindRenderbufferEXT(OpenGL.GL_RENDERBUFFER_EXT, _RaytracerBuffer[1]);
             *  gl.RenderbufferStorageEXT(OpenGL.GL_RENDERBUFFER_EXT, OpenGL.GL_RGBA32F, (int)viewport[2], (int)viewport[3]);
             */
            //     gl.GenRenderbuffersEXT(1, _RenderBuffer);
            //gl.BindRenderbufferEXT(OpenGL.GL_RENDERBUFFER_EXT, _RenderBuffer[0]);
            //gl.RenderbufferStorageEXT(OpenGL.GL_RENDERBUFFER_EXT, OpenGL.GL_RGBA, (int)viewport[2], (int)viewport[3]);
            Logger.Log("ShaderRenderer.Initialise Finished");
        }