Beispiel #1
0
        public void SetSpecificUniforms(IPipelineData pipelineData)
        {
            int texId = -1;
            int normId = -1; 
            int metRoughId = -1;
            int metallicId = -1;
            int roughnessId = -1;
            int ambientId = -1; 


            Textures.TryGetValue(Materials.FirstOrDefault().Value.DiffuseMap, out texId);
            Textures.TryGetValue(Materials.FirstOrDefault().Value.NormalMap, out normId);
            Textures.TryGetValue(Materials.FirstOrDefault().Value.MetallicRoughness, out metRoughId);
            Textures.TryGetValue(Materials.FirstOrDefault().Value.Metallic, out metallicId);
            Textures.TryGetValue(Materials.FirstOrDefault().Value.Roughness, out roughnessId);
            Textures.TryGetValue(Materials.FirstOrDefault().Value.AmbientMap, out ambientId);

            _uniformHelper.TryAddUniformTexture2D(texId, maintexture, shaderProg, TextureUnit.Texture0);

             _uniformHelper.TryAddUniformTexture2D(normId, normaltexture, shaderProg, TextureUnit.Texture1);

             _uniformHelper.TryAddUniformTexture2D(metRoughId, metallicRoughness, shaderProg, TextureUnit.Texture2);

            _uniformHelper.TryAddUniformTexture2D(metallicId, metallic, shaderProg, TextureUnit.Texture2);
            _uniformHelper.TryAddUniformTexture2D(roughnessId, roughness, shaderProg, TextureUnit.Texture2);

            _uniformHelper.TryAddUniformTexture2D(ambientId, amibenOclussionMap, shaderProg, TextureUnit.Texture3);
            
             _uniformHelper.TryAddUniformTextureCubemap(pipelineData.SkyboxComponent.IrradianceMap, irradianceMap, shaderProg, TextureUnit.Texture4);

             _uniformHelper.TryAddUniformTextureCubemap(pipelineData.SkyboxComponent.PrefilteredMap, prefilterMap, shaderProg, TextureUnit.Texture5);
      
             _uniformHelper.TryAddUniformTexture2D(pipelineData.SkyboxComponent.BrdfMap, brdf, shaderProg, TextureUnit.Texture6);

            _uniformHelper.TryAddUniform1(randomCoeff, "randomCoeff", shaderProg);

              _uniformHelper.TryAddUniform1(pipelineData.Lights.Count(), numberOfLights, shaderProg);
                Vector3[] lightpositions = new Vector3[pipelineData.Lights.Count()];
                Vector3[] lightcolors = new Vector3[pipelineData.Lights.Count()];
                for (int j = 0; j < pipelineData.Lights.Count; ++j) {
                    var lightComponent =  (PointLightSceneObjectComponent)pipelineData.Lights[j].components["PointLightSceneObjectComponent"];
                    lightcolors[j] = lightComponent.Color * lightComponent.LightStrenght;
                    lightpositions[j] = pipelineData.Lights[j].Position;
                }

                bool suc = _uniformHelper.TryAddUniform1(lightcolors, lightColor, shaderProg);
                bool suc2 = _uniformHelper.TryAddUniform1(lightpositions, lightPosition, shaderProg);

                _uniformHelper.TryAddUniform1(pipelineData.Cam.Position, cameraPosition, shaderProg);

                _uniformHelper.TryAddUniform1(Materials.FirstOrDefault().Value.NormalScale, "normalScale", shaderProg);
        }
