再谈glGenTextures

https://learnopengl.com/Advanced-OpenGL/Framebuffers

texture attachments
when attaching a texture to a framebuffer, all rendering commands will write to the texture
as if it was a normal color/depth or stencil buffer.

the advantage of using textures is that the result of all rendering operations will be stored
as a texture image
that we can then use in our shaders.

创建纹理对象——》绑定纹理对象——》分配内存空间——》设置图片格式——》绑定到帧对象

unsigned int texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
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);  
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0); 

the main differences here is that we set the dimensions equal to the screen size (这并不是必须的)
and we pass NULL as the texture’s data parameter.
for this texture, we are only allocating memory and not actually filling it.
filling the texture will happen as soon as we render to the framebuffer.

also note that we do not care about any of the wrapping methods or mipmapping
since we will not be needing those in most cases.

补充:
if u want to render your whole screen to a texture of a smaller or larger size
u need to call glViewport again (before rendering to your framebuffer) with the new dimensions of your texture,
otherwise only a small part of the texture or screen would be drawn onto the texture.

The glFrameBufferTexture2D has the following parameters:

  1. target: the framebuffer type we’re targeting (draw, read or both).
  2. attachment: the type of attachment we’re going to attach. Right now we’re attaching a color attachment. Note that the 0 at the end suggests we can attach more than 1 color attachment. We’ll get to that in a later tutorial.
  3. textarget: the type of the texture you want to attach.
  4. texture: the actual texture to attach.
  5. level: the mipmap level. We keep this at 0.

aside from the color attachments we can also attach a depth and a stencil texture to the framebuffer object.
to attach a depth attachment we specify the attachment type as GL_DEPTH_ATTACHMENT.
note that the texture’s format and internalformat type should then become GL_DEPTH_COMPONENT to reflect the depth buffer’s storage format.
to attach a stencil buffer you use GL_STENCIL_ATTACHMENT as the second argument and specify the texture’s formats as GL_STENCIL_INDEX.

It is also possible to attach both a depth buffer and a stencil buffer as a single texture. Each 32 bit value of the texture then consists for 24 bits of depth information and 8 bits of stencil information. To attach a depth and stencil buffer as one texture we use the GL_DEPTH_STENCIL_ATTACHMENT type and configure the texture’s formats to contain combined depth and stencil values. An example of attaching a depth and stencil buffer as one texture to the framebuffer is given below:

glTexImage2D(
  GL_TEXTURE_2D, 0, GL_DEPTH24_STENCIL8, 800, 600, 0, 
  GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, NULL
);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, texture, 0); 
发布了610 篇原创文章 · 获赞 96 · 访问量 33万+

猜你喜欢

转载自blog.csdn.net/wodownload2/article/details/104335451