2
votes

I'm new to OpenGL. I'm using Java with LWJGL and Slick. So far I can draw several textures and with buffers I can copy the images shown on the screen to a texture for postprocessing purposes. Using glColor3f() I can make the screen have the desired color (just blue, just red, only show blue and green channels, etc).

But what glColor3f(r, g, b) only does is multiply the values of r, g, b to the current pixels. If R, G, B are the current values of the pixel, what glColor does is (R*r, G*g, B*b) for all pixels.

What I want to do is use the current values R, G, B, so that for example I can swap the red channel for the blue Channel:

(B, R G)

Or use these values and new ones for arithmetic operations

(R+G*r, (B+g)/5, B*0.2)

My purpose is to make a grayscale texture, changing all pixels color (R, G, B) with

color = (R*0.5 + G*0.5 + B*0.5)/3
functionThatChangesColor(color, color, color);

How can I achieve this or something like it?

Thanks!

1

1 Answers

1
votes

Use a shader implementing a component swizzle. With later versions of OpenGL you also can set component swizzling for a texture as texture parameters. See http://www.opengl.org/wiki/Texture#Swizzle_mask

Swizzle mask

While GLSL shaders are perfectly capable of reordering the vec4​ value returned by a texture function, it is often more convenient to control the ordering of the data fetched from a texture from code. This is done through swizzle parameters.

Texture objects can have swizzling parameters. This only works for textures with color image formats. Each of the four output components, RGBA, can be set to come from a particular color channel. The swizzle mask is respected by all read accesses, whether via texture samplers or Image Load Store.

To set the output for a component, you would set the GL_TEXTURE_SWIZZLE_C texture parameter, where C is R, G, B, or A. These parameters can be set to the following values:

  • GL_RED: The value for this component comes from the red channel of the > image. All color formats have at least a red channel.
  • GL_GREEN: The value for this component comes from the green channel of > the image, or 0 if it has no green channel.
  • GL_BLUE: The value for this component comes from the blue channel of > the image, or 0 if it has no blue channel.
  • GL_ALPHA: The value for this component comes from the alpha channel of > the image, or 1 if it has no alpha channel.
  • GL_ZERO: The value for this component is always 0.
  • GL_ONE: The value for this component is always 1.

You can also use the GL_TEXTURE_SWIZZLE_RGBA parameter to set all four at > once. This one takes an array of four values. For example:

//Bind the texture 2D.
GLint swizzleMask[] = {GL_ZERO, GL_ZERO, GL_ZERO, GL_RED};
glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_RGBA, swizzleMask);

This will effectively map the red channel in the image to the alpha > channel when the shader accesses it.