Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7646197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:08:45+00:00 2026-05-31T10:08:45+00:00

I’d like to render a colour texture in gray. Doing it in ES 2.0

  • 0

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

Dot calculation

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T10:08:47+00:00Added an answer on May 31, 2026 at 10:08 am

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the
I would like to run a str_replace or preg_replace which looks for certain words

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.