func (glRenderer *OpenglRenderer) mainLoop() { glRenderer.onUpdate() glRenderer.updateLights() //set defaults glRenderer.UseRendererParams(renderer.DefaultRendererParams()) glRenderer.UseMaterial(nil) if len(glRenderer.postEffects) == 0 { gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) glRenderer.onRender() } else { //Render to the first post effect buffer gl.BindFramebuffer(gl.FRAMEBUFFER, glRenderer.postEffects[0].fboId) gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) glRenderer.onRender() //Render Post effects for i := 0; i < len(glRenderer.postEffects)-1; i = i + 1 { gl.BindFramebuffer(gl.FRAMEBUFFER, glRenderer.postEffects[i+1].fboId) gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) glRenderer.renderPostEffect(glRenderer.postEffects[i]) } //Render final post effect to the frame buffer gl.BindFramebuffer(gl.FRAMEBUFFER, 0) gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) glRenderer.renderPostEffect(glRenderer.postEffects[len(glRenderer.postEffects)-1]) } }
func (f *FrameBuffer) Unbind() { // finish drawing gl.Flush() // unbind gl.BindFramebuffer(gl.FRAMEBUFFER, 0) }
func (f *FrameBuffer) Bind() { gl.BindTexture(gl.TEXTURE_2D, 0) // why? // bind this frame buffer gl.BindFramebuffer(gl.FRAMEBUFFER, f.id) // set viewport size equal to buffer size gl.Viewport(0, 0, f.Width, f.Height) }
func CreateFrameBuffer(width, height int32) *FrameBuffer { f := &FrameBuffer { Width: width, Height: height, Buffers: []DrawBuffer { }, ClearColor: Color4(0,0,0,1), } gl.GenFramebuffers(1, &f.id) gl.BindFramebuffer(gl.FRAMEBUFFER, f.id) return f }
func (glRenderer *OpenglRenderer) CreatePostEffect(shader *renderer.Shader) { //Create program glRenderer.CreateShader(shader) gl.UseProgram(shader.Program) //Create Texture var fbo_texture uint32 gl.ActiveTexture(gl.TEXTURE0) gl.GenTextures(1, &fbo_texture) gl.BindTexture(gl.TEXTURE_2D, fbo_texture) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE) gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, int32(glRenderer.WindowWidth), int32(glRenderer.WindowHeight), 0, gl.RGBA, gl.UNSIGNED_BYTE, nil) //Create depth buffer var dbo uint32 gl.GenRenderbuffers(1, &dbo) gl.BindRenderbuffer(gl.RENDERBUFFER, dbo) gl.RenderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_COMPONENT16, int32(glRenderer.WindowWidth), int32(glRenderer.WindowHeight)) gl.BindRenderbuffer(gl.RENDERBUFFER, 0) //Create frame buffer var fbo uint32 gl.GenFramebuffers(1, &fbo) gl.BindFramebuffer(gl.FRAMEBUFFER, fbo) gl.FramebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, fbo_texture, 0) gl.FramebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.RENDERBUFFER, dbo) //add new postEffect to the queue newPe := postEffect{ program: shader.Program, textureId: fbo_texture, dboId: dbo, fboId: fbo, shader: shader, } glRenderer.postEffects = append(glRenderer.postEffects, newPe) }