uint CreateIrradianceMap(int frameBuffer, Matrix4x4 projection, Matrix4x4[] viewMatrices, uint cubeMap)
        {
            DefaultMesh cubeMesh           = Meshes.CreateCubeWithNormals();
            VAO         renderIrradMapCube = VAOLoader.FromMesh(cubeMesh, irradianceMapShader);
            //Create Irradiance Texture
            int fbIrrWidth  = 32;
            int fbIrrHeight = 32;
            int irradMap    = CreateCubeMap(32, 32);

            GL.BindFramebuffer(FramebufferTarget.Framebuffer, frameBuffer);
            //GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, TextureTarget.TextureCubeMap, irradMap, 0);
            //Render
            //Irradiance Map
            irradianceMapShader.Activate();
            SetSampler(irradianceMapShader.ProgramID, 0, "environmentMap", cubeMap, TextureTarget.TextureCubeMap);
            irradianceMapShader.Uniform("projection", projection, true);

            GL.Viewport(0, 0, fbIrrWidth, fbIrrHeight);
            GL.BindFramebuffer(FramebufferTarget.Framebuffer, frameBuffer);
            for (int i = 0; i < 6; i++)
            {
                irradianceMapShader.Uniform("view", viewMatrices[i], true);
                GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0,
                                        TextureTarget.TextureCubeMapPositiveX + i, irradMap, 0);

                GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

                renderIrradMapCube.Draw();
            }

            GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
            return((uint)irradMap);
        }
        public void GetCubeIBLMapsFromSphereMap(ITexture2D sphereTexture, ref uint cubeMap, ref uint irradianceMap, ref uint prefilterMap, ref uint integrationMap)
        {
            GL.CullFace(CullFaceMode.Front);
            DefaultMesh cubeMesh = Meshes.CreateCubeWithNormals();

            //Set up Projection Matrix and View Matrix for rendering into Cubemap
            Matrix4x4 captureProjection = Matrix4x4.CreatePerspectiveFieldOfView(MathHelper.DegreesToRadians(90.0f), 1.0f, 0.1f, 10.0f);

            Matrix4x4[] captureView =
            {
                Matrix4x4.CreateLookAt(Vector3.Zero, new Vector3(1.0f,   0.0f,  0.0f), new Vector3(0.0f, -1.0f,  0.0f)),
                Matrix4x4.CreateLookAt(Vector3.Zero, new Vector3(-1.0f,  0.0f,  0.0f), new Vector3(0.0f, -1.0f,  0.0f)),
                Matrix4x4.CreateLookAt(Vector3.Zero, new Vector3(0.0f,   1.0f,  0.0f), new Vector3(0.0f,  0.0f,  1.0f)),
                Matrix4x4.CreateLookAt(Vector3.Zero, new Vector3(0.0f,  -1.0f,  0.0f), new Vector3(0.0f,  0.0f, -1.0f)),
                Matrix4x4.CreateLookAt(Vector3.Zero, new Vector3(0.0f,   0.0f,  1.0f), new Vector3(0.0f, -1.0f,  0.0f)),
                Matrix4x4.CreateLookAt(Vector3.Zero, new Vector3(0.0f,   0.0f, -1.0f), new Vector3(0.0f, -1.0f,  0.0f)),
            };
            int captureFBO = GL.GenFramebuffer();

            //Basic Skybox/IBL CubeMap
            cubeMap = CreateEnvironmentMap(captureFBO, captureProjection, captureView, sphereTexture);
            //Irradiance map, which is presampled map of irradiance of IBL Cubemap
            irradianceMap = CreateIrradianceMap(captureFBO, captureProjection, captureView, cubeMap);

            prefilterMap   = CreatePrefilteredMap(captureFBO, captureProjection, captureView, cubeMap);
            integrationMap = CreateIntegratedMap(captureFBO);

            /*
             *
             * Create BDRF Integration Map
             *
             */

            GL.CullFace(CullFaceMode.Back);
        }
