OpenGL frame buffer

Briefly

Generally in the process of building an OpenGL environment, the window system will create a default frame buffer. This frame buffer is the only frame buffer that can be recognized by the display system of the graphics server, so the application-defined frame buffer can only be used for off-screen applications. Rendering occasions. The framebuffer does not store rendering data directly, the rendered results must be saved to some attachments (colors, depths and stencils). The framebuffer of the window system has its own cache attachments at the beginning of creation, and custom framebuffer objects also need to manually add these attachments.

The memory model of the frame buffer is as follows:

The red area is the memory space where the data is actually stored.

 

texture object

When a texture is attached to the framebuffer, all rendering instructions will be written to the texture as if it were a normal color buffer. The advantage of using textures is that the results of all rendering operations will be stored in a texture image, which we can easily use later in our shaders.

 

render cache object

The render buffer is an efficient memory area managed by OpenGL that stores formatted data. The data in the renderbuffer is only meaningful if it is associated with a framebuffer object. Since renderbuffer objects are usually write-only, they are often used for depth and stencil attachments, because most of the time we don't need to read values ​​from depth and stencil buffers, only depth and stencil testing. We need depth and stencil values ​​for testing, but we don't need to sample them, so renderbuffer objects are perfect for them.

 

Framebuffer Integrity

Format combinations of textures and caches are varied, and different framebuffer attachment settings can cause framebuffer objects to not render properly. So after creating the framebuffer and its attachments, check it once to see if it was created successfully.

 

framebuffer invalidation

The frame buffer will occupy a considerable amount of system resources, so it needs to be released after use. In OpenGL, an area or the whole of the frame buffer can be declared as not in use, and it can be released immediately through the functions gllnvalidateSubFramebuffer() and gllnvalidateFramebuffer().

 

Steps to build a framebuffer

1. Create a frame buffer object and bind the frame buffer object

glGenFramebuffers()

glBindFramebuffer()

2, create texture attachments

glGenTextures()

glBindTexture()

3, attach the texture attachment to the framebuffer

glFramebufferTexture2D()

 

4, create a renderbuffer object for depth and stencil attachment and bind

glGenRenderbuffers()

glBindRenderbuffer ()

5. Create a depth and stencil renderbuffer object and allocate space to the renderbuffer object

glRenderbufferStorage()

6. Attach the stencil and depth buffer objects to this renderbuffer object:

glFramebufferRenderbuffer()

7, to determine whether the creation is successful

glCheckFramebufferStatus()

 

Frame buffer implementation process

part of the code

//----------------------Build framebuffer----------------------
	glGenFramebuffers(1, &framebuffer);
	//Bind to the framebuffer, all subsequent read and write operations to the framebuffer will affect the currently bound framebuffer
	glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);

	//create color attachment
	glGenTextures(1, &textureColorbuffer);
	glBindTexture(GL_TEXTURE_2D, textureColorbuffer);
	// For this texture, we just allocated memory without filling it
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 800, 600, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	//Attach the texture attachment to the framebuffer
	glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textureColorbuffer, 0);


	//创建一个用于深度和模板附件的渲染缓冲对象
	unsigned int rbo;
	glGenRenderbuffers(1, &rbo);
	//绑定渲染缓冲对象
	glBindRenderbuffer(GL_RENDERBUFFER, rbo);
	//创建一个深度和模板渲染缓冲对象,并给渲染缓冲对象分配空间
	glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, 800, 600);
	//把模板和深度缓冲对象附加在这个渲染缓冲对象:
	glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, rbo);

	//判断检查帧缓冲是否完整,如果完整,则返回GL_FRAMEBUFFER_COMPLETE
	if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
	{
		cout << "ERROR::FRAMEBUFFER:: Framebuffer is not complete!" << endl;
	}
	//解除纹理绑定
	glBindTexture(GL_TEXTURE_2D, 0);
	//解除渲染缓冲绑定
	glBindRenderbuffer(GL_RENDERBUFFER, 0);
	//解除帧缓冲绑定
	glBindFramebuffer(GL_FRAMEBUFFER, 0);


参见:《OpenGL编程指南》第八版第3章

            learnOpengl教程


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325852052&siteId=291194637