开发者

Is it possible to do desaturation in OpenGL ES 1.0 and how?

I'd like to render a colour texture in gray. Doing it in ES 2.0 by using a shader is a piece of cake, but is it possible to do in ES 1.x?

UPDATE

Thanks to @datenwolf, I'm doing it like that:

GLfloat weights_vector[4] = {0.2126, 0.7152, 0.0722, 1.0};
GLfloat additions_vector[4] = {0.5, 0.5, 0.开发者_StackOverflow中文版5, 0.0};

glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);

/* First part */
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_DOT3_RGB);
glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_RGB, GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV, GL_SRC1_RGB, GL_CONSTANT);
glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, weights_vector);

/* Second part */
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_ADD);
glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_RGB, GL_PREVIOUS);
glTexEnvi(GL_TEXTURE_ENV, GL_SRC1_RGB, GL_CONSTANT);
glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, additions_vector);

The first part renders fine if I leave it on its own, but if I add the second one, it uses "black" as the previous colour and so I get only gray pixels. Am I doing it wrong here?

SECOND UPDATE

If I try to use GL_TEXTURE0 instead of GL_PREVIOUS I indeed get the same result as GL_TEXTURE. However, if I used GL_TEXTURE1 I get not even gray pixels, but black. I'm getting lost here...

THIRD UPDATE

The second part is working now. Should've just used the name of the previous texture as @datenwolf had suggested!

However, the output was still not correct as it was inverted. I fixed that by adding:

glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_ONE_MINUS_SRC_COLOR);

Now, it's too dark. And I can't get it right. Tried and didn't work:

  • multiplying the weights by two
  • adding 0.5
  • modulating the whole image by 2
  • multiplying the weights by 2 and then subtracting by 0.5

FOURTH UPDATE (Sorry there are so many)

I begin to suspect it's not possible due to the bias and multiplication in DOT3_RGB. The correct way to do it in a combiner is:

  1. Add 0.5 to the input texture
  2. Calculate to weighting factors: weight_new = (weight_real + 2.0) / (4.0);
  3. Do a GL_DOT3_RGB

For instance, instead of using:

GLfloat weights_vector[4] = {0.30, 0.59, 0.11, 0.0};

Use:

GLfloat weights_vector[4] = {0.575, 0.6475, 0.5275, 0.0};

This is indeed getting almost the right result, but some of the contrast is lost, due to the first step and the fact that numbers are clamped in the range [-1.0, 1.0]

Why the calculations? Well, according to the API:

Is it possible to do desaturation in OpenGL ES 1.0 and how?

So I don't see any other way different from the one I showed and because of the accuracy limitation. Of course, I could first divide the input by 2.0, then add 0.5, do a dot3 and then again multiply by 2, thus effectively having all values in the range [-1.0, 0.0]. But I fear it would still loose accuracy because of the division.

I suspect that the DOT wasn't intended for this purpose, more likely only for bumpmapping or something. Too bad. I hope I'm wrong, but I don't see how.


This is possible using either 2 or 3 texture combiners. Most likely you are looking to support an older device (such as the iPhone 3G) which only supports 2 texture combiners. You can determine how many texture combiners your device supports using the following code:

int maxTextureUnits;
glGetIntegerv(GL_MAX_TEXTURE_UNITS, &maxTextureUnits);

The basic steps to achieve rendering a grayscale texture without shaders is this:

  1. Divide all your pixel RGB values by 2
  2. Subtract .5 from all of your pixel RGB values
  3. Calculate the GL_DOT3_RGB value for each RGB value using custom weighted values

Now, each of these steps can be performed with a Texture Combiner. Or, alternatively, you can use only two texture combiners by replacing step 1 with custom code that adjusts your pixel values when you read in the texture. If you choose to use only two texture combiners you can still render your adjusted texture in color, you will just need a single texture combiner that doubles your RGB values before rendering.

The reason we are adding .5 to all of our pixel values is because the GL_DOT3_RGB equation we are using to calculate luminance will subtract .5 from each of our pixel values.

The reason we are dividing all pixel values by 2 is so that our values are not clamped when moving from step 2 to step 3. If we had a RGB value of (.5, .6, .7) and we added .5 to each of the RGB values, our resulting RGB value going into step 3 would be (1.0, 1.0, 1.0). After the DOT3 equation subtracts .5 from each value, it will be calculating luminance based on (.5, .5, .5).

Here is sample code that will render a texture grayscale using 3 texture units:

//Enable texture unit 0 to divide RGB values in our texture by 2
glActiveTexture(GL_TEXTURE0);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, m_textureId);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
glClientActiveTexture(GL_TEXTURE0);

//GL_MODULATE is Arg0 * Arg1    
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_MODULATE);

//Configure Arg0
glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_RGB, GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR);

//Configure Arg1
float multipliers[4] = {.5, .5, .5, 0.0};
glTexEnvi(GL_TEXTURE_ENV, GL_SRC1_RGB, GL_CONSTANT);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR);
glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, (GLfloat*)&multipliers);

//Remember to set your texture coordinates if you need them
//glEnableClientState(GL_TEXTURE_COORD_ARRAY);
//glTexCoordPointer...

//Enable texture unit 1 to increase RGB values by .5
glActiveTexture(GL_TEXTURE1);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, m_textureId);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
glClientActiveTexture(GL_TEXTURE1);

