c++ - OpenSceneGraph float 图像

标签 c++ image floating-point openscenegraph

使用 C++ 和 OSG 我试图将 float 纹理上传到我的着色器,但不知何故它似乎不起作用。最后,我发布了我的部分代码。主要问题是如何使用 float 组中的数据创建 osg::Image 对象。在 OpenGL 中,所需的代码将是

glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE32F_ARB, width, height, 0, 
             GL_LUMINANCE, GL_FLOAT, data);

但在这种情况下我必须使用 OSG。

使用时代码运行良好

Image* image = osgDB::readImageFile("someImage.jpg");  

代替

image = new Image;  

但我需要上传生成的 float 数据。也无法切换到无符号字符数组,因为我需要着色器代码中的 GL_LUMINANCE32F_ARB 数据范围。

我希望有人能在这里帮助我,因为谷歌无法帮助我(用谷歌搜索例如:osg float image)。所以这是我的代码。

using namespace std;
using namespace osg;
//...
float* data = new float[width*height];
fill_n(data, size, 1.0); // << I actually do this for testing purposes
Texture2D* texture = new Texture2D;
Image* image = new Image;
osg::State* state = new osg::State;
Uniform* uniform = new Uniform(Uniform::SAMPLER_2D, "texUniform");
texture->setInternalFormat(GL_LUMINANCE32F_ARB);
texture->setDataVariance(osg::Object::DYNAMIC);
texture->setFilter(osg::Texture2D::MIN_FILTER, osg::Texture2D::LINEAR);
texture->setFilter(osg::Texture2D::MAG_FILTER, osg::Texture2D::LINEAR);
texture->setWrap(osg::Texture2D::WRAP_T, osg::Texture2D::CLAMP_TO_EDGE);
texture->setWrap(osg::Texture2D::WRAP_S, osg::Texture2D::CLAMP_TO_EDGE);
if (data == NULL)
  cout << "texdata null" << endl; // << this is not printed
image->setImage(width, height, 1, GL_LUMINANCE32F_ARB,
                GL_LUMINANCE, GL_FLOAT,
                (unsigned char*)data, osg::Image::USE_NEW_DELETE);
if (image->getDataPointer() == NULL)
  cout << "datapointernull" << endl; // << this is printed
if (!image->valid())
  exit(1); //  << here the code exits (hard exit just for testing purposes)
osgDB::writeImageFile(*image, "blah.png");
texture->setInternalFormat(GL_LUMINANCE32F_ARB);
texture->setImage(image);
camera->getOrCreateStateSet()->setTextureAttributeAndModes(4, texture);
state->setActiveTextureUnit(4);
texture->apply(*state);
uniform->set(4);
addProgrammUniform(uniform);

我在网上找到了另一种方法,让osg::Image创建数据然后填充它。但不知何故,这也行不通。我在新的 XYZ 之后插入了这个;行。

image->setInternalTextureFormat(GL_LUMINANCE32F_ARB);
image->allocateImage(width,height,1,GL_LUMINANCE,GL_FLOAT);
if (image->data() == NULL)
  cout << "null here?!" << endl; // << this is printed.

最佳答案

我使用以下(简化的)代码来创建和设置浮点纹理:

// Create texture and image
osg::Texture* texture = new osg::Texture2D;
osg::Image* image = new osg::Image();
image->allocateImage(size, size, 1, GL_LUMINANCE, GL_FLOAT);
texture->setInternalFormat(GL_LUMINANCE32F_ARB);
texture->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR);
texture->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR);
texture->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
texture->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
texture->setImage(image);

// Set texture to node
osg::StateSet* stateSet = node->getOrCreateStateSet();
stateSet->setTextureAttributeAndModes(TEXTURE_UNIT_NUMBER, texture);

// Set data
float* data = reinterpret_cast<float*>(image->data());
/* ...data processing... */
image->dirty();

您可能想要更改一些参数,但这应该给您一个开始。我相信在您的情况下,TEXTURE_UNIT_NUMBER 应该设置为 4。

关于c++ - OpenSceneGraph float 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11735418/

相关文章:

c++ - 解析 wavefront obj 文件格式

c++ - 为什么这个乘法模板代码会导致溢出?

java - 使图像适合矩形

floating-point - 测试两个 float 是否为零,无需多次测试

c++ - C/C++ 中登录客户端软件的标准方法是什么?

c++ - 如果类型(不)相等,C++ 是否可以有条件地编译代码?

android - iText,将图像添加到 pdf 时出现 FileNotFoundException

html - 我想添加背景视频并将图像放在横幅中

java - IBM-IEEE double 浮点字节转换

python - Python 中的字符串格式 : can I use %s for all types?