Beispiel #2
0
        private int TransformHdrToPrefilteredCubemap(int textureId, ShaderProg shader, int framebuffer, int renderbuffer, int size = 128, uint mipnapLevels = 5)
        {
            Cube cube = new Cube();

            GL.BindFramebuffer(FramebufferTarget.Framebuffer, framebuffer);

            GL.FramebufferRenderbuffer(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, RenderbufferTarget.Renderbuffer, renderbuffer);

            int     cubemap    = AllocatePrefilterCubemap(size);
            Matrix4 projection = Matrix4.Identity;

            Matrix4[] views = new Matrix4[6];
            GenerateMatricesForMapHdrToCubemap(ref projection, ref views);

            ShaderProg prefilteredShader = shader;

            GL.UseProgram(prefilteredShader.ProgramID);
            prefilteredShader.EnableVertexAttribArrays();
            GL.UniformMatrix4(prefilteredShader.GetUniform("projection"), false, ref projection);
            UniformHelper helper = new UniformHelper();
            bool          suc;

            suc = helper.TryAddUniformTextureCubemap(textureId, "environmentMap", prefilteredShader, TextureUnit.Texture0);

            for (uint i = 0; i < mipnapLevels; ++i)
            {
                uint mipWidth  = (uint)(size * Math.Pow(0.5f, i));
                uint mipHeight = (uint)(size * Math.Pow(0.5f, i));

                GL.BindRenderbuffer(RenderbufferTarget.Renderbuffer, renderbuffer);
                GL.RenderbufferStorage(RenderbufferTarget.Renderbuffer, RenderbufferStorage.DepthComponent24, (int)mipWidth, (int)mipHeight);
                GL.Viewport(0, 0, (int)mipWidth, (int)mipHeight);

                float roughness = (float)i / (float)(mipnapLevels - 1);
                helper.TryAddUniform1(roughness, "roughness", prefilteredShader);
                GL.BindFramebuffer(FramebufferTarget.Framebuffer, framebuffer);
                // GL.Disable(EnableCap.FramebufferSrgb);
                //GL.ActiveTexture(TextureUnit.Texture0);
                for (uint j = 0; j < 6; ++j)
                {
                    GL.UniformMatrix4(prefilteredShader.GetUniform("view"), false, ref views[j]);
                    GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0,
                                            TextureTarget.TextureCubeMapPositiveX + (int)j, cubemap, (int)i);
                    GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
                    //      GL.Enable(EnableCap.FramebufferSrgb);
                    cube.RenderCube(); // renders a 1x1 cube
                    // Console.WriteLine(GL.CheckNamedFramebufferStatus(framebuffer, FramebufferTarget.Framebuffer));
                }


                prefilteredShader.DisableVertexAttribArrays();
            }
            GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);

            return(cubemap);
        }
Beispiel #3
0
        private int TransformHdrToCubemap(int textureId, string textureName, ShaderProg shader, int framebuffer, int renderbuffer, int size = 2048)
        {
            Cube cube = new Cube();

            GL.BindFramebuffer(FramebufferTarget.Framebuffer, framebuffer);
            GL.BindRenderbuffer(RenderbufferTarget.Renderbuffer, renderbuffer);
            GL.RenderbufferStorage(RenderbufferTarget.Renderbuffer, RenderbufferStorage.DepthComponent24, size, size);
            GL.FramebufferRenderbuffer(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, RenderbufferTarget.Renderbuffer, renderbuffer);

            int     cubemap    = AllocateCubemap(size);
            Matrix4 projection = Matrix4.Identity;

            Matrix4[] views = new Matrix4[6];
            GenerateMatricesForMapHdrToCubemap(ref projection, ref views);

            ShaderProg equirectangularToCubemapShader = shader;

            GL.UseProgram(equirectangularToCubemapShader.ProgramID);
            equirectangularToCubemapShader.EnableVertexAttribArrays();
            GL.UniformMatrix4(equirectangularToCubemapShader.GetUniform("projection"), false, ref projection);
            UniformHelper helper = new UniformHelper();
            bool          suc;

            if (textureName == "equirectangularMap")
            {
                suc = helper.TryAddUniformTexture2D(textureId, textureName, equirectangularToCubemapShader, TextureUnit.Texture0);
            }
            else
            {
                suc = helper.TryAddUniformTextureCubemap(textureId, textureName, equirectangularToCubemapShader, TextureUnit.Texture0);
            }

            GL.Viewport(0, 0, size, size);
            GL.BindFramebuffer(FramebufferTarget.Framebuffer, framebuffer);
            GL.ActiveTexture(TextureUnit.Texture0);
            for (int i = 0; i < 6; ++i)
            {
                GL.UniformMatrix4(equirectangularToCubemapShader.GetUniform("view"), false, ref views[i]);
                GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0,
                                        TextureTarget.TextureCubeMapPositiveX + i, cubemap, 0);
                GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
                cube.RenderCube(); // renders a 1x1 cube
            }


            equirectangularToCubemapShader.DisableVertexAttribArrays();
            GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);

            return(cubemap);
        }