//GL_ADD is Arg0 + Arg1
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_ADD);

//Configure Arg0
glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_RGB, GL_PREVIOUS);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR);

//Configure Arg1
GLfloat additions[4] = {.5, .5, .5, 0.0};
glTexEnvi(GL_TEXTURE_ENV, GL_SRC1_RGB, GL_CONSTANT);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR);
glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, (GLfloat*)&additions);

//Set your texture coordinates if you need them
//glEnableClientState(GL_TEXTURE_COORD_ARRAY);
//glTexCoordPointer...

//Enable texture combiner 2 to get a DOT3_RGB product of your RGB values
glActiveTexture(GL_TEXTURE2);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, m_textureId);
glClientActiveTexture(GL_TEXTURE2);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);

//GL_DOT3_RGB is 4*((Arg0r - 0.5) * (Arg1r - 0.5) + (Arg0g - 0.5) * (Arg1g - 0.5) + (Arg0b - 0.5) * (Arg1b - 0.5))
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_DOT3_RGB);   

//Configure Arg0
glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_RGB, GL_PREVIOUS);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR);

//Configure Arg1
//We want this to adjust our DOT3 by R*0.3 + G*0.59 + B*0.11
//So, our actual adjustment will need to take into consideration
//the fact that OpenGL will subtract .5 from our Arg1
//and we need to also take into consideration that we have divided 
//our RGB values by 2 and we are multiplying the entire
//DOT3 product by 4
//So, for Red adjustment you will get :
//   .65 = (4*(0.3))/2 + 0.5  = (0.3/2) + 0.5
GLfloat weights[4] = {.65, .795, .555, 1.};
glTexEnvi(GL_TEXTURE_ENV, GL_SRC1_RGB, GL_CONSTANT);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR);
glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, (GLfloat*)&weights);

//Set your texture coordinates if you need them
//glEnableClientState(GL_TEXTURE_COORD_ARRAY);
//glTexCoordPointer...

//Render your objects or sprite

//Clean up by disabling your texture combiners or texture units. 
glActiveTexture(GL_TEXTURE2);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glDisable(GL_TEXTURE_2D);

glActiveTexture(GL_TEXTURE1);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glDisable(GL_TEXTURE_2D);

glActiveTexture(GL_TEXTURE0);
glClientActiveTexture(GL_TEXTURE0);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);


You can use dot (scalar) product texture environment for this. Remember that the dot product of vectors is

dot(v1, v2) = v1[0]*v2[0] + v1[1]*v2[1] + ... + v1[n]*v2[n]

Desaturation is achieved by summing the channels with a weighting factor each

L{r,g,b} = w_r * R + w_g * G + w_b * B

But this is nothing else than a dot product of the colour with a weighting vector. OpenGL-1.5 has a texture environment called combiner and this combiner environment features a dot product mode:

GLfloat weighting_vector[4];
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_DOT3_RGB);
glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_RGB, GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV, GL_SRC1_RGB, GL_CONSTANT);
glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, weighting_vector);

EDIT due to comment

You can specify the operation on the alpha channel in the alpha combiner, token GL_COMBINE_ALPHA. In your case you simply want to use source alpha. Adding these configuration:

glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_REPLACE);
glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_ALPHA, GL_TEXTURE);

What I forgot was, that the dot product mode introduces a 0.5 bias. But this is no problem, as you are provided with at lease 3 combiner stages, so you conclude with a GL_SUBSTRACT stage subtracting 0.5 of each channel and multiply your weighting by 2.0 to compensate for this.

Take a look at the glTexEnv manpage http://www.opengl.org/sdk/docs/man/xhtml/glTexEnv.xml and the original extension specification http://www.opengl.org/registry/specs/ARB/texture_env_combine.txt (of the time when this was an extension). I admit that texture combiners are a bit mind twisting if you're new to them. Historically they are the predecessors of fragment shaders; NVidia started it all with what they then called "register combiners", which later became texture combiners.

EDIT2 due to addendum to question

You must do the second part in its own combiner stage (=texture unit). You switch texture units with glActiveTexture. Modify your code like this:

GLfloat weights_vector[4] = {0.2126, 0.7152, 0.0722, 1.0};
GLfloat additions_vector[4] = {0.5, 0.5, 0.5, 0.0};

glActiveTexture(GL_TEXTURE0);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texID);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);

/* First part */
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_DOT3_RGB);
glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_RGB, GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV, GL_SRC1_RGB, GL_CONSTANT);
glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, weights_vector);

glActiveTexture(GL_TEXTURE1);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texID); // we need some dummy texture active
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);

/* Second part */
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_SUBTRACT);
glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_RGB, GL_PREVIOUS);
glTexEnvi(GL_TEXTURE_ENV, GL_SRC1_RGB, GL_CONSTANT);
glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, additions_vector);

Also I think you intend to subtract, to compensate the 0.5 bias instead of adding 0.5; FTFY.


Before uploading your texture to OpenGL, desaturate it using a ColorMatrix

Paint paint = new Paint();
ColorMatrix matrix = new ColorMatrix();
matrix.setSaturation(0);
paint.setColorFilter(new ColorMatrixColorFilter(matrix));

Bitmap bmp = Bitmap.createBitmap(resource.getWidth(), resource.getHeight(), resource.getConfig());
Canvas canvas = new Canvas(bmp);
canvas.drawBitmap(resource, null, paint);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