/// <summary> /// Creates the render context provider. Must also create the OpenGL extensions. /// </summary> /// <param name="openGLVersion">The desired OpenGL version.</param> /// <param name="gl">The OpenGL context.</param> /// <param name="width">The width.</param> /// <param name="height">The height.</param> /// <param name="bitDepth">The bit depth.</param> /// <param name="parameter">The parameter</param> /// <returns></returns> public override bool Create(OpenGLVersion openGLVersion, OpenGL gl, int width, int height, int bitDepth, object parameter) { this.gl = gl; // Call the base class. base.Create(openGLVersion, gl, width, height, bitDepth, parameter); uint[] ids = new uint[1]; // First, create the frame buffer and bind it. gl.GenFramebuffersEXT(1, ids); gl.ThrowIfErrors(); frameBufferID = ids[0]; gl.BindFramebufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, frameBufferID); gl.ThrowIfErrors(); // Create the colour render buffer and bind it, then allocate storage for it. gl.GenRenderbuffersEXT(1, ids); gl.ThrowIfErrors(); colourRenderBufferID = ids[0]; gl.BindRenderbufferEXT(OpenGL.GL_RENDERBUFFER_EXT, colourRenderBufferID); gl.ThrowIfErrors(); gl.RenderbufferStorageEXT(OpenGL.GL_RENDERBUFFER_EXT, OpenGL.GL_RGBA, width, height); gl.ThrowIfErrors(); // Create the depth stencil render buffer and bind it, then allocate storage for it. gl.GenRenderbuffersEXT(1, ids); gl.ThrowIfErrors(); depthStencilRenderBufferID = ids[0]; gl.BindRenderbufferEXT(OpenGL.GL_RENDERBUFFER_EXT, depthStencilRenderBufferID); gl.ThrowIfErrors(); gl.RenderbufferStorageEXT(OpenGL.GL_RENDERBUFFER_EXT, OpenGL.GL_DEPTH24_STENCIL8, width, height); gl.ThrowIfErrors(); // Set the render buffer for colour, depth and stencil. gl.FramebufferRenderbufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, OpenGL.GL_COLOR_ATTACHMENT0_EXT, OpenGL.GL_RENDERBUFFER_EXT, colourRenderBufferID); gl.ThrowIfErrors(); gl.FramebufferRenderbufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, OpenGL.GL_DEPTH_ATTACHMENT_EXT, OpenGL.GL_RENDERBUFFER_EXT, depthStencilRenderBufferID); gl.ThrowIfErrors(); gl.FramebufferRenderbufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, OpenGL.GL_STENCIL_ATTACHMENT_EXT, OpenGL.GL_RENDERBUFFER_EXT, depthStencilRenderBufferID); gl.ThrowIfErrors(); ValidateFramebufferStatus(gl.CheckFramebufferStatusEXT(OpenGL.GL_FRAMEBUFFER_EXT)); dibSectionDeviceContext = Win32.CreateCompatibleDC(deviceContextHandle); // Create the DIB section. dibSection.Create(dibSectionDeviceContext, width, height, bitDepth); return(true); }
public FBO(OpenGL gl, int width, int height) { _gl = gl; if (width < 1) { width = 16; } if (height < 1) { height = 16; } gl.GenFramebuffersEXT(1, _fbo); ColorTexture = new Texture(gl); using (new Bind(ColorTexture)) { gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_WRAP_S, OpenGL.GL_REPEAT); gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_WRAP_T, OpenGL.GL_REPEAT); gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MIN_FILTER, OpenGL.GL_NEAREST); gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MAG_FILTER, OpenGL.GL_NEAREST); gl.TexImage2D(OpenGL.GL_TEXTURE_2D, 0, OpenGL.GL_RGBA, width, height, 0, OpenGL.GL_RGBA, OpenGL.GL_UNSIGNED_BYTE, null); } DepthTexture = new Texture(gl); using (new Bind(DepthTexture)) { gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_WRAP_S, OpenGL.GL_REPEAT); gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_WRAP_T, OpenGL.GL_REPEAT); gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MIN_FILTER, OpenGL.GL_NEAREST); gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MAG_FILTER, OpenGL.GL_NEAREST); gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_DEPTH_TEXTURE_MODE_ARB, OpenGL.GL_INTENSITY); gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_COMPARE_MODE, OpenGL.GL_COMPARE_R_TO_TEXTURE_ARB); gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_COMPARE_FUNC, OpenGL.GL_LEQUAL); gl.TexImage2D(OpenGL.GL_TEXTURE_2D, 0, OpenGL.GL_DEPTH_COMPONENT24, width, height, 0, OpenGL.GL_DEPTH_COMPONENT, OpenGL.GL_UNSIGNED_BYTE, null); } using (new Bind(this)) { gl.FramebufferTexture2DEXT(OpenGL.GL_FRAMEBUFFER_EXT, OpenGL.GL_COLOR_ATTACHMENT0_EXT, OpenGL.GL_TEXTURE_2D, ColorTexture.Handle, 0); gl.FramebufferTexture2DEXT(OpenGL.GL_FRAMEBUFFER_EXT, OpenGL.GL_DEPTH_ATTACHMENT_EXT, OpenGL.GL_TEXTURE_2D, DepthTexture.Handle, 0); var status = _gl.CheckFramebufferStatusEXT(OpenGL.GL_FRAMEBUFFER_EXT); if (status != OpenGL.GL_FRAMEBUFFER_COMPLETE_EXT) { throw new Exception(); } } }
/// <summary> /// Creates the render context provider. Must also create the OpenGL extensions. /// </summary> /// <param name="openGLVersion">The desired OpenGL version.</param> /// <param name="gl">The OpenGL context.</param> /// <param name="width">The width.</param> /// <param name="height">The height.</param> /// <param name="bitDepth">The bit depth.</param> /// <param name="parameter">The parameter</param> /// <returns></returns> public override bool Create(OpenGLVersion openGLVersion, OpenGL gl, int width, int height, int bitDepth, object parameter) { // Call the base class. base.Create(openGLVersion, gl, width, height, bitDepth, parameter); uint[] ids = new uint[1]; // Multi sampled fbo gl.GenFramebuffersEXT(1, ids); gl.ThrowIfErrors(); msFrameBufferID = ids[0]; gl.BindFramebufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, msFrameBufferID); gl.ThrowIfErrors(); // Create the colour render buffer and bind it, then allocate storage for it. gl.GenRenderbuffersEXT(1, ids); gl.ThrowIfErrors(); msColourRenderBufferID = ids[0]; gl.BindRenderbufferEXT(OpenGL.GL_RENDERBUFFER_EXT, msColourRenderBufferID); gl.ThrowIfErrors(); gl.RenderbufferStorageMultisampleEXT(OpenGL.GL_RENDERBUFFER_EXT, MSAA, OpenGL.GL_RGBA, width, height); gl.ThrowIfErrors(); gl.FramebufferRenderbufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, OpenGL.GL_COLOR_ATTACHMENT0_EXT, OpenGL.GL_RENDERBUFFER_EXT, msColourRenderBufferID); gl.ThrowIfErrors(); // Create the depth stencil render buffer and bind it, then allocate storage for it. gl.GenRenderbuffersEXT(1, ids); gl.ThrowIfErrors(); msDepthStencilRenderBufferID = ids[0]; gl.BindRenderbufferEXT(OpenGL.GL_RENDERBUFFER_EXT, msDepthStencilRenderBufferID); gl.ThrowIfErrors(); gl.RenderbufferStorageMultisampleEXT(OpenGL.GL_RENDERBUFFER_EXT, MSAA, OpenGL.GL_DEPTH24_STENCIL8, width, height); gl.ThrowIfErrors(); // Set the render buffer for depth and stencil. gl.FramebufferRenderbufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, OpenGL.GL_DEPTH_ATTACHMENT_EXT, OpenGL.GL_RENDERBUFFER_EXT, msDepthStencilRenderBufferID); gl.ThrowIfErrors(); gl.FramebufferRenderbufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, OpenGL.GL_STENCIL_ATTACHMENT_EXT, OpenGL.GL_RENDERBUFFER_EXT, msDepthStencilRenderBufferID); gl.ThrowIfErrors(); ValidateFramebufferStatus(gl.CheckFramebufferStatusEXT(OpenGL.GL_FRAMEBUFFER_EXT)); return(true); }
public GL_GUI_Context(Window window, float width, float height) { this.window = window; this.Width = width; this.Height = height; context = SDL.SDL_GL_CreateContext(window.Handle); SDL.SDL_GL_MakeCurrent(window.Handle, context); gl = new OpenGL(); gl.Enable(OpenGL.GL_TEXTURE_2D); shaderProgram = new ShaderProgram(); shaderProgram.Create(gl, VERTEX_SHADER, FRAGMENT_SHADER, null); shaderProgram.BindAttributeLocation(gl, 0, "in_Position"); shaderProgram.BindAttributeLocation(gl, 1, "in_Color"); shaderProgram.AssertValid(gl); gl.GenFramebuffersEXT(1, framebuffers); gl.BindFramebufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, framebuffers[0]); gl.GenTextures(1, framebufferTargets); gl.BindTexture(OpenGL.GL_TEXTURE_2D, framebufferTargets[0]); gl.TexImage2D(OpenGL.GL_TEXTURE_2D, 0, OpenGL.GL_RGB, window.DisplayWidth, window.DisplayHeight, 0, OpenGL.GL_RGB, OpenGL.GL_UNSIGNED_BYTE, IntPtr.Zero); gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MIN_FILTER, OpenGL.GL_NEAREST); gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MAG_FILTER, OpenGL.GL_NEAREST); gl.FramebufferTexture(OpenGL.GL_FRAMEBUFFER_EXT, OpenGL.GL_COLOR_ATTACHMENT0_EXT, framebufferTargets[0], 0); gl.DrawBuffers(1, new uint[] { OpenGL.GL_COLOR_ATTACHMENT0_EXT }); if (gl.CheckFramebufferStatusEXT(OpenGL.GL_FRAMEBUFFER_EXT) != OpenGL.GL_FRAMEBUFFER_COMPLETE_EXT) { throw new Exception("Frame buffer setup not complete"); } gl.BindFramebufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, 0); gl.BindTexture(OpenGL.GL_TEXTURE_2D, 0); ortho = new float[] { 2.0f / Width, 0.0f, 0.0f, 0.0f, 0.0f, -2.0f / Height, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, -1.0f, 1.0f, 0.0f, 1.0f, }; gl.Viewport(0, 0, window.DisplayWidth, window.DisplayHeight); gl.Enable(OpenGL.GL_BLEND); gl.BlendFunc(OpenGL.GL_SRC_ALPHA, OpenGL.GL_ONE_MINUS_SRC_ALPHA); gl.Disable(OpenGL.GL_CULL_FACE); gl.Disable(OpenGL.GL_DEPTH_TEST); gl.ActiveTexture(OpenGL.GL_TEXTURE0); gl.Enable(OpenGL.GL_SCISSOR_TEST); shaderProgram.Bind(gl); shaderProgram.SetUniformMatrix4(gl, "projectionMatrix", ortho); vertexBufferArray = new VertexBufferArray(); vertexBufferArray.Create(gl); vertexBufferArray.Bind(gl); }
public static FrameBuffer Create(OpenGL gl, int width, int height) { Texture tex = new Texture(); tex.Create(gl); tex.Bind(gl); gl.TexParameter(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); gl.TexParameter(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); gl.TexParameter(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); gl.TexParameter(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); gl.TexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_BGRA, GL_UNSIGNED_BYTE, null); gl.BindTexture(GL_TEXTURE_2D, 0); //------ uint[] frameBufferIds = new uint[1]; gl.GenFramebuffersEXT(1, frameBufferIds); gl.BindFramebufferEXT(GL_FRAMEBUFFER_EXT, frameBufferIds[0]); gl.FramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, tex.TextureName, 0); //------ uint[] renderBufferIds = new uint[1]; gl.GenRenderbuffersEXT(1, renderBufferIds); gl.BindRenderbufferEXT(GL_RENDERBUFFER_EXT, renderBufferIds[0]); gl.RenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT24, width, height); //------------------------- //Attach depth buffer to FBO gl.FramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, renderBufferIds[0]); //------ var status = gl.CheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT); if (status == GL_FRAMEBUFFER_COMPLETE_EXT) { FrameBuffer.Unbind(gl); return(new FrameBuffer(gl, frameBufferIds, renderBufferIds, tex)); } switch (status) { //case GL_FRAMEBUFFER_UNDEFINED_EXT: // throw new InvalidOperationException(nameof(GL_FRAMEBUFFER_UNDEFINED_EXT)); case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT: throw new InvalidOperationException(nameof(GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT)); case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT: throw new InvalidOperationException(nameof(GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT)); case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT: throw new InvalidOperationException(nameof(GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT)); case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT: throw new InvalidOperationException(nameof(GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT)); case GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE_EXT: throw new InvalidOperationException(nameof(GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE_EXT)); case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT: throw new InvalidOperationException(nameof(GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT)); case GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS: throw new InvalidOperationException(nameof(GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS)); case GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT: throw new InvalidOperationException(nameof(GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT)); default: throw new InvalidOperationException("GL_FRAMEBUFFER unknown error"); } }
private void UpdatePostProcessing(bool enabled) { if (!postprocessingEnabled && enabled) { // Setup back buffer gl.ActiveTexture(OpenGL.GL_TEXTURE0); gl.GenTextures(1, glFrameBufferTexArray); fboTexture = glFrameBufferTexArray[0]; gl.BindTexture(OpenGL.GL_TEXTURE_2D, fboTexture); gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MAG_FILTER, OpenGL.GL_LINEAR); gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MIN_FILTER, OpenGL.GL_LINEAR); gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_WRAP_S, OpenGL.GL_CLAMP_TO_EDGE); gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_WRAP_T, OpenGL.GL_CLAMP_TO_EDGE); gl.TexImage2D(OpenGL.GL_TEXTURE_2D, 0, OpenGL.GL_RGBA, viewportSize.Width, viewportSize.Height, 0, OpenGL.GL_RGBA, OpenGL.GL_UNSIGNED_BYTE, null); gl.BindTexture(OpenGL.GL_TEXTURE_2D, 0); // Setup depth buffer gl.GenRenderbuffersEXT(1, glRenderBufferArray); rboDepth = glRenderBufferArray[0]; gl.BindRenderbufferEXT(OpenGL.GL_RENDERBUFFER, rboDepth); gl.RenderbufferStorageEXT(OpenGL.GL_RENDERBUFFER, OpenGL.GL_DEPTH_COMPONENT16, viewportSize.Width, viewportSize.Height); gl.BindRenderbufferEXT(OpenGL.GL_RENDERBUFFER, 0); // Setup framebuffer gl.GenFramebuffersEXT(1, glFrameBufferArray); fbo = glFrameBufferArray[0]; gl.BindFramebufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, fbo); gl.FramebufferTexture2DEXT(OpenGL.GL_FRAMEBUFFER_EXT, OpenGL.GL_COLOR_ATTACHMENT0_EXT, OpenGL.GL_TEXTURE_2D, fboTexture, 0); gl.FramebufferRenderbufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, OpenGL.GL_DEPTH_ATTACHMENT_EXT, OpenGL.GL_RENDERBUFFER, rboDepth); uint fboStatus = gl.CheckFramebufferStatusEXT(OpenGL.GL_FRAMEBUFFER_EXT); if (gl.CheckFramebufferStatusEXT(OpenGL.GL_FRAMEBUFFER_EXT) != OpenGL.GL_FRAMEBUFFER_COMPLETE_EXT) { postprocessingEnabled = false; gl.DeleteRenderbuffersEXT(1, glRenderBufferArray); gl.DeleteTextures(1, glFrameBufferTexArray); gl.DeleteFramebuffersEXT(1, glFrameBufferArray); return; } gl.BindFramebufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, 0); // Setup post-processing shader postShader = new Shader(gl, ManifestResourceLoader.LoadTextFile(@"Resources\Shaders\screen.vert"), ManifestResourceLoader.LoadTextFile(@"Resources\Shaders\screen.frag"), null); if (!postShader.Compiled) { throw new Exception(postShader.CompilerOutput); } postprocessingEnabled = true; } else if (postprocessingEnabled && !enabled) { gl.DeleteRenderbuffersEXT(1, glRenderBufferArray); gl.DeleteTextures(1, glFrameBufferTexArray); gl.DeleteFramebuffersEXT(1, glFrameBufferArray); postShader.Dispose(); postShader = null; postprocessingEnabled = false; } }