Exemple #1
0
		public FrameBuffer(Size size)
		{
			this.size = size;
			if (!Exts.IsPowerOf2(size.Width) || !Exts.IsPowerOf2(size.Height))
				throw new InvalidDataException("Frame buffer size ({0}x{1}) must be a power of two".F(size.Width, size.Height));

			GL.Ext.GenFramebuffers(1, out framebuffer);
			ErrorHandler.CheckGlError();
			GL.Ext.BindFramebuffer(FramebufferTarget.FramebufferExt, framebuffer);
			ErrorHandler.CheckGlError();

			// Color
			texture = new Texture();
			texture.SetEmpty(size.Width, size.Height);
			GL.Ext.FramebufferTexture2D(FramebufferTarget.FramebufferExt, FramebufferAttachment.ColorAttachment0Ext, TextureTarget.Texture2D, texture.ID, 0);
			ErrorHandler.CheckGlError();

			// Depth
			GL.Ext.GenRenderbuffers(1, out depth);
			ErrorHandler.CheckGlError();

			GL.Ext.BindRenderbuffer(RenderbufferTarget.RenderbufferExt, depth);
			ErrorHandler.CheckGlError();

			GL.Ext.RenderbufferStorage(RenderbufferTarget.RenderbufferExt, RenderbufferStorage.DepthComponent, size.Width, size.Height);
			ErrorHandler.CheckGlError();

			GL.Ext.FramebufferRenderbuffer(FramebufferTarget.FramebufferExt, FramebufferAttachment.DepthAttachmentExt, RenderbufferTarget.RenderbufferExt, depth);
			ErrorHandler.CheckGlError();

			// Test for completeness
			var status = GL.Ext.CheckFramebufferStatus(FramebufferTarget.FramebufferExt);
			if (status != FramebufferErrorCode.FramebufferCompleteExt)
			{
				var error = "Error creating framebuffer: {0}\n{1}".F(status, new StackTrace());
				ErrorHandler.WriteGraphicsLog(error);
				throw new InvalidOperationException("OpenGL Error: See graphics.log for details.");
			}

			// Restore default buffer
			GL.Ext.BindFramebuffer(FramebufferTarget.FramebufferExt, 0);
			ErrorHandler.CheckGlError();
		}