If there are errors in code, terminology, etc. in the article, please correct me.
Preface
-
Recap
- From the previous section, you can already read the value of the color buffer at the mouse position.
- But when reading the value which is not a range of quad it is a strange number
- It's because glClearColor(color.r, color.g, color.b, color.a); is used in the cpp code to fill the buffer with color by default .
- This color is originally a float value, but when converted to int, it is read as a strange number.
-
of this program
Reading a value from a range that is not a quad returns a specific **-1** value
-
How to achieve
Use the new OpenGL function glClearTexImage to fill a buffer with a specific value
-
Implementation details
- Because this glClearTexImage function needs to specify different parameters depending on whether it is set to an int value or a float value.
- Scalability needs to be properly considered , but currently only int values need to be filled, so you can simply write it in and change it later if there is any increase.
key code
-
After filling the buffer with color, you need to fill the second buffer of the framebuffer with a specific value
// 将渲染的东西放到帧缓冲中 m_Framebuffer->Bind(); RenderCommand::SetClearColor({ 0.1f, 0.1f, 0.1f, 1 }); RenderCommand::Clear(); / // 用-1填充帧缓冲的第二个颜色缓冲区 m_Framebuffer->ClearAttachment(1, -1);
-
specific fill function
void OpenGLFramebuffer::ClearAttachment(uint32_t attachmentIndex, int value) { HZ_CORE_ASSERT(attachmentIndex < m_ColorAttachments.size()); auto& spec = m_ColorAttachmentSpecifications[attachmentIndex]; /// /// // 使用glClearTexImage函数 glClearTexImage(m_ColorAttachments[attachmentIndex], 0, Utils::FramebufferTextureFormatToGLenum(spec.TextureFormat), GL_INT, &value); } static GLenum FramebufferTextureFormatToGLenum(FramebufferTextureFormat format) { switch (format) { case FramebufferTextureFormat::RGBA8: return GL_RGBA8; case FramebufferTextureFormat::RED_INTEGER: return GL_RED_INTEGER; } HZ_CORE_ASSERT(false); return 0; }
Effect