PDA

View Full Version : Floating point texture retrieval



nospoon
04-11-2010, 06:06 PM
I want to have, in CPU memory, the xyz eye coordinate, and nx,ny,nz normal vector coordinates of all the points visible in my scene.

My approach is to render the scene to an FBO with 2 texture attachments. My vertex shader sets 2 varying variables: the eye coordinate, and the normal. The frag shader just writes these to the two textures. I want the values in floating point, so I'm using the GL_RGB32F_ARB internal format. When I read out my buffer, however, I get 0 where there is no hit, and 1 where there is a hit. How can I get the actual floating poitn values out? Thank you

//Application code:

void Scene::readCoordNormals()
{
//render scene to texture and read it out

GLuint fboid, coordTexBase;
glGenFramebuffersEXT(1, &fboid);
glGenTextures(2, &coordTexBase);
glBindTexture(GL_TEXTURE_2D, coordTexBase);


glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);


glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F_ARB, windWidth, windHeight,
0, GL_RGB, GL_FLOAT, NULL);

glBindTexture(GL_TEXTURE_2D, coordTexBase +1);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F_ARB, windWidth, windHeight,
0, GL_RGB, GL_FLOAT, NULL);

Helpers::getGLErrors("After texImage calls");

glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fboid);

Helpers::getGLErrors("after bindFramebuffer");
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
GL_TEXTURE_2D, coordTexBase, 0);
Helpers::getGLErrors("After first framebufferTex2D");
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT1_EXT,
GL_TEXTURE_2D, coordTexBase +1, 0);
Helpers::getGLErrors("After 2nd framebufferTex2D");
glViewport(0,0, windWidth, windHeight);
glCullFace(GL_BACK);


Helpers::getGLErrors("After cullFace, before drawBufs");
glReadBuffer(GL_NONE);
const GLenum drawbufs[2] = {GL_COLOR_ATTACHMENT0_EXT,
GL_COLOR_ATTACHMENT1_EXT};
glDrawBuffers(2, drawbufs);

glClear(GL_COLOR_BUFFER_BIT);
float eye[3];
float eyedir[3];
view->getEye(eye);
view->getCenter(eyedir);
viewProjSetup(eye, eyedir);

glUseProgram(coordNormalProgram);

drawObjects();

glFlush();

glIsShader(999);//debug

//read values
glBindTexture(GL_TEXTURE_2D, coordTexBase);
objectCoords = new float[3*sizeof(GLfloat)*windWidth*windHeight];
objectNormals = new float[3*sizeof(GLfloat)*windWidth*windHeight];
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGB, GL_FLOAT, objectCoords);

glBindTexture(GL_TEXTURE_2D, coordTexBase +1);
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGB, GL_FLOAT, objectNormals);

for(size_t i = 0; i < 3*windWidth*windHeight; ++i)
std::cout << objectCoords[i];
std::cout << std::endl;

glDeleteTextures(2, &amp;coordTexBase);
glDeleteFramebuffersEXT(1, &amp;fboid);

Helpers::getGLErrors("End of coordNormalSetup");

}


//vertex shadervarying vec3 eyeCoord;
varying vec3 normal;

void main()
{
ecPosition = vec3(gl_ModelViewMatrix * gl_Vertex);
normal = normalize(gl_NormalMatrix *gl_Normal);
gl_Position = ftransform();

}



//frag shader
varying vec3 eyeCoord;
varying vec3 normal;

void main()
{
gl_FragData[0] = eyeCoord;
gl_FragData[1] = normal;

}

nospoon
04-12-2010, 09:21 PM
Fixed the "obvious" errors with my failed texture generation, updated code here:

http://pastebin.com/Wg7TaxcX

Thanks


SOLVED Turns out I forgot to compile my shaders...