Пример #3
0
        private void UpdateRaytraceMesh()
        {
            DefaultMesh mesh = new DefaultMesh();

            mesh.Add(Meshes.CreateCubeWithNormals(_voxelSize).Transform(Matrix4x4.CreateTranslation(_raytraceVoxelPosition)));

            _raytraceGeometry = VAOLoader.FromMesh(mesh, _raytraceShader);
        }
        public PBRRenderer(IRenderState renderState, IContentLoader contentLoader)
        {
            iblMapList = new List <IBLSetup>();
            GL.Enable(EnableCap.DebugOutput);
            GL.Enable(EnableCap.TextureCubeMapSeamless);
            this.renderState = renderState;
            GL.Enable(EnableCap.Texture2D);
            GL.Enable(EnableCap.TextureCubeMap);
            renderState.Set(new DepthTest(true));
            //pbrShader = contentLoader.Load<IShaderProgram>("PBRLightingBasic.*");
            pbrShader = contentLoader.Load <IShaderProgram>("PBRLighting.*");
            //pbrShader = contentLoader.Load<IShaderProgram>("PBRReference.*");
            skyboxShader         = contentLoader.Load <IShaderProgram>("Skybox.*");
            cubeProjectionShader = contentLoader.Load <IShaderProgram>("CubeMapProjection.*");
            textureTest          = contentLoader.Load <IShaderProgram>("DisplayTexture2D.*");
            irradianceMapShader  = contentLoader.Load <IShaderProgram>("IrradianceMap.*");
            prefilterMapShader   = contentLoader.Load <IShaderProgram>("PrefilterIBLMap.*");
            integrationMapShader = contentLoader.Load <IShaderProgram>("BRDFIntegration.*");
            dLight           = new DirectionalLight();
            dLight.direction = new Vector3(0, 1, -1);
            dLight.color     = new Vector3(10);
            dLight.position  = new Vector3(0, 1, -1);

            Vector3 startPos = new Vector3(0, 0, 0.5f);

            pointLights = new PointLight[4];
            for (int i = 0; i < 4; i++)
            {
                pointLights[i]          = new PointLight();
                pointLights[i].color    = new Vector3(1);
                pointLights[i].radius   = 1;
                pointLights[i].position = startPos;
            }
            pointLights[0].position = new Vector3(-1, 1, 0.5f);
            pointLights[1].position = new Vector3(-1, -1, 0.5f);
            pointLights[2].position = new Vector3(1, -1, 0.5f);
            pointLights[3].position = new Vector3(1, 1, 0.5f);


            int size = System.Runtime.InteropServices.Marshal.SizeOf(typeof(PointLight)) * pointLights.Length;

            //Generate buffer and allocate memory
            ubo = GL.GenBuffer();
            GL.BindBuffer(BufferTarget.UniformBuffer, ubo);
            GL.BufferData(BufferTarget.UniformBuffer, size, pointLights, BufferUsageHint.DynamicDraw);

            //Assign Buffer Block to ubo
            int uniformID = GL.GetUniformBlockIndex(pbrShader.ProgramID, "BufferPointLights");

            GL.UniformBlockBinding(pbrShader.ProgramID, uniformID, 0);
            GL.BindBufferBase(BufferRangeTarget.UniformBuffer, 0, ubo);

            DefaultMesh cubeMesh = Meshes.CreateCubeWithNormals();

            unitCube = VAOLoader.FromMesh(cubeMesh, cubeProjectionShader);
            renderState.Set(new FaceCullingModeState(FaceCullingMode.BACK_SIDE));
        }
        public MainVisual(IRenderState renderState, IContentLoader contentLoader)
        {
            renderState.Set(BoolState <IDepthState> .Enabled);
            renderState.Set(BoolState <IBackfaceCullingState> .Enabled);
            shaderProgram = contentLoader.Load <IShaderProgram>("shader.*");
            var mesh = contentLoader.Load <DefaultMesh>("suzanne");

            geometryBody = VAOLoader.FromMesh(mesh, shaderProgram);

            floorShaderProgram = contentLoader.Load <IShaderProgram>("floor.*");

            floor     = VAOLoader.FromMesh(Meshes.CreatePlane(100, 100, 1, 1).Transform(new Translation3D(new Vector3(0, -30, 0))), floorShaderProgram);
            waterCube = VAOLoader.FromMesh(Meshes.CreateCubeWithNormals(100).Transform(new Translation3D(new Vector3(-50, -50, 0))), floorShaderProgram);
        }
        uint CreatePrefilteredMap(int frameBuffer, Matrix4x4 projection, Matrix4x4[] viewMatrices, uint cubeMap)
        {
            DefaultMesh cubeMesh            = Meshes.CreateCubeWithNormals();
            VAO         renderPrefilterCube = VAOLoader.FromMesh(cubeMesh, prefilterMapShader);
            int         texResX             = 128;
            int         texResY             = 128;

            int prefiltMap = CreateCubeMap(texResX, texResY);

            GL.TexParameter(TextureTarget.TextureCubeMap, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.LinearMipmapLinear);
            GL.GenerateMipmap(GenerateMipmapTarget.TextureCubeMap);


            prefilterMapShader.Activate();

            SetSampler(prefilterMapShader.ProgramID, 0, "environmentMap", cubeMap, TextureTarget.TextureCubeMap);
            GL.TexParameter(TextureTarget.TextureCubeMap, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.LinearMipmapLinear);


            prefilterMapShader.Uniform("projection", projection, true);
            GL.BindFramebuffer(FramebufferTarget.Framebuffer, frameBuffer);
            int maxMipLvl = 5;

            for (int i = 0; i < maxMipLvl; i++)
            {
                float mipWidth  = texResX * (float)Math.Pow(0.5, i);
                float mipHeight = texResY * (float)Math.Pow(0.5, i);
                GL.Viewport(0, 0, (int)mipWidth, (int)mipHeight);

                float roughness = (float)i / (float)(maxMipLvl - 1.0);
                prefilterMapShader.Uniform("roughness", roughness);
                for (int j = 0; j < 6; j++)
                {
                    prefilterMapShader.Uniform("view", viewMatrices[j], true);
                    GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0,
                                            TextureTarget.TextureCubeMapPositiveX + j, prefiltMap, i);

                    GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
                    renderPrefilterCube.Draw();
                }
            }
            GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);

            return((uint)prefiltMap);
        }
        uint CreateEnvironmentMap(int frameBuffer, Matrix4x4 projection, Matrix4x4[] viewMatrices, ITexture2D sphereTexture)
        {
            int         fbCubeWidht       = 512;
            int         fbCubeHeight      = 512;
            DefaultMesh cubeMesh          = Meshes.CreateCubeWithNormals();
            VAO         renderCubeMapCube = VAOLoader.FromMesh(cubeMesh, cubeProjectionShader);

            //Cubemap
            int envCubeMap = CreateCubeMap(fbCubeWidht, fbCubeHeight);

            GL.TexParameter(TextureTarget.TextureCubeMap, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.LinearMipmapLinear);
            //FBO

            GL.BindFramebuffer(FramebufferTarget.Framebuffer, frameBuffer);
            //GL.FramebufferTexture(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, TextureTarget.TextureCubeMap, envCubeMap, 0);

            //Render into Cubemap
            //Setting up shader
            cubeProjectionShader.Activate();
            cubeProjectionShader.Uniform("projection", projection, true);

            SetSampler(cubeProjectionShader.ProgramID, 0, "equirectangularMap", sphereTexture);
            GL.Viewport(0, 0, fbCubeWidht, fbCubeHeight);
            GL.BindFramebuffer(FramebufferTarget.Framebuffer, frameBuffer);
            //GL.DrawBuffer(DrawBufferMode.ColorAttachment0);
            for (int i = 0; i < 6; i++)
            {
                cubeProjectionShader.Uniform("view", viewMatrices[i], true);

                GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0,
                                        TextureTarget.TextureCubeMapPositiveX + i, envCubeMap, 0);

                GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

                renderCubeMapCube.Draw();
            }
            GL.BindTexture(TextureTarget.TextureCubeMap, envCubeMap);
            GL.GenerateMipmap(GenerateMipmapTarget.TextureCubeMap);

            GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
            DeactivateTexture(0);
            return((uint)envCubeMap);
        }
        public void ShowCubeMap(ITexture2D sphereTexture, int side)
        {
            GL.CullFace(CullFaceMode.Front);
            DefaultMesh cubeMesh   = Meshes.CreateCubeWithNormals();
            VAO         renderCube = VAOLoader.FromMesh(cubeMesh, cubeProjectionShader);

            //Set up Projection Matrix and View Matrix for rendering into Cubemap
            Matrix4x4 captureProjection = Matrix4x4.CreatePerspectiveFieldOfView(MathHelper.DegreesToRadians(90.0f), 1.0f, 0.1f, 10.0f);

            Matrix4x4[] captureView =
            {
                Matrix4x4.CreateLookAt(Vector3.Zero, new Vector3(1.0f,   0.0f,  0.0f), new Vector3(0.0f, -1.0f,  0.0f)),
                Matrix4x4.CreateLookAt(Vector3.Zero, new Vector3(-1.0f,  0.0f,  0.0f), new Vector3(0.0f, -1.0f,  0.0f)),
                Matrix4x4.CreateLookAt(Vector3.Zero, new Vector3(0.0f,   1.0f,  0.0f), new Vector3(0.0f,  0.0f,  1.0f)),
                Matrix4x4.CreateLookAt(Vector3.Zero, new Vector3(0.0f,  -1.0f,  0.0f), new Vector3(0.0f,  0.0f, -1.0f)),
                Matrix4x4.CreateLookAt(Vector3.Zero, new Vector3(0.0f,   0.0f,  1.0f), new Vector3(0.0f, -1.0f,  0.0f)),
                Matrix4x4.CreateLookAt(Vector3.Zero, new Vector3(0.0f,   0.0f, -1.0f), new Vector3(0.0f, -1.0f,  0.0f)),
            };

            //Setting up shader
            cubeProjectionShader.Activate();
            SetSampler(cubeProjectionShader.ProgramID, 0, "equirectangularMap", sphereTexture);
            cubeProjectionShader.Uniform("projection", captureProjection, true);

            GL.ActiveTexture(TextureUnit.Texture0);
            sphereTexture.Activate();

            //Render into Cubemap
            GL.Viewport(0, 0, 512, 512);
            cubeProjectionShader.Uniform("view", captureView[side], true);

            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

            renderCube.Draw();

            sphereTexture.Deactivate();
            DeactivateTexture(0);
            GL.CullFace(CullFaceMode.Back);
        }