I am trying to voxelize polygon meshes using a technique from Real-Time Volume Graphics p 319. The basis for this is using a FBO to render a bounding volume containing the mesh into a 3D texture. I’m having various issues, but before I start asking questions about those, I need to understand what is happening to my texture.
So that leads me to the use of glGetTexImage (and not glReadPixels as it give me FBO data, not texture data). I am able to render to a texture (though currently it is not my desired result), so I know that the FBO -> 3D texture works under certain conditions (these conditions being related to the issues alluded to above). But what I don’t understand is how to actually print the texel values for testing purposes. Say I want to print the 16 layers (slices) of a 16 x 16 x 16 3D texture, where each of the dimensions is referred to as texX, texY and texZ, respectively. I wrote the following function starting with sample code from allegro.com. The problem is, depending on where I call this function, it may print out the value 205 for every image[n] element, despite the contents of the 3D texture being a background cleared to transparent black and the object in the scene being opaque white. Or it may print out values that don’t seem to make any sense (RGBA: 238 254 238 254) when called before sampling the image to a stack of quads.
void printPixelsInSlice(int slice){
GLubyte* image;
image = new GLubyte[texX * texY * texZ * 4];
glGetTexImage(GL_TEXTURE_3D, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
cout << "Reading texels" << endl;
for(int i = 0; i < texY; i++){
for(int j = 0; j < texX; j++){
int start = ((texX * texY * slice) + (i * texX) + j) * 4;
cout << "Texel at " << i << " " << j << " " << slice << " has color " << (int)image[start] << " " << (int)image[start + 1] << " " << (int)image[start + 2] << " " << (int)image[start + 3] << endl;
}
}
delete[] image;
}
Before the call to this function, TEXTURE0 is active, and it is the only texture being used.
So the primrary question is, when and how is glGetTexImage used?
I have tried calling it when the FBO is rendering into one of its slices, but that’s when I get values of 205 despite the 3D texture actually having modified content. Note: rendering to texture is done in front to back direction.
void renderToTex(){
glGenFramebuffers(1,&framebufferObject);
glBindTexture(GL_TEXTURE_3D, texture);
glBindFramebuffer(GL_FRAMEBUFFER, framebufferObject);
glActiveTexture(GL_TEXTURE0);
for(int z = 0; z < texZ; z++){
glFramebufferTextureLayer(GL_FRAMEBUFFER,
GL_COLOR_ATTACHMENT0,
texture,
0,
z);
//Check frame buffer status
cout << endl << "FRAMEBUFFER STATUS: ";
checkFramebufferStatus();
printFramebufferInfo();
//Render into texture
renderIntoSlice(z);
glActiveTexture(GL_TEXTURE0);
//Print texel values by slice
printPixelsInSlice(z); //<---- function call here
// prints '205' for every component of RGBA
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
I have also tried calling it before I map the 3D texture to a stack of quads. In this case, when rendering from back to front (and printing in such a manner), the first few slices have bizarre values (RGBA: 238 254 238 254) . Then eventually I get values of 0 0 0 0 and 255 255 255 255, which is what I expect.
void drawSliceStack(){
float dzPos = -1.0; //Z coordinate in world space
float dzStep = 1.0 / texZ; // layer of texture space
float texStep = 1.0 / (float) texZ; //change in texture depth
float dzTex = 1.0 - texStep;//(texStep / 2.0 );//Start in the middle of a texel
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_3D, texture);
//Render textured quads
for(int slice = 0; slice < texZ; slice++){
printPixelsInSlice(texZ - slice); //function call here, gets better but strange results
glBegin(GL_QUADS);
glTexCoord3f(0.0, 0.0, dzTex);glVertex3f(0.0, 0.0, dzPos);
glTexCoord3f(1.0, 0.0, dzTex);glVertex3f(1.0, 0.0, dzPos);
glTexCoord3f(1.0, 1.0, dzTex);glVertex3f(1.0, 1.0, dzPos);
glTexCoord3f(0.0, 1.0, dzTex);glVertex3f(0.0, 1.0, dzPos);
glEnd();
glFlush();
dzPos += dzStep;
dzTex -= texStep;
}
}
Any help would be greatly appreciated!