Ben
Ben

Reputation: 84

Getting pixel colour not accurate

I'm currently using colour picking in my application. This works on the PC, however I'm having trouble to get it working on a variety of devices.

This is probably due to the context being set up differently, depending on the device. For example, as far as I'm aware the PC is set to a colour of 888, whereas a device might default to 565.

I was wondering if there's a way in OpenGL to get the current pixel/colour format, so that I can retrieve the colour data properly?

This is the function I'm using which works fine on the PC:

inline void ProcessColourPick(GLubyte *out, KDfloat32 x, KDfloat32 y)
{
    GLint viewport[4];
    GLubyte pixel[3];

    glGetIntegerv(GL_VIEWPORT,viewport);

    //Read colour of pixel at a specific point in the framebuffer

    glReadPixels(x,viewport[3]-y,1,1,
        GL_RGB,GL_UNSIGNED_BYTE,(void *)pixel);

    out[0] = pixel[0];
    out[1] = pixel[1];
    out[2] = pixel[2];
}

Any ideas?

Upvotes: 1

Views: 617

Answers (2)

qehgt
qehgt

Reputation: 2990

The format parameter for glReadPixels must be either GL_RGBA (always supported) or the GL_IMPLEMENTATION_COLOR_READ_FORMAT_OES (different on different devices). It's a OpenGL ES restriction.

Upvotes: 0

Nicol Bolas
Nicol Bolas

Reputation: 473174

Yes, but it's a bit complicated.

Querying the bitdepth of the current framebuffer is fairly easy in ES 2.0 (note: this is also legal in Desktop GL, but this functionality was removed in GL 3.1 core. It's still accessible from a compatibility profile). You have to get the bitdepth of each color component:

GLint bitdepth; glGetIntegerv(GL_x_DEPTH, &bitdepth);

Where x is one of RED, GREEN, BLUE, or ALPHA.

Once you have the bitdepth, you can test to see if it's 565 and use appropriate pixel transfer parameters and color values.

Upvotes: 1

Related Questions