private InvokeOnDisposal bindFrameBuffer(FrameBuffer frameBuffer, Vector2 requestedSize)
        {
            if (!frameBuffer.IsInitialized)
            {
                frameBuffer.Initialize();
            }

            // These additional render buffers are only required if e.g. depth
            // or stencil information needs to also be stored somewhere.
            foreach (var f in Formats)
            {
                frameBuffer.Attach(f);
            }

            // This setter will also take care of allocating a texture of appropriate size within the framebuffer.
            frameBuffer.Size = requestedSize;

            frameBuffer.Bind();

            return(new InvokeOnDisposal(() => frameBuffer.Unbind()));
        }
Example #2
0
        protected override void PreDraw()
        {
            base.PreDraw();

            foreach (var f in formats)
            {
                frameBuffer.Attach(f);
            }

            frameBuffer.Size = new Vector2(screenSpaceDrawQuad.Width, screenSpaceDrawQuad.Height);

            frameBuffer.Bind();

            // Set viewport to the texture size
            GLWrapper.PushViewport(new Rectangle(0, 0, frameBuffer.Texture.Width, frameBuffer.Texture.Height));
            // We need to draw children as if they were zero-based to the top-left of the texture
            // so we make the new zero be this container's position without affecting children in any negative ways
            GLWrapper.PushOrtho(new Rectangle((int)screenSpaceDrawQuad.TopLeft.X, (int)screenSpaceDrawQuad.TopLeft.Y, frameBuffer.Texture.Width, frameBuffer.Texture.Height));

            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit | ClearBufferMask.StencilBufferBit);
        }
        /// <summary>
        /// Binds and initialises a <see cref="FrameBuffer"/> if required.
        /// </summary>
        /// <param name="frameBuffer">The <see cref="FrameBuffer"/> to bind.</param>
        /// <returns>A token that must be disposed upon finishing use of <paramref name="frameBuffer"/>.</returns>
        protected ValueInvokeOnDisposal BindFrameBuffer(FrameBuffer frameBuffer)
        {
            if (!frameBuffer.IsInitialized)
            {
                frameBuffer.Initialize(true, filteringMode);
            }

            if (formats != null)
            {
                // These additional render buffers are only required if e.g. depth
                // or stencil information needs to also be stored somewhere.
                foreach (var f in formats)
                {
                    frameBuffer.Attach(f);
                }
            }

            // This setter will also take care of allocating a texture of appropriate size within the frame buffer.
            frameBuffer.Size = frameBufferSize;

            frameBuffer.Bind();

            return(new ValueInvokeOnDisposal(frameBuffer.Unbind));
        }
Example #4
0
        protected override void OnLoad(EventArgs e)
        {
            //WindowState = OpenTK.WindowState.Fullscreen;
            //WindowBorder = OpenTK.WindowBorder.Hidden;
            GL.Enable(EnableCap.DepthTest);
            _graphicsDevice = new GraphicsDevice(this);
            VSync           = VSyncMode.On;

            _graphicsDevice.ClearColor = new Color4(0.1f, 0.2f, 0.5f, 1.0f);

            _3DCamera     = new LookAtCamera(_graphicsDevice, new Vector3(0, 0, -5), Vector3.UnitY, Vector3.UnitZ);
            _screenCamera = new GuiCamera(_graphicsDevice);

            _graphicsDevice.Camera = _3DCamera;
            _graphicsDevice.DebugBatch.WorldCamera  = _3DCamera;
            _graphicsDevice.DebugBatch.ScreenCamera = _screenCamera;

            ContentManager content = new ContentManager(new ServiceProvider(), @"..\..\..\Content\");

            content.RegisterTypeReader <BitmapFont>(new SpriteFontReader(Shaders.FontShader));
            content.RegisterTypeReader <Texture2D>(new Texture2DReader());
            content.RegisterTypeReader <Model>(new ModelReader());
            _font = content.Get <BitmapFont>(@"SourceCodePro16.meb").Content as BitmapFont;
            _graphicsDevice.DebugBatch.DefaultFont = _font;

            string vsSrc = @"#version 330
in vec3 Position;
in vec3 Normal;
in vec2 TexCoord;
uniform mat4 WorldViewProj;
out vec2 fragTexCoord;
out vec3 fragNormal;
out vec3 position;
void main() {
  fragTexCoord = TexCoord;
  fragNormal = Normal;
  position = (WorldViewProj * vec4(Position, 1)).xyz;
  gl_Position = WorldViewProj * vec4(Position, 1);
}
";

            string fsSrc = @"#version 330
#extension GL_ARB_explicit_attrib_location : require
uniform sampler2D Texture;
in vec2 fragTexCoord;
in vec3 fragNormal;
in vec3 position;
layout(location = 0) out vec4 diffuseOutput;
layout(location = 1) out vec4 posOutput;
layout(location = 2) out vec4 normOutput;

void main() {
  diffuseOutput = texture(Texture, fragTexCoord);
  posOutput.xyz = position;
  normOutput = vec4(fragNormal * 0.5 + 0.5, 1);
}
";

            _defferedShader = new Program(vsSrc, fsSrc);

            _depthTexture    = Texture2D.CreateEmpty(_graphicsDevice.Width, _graphicsDevice.Height, PixelInternalFormat.DepthComponent24, PixelFormat.DepthComponent, PixelType.UnsignedInt);
            _diffuseTexture  = Texture2D.CreateEmpty(_graphicsDevice.Width, _graphicsDevice.Height);
            _normalTexture   = Texture2D.CreateEmpty(_graphicsDevice.Width, _graphicsDevice.Height);
            _positionTexture = Texture2D.CreateEmpty(_graphicsDevice.Width, _graphicsDevice.Height);

            _fbo = new FrameBuffer(_graphicsDevice);
            _fbo.Attach(_diffuseTexture, FramebufferAttachment.ColorAttachment0);
            _fbo.Attach(_positionTexture, FramebufferAttachment.ColorAttachment1);
            _fbo.Attach(_normalTexture, FramebufferAttachment.ColorAttachment2);
            _fbo.Attach(_depthTexture, FramebufferAttachment.DepthAttachment);
            _fbo.Unbind();

            Utilities.CheckGLError();

            _spriteBatch = new SpriteBatch(_graphicsDevice);

            _dwarf      = content.Get <Model>("\\dwarf1.meb").Content as Model;
            _dwarfWorld = Matrix4.Scale(0.5f) * Matrix4.CreateTranslation(0, -1.5f, 0);
        }
 internal void Attach(RenderbufferInternalFormat format)
 {
     frameBuffer.Attach(format);
 }