Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

24bit to grayscale

119 views
Skip to first unread message

Michał "Wołek"

unread,
May 15, 2003, 9:39:54 AM5/15/03
to
How to convert 24bit texture to grayscale in realtime?

fungus

unread,
May 15, 2003, 10:17:56 AM5/15/03
to
Michał "Wołek" wrote:

> How to convert 24bit texture to grayscale in realtime?

I don´t think it can be done without fragment programs.


You can make it pseudo-green by using glColorMask() but
not gray.


--
<\___/> For email, remove my socks.
/ O O \
\_____/ FTB. The Cheat is not dead!

Michał "Wołek"

unread,
May 15, 2003, 11:19:02 AM5/15/03
to
fungus <open...@SOCKSartlum.com> wrote:

> Michał "Wołek" wrote:
>
>> How to convert 24bit texture to grayscale in realtime?
>
> I don´t think it can be done without fragment programs.
>

i'm sure there are some functions that do it



> You can make it pseudo-green by using glColorMask() but
> not gray.
>

i want gray

fungus

unread,
May 15, 2003, 12:41:04 PM5/15/03
to
Michał "Wołek" wrote:
>
> i want gray

How do you define "real time"?

Michał "Wołek"

unread,
May 15, 2003, 4:36:12 PM5/15/03
to
fungus <open...@SOCKSartlum.com> wrote:

> Michał "Wołek" wrote:
>>
>> i want gray
>
> How do you define "real time"?
>

i want to implement that in my game so it MUST be fast enought to
process many frames per second.

dxcoder

unread,
May 16, 2003, 12:51:15 AM5/16/03
to
> i want to implement that in my game so it MUST be fast enought to
> process many frames per second.

Here's a Cg PixelShader for the task.

struct vertex
{
float4 position : POSITION;
float2 texc : TEXCOORD0;
};

struct pixel
{
float4 color : COLOR0;
};

uniform sampler2D texmap;

pixel main(const vertex v)
{
pixel p;

float4 ntsc = float4(0.299,0.587,0.114,0.0);
float4 sample = tex2D(texmap,v.texc);
float gray = dot(sample,ntsc);

p.color = float4(gray,gray,gray,1);

return p;
}

--
-xXx-


Vincent DAVID

unread,
May 16, 2003, 9:01:14 AM5/16/03
to
It can be done very fast if you setup a DOT3 texture environment.
For this to work, the graphics card/driver must support the
ARB_texture_env_dot3 extension.

Where do you get your texture from ? Is is read from disk or is it
rendered from a buffer ?

Michał "Wołek"

unread,
May 17, 2003, 8:14:41 PM5/17/03
to
Vincent DAVID <vincen...@c-s.fr> wrote:

> It can be done very fast if you setup a DOT3 texture environment.
> For this to work, the graphics card/driver must support the
> ARB_texture_env_dot3 extension.
>
> Where do you get your texture from ? Is is read from disk or is it
> rendered from a buffer ?
>

both

Vincent DAVID

unread,
May 17, 2003, 8:31:23 PM5/17/03
to
ok then I think you should stick to dot3. doesn't dot3 work ?

Michał "Wołek"

unread,
May 17, 2003, 8:54:09 PM5/17/03
to
Vincent DAVID <vin...@wanadoo.fr> wrote:

> ok then I think you should stick to dot3. doesn't dot3 work ?
>

i didn't try it first i must find some manual on net

Vincent DAVID

unread,
May 19, 2003, 9:14:31 AM5/19/03
to
You may search for info about ARB_texture_env_dot3 and
ARB_texture_env_combine ... it is pretty easy to setup.

For the greyscale algorithm, it is something like :
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_DOT3_RGB_ARB);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, GL_SRC_COLOR);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_CONSTANT_ARB);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB, GL_SRC_COLOR);

And you have to setup the constant color like this :
GLfloat black_n_white_mapping[4] = {0.33333f, 0.33333f, 0.33333f, 1.0f};

glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, black_n_white_mapping);

You can map rgb->b&w with another constant, for instance with {0.1, 0.5,
0.4, 1.0} (note: the alpha is ignored) or whatever RGB triplet you like.
Also note that it looks better if R+G+B=1.
There is a good rgb->b&w mapping function at the www.opengl.org faq. I
don't remember it and I can't check it because my net is dead but I'm
sure you'll find it easily.

Mikael Alfredsson

unread,
May 19, 2003, 10:34:14 AM5/19/03
to
Just a arb_dot3 isnt enough

that dot3 is signed, so it expand the color values first ( (color-0.5)*2 )
.. in order to get correct grayscale conversion you need to tweak the
incomming values first ( color * 0.5 + 0.5) to keep them in th 0-1 span even
after the expand of the values. That can be done by using 2 more texture
units.. it means that a grayscale conversion costs 3 TMUs.. but if you have
gf3 or better hardware its not a problem.

you can also use nv_reg_combiners instead if you are using nvidias HW..
there you can make sure that the incoming values doesnt expand, and you can
preform it in 1 TMU only.


/Mikael Alfredsson

"Michał "Wołek"" <mail...@poczta.onet.pl> wrote in message
news:Xns937F1ECED7CA08...@213.180.128.20...

Vincent DAVID

unread,
May 19, 2003, 2:36:56 PM5/19/03
to
You're right about the color dynamic, even though unchanged dot3 would still
give decent results.
However, I hink you can do a correct greyscale conversion with dot3 thanks to
TMUs, not 3.
Anyway, that's right that if you card supports register combiners, fragment
shaders or fragment programs, you can do it in a different way which *might* use
less instructions.

Vincent DAVID

unread,
May 19, 2003, 2:39:41 PM5/19/03
to
Argh, some characters disappeared into hyperspace. Let me write the message again.

You're right about the color dynamic, even though unchanged dot3 would still
give decent results.

However, I think you can do a correct greyscale conversion with dot3 thanks to 2


TMUs, not 3.
Anyway, that's right that if you card supports register combiners, fragment
shaders or fragment programs, you can do it in a different way which *might* use
less instructions.

0 new messages