I want to leverage OpenGL to sample a discrete scalar map. The scalar map is given on per-triangle basis, i.e. one scalar per triangle.
First, I draw each triangle with flat shading and a color value corresponding to the scalar map. Then, I read the sampled scalar map from the framebuffer using glReadPixels(...).
My problem is as follows:
For some reason I can only recover color values up to 1e-2! I have no clue why this precision is so bad. I'm writing and reading floats from the framebuffer.
Does anyone know why I can recover only such a bad floating point precision from the framebuffer?
Here is the important code snippet, where I simply use a constant color value colorValue for each triangle. If I set colorValue e.g. to 1e-4, I can only recover zeros from the framebuffer.
// Set viewport to desired map size
glViewport(0,0,imageWidth, imageHeight);
glClearColor(0.,0.,0.,1.); // Black background
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDisable(GL_LIGHTING);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-1,1.,-1,1);
glMatrixMode(GL_MODELVIEW);
// Draw scalar map
glBegin(GL_TRIANGLES);
for (unsigned int f=0; f<F.rows(); ++f)
{
val = colorVal;
glColor3f(val, val, val);
glVertex3f(...);
glVertex3f(...);
glVertex3f(...);
}
glEnd();
glFlush();
glFinish();
GLfloat* fbuffer = new GLfloat[imageWidth*imageHeight];
glReadPixels(0,0,imageWidth,imageHeight,GL_RED,GL_FLOAT,fbuffer);