Exemplo n.º 1
0
        public void BindForPostProcessEffects(IPostProcessEffect postProcess)
        {
            var shader          = postProcess.Shader;
            var generateMipMaps = postProcess.RequiresGBufferMipMaps;

            // generate mip maps for final texture, so it can be used in post processing effects, many post processing effects require blurred texture
            if (generateMipMaps)
            {
                /*GL.ActiveTexture(TextureUnit.Texture0);
                 * GL.BindTexture(TextureTarget.Texture2D, finalTextureToWriteTo.GetNativeTextureID()); My.Check();
                 * GL.GenerateMipmap(GenerateMipmapTarget.Texture2D); My.Check();*/

                GL.ActiveTexture(TextureUnit.Texture0); MyGL.Check();
                GL.BindTexture(TextureTarget.Texture2D, FinalTextureToRead.GetNativeTextureID()); MyGL.Check();
                GL.GenerateMipmap(GenerateMipmapTarget.Texture2D); MyGL.Check();
            }

            GL.Disable(EnableCap.DepthTest); MyGL.Check();
            GL.DepthMask(false); MyGL.Check();
            GL.CullFace(CullFaceMode.Back); MyGL.Check();

            GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, frameBufferObjectHandle); MyGL.Check();

            // draw to the one we are not reading
            if (readFirstFinalTexture == false)
            {
                GL.DrawBuffer(DrawBufferMode.ColorAttachment4); MyGL.Check();
            }
            else
            {
                GL.DrawBuffer(DrawBufferMode.ColorAttachment5); MyGL.Check();
            }

            shader.Uniforms.Set("gBufferUniform.depth", depthTexture);
            shader.Uniforms.Set("gBufferUniform.final", FinalTextureToRead);

            for (int i = 0; i < textures.Length - 2; i++)
            {
                shader.Uniforms.Set("gBufferUniform." + ((GBufferTextures)i).ToString().ToLower(), textures[i]);
            }

            readFirstFinalTexture = !readFirstFinalTexture;
        }
Exemplo n.º 2
0
        public void DebugDrawContents()
        {
            GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, frameBufferObjectHandle); MyGL.Check();

            int qh = Height / 4;
            int qw = Width / 4;

            if (readFirstFinalTexture)
            {
                DrawBufferToQuarterOfScreen(ReadBufferMode.ColorAttachment4, qw * 1, qh * 0);
            }
            else
            {
                DrawBufferToQuarterOfScreen(ReadBufferMode.ColorAttachment5, qw * 1, qh * 0);
            }
            if (readFirstFinalTexture)
            {
                DrawBufferToQuarterOfScreen(ReadBufferMode.ColorAttachment5, qw * 2, qh * 0);
            }
            else
            {
                DrawBufferToQuarterOfScreen(ReadBufferMode.ColorAttachment4, qw * 2, qh * 0);
            }

            DrawBufferToQuarterOfScreen(ReadBufferMode.ColorAttachment0, qw * 0, qh * 0);
            DrawBufferToQuarterOfScreen(ReadBufferMode.ColorAttachment1, qw * 0, qh * 1);
            DrawBufferToQuarterOfScreen(ReadBufferMode.ColorAttachment2, qw * 1, qh * 1);
            DrawBufferToQuarterOfScreen(ReadBufferMode.ColorAttachment3, qw * 0, qh * 2);
        }
Exemplo n.º 3
0
        public void BindForTransparentPass(Shader shader)
        {
            GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, frameBufferObjectHandle); MyGL.Check();
            GL.DrawBuffer(DrawBufferMode.ColorAttachment4); MyGL.Check();

            shader.Uniforms.Set("gBufferUniform.depth", depthTexture);
            for (int i = 0; i < textures.Length - 2; i++)
            {
                shader.Uniforms.Set("gBufferUniform." + ((GBufferTextures)i).ToString().ToLower(), textures[i]);
            }
        }
Exemplo n.º 4
0
 public void Unbind()
 {
     GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, 0); MyGL.Check();
     GL.DrawBuffer(DrawBufferMode.Back); MyGL.Check();
 }
Exemplo n.º 5
0
 public void BindAllFrameBuffersForDrawing()
 {
     GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, frameBufferObjectHandle); MyGL.Check();
     GL.DrawBuffers(buffers.Length, buffers); MyGL.Check();
 }
