OpenGL blending problem

Started by
29 comments, last by clearly 17 years, 11 months ago
what do u mean when u say mask ?
can u show me an example functions ?
or something like it?
Advertisement
From what i can tell from that image u are still drawing, it just looks like the background blend with the tank. Just to be sure, u sure the tank image has an alpha channel? Can u show us a few image, i want to see the tank image without the background, the background alone without the tank. And I want to know if u turn off blending, and draw both the tank and background, can u see the tank?
this is when i turn off the blending
http://members.lycos.co.uk/clearly/2.JPG
this is when i turn off the blending
http://members.lycos.co.uk/clearly/2.JPG
this is when i take off the tank
http://members.lycos.co.uk/clearly/3.JPG
this is when i take off the background
http://members.lycos.co.uk/clearly/4.JPG
From what i can tell, it seem that u don't have a proper alpha channel. Check your tank image and make sure u have a alpha channel with a correct mask.
how can i check if i have a alpha channel ?
Go to Photoshop and open the tank texture u load from in it, anyway if the tank texture u are loading from is of jpg format, u definitely don't have an alpha channel. Look at the channels, you should see RGBA, where in the A u will see the alpha channel, if u don't see a tank shape in white in the A channel, u don't have a mask of the tank.

Only file formats like PNG, TGA can contain alpha channels.
so there is a way to load a BITMAP file
without a alpha channel ?
common help me out here :X
If you are using .bmp then you dont have an alpha channel. they are RGB only.

.TGA or .png are good image formats, both have lossless compression and support for alpha channels. I recommend you get yourself a copy of the Gimp, this allows you to create images with transparency and to save them in a wide range of formats.

There is alot of .tga and .png file loading code out there. Search the forums and you'll find what you need!

- Al

Twitter: [twitter]CaffinePwrdAl[/twitter]

Website: (Closed for maintainance and god knows what else)

As for the mask method, what that does is draws your tank in black (just the tank, not the rest of the texture), then you need to draw your tank over the top with additive blending. That will make it look like you tank is solid, and ontop of the grass. And is probably the simpler approach if you are new to coding and graphics apps.

As for the code I gave you all the code there, the blending functions are the important bit, you just needed to combine that with your existing code and create the mask image like I said.

This is what you (should) have ended up with.


glDisable(GL_DEPTH_TEST);
glDisable(GL_BLEND);

glLoadIdentity(); // background
glBindTexture(GL_TEXTURE_2D, texture[7]);
glColor3f(1.0f,1.0f,1.5f);
glTranslatef(0.0f,0.0f,-98.0f);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);glVertex3f(-50.0f, -50.0f, -2.0f);
glTexCoord2f(1.0f, 0.0f);glVertex3f( 50.0f, -50.0f, -2.0f);
glTexCoord2f(1.0f, 1.0f);glVertex3f( 50.0f, 50.0f, -2.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-50.0f, 50.0f, -2.0f);
glEnd();

glEnable(GL_BLEND);

// Set the blending mode to Masking, all black pixels on the image
// will appear black on the image, all white will appear transparent
glBlendFunc(GL_DST_COLOR,GL_ZERO);

glLoadIdentity(); // the tank mask
glBindTexture(GL_TEXTURE_2D, texture[XXXXXXX]); <--- you need to load the mask image with your other ones and use that here
glColor3f(1.0f,1.0f,1.0f);
glTranslatef(x2rot,y2rot,-49.0f); //-39
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);glVertex3f(-7.0f, -7.0f, 5.0f);
glTexCoord2f(1.0f, 0.0f);glVertex3f( 7.0f, -7.0f, 5.0f);
glTexCoord2f(1.0f, 1.0f);glVertex3f( 7.0f, 7.0f, 5.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-7.0f, 7.0f, 5.0f);
glEnd();

// Set blending to additive mode
// This adds the incoming colour to the current colour, so the pixels
// of the tank will be added to the black area created by the mask
// and the black pixels in the tank texture (0,0,0) when added cause
// no effect!
glBlendFunc(GL_ONE, GL_ONE);
glLoadIdentity(); // the tank
glBindTexture(GL_TEXTURE_2D, texture[4]);
glColor3f(1.0f,1.0f,1.0f);
glTranslatef(x2rot,y2rot,-49.0f); //-39
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);glVertex3f(-7.0f, -7.0f, 5.0f);
glTexCoord2f(1.0f, 0.0f);glVertex3f( 7.0f, -7.0f, 5.0f);
glTexCoord2f(1.0f, 1.0f);glVertex3f( 7.0f, 7.0f, 5.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-7.0f, 7.0f, 5.0f);
glEnd();

As I said before you will need to create yourself a mask texture. Just copy the tank texture and make all the pixels you want to see, black, and all the pixels you dont white.

- Al

Twitter: [twitter]CaffinePwrdAl[/twitter]

Website: (Closed for maintainance and god knows what else)

This topic is closed to new replies.

Advertisement