Jump to content
  • Advertisement
Sign in to follow this  
Jonbca

OpenGL Blending help

This topic is 5212 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 everyone, I'm just starting to work with opengl, and I'm having some issues with blending. I'm making a set of widgets for some stuff im working on. I'm planning on having it so that I: - set the colour of a button (glColor4f) - then blend a texture on it to give it the shape i want The problem I'm having, is that once blended, the colour looks washed out. Its almost like im setting the alpha to 0.5 or something. Anyways, here's the code (it's in java, but it's a pretty straight port to openGL. I'm using LWJGL).
// texOff sets to use the upper half of the texture (button up), or lower half(button down)
GL11.glDisable(GL11.GL_DEPTH_TEST);
            GL11.glEnable(GL11.GL_BLEND);
            GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);

            GL11.glBegin(GL11.GL_QUADS);
            {
                GL11.glTexCoord2f(0.0f, 0.5f + texOff);
                GL11.glVertex2f(x, y + height);
                GL11.glTexCoord2f(0.0f, 0.0f + texOff);
                GL11.glVertex2f(x, y);
                GL11.glTexCoord2f(1.0f, 0.0f + texOff);
                GL11.glVertex2f(x + width, y);
                GL11.glTexCoord2f(1.0f, 0.5f + texOff);
                GL11.glVertex2f(x + width, y + height);
            }
            GL11.glEnd();
            GL11.glDisable(GL11.GL_BLEND);
Any help would be great.

Share this post


Link to post
Share on other sites
Advertisement
It seems to me that you have misunderstood the term "blending".When you enable blending,the pixel colors are blended with the pixels that are already drawn in the framebuffer.From what I understand,you just want to modulate the texture with the primary color.Just don't use blending,but set the TexEnv to GL_MODULATE(which is the default anyway).

Share this post


Link to post
Share on other sites
The textures I'm using are greyscale where the white is the colour i want and black is transparent. With modulation, the colour of the buttons shows up right, but he black shows up aswell. Can I change the texEnv to something else to get the result i want?

Share this post


Link to post
Share on other sites
Have you tried GL_SRC_COLOR, GL_ONE as your blend factors?

I generally play around with them until it appears right. It's probably not the best way to do it, but it works for me [grin]

Share this post


Link to post
Share on other sites
Ok,now I understand what are you trying to do.You're trying to draw monochrome buttons(specifying the color with glColor4f),but the texture defines their shape.To do that,you need to create an RGBA texture(you can create an ALPHA texture,but I don't really know how modulation works with that).The R,G,B values will be 1.0,but the ALPHA value will be as you describe(0 for transparent,1 for solid).You keep TexEnv as modulate,set the color with glColor4f(cr,cg,cb,1.0),and set the blendfunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA).Later on,you can use the RGB values of the texture if you want to put an image on the button.

Share this post


Link to post
Share on other sites
Thats exactly what im looking at. I have a png with the alpha values I want for the shape, but i dont know how to load it into GL. do i simply have to change:

glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGB, tex.getWidth(), tex.getHeight(), 0, GL11.GL_RGB,GL11.GL_UNSIGNED_BYTE,scratch);

to

glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, tex.getWidth(), tex.getHeight(), 0, GL11.GL_RGBA,GL11.GL_UNSIGNED_BYTE,scratch);

Share this post


Link to post
Share on other sites
I don't know anything about PNG,or how to load them.But you say "I have a PNG with alpha values".The alpha value is a seperate component,and it's invisible,it's not part of the RGB.Have you incorporated alpha values on the PNG,and have a loader that can handle these values?

Share this post


Link to post
Share on other sites
Ok, so have to add the alpha values to the texture seperatly. by any chance, would you have some sample code to show what all this entails? Thanks for the help so far by the way.

Share this post


Link to post
Share on other sites
Ok,let's say RGB_Data pointer contains the RGB image.(black=transparent,white=solid).



unsigned char *RGBA_Data;
unsigned char *p1,*p2,alpha;
int i,j;

RGBA_Data=malloc(TexWidth*TexHeight*4);//Allocate mem for RGBA texture

p1=RGB_Data;
p2=RGBA_Data;
for (i=0;i<(TexWidth*TexHeight);++i)//Loop all the pixels
{alpha=*p1;//Store alpha=RED
for (j=0;j<3;++j) {*p2=*p1;++p1;++p2;}//Copy RGB values

*p2=alpha;++p2;//Set alpha value

}






After that,you can load the RGBA texture using
glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,TexWidth,TexHeight, 0, GL_RGBA,GL_UNSIGNED_BYTE,RGBA_Data);

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!