Exemplo n.º 6
0
        public void BindForLightPass(Shader shader)
        {
            readFirstFinalTexture = true;

            GL.Disable(EnableCap.DepthTest); MyGL.Check();
            GL.DepthMask(false); MyGL.Check();
            GL.CullFace(CullFaceMode.Back); MyGL.Check();

            GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, frameBufferObjectHandle); MyGL.Check();
            GL.DrawBuffer(DrawBufferMode.ColorAttachment4); MyGL.Check();

            shader.Uniforms.Set("gBufferUniform.depth", depthTexture);
            for (int i = 0; i < textures.Length - 2; i++)
            {
                shader.Uniforms.Set("gBufferUniform." + ((GBufferTextures)i).ToString().ToLower(), textures[i]);
            }
        }
Exemplo n.º 7
0
 public void Dispose()
 {
     GL.DeleteFramebuffer(FrameBufferObjectHandle); MyGL.Check();
     DepthMap.Dispose();
 }
Exemplo n.º 8
0
            public UniformBufferObject(int index, Func <T> getData)
            {
                bufferIndex  = index;
                this.getData = getData;
                size         = System.Runtime.InteropServices.Marshal.SizeOf(typeof(T));

                bufferUBO = GL.GenBuffer(); MyGL.Check();                                                                            // Generate the buffer
                GL.BindBuffer(BufferTarget.UniformBuffer, bufferUBO); MyGL.Check();                                                  // Bind the buffer for writing
                GL.BufferData(BufferTarget.UniformBuffer, (IntPtr)(size), (IntPtr)(null), BufferUsageHint.StreamDraw); MyGL.Check(); // Request the memory to be allocated
                GL.BindBufferRange(BufferRangeTarget.UniformBuffer, bufferIndex, bufferUBO, (IntPtr)0, size); MyGL.Check();
                GL.BindBuffer(BufferTarget.UniformBuffer, 0); MyGL.Check();                                                          //unbind
            }
Exemplo n.º 9
0
            public void DownloadDataFromGPU()
            {
                GL.BindBuffer(BufferTarget.ShaderStorageBuffer, VboHandle); MyGL.Check();
                //var intPtr = GL.MapBufferRange(BufferTarget.ShaderStorageBuffer, new IntPtr(), Count, BufferAccessMask.MapReadBit); MyGL.Check();
                var intPtr = GL.MapBuffer(BufferTarget.ShaderStorageBuffer, BufferAccess.ReadOnly); MyGL.Check();

                SetData(intPtr, Count);
                GL.UnmapBuffer(BufferTarget.ShaderStorageBuffer); MyGL.Check();
            }
Exemplo n.º 10
0
        public ShadowMap(Light light, int width, int height)
        {
            this.light = light;

            ShadowViewCamera = light.Entity.AddComponent <Camera>();
            ShadowViewCamera.SetSize(width, height);
            ShadowViewCamera.IsOrthographic   = true;
            ShadowViewCamera.OrthographicSize = 50;

            this.Width  = width;
            this.Height = height;

            // create frame buffer object
            FrameBufferObjectHandle = GL.GenFramebuffer(); MyGL.Check();
            GL.BindFramebuffer(FramebufferTarget.Framebuffer, FrameBufferObjectHandle); MyGL.Check();

            DepthMap = new Texture2D(GL.GenTexture());

            GL.BindTexture(TextureTarget.Texture2D, DepthMap.GetNativeTextureID()); MyGL.Check();

            GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.DepthComponent32f, width, height, 0, PixelFormat.DepthComponent, PixelType.Float, new IntPtr(0)); MyGL.Check();

            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear); MyGL.Check();
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear); MyGL.Check();
            //GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest);
            //GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Nearest);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Clamp); MyGL.Check();
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Clamp); MyGL.Check();

            // breaks it, but should enable hardware 4 pcf sampling
            //GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureCompareMode, (int)TextureCompareMode.CompareRToTexture);
            //GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureCompareFunc, (int)All.Lequal);
            //GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.DepthTextureMode, (int)All.Intensity);

            GL.FramebufferTexture(FramebufferTarget.Framebuffer, FramebufferAttachment.DepthAttachment, DepthMap.GetNativeTextureID(), 0); MyGL.Check();
            GL.DrawBuffer(DrawBufferMode.None); MyGL.Check();
            var status = GL.CheckFramebufferStatus(FramebufferTarget.Framebuffer); MyGL.Check();

            if (status != FramebufferErrorCode.FramebufferComplete)
            {
                throw new GLError(status);
            }

            GL.ReadBuffer(ReadBufferMode.None); MyGL.Check();
            GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0); MyGL.Check();             //restore default FBO
        }
