CanvasImage: fillRect / setPixel tweaks

Fix image dirtying and allocation without an underlying fike
This commit is contained in:
James Turner 2020-03-11 17:12:42 +00:00
parent 3e4d528859
commit f350bd31bd
2 changed files with 34 additions and 6 deletions

View File

@ -866,6 +866,11 @@ SGRect<int> intersectRect(const SGRect<int>& a, const SGRect<int>& b)
void Image::fillRect(const SGRect<int>& rect, const osg::Vec4& color)
{
osg::ref_ptr<osg::Image> image = _texture->getImage();
if (!image) {
allocateImage();
image = _texture->getImage();
}
const auto format = image->getInternalTextureFormat();
auto clippedRect = intersectRect(rect, SGRect<int>(0, 0, image->s(), image->t()));
@ -881,6 +886,7 @@ SGRect<int> intersectRect(const SGRect<int>& a, const SGRect<int>& b)
switch (format) {
case GL_RGBA8:
case GL_RGBA:
rowByteSize = pixelWidth * 4;
rowData = static_cast<GLubyte*>(alloca(rowByteSize));
@ -892,6 +898,7 @@ SGRect<int> intersectRect(const SGRect<int>& a, const SGRect<int>& b)
break;
case GL_RGB8:
case GL_RGB:
rowByteSize = pixelWidth * 3;
rowData = static_cast<GLubyte*>(alloca(rowByteSize));
pixel = color.asABGR();
@ -908,8 +915,8 @@ SGRect<int> intersectRect(const SGRect<int>& a, const SGRect<int>& 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<int> intersectRect(const SGRect<int>& a, const SGRect<int>& b)
void Image::setPixel(int x, int y, const osg::Vec4& color)
{
osg::ref_ptr<osg::Image> 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<osg::Image> Image::getImage() const
{
if (!_texture)
return {};
return _texture->getImage();
}
} // namespace canvas
} // namespace simgear

View File

@ -115,6 +115,7 @@ namespace canvas
void setPixel(int x, int y, const osg::Vec4& color);
osg::ref_ptr<osg::Image> 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<osg::Texture2D> _texture;
// TODO optionally forward events to canvas
CanvasWeakPtr _src_canvas;