32 #define TEXTUREOBJ_USE_MEMPOOL 35 #ifdef TEXTUREOBJ_USE_MEMPOOL 39 struct CTexturedObject_MemPoolParams
44 inline bool isSuitable(
const CTexturedObject_MemPoolParams& req)
const 46 return len == req.len;
51 vector<unsigned char>
data;
68 m_textureImageAlpha = imgAlpha;
70 m_enableTransparency =
true;
89 m_enableTransparency =
false;
106 m_textureImage = std::move(
img);
107 m_textureImageAlpha = std::move(imgAlpha);
109 m_enableTransparency =
true;
126 m_textureImage = std::move(
img);
128 m_enableTransparency =
false;
137 #ifdef TEXTUREOBJ_USE_MEMPOOL 141 CTexturedObject_MemPoolParams mem_params;
142 mem_params.len =
len;
155 void* ptr = &
data[0];
157 return reinterpret_cast<unsigned char*
>(
158 std::align(16, 1 , ptr, space));
166 #if MRPT_HAS_OPENGL_GLUT 167 unsigned char* dataAligned =
nullptr;
168 vector<unsigned char>
data;
170 #ifdef TEXTUREOBJ_PROFILE_MEM_ALLOC 176 if (m_texture_is_loaded)
187 m_glTextureName = getNewTextureNumber();
221 while (m_textureImage.getHeight() > (
unsigned int)texSize ||
222 m_textureImage.getWidth() > (
unsigned int)texSize)
226 m_textureImageAlpha =
230 const int width = m_textureImage.getWidth();
231 const int height = m_textureImage.getHeight();
233 #ifdef TEXTUREOBJ_PROFILE_MEM_ALLOC 236 "opengl_texture: load %ix%i %s %stransp",
width,
height,
237 m_textureImage.isColor() ?
"RGB" :
"BW",
238 m_enableTransparency ?
"" :
"no ");
239 tim.
enter(sSec.c_str());
247 m_pad_x_right = (r_width -
width);
248 m_pad_y_bottom = (r_height -
height);
250 if (m_enableTransparency)
252 ASSERT_(!m_textureImageAlpha.isColor());
254 m_textureImageAlpha.getWidth() == m_textureImage.getWidth());
256 m_textureImageAlpha.getHeight() == m_textureImage.getHeight());
259 if (m_textureImage.isColor())
262 if (m_enableTransparency)
266 #ifdef TEXTUREOBJ_PROFILE_MEM_ALLOC 268 "opengl_texture_alloc %ix%i (color,trans)",
width,
height);
269 tim.
enter(sSec.c_str());
274 #ifdef TEXTUREOBJ_PROFILE_MEM_ALLOC 275 tim.
leave(sSec.c_str());
280 unsigned char* ptrSrcCol = m_textureImage(0,
y, 0);
281 unsigned char* ptrSrcAlfa = m_textureImageAlpha(0,
y);
282 unsigned char* ptr = dataAligned +
y *
width * 4;
286 *ptr++ = *ptrSrcCol++;
287 *ptr++ = *ptrSrcCol++;
288 *ptr++ = *ptrSrcCol++;
289 *ptr++ = *ptrSrcAlfa++;
296 const bool is_RGB_order =
297 (m_textureImage.getChannelsOrder() ==
std::string(
"RGB"));
305 height, 0 , img_format, img_type, dataAligned);
320 const int nBytesPerPixel = m_textureImage.isColor() ? 3 : 1;
322 const bool is_RGB_order =
323 (m_textureImage.getChannelsOrder() ==
std::string(
"RGB"));
324 const GLenum img_format = nBytesPerPixel == 3
332 m_textureImage.getRowStride() / nBytesPerPixel);
335 height, 0 , img_format, img_type,
336 m_textureImage.ptrLine<
uint8_t>(0));
348 if (m_enableTransparency)
350 #ifdef TEXTUREOBJ_PROFILE_MEM_ALLOC 352 "opengl_texture_alloc %ix%i (gray,transp)",
width,
height);
353 tim.
enter(sSec.c_str());
359 #ifdef TEXTUREOBJ_PROFILE_MEM_ALLOC 360 tim.
leave(sSec.c_str());
365 unsigned char* ptrSrcCol = m_textureImage(0,
y);
366 unsigned char* ptrSrcAlfa = m_textureImageAlpha(0,
y);
367 unsigned char* ptr = dataAligned +
y *
width * 2;
370 *ptr++ = *ptrSrcCol++;
371 *ptr++ = *ptrSrcAlfa++;
384 height, 0 , img_format, img_type, dataAligned);
405 height, 0 , img_format, img_type,
406 m_textureImage(0, 0));
417 m_texture_is_loaded =
true;
419 #ifdef TEXTUREOBJ_PROFILE_MEM_ALLOC 422 "opengl_texture: load %ix%i %s %stransp",
width,
height,
423 m_textureImage.isColor() ?
"RGB" :
"BW",
424 m_enableTransparency ?
"" :
"no ");
425 tim.
leave(sSec.c_str());
429 #ifdef TEXTUREOBJ_USE_MEMPOOL 436 CTexturedObject_MemPoolParams mem_params;
437 mem_params.len =
data.size();
440 data.swap(mem_block->data);
450 format(
"m_glTextureName=%i\n%s", m_glTextureName, e.what()));
468 if (m_texture_is_loaded)
470 m_texture_is_loaded =
false;
471 releaseTextureName(m_glTextureName);
482 out << m_enableTransparency;
483 out << m_textureImage;
484 if (m_enableTransparency) out << m_textureImageAlpha;
489 #if MRPT_HAS_OPENGL_GLUT 492 std::cerr <<
"render_pre: Error" << std::endl;
493 render_texturedobj();
495 std::cerr <<
"render_texturedobj: Error" << std::endl;
498 std::cerr <<
"render_post: Error" << std::endl;
504 #if MRPT_HAS_OPENGL_GLUT 509 if (m_enableTransparency || m_color.A != 255)
523 loadTextureInOpenGL();
530 #if MRPT_HAS_OPENGL_GLUT 533 if (m_enableTransparency || m_color.A != 255)
563 in >> m_enableTransparency;
564 in >> m_textureImage;
565 if (m_enableTransparency)
567 in >> m_textureImageAlpha;
568 assignImage(m_textureImage, m_textureImageAlpha);
572 assignImage(m_textureImage);
virtual void render_post() const
void notifyChange() const
Must be called to notify that the object has changed (so, the display list must be updated) ...
#define GL_TEXTURE_WRAP_T
A base class for all OpenGL objects with loadable textures.
void render_dl() const override
Derived classes must implement this method to the render the object.
GLAPI void GLAPIENTRY glEnable(GLenum cap)
~CTexturedObject() override
#define THROW_EXCEPTION(msg)
void dump_to_pool(const DATA_PARAMS ¶ms, POOLABLE_DATA *block)
Saves the passed data block (characterized by params) to the pool.
#define GL_ONE_MINUS_SRC_ALPHA
POOLABLE_DATA * request_memory(const DATA_PARAMS ¶ms)
Request a block of data which fulfils the size requirements stated in params.
void assignImage_fast(mrpt::img::CImage &img, mrpt::img::CImage &imgAlpha)
Similar to assignImage, but the passed images will be returned as empty: it avoids making a copy of t...
GLAPI void GLAPIENTRY glBlendFunc(GLenum sfactor, GLenum dfactor)
A renderizable object suitable for rendering with OpenGL's display lists.
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
GLAPI void GLAPIENTRY glTexParameterf(GLenum target, GLenum pname, GLfloat param)
A generic system for versatile memory pooling.
GLAPI void GLAPIENTRY glBindTexture(GLenum target, GLuint texture)
#define ASSERT_(f)
Defines an assertion mechanism.
vector< unsigned char > data
This base provides a set of functions for maths stuff.
IMPLEMENTS_VIRTUAL_SERIALIZABLE(CTexturedObject, CRenderizableDisplayList, mrpt::opengl) struct CTexturedObject_MemPoolParams
virtual void render_pre() const
GLsizei const GLchar ** string
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
unsigned char * reserveDataBuffer(const size_t len, vector< unsigned char > &data)
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
#define GL_UNPACK_ROW_LENGTH
#define GL_TEXTURE_MIN_FILTER
Virtual base class for "archives": classes abstracting I/O streams.
#define GL_UNPACK_ALIGNMENT
A versatile "profiler" that logs the time spent within each pair of calls to enter(X)-leave(X), among other stats.
#define GL_TEXTURE_MAG_FILTER
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
void checkOpenGLError()
Checks glGetError and throws an exception if an error situation is found.
GLAPI void GLAPIENTRY glGetIntegerv(GLenum pname, GLint *params)
GLAPI GLenum GLAPIENTRY glGetError(void)
The namespace for 3D scene representation and rendering.
void readFromStreamTexturedObject(mrpt::serialization::CArchive &in)
#define GL_MAX_TEXTURE_SIZE
#define GL_LUMINANCE_ALPHA
void loadTextureInOpenGL() const
VERY IMPORTANT: If you use a multi-thread application, you MUST call this from the same thread that w...
void assignImage(const mrpt::img::CImage &img, const mrpt::img::CImage &imgAlpha)
Assigns a texture and a transparency image, and enables transparency (If the images are not 2^N x 2^M...
GLenum GLsizei GLsizei height
GLAPI void GLAPIENTRY glDisable(GLenum cap)
GLsizei GLsizei GLenum GLenum const GLvoid * data
GLAPI void GLAPIENTRY glPixelStorei(GLenum pname, GLint param)
void writeToStreamTexturedObject(mrpt::serialization::CArchive &out) const
#define GL_TEXTURE_WRAP_S
void enter(const std::string_view &func_name)
Start of a named section.
double leave(const std::string_view &func_name)
End of a named section.
GLAPI void GLAPIENTRY glTexImage2D(GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid *pixels)
A class for storing images as grayscale or RGB bitmaps.
static CGenericMemoryPool< DATA_PARAMS, POOLABLE_DATA > * getInstance(const size_t max_pool_entries=5)
Construct-on-first-use (~singleton) pattern: Return the unique instance of this class for a given tem...