Texturing Issue with OpenGL

Started by
4 comments, last by 21st Century Moose 10 years, 11 months ago

Hi,

I'm working with opengl texturing in 2D, but I need help :) first of all, I'm new at OpenGL, so try not to be so technical in your explanation, i will thank you so much :)

To explain my problem, look at this picture:

texturing.png

as you can see, the ship and the asteroids are quads textured, but i want to "not print" the "black" parts of the quads.

I try to use BLENDING, and although the black parts disappear, the colors blend with the background.

This is how i am blending

glMatrixMode(GL_MODELVIEW);
glPushMatrix(); // guardamos la matriz actual
glTranslatef(centro.x, centro.y, 0.0);
glRotatef(theta, 0.0, 0.0, 1.0);
glTranslatef(-centro.x, -centro.y, 0.0);
glEnable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glBindTexture(GL_TEXTURE_2D, texture.TexName());
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0); glVertex3f(centro.x - dx, centro.y + dy, 0.0);
glTexCoord2f(0.0, 1.0); glVertex3f(centro.x + dx, centro.y + dy, 0.0);
glTexCoord2f(1.0, 1.0); glVertex3f(centro.x + dx, centro.y - dy, 0.0);
glTexCoord2f(1.0, 0.0); glVertex3f(centro.x - dx, centro.y - dy, 0.0);
glEnd();
glDisable(GL_BLEND);
glDisable(GL_TEXTURE_2D);
glPopMatrix(); // restablecemos la matriz original

Any ideas?

Thank you!!

Advertisement

Is the alpha channel of the texture properly defined to mask out the unwanted areas of the sprites?

It looks like your ship and asteroid textures aren't defined with an alpha channels to define the transparent bits. Wikipedia article on alpha compositing which explains the idea. Search for creating alpha channels in your preferred bitmap editor for how to do it. You'll need to change your texture bits to load an RGBA texture (not just an RGB one).

When you've got that sorted out, change you blend func to glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA). The first value is how much of the source texture to use, the second is now much of the background is used. So when a pixel's alpha is one, all the source will be used and none of the background. When it's zero, none of the source and all of the background. If a pixels alpha is 0.5, half of each will be used. It calculates the colour as:

final colour = src colour * src alpha + dst colour * (1 - src alpha)

Is the alpha channel of the texture properly defined to mask out the unwanted areas of the sprites?

where do I do that?

Your paint program can probably add an alpha channel to your image. Unless you're using color keying, the alpha channel has to be in the image to begin with.

And make sure that you load with an internal format of GL_RGBA (GL_RGBA8 if using modern OpenGL) otherwise you'll lose that alpha channel when you create a texture from the image.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

This topic is closed to new replies.

Advertisement