Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

get pixel color

206 views
Skip to first unread message

Rositsa Ilova

unread,
Dec 27, 2009, 11:44:35 AM12/27/09
to
Hello,
I try to write my own getPixel - get the pixel color and setPixel -
set the pixel color functions. I draw a green point on white
background. And a want to get the green color. But I have problem -
getPixel read background color (white), not the point color. I think I
make a mess with color buffers. (I know that my getPixel function
return only one parameter from RGB array). Could you help me please?
Thanks a lot :)
Here is part of my code :

int RGB_2(float r, float g, float b)
{ glColor3f(r,g,b);
return 1; }

void SetPixel(int x, int y, int r)
{
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
glFlush();
}
int GetPixel (int x, int y)
{
GLubyte pixel[3] ;
glReadPixels(x,y,1,1, GL_RGB ,GL_UNSIGNED_BYTE,(void *)pixel);
return (int)pixel[1];
}

GLvoid ReSizeGLScene(GLsizei width, GLsizei height)
{
if (height==0) height=1;
glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-width/2,width/2,height/2,-height/2);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int InitGL(GLvoid)
{
glShadeModel(GL_SMOOTH);
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
return TRUE;
}
int DrawGLScene(GLvoid) {
glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT );
glEnable(GL_BLEND);
glEnable(GL_SMOOTH);
glLoadIdentity();
SetPixel (0,0, RGB_2(0,0.5,0));
GetPixel (0,0);
return TRUE;
}

fungus

unread,
Dec 27, 2009, 12:16:40 PM12/27/09
to
On Dec 27, 5:44 pm, Rositsa Ilova <rositsa.il...@gmail.com> wrote:
>
> void SetPixel(int x, int y, int r)
> {
>     glBegin(GL_POINTS);
>            glVertex2i(x, y);
>      glEnd();
>   glFlush();}
>

Where do you set the color...?

--
<\___/>
/ O O \
\_____/ FTB.

http://www.topaz3d.com/ - New 3D editor for real time simulation

Marcel Heinz

unread,
Dec 27, 2009, 1:52:07 PM12/27/09
to
Hi,

Rositsa Ilova wrote:
> Hello,
> I try to write my own getPixel - get the pixel color and setPixel -
> set the pixel color functions. I draw a green point on white
> background. And a want to get the green color. But I have problem -
> getPixel read background color (white), not the point color. I think I
> make a mess with color buffers. (I know that my getPixel function
> return only one parameter from RGB array). Could you help me please?
> Thanks a lot :)
> Here is part of my code :
>
> int RGB_2(float r, float g, float b)
> { glColor3f(r,g,b);
> return 1; }
>
> void SetPixel(int x, int y, int r)
> {
> glBegin(GL_POINTS);
> glVertex2i(x, y);
> glEnd();
> glFlush();
> }
> int GetPixel (int x, int y)
> {
> GLubyte pixel[3] ;
> glReadPixels(x,y,1,1, GL_RGB ,GL_UNSIGNED_BYTE,(void *)pixel);
> return (int)pixel[1];
> }

glReadPixels() expects window coordinates, while your drawing code uses
the full-blown Render-Pipleline with object coordinates - There is
nothing wrong with this approach, as long as you get the coordinate
transformations right.

> [...]


> glMatrixMode(GL_PROJECTION);
> glLoadIdentity();
> gluOrtho2D(-width/2,width/2,height/2,-height/2);
> glMatrixMode(GL_MODELVIEW);
> glLoadIdentity();

You set up your projection matrix to map (0,0) to the center of the
window, but are reading back the bottom-left corner pixel, so you are
getting your (white) background...

Regards,
Marcel

--
Marcel Heinz | <marcel...@informatik.tu-chemnitz.de> | PGP-KeyID: E78E9442

"Perfection is attained not when there is nothing more to add,
but when there is nothing more to remove." -- Antoine de Saint-Exup�ry

jbwest

unread,
Dec 27, 2009, 4:16:30 PM12/27/09
to

"Marcel Heinz" <marcel...@informatik.tu-chemnitz.de> wrote in message
news:hh8ab9$h8g$1...@bruford.hrz.tu-chemnitz.de...


Plus, you are promoting a byte (pixel[1]) to int and returning it.
You want to return all 3 bytes for RGB!

GL_BGR or GL_BGRA may be faster, but be aware that this whole idea is
extremely slow. Think in terms of bulk buffers.

jbw


0 new messages