Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find out how many colors my OpenGL can represent on it's color buffer?

Tags:

opengl

I am needing to know how many different colors I can use in my OpenGL application.

My situation: I am going to read back the drew pixels using glReadPixels but the color will have an integer meaning instead of color. So I need to know how much information I can represent.

I was going to use glGet with GL_RED_BITS,GL_GREEN_BITS and GL_BLUE_BITS. But these are deprecated. How can I achieve this using a function that is available at least at OpenGL 3?

like image 386
André Puel Avatar asked Sep 14 '25 11:09

André Puel


1 Answers

Okey, I've found what I was looking for. My comment about glGetFramebufferAttachmentParameteriv was correct, and here's some reference:

glGetFramebufferAttachmentParameteriv — retrieve information about attachments of a bound framebuffer object

So far so good.

If the default framebuffer is bound to target then attachment must be one of GL_FRONT_LEFT, GL_FRONT_RIGHT, GL_BACK_LEFT, or GL_BACK_RIGHT, identifying a color buffer.

So yup, we can use it for screen buffer! And now the part that interests you:

pname can be either:

  • GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE
  • GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE
  • GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE
  • GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE
  • GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE

params will contain the number of bits in the corresponding red, green, blue, alpha, depth, or stencil component of the specified attachment. Zero is returned if the requested component is not present in attachment.

And oh, target can be just GL_FRAMEBUFFER, I think.

EDIT: You requested an example to sum it all up, so...

glBindFramebuffer(GL_FRAMEBUFFER, 0);
GLint ret;

glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER, GL_FRONT_LEFT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &ret);

if (ret == GL_NONE)
    exit(1); // something is really bad there or FRONT_LEFT isn't your default buffer. Check it!

glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER, GL_FRONT_LEFT, GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE, &ret);

And here are possible INVALID_ENUMS you can get:

  • GL_INVALID_ENUM is generated if target is not one of the accepted tokens.
  • GL_INVALID_ENUM is generated if pname is not valid for the value of GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE.
like image 153
Bartek Banachewicz Avatar answered Sep 17 '25 21:09

Bartek Banachewicz