public void ResizeViewport(Size newSize) { viewportSize = newSize; if (camera != null) { camera.AspectRatio = (float)viewportSize.Width / (float)viewportSize.Height; camera.Project(gl); } if (postprocessingEnabled) { // Resize frame buffer gl.BindTexture(OpenGL.GL_TEXTURE_2D, fboTexture); 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); // Resize render buffer 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); } }
private void CreateDBOs(OpenGLVersion openGLVersion, OpenGL gl, int width, int height, int bitDepth, object parameter) { uint[] ids = new uint[1]; // First, create the frame buffer and bind it. ids = new uint[1]; gl.GenFramebuffersEXT(1, ids); frameBufferID = ids[0]; gl.BindFramebufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, frameBufferID); // Create the colour render buffer and bind it, then allocate storage for it. gl.GenRenderbuffersEXT(1, ids); colourRenderBufferID = ids[0]; gl.BindRenderbufferEXT(OpenGL.GL_RENDERBUFFER_EXT, colourRenderBufferID); gl.RenderbufferStorageEXT(OpenGL.GL_RENDERBUFFER_EXT, OpenGL.GL_RGBA, width, height); // Create the depth render buffer and bind it, then allocate storage for it. gl.GenRenderbuffersEXT(1, ids); depthRenderBufferID = ids[0]; gl.BindRenderbufferEXT(OpenGL.GL_RENDERBUFFER_EXT, depthRenderBufferID); gl.RenderbufferStorageEXT(OpenGL.GL_RENDERBUFFER_EXT, OpenGL.GL_DEPTH_COMPONENT24, width, height); // Set the render buffer for colour and depth. gl.FramebufferRenderbufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, OpenGL.GL_COLOR_ATTACHMENT0_EXT, OpenGL.GL_RENDERBUFFER_EXT, colourRenderBufferID); gl.FramebufferRenderbufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, OpenGL.GL_DEPTH_ATTACHMENT_EXT, OpenGL.GL_RENDERBUFFER_EXT, depthRenderBufferID); dibSectionDeviceContext = Win32.CreateCompatibleDC(deviceContextHandle); // Create the DIB section. dibSection.Create(dibSectionDeviceContext, width, height, bitDepth); }
/// <summary> /// Handles the OpenGLInitialized event of the openGLControl control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> private void openGLControl_OpenGLInitialized(object sender, EventArgs e) { // Get the OpenGL object. OpenGL gl = openGLControl.OpenGL; // Initial texture projector. projTexture = new ProjectiveTexture(gl); projector = new Projector(); // Initial something for creating texture. gl.GenTextures(1, texture_name); gl.BindTexture(OpenGL.GL_TEXTURE_2D, texture_name[0]); 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_MIPMAP_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.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_GENERATE_MIPMAP_HINT_SGIS, OpenGL.GL_TRUE); // automatic mipmap generation included in OpenGL v1.4 gl.TexImage2D(OpenGL.GL_TEXTURE_2D, 0, (int)OpenGL.GL_RGBA8, TEXTURE_WIDTH, TEXTURE_HEIGHT, 0, OpenGL.GL_RGBA, OpenGL.GL_UNSIGNED_BYTE, null); gl.BindTexture(OpenGL.GL_TEXTURE_2D, 0); // InitFBO // create a framebuffer object, you need to delete them when program exits. gl.GenFramebuffersEXT(1, framebuffer_name); gl.BindFramebufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, framebuffer_name[0]); // create a renderbuffer object to store depth info // NOTE: A depth renderable image should be attached the FBO for depth test. // If we don't attach a depth renderable image to the FBO, then // the rendering output will be corrupted because of missing depth test. // If you also need stencil test for your rendering, then you must // attach additional image to the stencil attachement point, too. gl.GenRenderbuffersEXT(1, renderbuffer_name); gl.BindRenderbufferEXT(OpenGL.GL_RENDERBUFFER_EXT, renderbuffer_name[0]); gl.RenderbufferStorageEXT(OpenGL.GL_RENDERBUFFER_EXT, OpenGL.GL_DEPTH_COMPONENT, TEXTURE_WIDTH, TEXTURE_HEIGHT); gl.BindRenderbufferEXT(OpenGL.GL_RENDERBUFFER_EXT, 0); // attach a texture to FBO color attachement point gl.FramebufferTexture2DEXT(OpenGL.GL_FRAMEBUFFER_EXT, OpenGL.GL_COLOR_ATTACHMENT0_EXT, OpenGL.GL_TEXTURE_2D, texture_name[0], 0); // attach a renderbuffer to depth attachment point gl.FramebufferRenderbufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, OpenGL.GL_DEPTH_ATTACHMENT_EXT, OpenGL.GL_RENDERBUFFER_EXT, renderbuffer_name[0]); gl.BindFramebufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, 0); // initial projection matrix gl.MatrixMode(OpenGL.GL_PROJECTION); // Create a perspective transformation. gl.Perspective(60.0f, (double)Width / (double)Height, 0.01, 100.0); // Use the 'look at' helper function to position and aim the camera. gl.LookAt(0, 0, 10, 0, 0, 0, 0, 1, 0); // Set the clear color. gl.ClearColor(0, 0, 0, 0); }
/// <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 void Create() { uint[] texture = new uint[1]; Device.GenTextures(1, texture); TextureID = texture[0]; Device.BindTexture(OpenGL.GL_TEXTURE_2D, TextureID); Device.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_WRAP_S, OpenGL.GL_CLAMP_TO_EDGE); Device.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_WRAP_T, OpenGL.GL_CLAMP_TO_EDGE); Device.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MAG_FILTER, OpenGL.GL_LINEAR); Device.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MIN_FILTER, OpenGL.GL_LINEAR); Device.TexImage2D(OpenGL.GL_TEXTURE_2D, 0, OpenGL.GL_RGBA8, (int)Width, (int)Height, 0, OpenGL.GL_RGBA, OpenGL.GL_UNSIGNED_BYTE, null); uint[] frameID = new uint[1]; Device.GenFramebuffersEXT(1, frameID); FrameBufferID = frameID[0]; Device.BindFramebufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, FrameBufferID); Device.FramebufferTexture2DEXT(OpenGL.GL_FRAMEBUFFER_EXT, OpenGL.GL_COLOR_ATTACHMENT0_EXT, OpenGL.GL_TEXTURE_2D, TextureID, 0); uint[] dephtID = new uint[1]; Device.GenRenderbuffersEXT(1, dephtID); DepthBufferID = dephtID[0]; Device.BindRenderbufferEXT(OpenGL.GL_RENDERBUFFER, DepthBufferID); Device.RenderbufferStorageEXT(OpenGL.GL_RENDERBUFFER_EXT, OpenGL.GL_DEPTH_COMPONENT24, (int)Width, (int)Height); Device.FramebufferRenderbufferEXT( OpenGL.GL_FRAMEBUFFER_EXT, OpenGL.GL_DEPTH_ATTACHMENT_EXT, OpenGL.GL_RENDERBUFFER, DepthBufferID); Device.BindFramebufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, 0); }
/// <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); }
/// <summary> /// Creates the render context provider. Must also create the OpenGL extensions. /// </summary> /// <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"></param> /// <returns></returns> public override bool Create(OpenGL gl, int width, int height, int bitDepth, object parameter) { this.gl = gl; // Call the base class. base.Create(gl, width, height, bitDepth, parameter); uint[] ids = new uint[1]; // First, create the frame buffer and bind it. ids = new uint[1]; gl.GenFramebuffersEXT(1, ids); frameBufferID = ids[0]; gl.BindFramebufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, frameBufferID); // Create the colour render buffer and bind it, then allocate storage for it. gl.GenRenderbuffersEXT(1, ids); colourRenderBufferID = ids[0]; gl.BindRenderbufferEXT(OpenGL.GL_RENDERBUFFER_EXT, colourRenderBufferID); gl.RenderbufferStorageEXT(OpenGL.GL_RENDERBUFFER_EXT, OpenGL.GL_RGBA, width, height); // Create the depth render buffer and bind it, then allocate storage for it. gl.GenRenderbuffersEXT(1, ids); depthRenderBufferID = ids[0]; gl.BindRenderbufferEXT(OpenGL.GL_RENDERBUFFER_EXT, depthRenderBufferID); gl.RenderbufferStorageEXT(OpenGL.GL_RENDERBUFFER_EXT, OpenGL.GL_DEPTH_COMPONENT24, width, height); // Set the render buffer for colour and depth. gl.FramebufferRenderbufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, OpenGL.GL_COLOR_ATTACHMENT0_EXT, OpenGL.GL_RENDERBUFFER_EXT, colourRenderBufferID); gl.FramebufferRenderbufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, OpenGL.GL_DEPTH_ATTACHMENT_EXT, OpenGL.GL_RENDERBUFFER_EXT, depthRenderBufferID); dibSectionDeviceContext = Win32.CreateCompatibleDC(deviceContextHandle); // Create the DIB section. dibSection.Create(dibSectionDeviceContext, width, height, bitDepth); return true; }
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"); } }
public static void Unbind(OpenGL gl) { gl.BindRenderbufferEXT(GL_RENDERBUFFER, 0); gl.BindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); }
public void Bind() { GL.BindRenderbufferEXT(GL_RENDERBUFFER, renderBufferIds[0]); GL.BindFramebufferEXT(GL_FRAMEBUFFER_EXT, frameBufferIds[0]); }