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> /// 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); }
public void CleanBuffers(OpenGL gl) { var shader = shaderClean; gl.BindFramebufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, _FrameBuffer[0]); shader.Bind(gl); gl.DrawArrays(OpenGL.GL_TRIANGLES, 0, 3); shader.Unbind(gl); gl.BindFramebufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, _FrameBuffer[1]); shader.Bind(gl); gl.DrawArrays(OpenGL.GL_TRIANGLES, 0, 3); shader.Unbind(gl); gl.BindFramebufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, 0); Debug.WriteLine("Cleaning buffers"); }
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> /// 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); }
/// <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; }
/// <summary> /// Renders the scene in retained mode. /// </summary> /// <param name="gl">The OpenGL instance.</param> /// <param name="useToonShader">if set to <c>true</c> use the toon shader, otherwise use a per-pixel shader.</param> public void Render(OpenGL gl) { if (_SaveNextRender) { SaveInternal(gl); } if (_MaxIterations > 0 && _FramesRendered > _MaxIterations - 1 && _Rendering == false) { return; } // Clear the color and depth buffer. gl.ClearColor(0f, 0f, 0f, 0f); gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT | OpenGL.GL_STENCIL_BUFFER_BIT); int renderBuffer = 0; if (_PingPong) { renderBuffer = 1; } float[] viewport = new float[4]; gl.GetFloat(OpenGL.GL_VIEWPORT, viewport); Debug.WriteLine("Rendering renderbuffer : " + renderBuffer); if (_Depth) { gl.Viewport(0, 0, 1, 1); gl.BindFramebufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, _DepthFrameBuffer[0]); SceneRender(gl); gl.Viewport(0, 0, (int)viewport[2], (int)viewport[3]); gl.BindFramebufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, 0); gl.BindTexture(OpenGL.GL_TEXTURE_2D, _DepthCalcBuffer[0]); int[] pixels = new int[4]; gl.GetTexImage(OpenGL.GL_TEXTURE_2D, 0, OpenGL.GL_RGBA, OpenGL.GL_FLOAT, pixels); float valr = BitConverter.ToSingle(BitConverter.GetBytes(pixels[0]), 0); // if (valr != valr) // throw new Exception("Depth test failed"); // float valg = BitConverter.ToSingle(BitConverter.GetBytes(pixels[1]), 0); // float valb = BitConverter.ToSingle(BitConverter.GetBytes(pixels[2]), 0); // float vala = BitConverter.ToSingle(BitConverter.GetBytes(pixels[3]), 0); _ImageDepth = valr; _ImageDepthSet = true; _Depth = false; } gl.Viewport(0, 0, _TargetWidth, _TargetHeight); gl.BindFramebufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, _FrameBuffer[renderBuffer]); SceneRender(gl); int target = 0; _PostProcess.Render(gl, _TargetWidth, _TargetHeight, ref target, _EffectFrameBuffer, _RaytracerBuffer[renderBuffer], _EffectRaytracerBuffer); // !!!!!!!!!!!!!!!! Tonemapping gl.Viewport(0, 0, _TargetWidth, _TargetHeight); gl.BindFramebufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, _IntFrameBuffer[0]); var shader = shaderTransfer; gl.ActiveTexture(OpenGL.GL_TEXTURE0); gl.BindTexture(OpenGL.GL_TEXTURE_2D, _EffectRaytracerBuffer[target]); shader.Bind(gl); shader.SetUniform1(gl, "renderedTexture", 0); shader.SetUniform1(gl, "gammaFactor", (float)_PostProcess._GammaFactor); shader.SetUniform1(gl, "gammaContrast", (float)_PostProcess._GammaContrast); shader.SetUniform1(gl, "mode", _PostProcess._ToneMappingMode); shader.SetUniform1(gl, "toneFactor", (float)_PostProcess._ToneFactor); gl.DrawArrays(OpenGL.GL_TRIANGLES, 0, 3); // CheckForError(gl); shader.Unbind(gl); // !!!!!!!!!!!!!!!!! Int to final framebuffer gl.Viewport(0, 0, (int)viewport[2], (int)viewport[3]); gl.BindFramebufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, 0); // get a reference to the transfer shader shader = shaderIntTransfer; gl.ActiveTexture(OpenGL.GL_TEXTURE0); gl.BindTexture(OpenGL.GL_TEXTURE_2D, _PostprocessBuffer[0]); shader.Bind(gl); shader.SetUniform1(gl, "renderedTexture", 0); gl.DrawArrays(OpenGL.GL_TRIANGLES, 0, 3); gl.Viewport(0, 0, (int)viewport[2], (int)viewport[3]); // CheckForError(gl); shader.Unbind(gl); // TIDY UP gl.BindTexture(OpenGL.GL_TEXTURE_2D, 0); if (_Rendering) { _FramesRendered += 1.0 / (double)_ProgressiveSteps; if (_MaxIterations > 0 && _FramesRendered > _MaxIterations - 1) { _Rendering = false; } else { _ProgressiveIndex += 256 / _ProgressiveSteps; _Rays = _TargetHeight * _TargetHeight * _FramesRendered; if (_ProgressiveIndex >= 256) { _ProgressiveIndex = 0; _PingPong = !_PingPong; } } } }
/// <summary> /// Initialises the Scene. /// </summary> /// <param name="gl">The OpenGL instance.</param> public void Initialise(OpenGL gl, int width, int height, mat4 viewMatrix, vec3 position, bool burnVariables) { Logger.Log("ShaderRenderer.Initialise Started"); _GL = gl; if (_Initialised) { Destroy(gl); _Initialised = true; } _ViewMatrix = viewMatrix; _Position = position; _TargetWidth = width; _TargetHeight = height; _ProgressiveSteps = 1; _ProgressiveIndex = 0; _BurnVariables = burnVariables; // We're going to specify the attribute locations for the position and normal, // so that we can force both shaders to explicitly have the same locations. const uint positionAttribute = 0; var attributeLocations = new Dictionary <uint, string> { { positionAttribute, "Position" } }; Logger.Log("ShaderRenderer.Initialise Loading shaders from manifest"); // Create the raymarch shader shaderRayMarch = new ShaderProgram(); if (_Program == null) { shaderRayMarch.Create(gl, ManifestResourceLoader.LoadTextFile(@"Shaders\RayMarchProgressive.vert"), ManifestResourceLoader.LoadTextFile(@"Shaders\RayMarch.frag"), attributeLocations); } else { if (_BurnVariables) { ShaderVariables shaderVars = GetShaderVars(); shaderVars.BurnVariables(ref _Program); } _ShaderError = false; try { shaderRayMarch.Create(gl, ManifestResourceLoader.LoadTextFile(@"Shaders\RayMarchProgressive.vert"), _Program, attributeLocations); } catch (ShaderCompilationException exception) { _ShaderError = true; MessageBox.Show(exception.Message + "\r\n" + exception.CompilerOutput); } } // Create the transfer shader string fragShader = @" #version 130 in vec2 texCoord; out vec4 FragColor; uniform float mode; // 0=ramp, 1=exposure, 2=standard uniform float toneFactor; uniform float gammaFactor; uniform float gammaContrast; uniform sampler2D renderedTexture; vec3 filmic(vec3 value) { float A=0.22; float B=0.30; float C=0.1; float D=0.2; float E=0.01; float F=0.3; return ((value*(A*value+C*B)+D*E)/(value*(A*value+B)+D*F)) - E/F; } void main() { vec4 rgb = texture(renderedTexture, vec2((texCoord.x+1)*0.5, (texCoord.y+1)*0.5)); FragColor=rgb; // FragColor.rgb /= FragColor.a; // brightness/contrast float luminance = dot(FragColor.rgb, vec3(0.2126,0.7152,0.0722)); float luminanceOut = gammaFactor * pow(luminance, gammaContrast); float multiplier = (max(0, luminance) * luminanceOut) / (luminance * luminance); FragColor.rgb *= multiplier; if (mode>2.9 && mode<3.1) { //filmic https://www.slideshare.net/ozlael/hable-john-uncharted2-hdr-lighting FragColor.rgb = filmic(FragColor.rgb)/filmic(vec3(toneFactor)); } else if (mode>1.9 && mode<2.1) { //reinhard https://imdoingitwrong.wordpress.com/2010/08/19/why-reinhard-desaturates-my-blacks-3/ float nL = luminance * (1+luminance/(toneFactor*toneFactor)) / (1+luminance); FragColor.rgb *= nL; } else if (mode>0.9 && mode<1.1) { //exposure originally Matt Fairclough FragColor.rgb = 1 - exp(-FragColor.rgb * toneFactor); } else { FragColor.rgb /= toneFactor; } } "; shaderTransfer = new ShaderProgram(); shaderTransfer.Create(gl, ManifestResourceLoader.LoadTextFile(@"Shaders\RayMarch.vert"), fragShader, attributeLocations); CheckForError(gl); fragShader = @" #version 130 in vec2 texCoord; out vec4 FragColor; void main() { FragColor=vec4(0,0,0,0); } "; shaderClean = new ShaderProgram(); shaderClean.Create(gl, ManifestResourceLoader.LoadTextFile(@"Shaders\RayMarch.vert"), fragShader, attributeLocations); CheckForError(gl); // Create the transfer shader string fragShaderIntTransfer = @" #version 130 in vec2 texCoord; out vec4 FragColor; uniform sampler2D renderedTexture; void main() { vec4 rgb = texture(renderedTexture, vec2((texCoord.x+1)*0.5, (texCoord.y+1)*0.5)); FragColor=rgb; } "; shaderIntTransfer = new ShaderProgram(); shaderIntTransfer.Create(gl, ManifestResourceLoader.LoadTextFile(@"Shaders\RayMarch.vert"), fragShaderIntTransfer, attributeLocations); CheckForError(gl); Logger.Log("ShaderRenderer.Initialise Loading random numbers"); LoadRandomNumbers(gl); Logger.Log("ShaderRenderer.Initialise Finished loading random numbers"); float[] viewport = new float[4]; gl.GetFloat(OpenGL.GL_VIEWPORT, viewport); gl.GenFramebuffersEXT(2, _FrameBuffer); CheckForError(gl); gl.GenTextures(2, _RaytracerBuffer); CheckForError(gl); for (int i = 0; i < 2; i++) { gl.BindFramebufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, _FrameBuffer[i]); gl.BindTexture(OpenGL.GL_TEXTURE_2D, _RaytracerBuffer[i]); 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.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_GENERATE_MIPMAP_SGIS, OpenGL.GL_FALSE); // automatic mipmap // gl.TexImage2D(OpenGL.GL_TEXTURE_2D, 0, OpenGL.GL_RGBA, (int)viewport[2], (int)viewport[3], 0, // OpenGL.GL_RGBA, OpenGL.GL_FLOAT, null); gl.TexImage2D(OpenGL.GL_TEXTURE_2D, 0, OpenGL.GL_RGBA32F, _TargetWidth, _TargetHeight, 0, OpenGL.GL_RGBA, OpenGL.GL_FLOAT, null); CheckForError(gl); gl.FramebufferTexture2DEXT(OpenGL.GL_FRAMEBUFFER_EXT, OpenGL.GL_COLOR_ATTACHMENT0_EXT, OpenGL.GL_TEXTURE_2D, _RaytracerBuffer[i], 0); gl.FramebufferRenderbufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, OpenGL.GL_DEPTH_ATTACHMENT_EXT, OpenGL.GL_RENDERBUFFER_EXT, 0); } gl.BindTexture(OpenGL.GL_TEXTURE_2D, 0); gl.GenFramebuffersEXT(2, _EffectFrameBuffer); CheckForError(gl); gl.GenTextures(2, _EffectRaytracerBuffer); CheckForError(gl); for (int i = 0; i < 2; i++) { gl.BindFramebufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, _EffectFrameBuffer[i]); gl.BindTexture(OpenGL.GL_TEXTURE_2D, _EffectRaytracerBuffer[i]); 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.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_GENERATE_MIPMAP_SGIS, OpenGL.GL_FALSE); // automatic mipmap gl.TexImage2D(OpenGL.GL_TEXTURE_2D, 0, OpenGL.GL_RGBA32F, _TargetWidth, _TargetHeight, 0, OpenGL.GL_RGBA, OpenGL.GL_FLOAT, null); CheckForError(gl); gl.FramebufferTexture2DEXT(OpenGL.GL_FRAMEBUFFER_EXT, OpenGL.GL_COLOR_ATTACHMENT0_EXT, OpenGL.GL_TEXTURE_2D, _EffectRaytracerBuffer[i], 0); gl.FramebufferRenderbufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, OpenGL.GL_DEPTH_ATTACHMENT_EXT, OpenGL.GL_RENDERBUFFER_EXT, 0); } gl.BindTexture(OpenGL.GL_TEXTURE_2D, 0); // and now initialise the integer framebuffer gl.GenFramebuffersEXT(1, _IntFrameBuffer); CheckForError(gl); gl.GenTextures(1, _PostprocessBuffer); CheckForError(gl); gl.BindFramebufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, _IntFrameBuffer[0]); gl.BindTexture(OpenGL.GL_TEXTURE_2D, _PostprocessBuffer[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); 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_SGIS, OpenGL.GL_FALSE); // automatic mipmap gl.TexImage2D(OpenGL.GL_TEXTURE_2D, 0, OpenGL.GL_RGBA, _TargetWidth, _TargetHeight, 0, OpenGL.GL_RGBA, OpenGL.GL_UNSIGNED_BYTE, null); CheckForError(gl); gl.FramebufferTexture2DEXT(OpenGL.GL_FRAMEBUFFER_EXT, OpenGL.GL_COLOR_ATTACHMENT0_EXT, OpenGL.GL_TEXTURE_2D, _PostprocessBuffer[0], 0); gl.FramebufferRenderbufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, OpenGL.GL_DEPTH_ATTACHMENT_EXT, OpenGL.GL_RENDERBUFFER_EXT, 0); gl.BindTexture(OpenGL.GL_TEXTURE_2D, 0); _PostProcess = new PostProcess(); _PostProcess.Initialise(gl); gl.GenFramebuffersEXT(1, _DepthFrameBuffer); gl.GenTextures(1, _DepthCalcBuffer); gl.BindFramebufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, _DepthFrameBuffer[0]); gl.BindTexture(OpenGL.GL_TEXTURE_2D, _DepthCalcBuffer[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_SGIS, OpenGL.GL_TRUE); gl.TexImage2D(OpenGL.GL_TEXTURE_2D, 0, OpenGL.GL_RGBA32F, 1, 1, 0, OpenGL.GL_RGBA, OpenGL.GL_FLOAT, null); gl.FramebufferTexture2DEXT(OpenGL.GL_FRAMEBUFFER_EXT, OpenGL.GL_COLOR_ATTACHMENT0_EXT, OpenGL.GL_TEXTURE_2D, _DepthCalcBuffer[0], 0); gl.FramebufferRenderbufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, OpenGL.GL_DEPTH_ATTACHMENT_EXT, OpenGL.GL_RENDERBUFFER_EXT, 0); _Initialised = true; /* * gl.GenRenderbuffersEXT(2, _RaytracerBuffer); * gl.BindRenderbufferEXT(OpenGL.GL_RENDERBUFFER_EXT, _RaytracerBuffer[0]); * gl.RenderbufferStorageEXT(OpenGL.GL_RENDERBUFFER_EXT, OpenGL.GL_RGBA32F, (int)viewport[2], (int)viewport[3]); * gl.BindRenderbufferEXT(OpenGL.GL_RENDERBUFFER_EXT, _RaytracerBuffer[1]); * gl.RenderbufferStorageEXT(OpenGL.GL_RENDERBUFFER_EXT, OpenGL.GL_RGBA32F, (int)viewport[2], (int)viewport[3]); */ // gl.GenRenderbuffersEXT(1, _RenderBuffer); //gl.BindRenderbufferEXT(OpenGL.GL_RENDERBUFFER_EXT, _RenderBuffer[0]); //gl.RenderbufferStorageEXT(OpenGL.GL_RENDERBUFFER_EXT, OpenGL.GL_RGBA, (int)viewport[2], (int)viewport[3]); Logger.Log("ShaderRenderer.Initialise Finished"); }
public void BeginBuffering() { gl.BindFramebufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, framebuffers[0]); }
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"); } }
public void Render() { if (postprocessingEnabled && postShader != null && postShader.Compiled) { gl.BindFramebufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, fbo); } float[] clear = Convert.ColorToGLColor(ClearColor); gl.ClearColor(clear[0], clear[1], clear[2], clear[3]); camera.Project(gl); gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT | OpenGL.GL_STENCIL_BUFFER_BIT); attrs.Push(gl, null); if (RenderGrid) grid.Render(gl); if (RenderAxies) axies.Render(gl); gl.Enable(OpenGL.GL_BLEND); gl.BlendFunc(OpenGL.GL_ONE, OpenGL.GL_ONE); foreach (Node node in children) { if (node is Light) node.Render(gl); } if (Model != null) Model.Render(gl); foreach (Node node in children) { if (node is Light) ((Light)node).Pop(gl); } gl.Disable(OpenGL.GL_BLEND); attrs.Pop(gl, null); gl.Flush(); if (postprocessingEnabled && postShader != null && postShader.Compiled) { gl.BindFramebufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, 0); gl.ClearColor(0, 0, 0, 1); gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT); gl.MatrixMode(OpenGL.GL_PROJECTION); gl.LoadIdentity(); gl.Ortho(0, viewportSize.Width, viewportSize.Height, 0, -1, 1); gl.MatrixMode(OpenGL.GL_MODELVIEW); gl.LoadIdentity(); postShader.Bind(); postShader.SetUniform1("resolution_x", viewportSize.Width); postShader.SetUniform1("resolution_y", viewportSize.Height); //gl.Uniform2(postShader.GetUniformLocation("resolution"), // viewportSize.Width, viewportSize.Height); gl.ActiveTexture(OpenGL.GL_TEXTURE0); gl.BindTexture(OpenGL.GL_TEXTURE_2D, fboTexture); postShader.SetUniform1("texFramebuffer", 0); gl.Begin(OpenGL.GL_QUADS); gl.TexCoord(0, 1); gl.Vertex(0, 0); gl.TexCoord(0, 0); gl.Vertex(0, (float)viewportSize.Height); gl.TexCoord(1, 0); gl.Vertex((float)viewportSize.Width, (float)viewportSize.Height); gl.TexCoord(1, 1); gl.Vertex((float)viewportSize.Width, 0); gl.End(); postShader.Unbind(); } }
public void Bind() { GL.BindRenderbufferEXT(GL_RENDERBUFFER, renderBufferIds[0]); GL.BindFramebufferEXT(GL_FRAMEBUFFER_EXT, frameBufferIds[0]); }
/// <summary> /// Render texture with FBO and draw it to memory. /// </summary> private void RenderTexWithFBOandDraw() { OpenGL gl = openGLControl.OpenGL; // render to texture // adjust viewport and projection matrix to texture dimension gl.Viewport(0, 0, TEXTURE_WIDTH, TEXTURE_HEIGHT); gl.MatrixMode(OpenGL.GL_PROJECTION); gl.LoadIdentity(); gl.Perspective(fovy, (double)Width / (double)Height, 0.01f, 100.0); gl.LookAt( status.eye.x + status.kinect.x, status.eye.y + status.kinect.y, status.eye.z + status.kinect.z, buildingModelCursor.GetCoordinate().x, buildingModelCursor.GetCoordinate().y, buildingModelCursor.GetCoordinate().z, 0, 1, 0 ); // with FBO // set the rendering destination to FBO gl.BindFramebufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, framebuffer_name[0]); gl.MatrixMode(OpenGL.GL_MODELVIEW); gl.LoadIdentity(); // clear buffer gl.ClearColor(0, 0, 0, 1); gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT); // start to draw gl.PushMatrix(); // Load the identity matrix. gl.Enable(OpenGL.GL_TEXTURE_2D); drawer.DrawBuildingPart(gl, buildingModelCursor, DrawType.Full); if (status != null && buildingModelCursor != null) { switch (buildingModelCursor.GetBuildingType()) { case BuildingObjectType.Floor: drawer.DrawBuildingPart(gl, buildingOutsideModel, DrawType.WireFrame); drawer.DrawBuildingPart(gl, buildingModelCursor, DrawType.Full); break; case BuildingObjectType.Object: System.Collections.Hashtable childs = buildingModelCursor.Father.GetChilds(); foreach (System.Collections.DictionaryEntry childEntry in buildingModelCursor.Father.GetChilds()) { BuildingObjectLib3DS child = childEntry.Value as BuildingObjectLib3DS; if (child != buildingModelCursor) { drawer.DrawBuildingPart(gl, child, DrawType.WireFrame); } else { drawer.DrawBuildingPart(gl, child, DrawType.Full); } } break; case BuildingObjectType.Room: // TODO case BuildingObjectType.Outside: case BuildingObjectType.Building: drawer.DrawBuildingPart(gl, buildingModelCursor, DrawType.Full); break; } } //drawer.DrawBuildingPart(gl, modelForFun, DrawType.Full); gl.PopMatrix(); // back to normal window-system-provided framebuffer gl.BindFramebufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, 0); // unbind // trigger mipmaps generation explicitly // NOTE: If GL_GENERATE_MIPMAP is set to GL_TRUE, then glCopyTexSubImage2D() // triggers mipmap generation automatically. However, the texture attached // onto a FBO should generate mipmaps manually via glGenerateMipmapEXT(). gl.BindTexture(OpenGL.GL_TEXTURE_2D, texture_name[0]); gl.GenerateMipmapEXT(OpenGL.GL_TEXTURE_2D); //gl.TexEnv(OpenGL.GL_TEXTURE_ENV, OpenGL.GL_TEXTURE_ENV_MODE, OpenGL.GL_ADD); gl.BindTexture(OpenGL.GL_TEXTURE_2D, 0); // TODO: without FBO, maybe need }
public void Render(OpenGL gl, int targetWidth, int targetHeight, ref int target, uint[] effectFrameBuffer, uint raytracerBuffer, uint[] effectRaytracerBuffer) { if (_PostProcessFilter == 0) { // !!!!!!!!!!!!!!! rgb / a gl.Viewport(0, 0, targetWidth, targetHeight); gl.BindFramebufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, effectFrameBuffer[target]); var shader = shaderRGBA; gl.ActiveTexture(OpenGL.GL_TEXTURE0); gl.BindTexture(OpenGL.GL_TEXTURE_2D, raytracerBuffer); shader.Bind(gl); shader.SetUniform1(gl, "renderedTexture", 0); gl.DrawArrays(OpenGL.GL_TRIANGLES, 0, 3); shader.Unbind(gl); } else if (_PostProcessFilter == 1) { // !!!!!!!!!!!!!!! rgb / a gl.Viewport(0, 0, targetWidth, targetHeight); gl.BindFramebufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, effectFrameBuffer[target]); var shader = shaderRGBA; gl.ActiveTexture(OpenGL.GL_TEXTURE0); gl.BindTexture(OpenGL.GL_TEXTURE_2D, raytracerBuffer); shader.Bind(gl); shader.SetUniform1(gl, "renderedTexture", 0); gl.DrawArrays(OpenGL.GL_TRIANGLES, 0, 3); shader.Unbind(gl); // !!!!!!!!!!!!!!! exponent target = target > 0 ? 0 : 1; gl.Viewport(0, 0, targetWidth, targetHeight); gl.BindFramebufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, effectFrameBuffer[target]); shader = shaderHighlights; gl.ActiveTexture(OpenGL.GL_TEXTURE0); gl.BindTexture(OpenGL.GL_TEXTURE_2D, effectRaytracerBuffer[target > 0 ? 0 : 1]); shader.Bind(gl); shader.SetUniform1(gl, "exponent", (float)_GaussianExposure); shader.SetUniform1(gl, "renderedTexture", 0); gl.DrawArrays(OpenGL.GL_TRIANGLES, 0, 3); shader.Unbind(gl); // !!!!!!!!!!!!!!! gaussianShaderX target = target > 0 ? 0 : 1; gl.Viewport(0, 0, targetWidth, targetHeight); gl.BindFramebufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, effectFrameBuffer[target]); shader = shaderGaussianX; gl.ActiveTexture(OpenGL.GL_TEXTURE0); gl.BindTexture(OpenGL.GL_TEXTURE_2D, effectRaytracerBuffer[target > 0 ? 0 : 1]); shader.Bind(gl); shader.SetUniform1(gl, "screenWidth", targetWidth); shader.SetUniform1(gl, "screenHeight", targetHeight); shader.SetUniform1(gl, "renderedTexture", 0); gl.DrawArrays(OpenGL.GL_TRIANGLES, 0, 3); shader.Unbind(gl); // !!!!!!!!!!!!!!! gaussianShaderY target = target > 0 ? 0 : 1; gl.Viewport(0, 0, targetWidth, targetHeight); gl.BindFramebufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, effectFrameBuffer[target]); shader = shaderGaussianY; gl.ActiveTexture(OpenGL.GL_TEXTURE0); gl.BindTexture(OpenGL.GL_TEXTURE_2D, effectRaytracerBuffer[target > 0 ? 0 : 1]); shader.Bind(gl); shader.SetUniform1(gl, "screenWidth", targetWidth); shader.SetUniform1(gl, "screenHeight", targetHeight); shader.SetUniform1(gl, "renderedTexture", 0); gl.DrawArrays(OpenGL.GL_TRIANGLES, 0, 3); shader.Unbind(gl); // !!!!!!!!!!!!!!! blend target = target > 0 ? 0 : 1; gl.Viewport(0, 0, targetWidth, targetHeight); gl.BindFramebufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, effectFrameBuffer[target]); shader = shaderBlend; gl.ActiveTexture(OpenGL.GL_TEXTURE0); gl.BindTexture(OpenGL.GL_TEXTURE_2D, raytracerBuffer); gl.ActiveTexture(OpenGL.GL_TEXTURE1); gl.BindTexture(OpenGL.GL_TEXTURE_2D, effectRaytracerBuffer[target > 0 ? 0 : 1]); shader.Bind(gl); shader.SetUniform1(gl, "factor1", 1); shader.SetUniform1(gl, "factor2", (float)_PostProcessAmount); int rte1 = shader.GetUniformLocation(gl, "renderedTexture1"); int rne1 = shader.GetUniformLocation(gl, "renderedTexture2"); gl.Uniform1(rte1, 0); gl.Uniform1(rne1, 1); gl.DrawArrays(OpenGL.GL_TRIANGLES, 0, 3); shader.Unbind(gl); } }
public void Bind() { _gl.BindFramebufferEXT(OpenGL.GL_FRAMEBUFFER_EXT, _fbo[0]); }
public static void Unbind(OpenGL gl) { gl.BindRenderbufferEXT(GL_RENDERBUFFER, 0); gl.BindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); }