Hi, I'm using QGLContext to render a small viewport with some triangles and rectangles in it. I have problems when trying to read the alpha channel back out using glReadPixels. Can this have something to do with using QGLContext?
Please see below:
I initialize like so:
context->create();
context->makeCurrent();
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0f);
QGLContext * context = 0;
QGLFormat format;
context = new QGLContext(format, (QPaintDevice *)glPanel);
context->create();
context->makeCurrent();
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0f);
To copy to clipboard, switch view to plain text mode
I draw like so:
context->makeCurrent();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_CULL_FACE);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_GREATER, 0.0);
for(int i = 0; i < numWidgets; i++)
{
if(Widget[i]->type == TriangleWidget)
{
Widget[i]->DrawTriangle(mode);
}
else
{
Widget[i]->DrawRectangle(mode);
}
}
glFlush();
if(context->format().doubleBuffer())
context->swapBuffers();
context->makeCurrent();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_CULL_FACE);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_GREATER, 0.0);
for(int i = 0; i < numWidgets; i++)
{
if(Widget[i]->type == TriangleWidget)
{
Widget[i]->DrawTriangle(mode);
}
else
{
Widget[i]->DrawRectangle(mode);
}
}
glFlush();
if(context->format().doubleBuffer())
context->swapBuffers();
To copy to clipboard, switch view to plain text mode
I use glReadPixels like so:
unsigned char * LUT2D = new unsigned char[256 * 256 * 4];
context->makeCurrent();
glReadBuffer(GL_FRONT);
glReadPixels(0, 0, 256, 256, GL_RGBA, GL_UNSIGNED_BYTE, LUT2D);
// debug
FILE * fp = fopen("/tmp/LUT2D.txt", "w+");
if(fp)
{
unsigned int * tmp = (unsigned int *)LUT2D;
for(int h = 0; h < 256; h++)
{
for(int w = 0; w < 256; w++)
{
fprintf(fp, "0x%08X, ", tmp[w * h]);
}
fprintf(fp, "\n");
}
fclose(fp);
}
// debug
unsigned char * LUT2D = new unsigned char[256 * 256 * 4];
context->makeCurrent();
glReadBuffer(GL_FRONT);
glReadPixels(0, 0, 256, 256, GL_RGBA, GL_UNSIGNED_BYTE, LUT2D);
// debug
FILE * fp = fopen("/tmp/LUT2D.txt", "w+");
if(fp)
{
unsigned int * tmp = (unsigned int *)LUT2D;
for(int h = 0; h < 256; h++)
{
for(int w = 0; w < 256; w++)
{
fprintf(fp, "0x%08X, ", tmp[w * h]);
}
fprintf(fp, "\n");
}
fclose(fp);
}
// debug
To copy to clipboard, switch view to plain text mode
As you can see, I'm writing the buffer to a file to see what's in the alpha channel. It's 0xFF (or 1.0 or 255 depending on your preference) for every value.
Once again, is this some weirdness with using QGLContext?
Bookmarks