| 1 |
/* +------------------------------------------------------------------------+ |
| 2 |
| Mobile Robot Programming Toolkit (MRPT) | |
| 3 |
| https://www.mrpt.org/ | |
| 4 |
| | |
| 5 |
| Copyright (c) 2005-2026, Individual contributors, see AUTHORS file | |
| 6 |
| See: https://www.mrpt.org/Authors - All rights reserved. | |
| 7 |
| Released under BSD License. See: https://www.mrpt.org/License | |
| 8 |
+------------------------------------------------------------------------+ */ |
| 9 |
|
| 10 |
#include "opengl-precomp.h" // Precompiled header |
| 11 |
// |
| 12 |
#include <mrpt/core/backtrace.h> |
| 13 |
#include <mrpt/core/get_env.h> |
| 14 |
#include <mrpt/opengl/FrameBuffer.h> |
| 15 |
#include <mrpt/opengl/opengl_api.h> |
| 16 |
|
| 17 |
using namespace mrpt::opengl; |
| 18 |
|
| 19 |
#if MRPT_HAS_OPENGL_GLUT || MRPT_HAS_EGL |
| 20 |
namespace |
| 21 |
{ |
| 22 |
bool isExtensionSupported(const std::string& extension) |
| 23 |
{ |
| 24 |
MRPT_START |
| 25 |
for (int index = 0;; index++) |
| 26 |
{ |
| 27 |
const auto extName = glGetStringi(GL_EXTENSIONS, index); |
| 28 |
if (!extName) break; |
| 29 |
const auto sExt = std::string(reinterpret_cast<const char*>(extName)); |
| 30 |
// std::cout << sExt << "\n"; |
| 31 |
if (sExt == extension) return true; |
| 32 |
} |
| 33 |
MRPT_END |
| 34 |
return false; |
| 35 |
} |
| 36 |
} // namespace |
| 37 |
#endif |
| 38 |
|
| 39 |
void FrameBuffer::RAII_Impl::create(unsigned int width, unsigned int height, int nSamples) |
| 40 |
{ |
| 41 |
#if MRPT_HAS_OPENGL_GLUT || MRPT_HAS_EGL |
| 42 |
|
| 43 |
if (!isExtensionSupported("GL_EXT_framebuffer_object")) |
| 44 |
THROW_EXCEPTION( |
| 45 |
"Framebuffer Object extension unsupported " |
| 46 |
"(GL_EXT_framebuffer_object)"); |
| 47 |
|
| 48 |
auto& _ = m_state.get(); |
| 49 |
|
| 50 |
_.m_width = width; |
| 51 |
_.m_height = height; |
| 52 |
_.m_Samples = nSamples; |
| 53 |
_.m_isDepthMap = false; |
| 54 |
|
| 55 |
const auto oldFBs = CurrentBinding(); |
| 56 |
|
| 57 |
// Render buffer: RGB |
| 58 |
glGenRenderbuffers(1, &_.m_Color); |
| 59 |
CHECK_OPENGL_ERROR_IN_DEBUG(); |
| 60 |
glBindRenderbuffer(GL_RENDERBUFFER, _.m_Color); |
| 61 |
CHECK_OPENGL_ERROR_IN_DEBUG(); |
| 62 |
|
| 63 |
if (nSamples <= 1) |
| 64 |
{ |
| 65 |
glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, _.m_width, _.m_height); |
| 66 |
CHECK_OPENGL_ERROR_IN_DEBUG(); |
| 67 |
} |
| 68 |
else |
| 69 |
{ |
| 70 |
glRenderbufferStorageMultisample(GL_RENDERBUFFER, nSamples, GL_RGBA8, _.m_width, _.m_height); |
| 71 |
CHECK_OPENGL_ERROR_IN_DEBUG(); |
| 72 |
} |
| 73 |
|
| 74 |
// Render buffer: DEPTH |
| 75 |
glGenRenderbuffers(1, &_.m_Depth); |
| 76 |
CHECK_OPENGL_ERROR_IN_DEBUG(); |
| 77 |
glBindRenderbuffer(GL_RENDERBUFFER, _.m_Depth); |
| 78 |
CHECK_OPENGL_ERROR_IN_DEBUG(); |
| 79 |
|
| 80 |
if (nSamples <= 1) |
| 81 |
{ |
| 82 |
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, _.m_width, _.m_height); |
| 83 |
CHECK_OPENGL_ERROR_IN_DEBUG(); |
| 84 |
} |
| 85 |
else |
| 86 |
{ |
| 87 |
glRenderbufferStorageMultisample( |
| 88 |
GL_RENDERBUFFER, nSamples, GL_DEPTH_COMPONENT24, _.m_width, _.m_height); |
| 89 |
CHECK_OPENGL_ERROR_IN_DEBUG(); |
| 90 |
} |
| 91 |
|
| 92 |
// Frame buffer: |
| 93 |
glGenFramebuffers(1, &_.m_Framebuffer); |
| 94 |
CHECK_OPENGL_ERROR_IN_DEBUG(); |
| 95 |
|
| 96 |
// bind the framebuffer, fbo, so operations will now occur on it |
| 97 |
glBindFramebuffer(GL_FRAMEBUFFER, _.m_Framebuffer); |
| 98 |
CHECK_OPENGL_ERROR_IN_DEBUG(); |
| 99 |
|
| 100 |
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, _.m_Color); |
| 101 |
CHECK_OPENGL_ERROR_IN_DEBUG(); |
| 102 |
|
| 103 |
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, _.m_Depth); |
| 104 |
CHECK_OPENGL_ERROR_IN_DEBUG(); |
| 105 |
|
| 106 |
#if 0 |
| 107 |
glFramebufferRenderbuffer( |
| 108 |
GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, _.m_Depth); |
| 109 |
CHECK_OPENGL_ERROR_IN_DEBUG(); |
| 110 |
#endif |
| 111 |
|
| 112 |
glDrawBuffer(GL_COLOR_ATTACHMENT0); |
| 113 |
CHECK_OPENGL_ERROR_IN_DEBUG(); |
| 114 |
|
| 115 |
glReadBuffer(GL_COLOR_ATTACHMENT0); |
| 116 |
CHECK_OPENGL_ERROR(); |
| 117 |
|
| 118 |
GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER); |
| 119 |
if (status != GL_FRAMEBUFFER_COMPLETE) THROW_EXCEPTION("Could not create framebuffer object."); |
| 120 |
|
| 121 |
_.m_created = true; |
| 122 |
|
| 123 |
// Restore: |
| 124 |
Bind(oldFBs); |
| 125 |
#else |
| 126 |
THROW_EXCEPTION("MRPT built without OpenGL support"); |
| 127 |
#endif |
| 128 |
} |
| 129 |
|
| 130 |
void FrameBuffer::RAII_Impl::createDepthMap(unsigned int width, unsigned int height) |
| 131 |
{ |
| 132 |
#if MRPT_HAS_OPENGL_GLUT || MRPT_HAS_EGL |
| 133 |
if (!isExtensionSupported("GL_EXT_framebuffer_object")) |
| 134 |
THROW_EXCEPTION( |
| 135 |
"Framebuffer Object extension unsupported " |
| 136 |
"(GL_EXT_framebuffer_object)"); |
| 137 |
|
| 138 |
auto& _ = m_state.get(); |
| 139 |
|
| 140 |
_.m_isDepthMap = true; |
| 141 |
_.m_width = width; |
| 142 |
_.m_height = height; |
| 143 |
|
| 144 |
const auto oldFBs = CurrentBinding(); |
| 145 |
|
| 146 |
// Depth FBO: |
| 147 |
glGenFramebuffers(1, &_.m_Framebuffer); |
| 148 |
CHECK_OPENGL_ERROR_IN_DEBUG(); |
| 149 |
|
| 150 |
// Create depth texture: |
| 151 |
glGenTextures(1, &_.m_DepthMapTexture); |
| 152 |
|
| 153 |
glBindTexture(GL_TEXTURE_2D, _.m_DepthMapTexture); |
| 154 |
|
| 155 |
glTexImage2D( |
| 156 |
GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, width, height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, |
| 157 |
nullptr); |
| 158 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 159 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 160 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER); |
| 161 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER); |
| 162 |
|
| 163 |
const float borderColor[4] = {1.0, 1.0, 1.0, 1.0}; |
| 164 |
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor); |
| 165 |
|
| 166 |
// attach depth texture as FBO's depth buffer |
| 167 |
glBindFramebuffer(GL_FRAMEBUFFER, _.m_Framebuffer); |
| 168 |
glFramebufferTexture2D( |
| 169 |
GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, _.m_DepthMapTexture, 0); |
| 170 |
glDrawBuffer(GL_NONE); // dont draw color |
| 171 |
glReadBuffer(GL_NONE); |
| 172 |
|
| 173 |
GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER); |
| 174 |
if (status != GL_FRAMEBUFFER_COMPLETE) THROW_EXCEPTION("Could not create depth map FBO."); |
| 175 |
|
| 176 |
_.m_created = true; |
| 177 |
|
| 178 |
// Restore: |
| 179 |
glBindFramebuffer(GL_FRAMEBUFFER, 0); |
| 180 |
// Restore: |
| 181 |
Bind(oldFBs); |
| 182 |
#else |
| 183 |
THROW_EXCEPTION("MRPT built without OpenGL support"); |
| 184 |
#endif |
| 185 |
} |
| 186 |
|
| 187 |
void FrameBuffer::RAII_Impl::destroy() |
| 188 |
{ |
| 189 |
auto& _ = m_state.get(); |
| 190 |
|
| 191 |
if (!_.m_created) return; |
| 192 |
|
| 193 |
#if MRPT_HAS_OPENGL_GLUT || MRPT_HAS_EGL |
| 194 |
unbind(); |
| 195 |
|
| 196 |
if (_.m_isDepthMap) |
| 197 |
{ |
| 198 |
glDeleteFramebuffers(1, &_.m_Framebuffer); |
| 199 |
CHECK_OPENGL_ERROR_IN_DEBUG(); |
| 200 |
} |
| 201 |
else |
| 202 |
{ |
| 203 |
glDeleteRenderbuffers(1, &_.m_Color); |
| 204 |
CHECK_OPENGL_ERROR_IN_DEBUG(); |
| 205 |
glDeleteRenderbuffers(1, &_.m_Depth); |
| 206 |
CHECK_OPENGL_ERROR_IN_DEBUG(); |
| 207 |
glDeleteFramebuffers(1, &_.m_Framebuffer); |
| 208 |
CHECK_OPENGL_ERROR(); |
| 209 |
} |
| 210 |
#endif |
| 211 |
_.m_Color = _.m_Depth = 0; |
| 212 |
_.m_Framebuffer = _.m_DepthMapTexture = 0; |
| 213 |
_.m_created = false; |
| 214 |
} |
| 215 |
|
| 216 |
FrameBufferBinding FrameBuffer::RAII_Impl::bind() |
| 217 |
{ |
| 218 |
#if MRPT_HAS_OPENGL_GLUT || MRPT_HAS_EGL |
| 219 |
const FrameBufferBinding ids = CurrentBinding(); |
| 220 |
|
| 221 |
auto& _ = m_state.get(); |
| 222 |
|
| 223 |
glBindFramebuffer(GL_FRAMEBUFFER, _.m_Framebuffer); |
| 224 |
CHECK_OPENGL_ERROR_IN_DEBUG(); |
| 225 |
if (_.m_Samples > 1) |
| 226 |
{ |
| 227 |
glEnable(GL_MULTISAMPLE); |
| 228 |
CHECK_OPENGL_ERROR_IN_DEBUG(); |
| 229 |
} |
| 230 |
|
| 231 |
return ids; |
| 232 |
#else |
| 233 |
THROW_EXCEPTION("MRPT built without OpenGL support"); |
| 234 |
#endif |
| 235 |
} |
| 236 |
|
| 237 |
void FrameBuffer::RAII_Impl::unbind() |
| 238 |
{ |
| 239 |
#if MRPT_HAS_OPENGL_GLUT || MRPT_HAS_EGL |
| 240 |
auto& _ = m_state.get(); |
| 241 |
if (_.m_Samples > 1) glDisable(GL_MULTISAMPLE); |
| 242 |
|
| 243 |
glBindFramebuffer(GL_FRAMEBUFFER, 0); |
| 244 |
CHECK_OPENGL_ERROR(); |
| 245 |
#endif |
| 246 |
} |
| 247 |
|
| 248 |
void FrameBuffer::blit() |
| 249 |
{ |
| 250 |
#if MRPT_HAS_OPENGL_GLUT || MRPT_HAS_EGL |
| 251 |
auto& _ = m_impl.m_state.get(); |
| 252 |
|
| 253 |
glBindFramebuffer(GL_READ_FRAMEBUFFER, _.m_Framebuffer); |
| 254 |
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); |
| 255 |
glDrawBuffer(GL_BACK); |
| 256 |
|
| 257 |
glBlitFramebuffer( |
| 258 |
0, 0, _.m_width, _.m_height, 0, 0, _.m_width, _.m_height, |
| 259 |
GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT, GL_NEAREST); |
| 260 |
|
| 261 |
glBindFramebuffer(GL_FRAMEBUFFER, 0); |
| 262 |
#endif |
| 263 |
} |
| 264 |
|
| 265 |
void FrameBuffer::Bind(const FrameBufferBinding& ids) |
| 266 |
{ |
| 267 |
#if MRPT_HAS_OPENGL_GLUT || MRPT_HAS_EGL |
| 268 |
glBindFramebuffer(GL_READ_FRAMEBUFFER, ids.readFbId); |
| 269 |
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, ids.drawFbId); |
| 270 |
#endif |
| 271 |
} |
| 272 |
|
| 273 |
void FrameBuffer::Unbind() |
| 274 |
{ |
| 275 |
#if MRPT_HAS_OPENGL_GLUT || MRPT_HAS_EGL |
| 276 |
glBindFramebuffer(GL_FRAMEBUFFER, 0); |
| 277 |
#endif |
| 278 |
} |
| 279 |
|
| 280 |
FrameBufferBinding FrameBuffer::CurrentBinding() |
| 281 |
{ |
| 282 |
#if MRPT_HAS_OPENGL_GLUT || MRPT_HAS_EGL |
| 283 |
GLint drawFboId = 0, readFboId = 0; |
| 284 |
glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &drawFboId); |
| 285 |
glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &readFboId); |
| 286 |
|
| 287 |
FrameBufferBinding ids; |
| 288 |
ids.drawFbId = drawFboId; |
| 289 |
ids.readFbId = readFboId; |
| 290 |
return ids; |
| 291 |
#else |
| 292 |
THROW_EXCEPTION("MRPT built without OpenGL support"); |
| 293 |
#endif |
| 294 |
} |