Hello,

I am trying to encode some position information in a Texture2D for consumption 
by a vertex shader. I found a pretty good example in the OSG examples directory 
of something similar, osgtexture1d and I have successfully implemented the 
method used there. For your reference, the relevant lines are below.

== osgtexture1d.cpp ==
osg::Image* image = new osg::Image;
int noPixels = 1024;
image->allocateImage(noPixels,1,1,GL_RGBA,GL_FLOAT);
image->setInternalTextureFormat(GL_RGBA);
osg::Vec4* dataPtr = (osg::Vec4*)image->data();

(A for loop populating the dataPtr follows, it uses the following syntax)
*dataPtr++ = color;
== end ==

I implemented my code as follows and it works.

unsigned int tex_size = 2048;
positionImage->allocateImage( 2, tex_size, 1, GL_RGBA, GL_FLOAT );
positionImage->setInternalTextureFormat(GL_RGBA32F_ARB);
                        
osg::Vec4 *dataPtr = (osg::Vec4*)positionImage->data();
memset(dataPtr, 0, sizeof(osg::Vec4)*2*tex_size);
(a for loop follows, it uses the following syntax, it moves in increments of 
two, basically angles get stored in the second column, positions in the first)
dataPtr[ii]     = positions[ii / 2]; // this is a vec4
dataPtr[ii + 1] = osg::Vec4( sin(angle), cos(angle), 0., 1.);


The problem arises when I want to instance more geometry than a single 1d map 
can reasonably hold, e.g. 10k. I decided to try and use a 1024 x 512 map to 
hold the data, with the capacity being 512*512 entities.
The implementation looks like this:

===== begin =====
const int tex_width  = 1024; // [0] = position, angle [1] = sin, cos, 0.0, 1.0
const int tex_height = 512; 
positionImage->allocateImage( tex_width, tex_height, 1, GL_RGBA, GL_FLOAT );
positionImage->setInternalTextureFormat(GL_RGBA);

unsigned char* dataPtr = (unsigned char*)positionImage->data();
//memset(dataPtr, 0, sizeof(osg::Vec4)*tex_width*tex_height);
int row_c = 0, col_c = 0;

for(unsigned int ii=0; ii<numInstances; ++ii)
{
        dataPtr[row_c * tex_width + col_c + 0].set( 1., 0., 0., 1. );
        dataPtr[row_c * tex_width + col_c                               col_c 
+= 2;

        col_c +=2; // increment column by 2

        if(col_c == tex_width)
        {
                col_c = 0;
                ++row_c;
        }
}
===== end =====
                
If I save that image out to a .png file, what emerges looks like this:

(Ascii art ahoy!)
1
.
.
.
| (random pixels)
|#..#.#.##..#.#.## (etc)
0-----------------....1
# is a blue pixel
. is a clear pixel

What appears to be happening is that the blue pixels line up with the 
components of the vec4 that I set to be nonzero. So I think that it is 
interpreting the floats as entire pixels. Clearly, I must be doing something 
wrong. Does anyone see anything obvious?      


Thank you for your assistance.


_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to