• Advertisement

Archived

This topic is now archived and is closed to further replies.

Masking Textures with color

This topic is 6059 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi, i''m trying to mask a b&w texture with the help of a mask and the color it. here''s the code i''m using so far: //not optimised glPushMatrix (); glBlendFunc(GL_DST_COLOR,GL_ZERO); glBindTexture (GL_TEXTURE_2D, texture[0]); glTranslatef (STARS.Position.x, STARS[i].Position.y, STARS[i].Position.z); glBegin(GL_QUADS); glTexCoord2f (0.0,0.0); glVertex3f (0.0f,0.0f, 0.0f ); glTexCoord2f (1.0,0.0); glVertex3f (5.0f*STARS[i].Size,0.0f,0.0f ); glTexCoord2f (1.0,1.0); glVertex3f (5.0f*STARS[i].Size,5.0f*STARS[i].Size,0.0f ); glTexCoord2f (0.0,1.0); glVertex3f (0.0f,5.0f*STARS[i].Size,0.0f ); glEnd(); glColor4ub (STARS[i].Color.r, STARS[i].Color.g, STARS[i].Color.b, 150);//won''t work!!!! glBlendFunc(GL_ONE,GL_ONE); glBindTexture (GL_TEXTURE_2D, texture[1]); glBegin(GL_QUADS); //same as above! glEnd(); glPopMatrix (); what am i doing wrong? thanks

Share this post


Link to post
Share on other sites
Advertisement
not sure I totally follow what you''re trying to do...

I see that the first call to glTranslatef leaves the index off of "STARS", but I would think the compiler would catch that... seeing the "i" index makes me think this is in a loop. Are you setting your color for the first pass correctly? What is already on the screen? The GL_DST_COLOR means multiply the source color by the color that''s already there. Is this part not working or is it the second part? The second part seems pretty bullet-proof assuming your texture is okay...

What the result when this runs?

Share this post


Link to post
Share on other sites
Hi!!

I am not sure if this is totaly correct...but here some ps.code :

Enable Blending (DST, ALPHA, Whatever);
Draw the poly / tris with the mask texture
Disable Blending.
Draw the poly / tris with the color texture!!

All this is right out of my head, and i really haven''t done
this much...but if i am wrong here....someone let me know!!

As a reference you should try NeHe''s tutorials on masking!!
I think he does the same that i was trying to make out here! :

Hope this can help you a little!!

btw : You are refering to a number in an array ("i"), but you
don''t set the value for "i" anywhere......

Take Care!

- -- ---[XaOs]--- -- -

Share this post


Link to post
Share on other sites
Hello and thaks for the help.
yes, "i" refers to a loop.
i did not add it since it doesn''t really have anything to do
with my problem. :-)
what i would like to have is:
mask the negative
and render the original in the color defined.
is that even possible, or would i have to make different
colored originals???

thanks again.

frank

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
As far as I understood, do you want to change the color of the texture you have (b&w) to the color you have given in
glColor4ub(...) right?

You have to call
glEnable(GL_COLOR_MATERIAL); to get the effect you want.

cya
C_L

Share this post


Link to post
Share on other sites
do i have to enable something?
atm i have:
glEnable (GL_TEXTURE_2D);
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
glClearDepth(1.0f);
glEnable(GL_COLOR_MATERIAL);
glHint(GL_POINT_SMOOTH_HINT,GL_NICEST);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glEnable(GL_BLEND);

anything else?
do i need any of these?
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_EQUAL);

thanks again

Share this post


Link to post
Share on other sites
another Q:
would i have to do my own depth-sorting?
atm i am just rendering them to the screen.

thanks again

frank

Share this post


Link to post
Share on other sites
You only need GL_COLOR_MATERIAL if lighting is enabled, which it doesn''t appear to be.

Sorry, I still don''t think I fully understand the problem.

If there are overlaps, you''ll have to split up your drawing routines. Given the functions as they are currently, all multiplies can be done at one time, then all adds (ie, the first part for each star, then the second part for each star). This is because multiplies and adds are order independent amongs themselves, but mixed cause problems. by splitting them up, you avoid having to sort.

Share this post


Link to post
Share on other sites
Found my problem:
i was hoping to have the white part of my orig image colored.
stupid me!
is there a way to color textures?
or define transparent area''s?

Share this post


Link to post
Share on other sites
How should i do my blending?
first glBlendFunc(GL_DST_COLOR,GL_ZERO);
then glBlendFunc(GL_ONE,GL_ONE);???
and does anyone have a explanation of the parameters?
glBlendFunc(source, dest); correct
so GL_One means that all of the info from the source is used,
and all the dest info, correct?
GL_DST_COLOR means that the destination info is used
GL_ZERO that that no dest info is used.

any help in clearing this up would be appreciated.
(i have nehe''s tutorials and the red book, but cannot figure it out! :-))

thakns

Share this post


Link to post
Share on other sites
Sure, the way it works is this:

you''ve got "source" pixels -- the pixels that have just been calculated and are ready to write to the framebuffer -- and "destination" pixels -- the pixels that are already on the framebuffer.

the first argument in glBlendFunc is the token describing how to use the source pixels, the second is how to use the destination pixels. GL_ONE, GL_ONE means literally multiply the source pixels by 1, then multiply the desination pixels by 1, then add them up and store them to the framebuffer. The add them up and store them is always the case, so it''s a matter of what you''re multiplying them by.

The valid tokens are:
GL_ONE (multiply by 1), GL_ZERO (multiply by 0), GL_DST_COLOR (multiply by the destination color), GL_ONE_MINUS_DST_COLOR (multiply by the inverse of the destination color), GL_SRC_ALPHA (multiply by the source alpha value), GL_ONE_MINUS_SRC_ALPHA (multiply by the inverse of the source alpha), GL_DST_ALPHA (multiply by the destination alpha), GL_ONE_MINUS_DST_ALPHA (multiply by the inverse of the destination alpha), GL_SRC_ALPHA_SATURATE (multiply by sourc alpha or inverse of destination alpha, whichever is less)

So GL_DST_COLOR, GL_ZERO means that you are essentially multiplying the source pixels by the destination pixels, then multiplying the destination pixels by zero, then adding them up and storing them.

GL_ONE, GL_ONE_MINUS_SRC_ALPHA is typical for a standard transparent effect. This means that you''re taking your full range of color from the source pixels, then multiplying the destination pixels by the inverse of the source alpha (thus knocking a black "hole" where there is high alpha in your source pixels), then adding them up. You can use GL_ALPHA, GL_ONE_MINUS_SRC_ALPHA which will multiply your source pixels by their alpha, but I prefer the "premultiplied" approach which assumes that your color has already been affected by alpha.

If you think about your colors as just being numbers and the blend functions as being math functions, it helps you realize how to group your effects and in what order you need to do things.

Share this post


Link to post
Share on other sites

  • Advertisement