2012-07-31 24 views
3

使用C++和OSG我試圖上傳一個浮動紋理到我的着色器,但不知怎的,它似乎並沒有工作。最後,我發佈了一部分代碼。主要問題是如何使用float數組中的數據創建osg :: Image對象。在OpenGL所需的代碼將是OpenSceneGraph float圖片

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; 

但我需要上傳生成浮點數據運行良好。由於我需要着色器代碼中的GL_LUMINANCE32F_ARB數據範圍,因此也無法切換到無符號字符數組。

我希望有人能幫助我,因爲Google無法幫助我(例如: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 ::圖像創建數據事後填充它。但不知何故,這也不起作用。我在新的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. 
+1

當然,這應該是'圖像圖像;'或'圖像*圖像=新圖像'。這同樣適用於「國家」,「制服」等。你的代碼是否可以編譯? – Bart 2012-07-31 09:11:41

+0

哎呀,是的,你是對的。代碼是從我的課程中獲取的,其中紋理,圖像,狀態和制服在頭文件中定義爲指針。在添加片段中的類型時,我只是忘了*。我會糾正這一點。謝謝 – Nero 2012-07-31 10:16:06

+0

我增加了一種方法來解決問題,但這也行不通。雖然allocateImage(。)被調用,但數據指針爲NULL。 – Nero 2012-08-01 10:43:21

回答

0

我用下面(簡化)代碼來創建和設置浮點紋理:

// 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.