Exemplo n.º 11
0
 public void BindBufferToVAO()
 {
     if (UsesLayoutIndex)
     {
         GL.EnableVertexAttribArray(LayoutIndex); MyGL.Check();
     }
     GL.BindBuffer(GL_BufferTarget, VboHandle); MyGL.Check();
     if (UsesLayoutIndex)
     {
         //GL.VertexAttribPointer(Shader.positionLocation, 3, VertexAttribPointerType.Float, false, Vector3.SizeInBytes, 0); My.Check();
         GL.VertexAttribPointer(LayoutIndex, DataStrideInElementsNumber, GL_PointerType, false, DataSizeOfOneElementInBytes, offset); MyGL.Check();
     }
 }
Exemplo n.º 12
0
            public void UploadDataToGPU()
            {
                CreateBuffer();
                int sizeFromGpu;

                GL.BindBuffer(GL_BufferTarget, VboHandle); MyGL.Check();
                var arr  = this.ToArray();
                var size = NumberOfElements * DataSizeOfOneElementInBytes;

                GL.BufferData(GL_BufferTarget, size, arr, BufferUsageHint.DynamicRead); MyGL.Check();                 // BufferUsageHint explained: http://www.informit.com/articles/article.aspx?p=1377833&seqNum=7
                GL.GetBufferParameter(GL_BufferTarget, BufferParameterName.BufferSize, out sizeFromGpu); MyGL.Check();
                // if (size != sizeFromGpu) Log.Error(myName + " size mismatch size=" + GL_BufferTarget + " sizeFromGpu=" + sizeFromGpu);
                GL.BindBuffer(GL_BufferTarget, 0); MyGL.Check();
            }
Exemplo n.º 13
0
 public void Draw(bool drawWithTesselationSupport = false)
 {
     if (isOnGPU == false)
     {
         UploadDataToGpu();
     }
     GL.BindVertexArray(VertexArray.VaoHandle); MyGL.Check();
     GL.DrawElements(drawWithTesselationSupport ? PrimitiveType.Patches : PrimitiveType.Triangles, TriangleIndicies.Count, DrawElementsType.UnsignedInt, IntPtr.Zero); MyGL.Check();
     GL.BindVertexArray(0); MyGL.Check();
 }
Exemplo n.º 14
0
 public void DebugDrawNormal()
 {
     GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, frameBufferObjectHandle); MyGL.Check();
     GL.ReadBuffer(ReadBufferMode.ColorAttachment2); MyGL.Check();
     GL.BlitFramebuffer(0, 0, Width, Height, 0, 0, Width, Height, ClearBufferMask.ColorBufferBit, BlitFramebufferFilter.Linear); MyGL.Check();
 }
Exemplo n.º 15
0
 public void FrameBufferForWriting()
 {
     GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, FrameBufferObjectHandle); MyGL.Check();
 }
Exemplo n.º 16
0
        void DrawBufferToQuarterOfScreen(ReadBufferMode buffer, int x, int y)
        {
            int qh = Height / 4;
            int qw = Width / 4;

            GL.ReadBuffer(buffer); MyGL.Check();
            GL.BlitFramebuffer(0, 0, Width, Height, x, y, x + qw, y + qh, ClearBufferMask.ColorBufferBit, BlitFramebufferFilter.Linear); MyGL.Check();
        }
Exemplo n.º 17
0
            public void UploadToGPU()
            {
                T d = getData();

                GL.BindBuffer(BufferTarget.UniformBuffer, bufferUBO); MyGL.Check();
                GL.BufferSubData(BufferTarget.UniformBuffer, (IntPtr)0, size, ref d); MyGL.Check();
                GL.BindBuffer(BufferTarget.UniformBuffer, 0); MyGL.Check(); //unbind
            }