I have just started using OpenGL i was writing a program for boundary fill algorithm ,i used glClearColor
to change the background colour to red but when i am reading a pixel color using glReadPixels
it gives RGB value 0,0,0 instead of 1,0,0
#include<stdio.h>
#include<math.h>
#include<GL/glut.h>
#include<GL/freeglut.h>
int WIDTH=500,HEIGHT=500;
void boundaryfill(int x, int y)
{
int pixel[4];
glReadPixels(x,y,1,1, GL_RGB, GL_FLOAT, &pixel);
printf("%f %f %f %f\n",pixel[0],pixel[1],pixel[2],pixel[4]);
if(pixel[0]==(float)1 && pixel[1]==(float)0 && pixel[2]==(float)0)
{
printf("njerngwneroingoierngoineriongioerngiernogineiorngoiern");
}
else
{
glBegin(GL_POINTS);
glColor3f(0,1,0);
glVertex2i(x,y);
glEnd();
glFlush();
//boundaryfill(x,y+1);
//boundaryfill(x+1,y);
//boundaryfill(x,y-1);
//boundaryfill(x-1,y);
}
}
void display()
{
glClearColor(1.0,0.0,0.0,1.0);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,0.0,0.0);
glBegin(GL_LINE_LOOP);
glVertex2i(250,250);
glVertex2i(-250,250);
glVertex2i(-250,-250);
glVertex2i(250,-250);
glEnd();
boundaryfill(250,250);
glFlush();
}
void myinit_func()
{
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
gluOrtho2D(-250,250,-250,250);
}
int main(int argc, char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(0,0);
glutInitWindowSize(WIDTH,HEIGHT);
glutCreateWindow("Boundary fill");
myinit_func();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
First of all you call glReadPixels()
with GL_FLOAT
however pixel
is an int[]
. Additionally you call it with GL_RGB
and since you want the alpha you need to pass GL_RGBA
.
float pixel[4];
glReadPixels(x, y, 1, 1, GL_RGBA, GL_FLOAT, &pixel);
Next in printf()
you get the alpha like this pixel[4]
, I suspect this is a typo, as you need to do pixel[3]
. You also use %d
instead of %f
.
printf("%f %f %f %f\n", pixel[0], pixel[1], pixel[2], pixel[3]);
Last but not least, by default GLUT doesn't setup an alpha buffer, which you can check this by doing:
int bits = 0;
glGetIntegerv(GL_ALPHA_BITS, &bits);
To which bits
should remain 0
. So instead you need to call glutInitDisplayMode
like this:
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_ALPHA);
Note that in glutInitDisplayMode
you don't need to pass GLUT_RGBA
as it's the same as GLUT_RGB
. Which additionally is default, so it can be omitted.
Additionally I would discourage doing pixel[0] == (float)1
due to floating point precision. I would instead recommend using:
GLubyte pixel[4];
glReadPixels(x, y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, &pixel);
printf("%u %u %u %u\n", pixel[0], pixel[1], pixel[2], pixel[3]);
Then doing pixel[0] == 255
instead of course.