tags:

views:

1029

answers:

4

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?

+1  A: 

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
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
the alpha component will only affect the final image if transparency is enabled, which it apparently is not.
SauceMaster
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
+1  A: 

"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
As far as I know, ARB_imaging is enabled, but how do I find out? I'm using VS 2008.
Anthony Johnson
I've run the OpenGL Extensions Viewer, and it does say that GL_ARB_imaging is a valid extension on my machine.
Anthony Johnson
+4  A: 

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
A: 

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....

Louis