Exemple #1
0
        public static void CreateCubemap(GLContext control, GLTextureCube cubemapInput,
                                         GLTexture cubemapOutput, int layer, int numMips)
        {
            int size = cubemapInput.Width;

            Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView(MathHelper.DegreesToRadians(90), 1.0f, 0.1f, 10.0f);

            Matrix4[] captureViews =
            {
                Matrix4.LookAt(new Vector3(0.0f, 0.0f, 0.0f), new Vector3(1.0f,   0.0f,  0.0f), new Vector3(0.0f, -1.0f,  0.0f)),
                Matrix4.LookAt(new Vector3(0.0f, 0.0f, 0.0f), new Vector3(-1.0f,  0.0f,  0.0f), new Vector3(0.0f, -1.0f,  0.0f)),
                Matrix4.LookAt(new Vector3(0.0f, 0.0f, 0.0f), new Vector3(0.0f,   1.0f,  0.0f), new Vector3(0.0f,  0.0f,  1.0f)),
                Matrix4.LookAt(new Vector3(0.0f, 0.0f, 0.0f), new Vector3(0.0f,  -1.0f,  0.0f), new Vector3(0.0f,  0.0f, -1.0f)),
                Matrix4.LookAt(new Vector3(0.0f, 0.0f, 0.0f), new Vector3(0.0f,   0.0f,  1.0f), new Vector3(0.0f, -1.0f,  0.0f)),
                Matrix4.LookAt(new Vector3(0.0f, 0.0f, 0.0f), new Vector3(0.0f,   0.0f, -1.0f), new Vector3(0.0f, -1.0f,  0.0f)),
            };

            GL.BindTexture(TextureTarget.TextureCubeMap, 0);

            //Bind the cubemap's texture into a filtered quad.
            //Bind the drawn filter to a cubemap array layer
            Framebuffer frameBuffer = new Framebuffer(FramebufferTarget.Framebuffer, size, size, PixelInternalFormat.Rgba32f);

            frameBuffer.Bind();

            GL.Disable(EnableCap.Blend);

            var cubemapFilter = GlobalShaders.GetShader("CUBEMAP_HDRDECODE");

            cubemapFilter.Enable();

            GL.ActiveTexture(TextureUnit.Texture0 + 1);
            cubemapInput.Bind();
            cubemapFilter.SetInt("cubemapTexture", 1);
            cubemapFilter.SetMatrix4x4("projection", ref projection);
            cubemapFilter.SetFloat("gamma", 2.2f);
            cubemapFilter.SetFloat("range", 1024.0f);
            cubemapFilter.SetFloat("scale", 4.0f);

            GL.Disable(EnableCap.CullFace);
            for (int mip = 0; mip < numMips; mip++)
            {
                int mipWidth  = (int)(size * Math.Pow(0.5, mip));
                int mipHeight = (int)(size * Math.Pow(0.5, mip));

                frameBuffer.Resize(mipWidth, mipHeight);
                GL.Viewport(0, 0, mipWidth, mipHeight);

                cubemapFilter.SetFloat("mipLevel", mip);
                for (int i = 0; i < 6; i++)
                {
                    cubemapFilter.SetInt("faceLevel", i);

                    //attach face to fbo as color attachment 0
                    if (cubemapOutput is GLTextureCubeArray)
                    {
                        GL.FramebufferTextureLayer(FramebufferTarget.Framebuffer,
                                                   FramebufferAttachment.ColorAttachment0, cubemapOutput.ID, mip, (layer * 6) + i);
                    }
                    else
                    {
                        GL.FramebufferTexture2D(FramebufferTarget.Framebuffer,
                                                FramebufferAttachment.ColorAttachment0,
                                                TextureTarget.TextureCubeMapPositiveX + i, cubemapOutput.ID, mip);
                    }

                    cubemapFilter.SetMatrix4x4("view", ref captureViews[i]);

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

            var errorcheck = GL.CheckFramebufferStatus(FramebufferTarget.Framebuffer);

            if (errorcheck != FramebufferErrorCode.FramebufferComplete)
            {
                throw new Exception(errorcheck.ToString());
            }

            GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
            GL.Enable(EnableCap.CullFace);
            GL.Enable(EnableCap.Blend);

            frameBuffer.Dispoe();
            frameBuffer.DisposeRenderBuffer();

            GL.UseProgram(0);
        }
Exemple #2
0
        public static void CreateCubemap(GLContext control, GLTextureCube cubemapInput,
                                         GLTexture cubemapOutput, int layer)
        {
            int size = cubemapOutput.Width;

            Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView(MathHelper.DegreesToRadians(90), 1.0f, 0.1f, 10.0f);

            Matrix4[] captureViews =
            {
                Matrix4.LookAt(new Vector3(0.0f, 0.0f, 0.0f), new Vector3(1.0f,   0.0f,  0.0f), new Vector3(0.0f, -1.0f,  0.0f)),
                Matrix4.LookAt(new Vector3(0.0f, 0.0f, 0.0f), new Vector3(-1.0f,  0.0f,  0.0f), new Vector3(0.0f, -1.0f,  0.0f)),
                Matrix4.LookAt(new Vector3(0.0f, 0.0f, 0.0f), new Vector3(0.0f,   1.0f,  0.0f), new Vector3(0.0f,  0.0f,  1.0f)),
                Matrix4.LookAt(new Vector3(0.0f, 0.0f, 0.0f), new Vector3(0.0f,  -1.0f,  0.0f), new Vector3(0.0f,  0.0f, -1.0f)),
                Matrix4.LookAt(new Vector3(0.0f, 0.0f, 0.0f), new Vector3(0.0f,   0.0f,  1.0f), new Vector3(0.0f, -1.0f,  0.0f)),
                Matrix4.LookAt(new Vector3(0.0f, 0.0f, 0.0f), new Vector3(0.0f,   0.0f, -1.0f), new Vector3(0.0f, -1.0f,  0.0f)),
            };

            GL.BindTexture(TextureTarget.TextureCubeMap, 0);

            //Bind the cubemap's texture into a filtered quad.
            //Bind the drawn filter to a cubemap array layer
            Framebuffer frameBuffer = new Framebuffer(FramebufferTarget.Framebuffer, size, size, PixelInternalFormat.Rgba32f);

            frameBuffer.Bind();

            GL.Disable(EnableCap.Blend);

            var cubemapFilter = GlobalShaders.GetShader("CUBEMAP_PREFILTER");

            cubemapFilter.Enable();

            //Allocate mipmaps
            cubemapOutput.Bind();
            cubemapOutput.GenerateMipmaps();
            cubemapOutput.Unbind();

            GL.ActiveTexture(TextureUnit.Texture0 + 1);
            cubemapInput.Bind();
            cubemapFilter.SetInt("environmentMap", 1);
            cubemapFilter.SetMatrix4x4("projection", ref projection);

            //Quick hack, draw once before rendering (first buffer not updating for some reason??)
            RenderTools.DrawCube();

            GL.Disable(EnableCap.CullFace);
            for (int mip = 0; mip < cubemapOutput.MipCount; mip++)
            {
                int mipWidth  = (int)(size * Math.Pow(0.5, mip));
                int mipHeight = (int)(size * Math.Pow(0.5, mip));

                frameBuffer.Resize(mipWidth, mipHeight);
                GL.Viewport(0, 0, mipWidth, mipHeight);

                float roughness = (float)mip / (float)(cubemapOutput.MipCount - 1);
                cubemapFilter.SetFloat("roughness", roughness);

                for (int i = 0; i < 6; i++)
                {
                    //attach face to fbo as color attachment 0
                    if (cubemapOutput is GLTextureCubeArray)
                    {
                        GL.FramebufferTextureLayer(FramebufferTarget.Framebuffer,
                                                   FramebufferAttachment.ColorAttachment0, cubemapOutput.ID, mip, (layer * 6) + i);
                    }
                    else
                    {
                        GL.FramebufferTexture2D(FramebufferTarget.Framebuffer,
                                                FramebufferAttachment.ColorAttachment0,
                                                TextureTarget.TextureCubeMapPositiveX + i, cubemapOutput.ID, mip);
                    }

                    cubemapFilter.SetMatrix4x4("view", ref captureViews[i]);

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

            var errorcheck = GL.CheckFramebufferStatus(FramebufferTarget.Framebuffer);

            if (errorcheck != FramebufferErrorCode.FramebufferComplete)
            {
                throw new Exception(errorcheck.ToString());
            }

            GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
            GL.Enable(EnableCap.CullFace);
            GL.Enable(EnableCap.Blend);

            frameBuffer.Dispoe();
            frameBuffer.DisposeRenderBuffer();

            GL.UseProgram(0);
        }
        public void DrawBoundings(GLContext control)
        {
            foreach (BfresModelAsset model in Models)
            {
                if (!model.IsVisible)
                {
                    continue;
                }

                foreach (var mesh in model.Meshes)
                {
                    if (!mesh.IsVisible || !mesh.InFrustum)
                    {
                        continue;
                    }

                    //Go through each bounding in the current displayed mesh
                    var polygonGroup = mesh.Shape.PolygonGroups[mesh.LODMeshLevel] as MeshPolygonGroup;
                    foreach (var bounding in polygonGroup.Boundings)
                    {
                        var min = bounding.Center - bounding.Extent;
                        var max = bounding.Center + bounding.Extent;

                        var shader = GlobalShaders.GetShader("PICKING");
                        control.CurrentShader = shader;
                        control.CurrentShader.SetVector4("color", new Vector4(1));

                        Matrix4 transform = Matrix4.Identity;

                        if (mesh.SkinCount == 0)
                        {
                            transform = model.ModelData.Skeleton.Bones[mesh.BoneIndex].Transform;
                            control.CurrentShader.SetMatrix4x4("mtxMdl", ref transform);

                            var bnd = mesh.BoundingNode.Box;
                            BoundingBoxRender.Draw(control, bnd.Min, bnd.Max);
                        }
                        else
                        {
                            foreach (var boneIndex in mesh.Shape.Shape.SkinBoneIndices)
                            {
                                transform = model.ModelData.Skeleton.Bones[boneIndex].Transform;
                                control.CurrentShader.SetMatrix4x4("mtxMdl", ref transform);

                                BoundingBoxRender.Draw(control,
                                                       new Vector3(min.X, min.Y, min.Z),
                                                       new Vector3(max.X, max.Y, max.Z));
                            }
                        }
                    }


                    /*
                     *                  var center = mesh.BoundingNode.Center;
                     *                  var radius = mesh.BoundingNode.Radius;
                     *
                     *                  GL.Enable(EnableCap.Blend);
                     *
                     *                  transform = Matrix4.CreateScale(radius) * Matrix4.CreateTranslation(center) * transform;
                     *                  control.CurrentShader.SetMatrix4x4("mtxMdl", ref transform);
                     *                  control.CurrentShader.SetVector4("color", new Vector4(0,0,0,0.2f));
                     *
                     *                  SphereRender.Draw(control);
                     *
                     *                  GL.Disable(EnableCap.Blend);*/
                }
            }
            control.CurrentShader = null;
        }