diff --git a/simgear/canvas/elements/CanvasImage.cxx b/simgear/canvas/elements/CanvasImage.cxx index 3964653b..09e09edf 100644 --- a/simgear/canvas/elements/CanvasImage.cxx +++ b/simgear/canvas/elements/CanvasImage.cxx @@ -866,6 +866,11 @@ SGRect intersectRect(const SGRect& a, const SGRect& b) void Image::fillRect(const SGRect& rect, const osg::Vec4& color) { osg::ref_ptr image = _texture->getImage(); + if (!image) { + allocateImage(); + image = _texture->getImage(); + } + const auto format = image->getInternalTextureFormat(); auto clippedRect = intersectRect(rect, SGRect(0, 0, image->s(), image->t())); @@ -881,6 +886,7 @@ SGRect intersectRect(const SGRect& a, const SGRect& b) switch (format) { case GL_RGBA8: + case GL_RGBA: rowByteSize = pixelWidth * 4; rowData = static_cast(alloca(rowByteSize)); @@ -892,6 +898,7 @@ SGRect intersectRect(const SGRect& a, const SGRect& b) break; case GL_RGB8: + case GL_RGB: rowByteSize = pixelWidth * 3; rowData = static_cast(alloca(rowByteSize)); pixel = color.asABGR(); @@ -908,8 +915,8 @@ SGRect intersectRect(const SGRect& a, const SGRect& b) GLubyte* imageData = image->data(clippedRect.l(), row); memcpy(imageData, rowData, rowByteSize); } - - setImage(image); + + image->dirty(); } void Image::setPixel(int x, int y, const std::string& c) @@ -924,12 +931,30 @@ SGRect intersectRect(const SGRect& a, const SGRect& b) void Image::setPixel(int x, int y, const osg::Vec4& color) { osg::ref_ptr image = _texture->getImage(); + if (!image) { + allocateImage(); + image = _texture->getImage(); + } + image->setColor(color, x, y); - - // is this needed, or does OSG track modifications to the data - // automatically? - setImage(image); } + void Image::allocateImage() + { + osg::Image* image = new osg::Image; + // default to RGBA + image->allocateImage(_node->getIntValue("size[0]"), _node->getIntValue("size[1]"), 1, GL_RGBA, GL_UNSIGNED_BYTE); + image->setInternalTextureFormat(GL_RGBA); + _texture->setImage(image); + } + + +osg::ref_ptr Image::getImage() const +{ + if (!_texture) + return {}; + return _texture->getImage(); +} + } // namespace canvas } // namespace simgear diff --git a/simgear/canvas/elements/CanvasImage.hxx b/simgear/canvas/elements/CanvasImage.hxx index dedcaf01..a1aeed2f 100644 --- a/simgear/canvas/elements/CanvasImage.hxx +++ b/simgear/canvas/elements/CanvasImage.hxx @@ -115,6 +115,7 @@ namespace canvas void setPixel(int x, int y, const osg::Vec4& color); + osg::ref_ptr getImage() const; // void setRow(int row, int offset, ) protected: @@ -141,6 +142,8 @@ namespace canvas HTTP::Request& request, const std::string& type ); + void allocateImage(); + osg::ref_ptr _texture; // TODO optionally forward events to canvas CanvasWeakPtr _src_canvas;