1

How do I get the OpenGL color matrix transforms working?

I've modified a sample program that just draws a triangle, and added some color matrix code to see if I can change the colors of the triangle but it doesn't seem to work.

    static float theta = 0.0f;
glClearColor( 1.0f, 1.0f, 1.0f, 1.0f );
glClearDepth(1.0);
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glPushMatrix();
glRotatef( theta, 0.0f, 0.0f, 1.0f );
glMatrixMode(GL_COLOR);

GLfloat rgbconversion[16] = 
{
    0.0f, 0.0f, 0.0f, 0.0f,
    0.0f, 0.0f, 0.0f, 0.0f,
    0.0f, 0.0f, 0.0f, 0.0f,
    0.0f, 0.0f, 0.0f, 0.0f
};
glLoadMatrixf(rgbconversion);
glMatrixMode(GL_MODELVIEW);

glBegin( GL_TRIANGLES );
glColor3f( 1.0f, 0.0f, 0.0f ); glVertex3f( 0.0f, 1.0f , 0.5f);
glColor3f( 0.0f, 1.0f, 0.0f ); glVertex3f( 0.87f, -0.5f, 0.5f );
glColor3f( 0.0f, 0.0f, 1.0f ); glVertex3f( -0.87f, -0.5f, 0.5f );
glEnd();
glPopMatrix();

As far as I can tell, the color matrix I'm loading should change the triangle to black, but it doesn't seem to work. Is there something I'm missing?

Anthony Johnson
  • 636
  • 8
  • 14

4 Answers4

7

The color matrix only applies to pixel transfer operations such as glDrawPixels which aren't hardware accelerated on current hardware. However, implementing a color matrix using a fragment shader is really easy. You can just pass your matrix as a uniform mat4 then mulitply it with gl_FragColor

Lucas
  • 631
  • 4
  • 9
  • It's worth noting that if you forget to reset the color matrix with `glLoadIdentity()` and try to use glReadPixels(), glReadPixels() will be extremely slow. – nitrogen Jul 02 '20 at 04:02
1

It looks like you're doing it correctly, but your current color matrix sets the triangle's alpha value to 0 as well, so while it is being drawn, it does not appear on the screen.

Andrei Krotkov
  • 5,556
  • 3
  • 33
  • 36
  • Actually, the triangle is still drawn in its original red, green and blue, so I don't believe the color matrix is affecting the alpha component. – Anthony Johnson Apr 27 '09 at 15:34
  • the alpha component will only affect the final image if transparency is enabled, which it apparently is not. – AndyG Apr 28 '09 at 17:48
  • I added a glEnable(GL_BLEND); but it still doesn't affect the picture. It's still a red/green/blue triangle. It appears that the color matrix is having no effect. – Anthony Johnson May 05 '09 at 15:37
1

"Additionally, if the ARB_imaging extension is supported, GL_COLOR is also accepted."

From the glMatrixMode documentation. Is the extension supported on your machine?

Andrei Krotkov
  • 5,556
  • 3
  • 33
  • 36
0

I have found the possible problem. The color matrix is supported by the "Image Processing Subset". In most HW, it was supported by driver.(software implementation)

Solution: Add this line after glEnd():

glCopyPixels(0,0, getWidth(), getHeight(),GL_COLOR);

It's very slow....

robert
  • 33,242
  • 8
  • 53
  • 74
Louis
  • 1