PDA

View Full Version : Polygons disappear with transparency



DoomMuffins
11-07-2009, 07:58 PM
I'm planning on making a 2D game that uses sprites, so I'd like to have a transparent polygon I can put sprites on. However, my attempts to make one, with a texture on it or not, have been unsuccessful. Here's my code:


glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
glAlphaFunc(GL_GREATER,0.1f);
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
glBegin(GL_QUADS);
glTexCoord2i(0, 0);
glVertex2i(10, 10);
glTexCoord2i(0, 1);
glVertex2i(10, 138);
glTexCoord2i(1, 1);
glVertex2i(74, 138);
glTexCoord2i(1, 0);
glVertex2i(74, 10);
glEnd();
glColor4f(1.0f, 0.0f, 0.0f, 0.6f);
glBegin(GL_QUADS);
glVertex2i(45, 115);
glVertex2i(45, 165);
glVertex2i(100, 165);
glVertex2i(100, 115);
glEnd();


When I run this, neither polygon shows up, I just get a blank screen. If I take out glEnable(GL_BLEND), they both show up fine, except they're both opaque. I also tried placing the glEnable in between the two polygons; this caused the second one to not appear, and the first one to appear as if it were the only one (even though the two polygons are supposed to overlap!)

Anyone have any ideas on what I'm doing wrong?

Alfonse Reinheart
11-08-2009, 01:11 AM
First, you didn't glEnable(GL_ALPHA_TEST), so your glAlphaFunc call is meaningless.

Also, try putting the call to glColor inside the glBegin/glEnd pairs.

Lastly, since you're using the fixed-function pipeline, what does your texture environment look like? You rendered the first quad with texture coordinates, so you must be using a texture in that one. You should probably be using texture coordinates in the second one too, or you should be turning off the texture environment between the two quads.

DoomMuffins
11-08-2009, 06:47 PM
Strangely enough, using glDisable(GL_TEXTURE_2D) before I drew the second poly caused it to show up properly, with transparency (although the first poly still disappears.)

Here's the texture code. It uses SDL to load the texture.

GLuint texture;
SDL_Surface* surface;
GLenum texture_format;
GLuint nOfColors;

if (surface = SDL_LoadBMP("C:/dwighttest32.bmp"))
{
if ((surface->w & (surface->w-1)) != 0)
MessageBox(NULL, _T("Width not a power of 2!"), _T("Error"), NULL);
if ((surface->h & (surface->h-1)) != 0)
MessageBox(NULL, _T("Height not a power of 2!"), _T("Error"), NULL);
nOfColors = surface->format->BitsPerPixel;
if (nOfColors == 32)
{
if (surface->format->Rmask == 0x000000ff)
texture_format = GL_RGBA;
else
texture_format = GL_BGRA;
}
else if (nOfColors == 24)
{
if (surface->format->Rmask == 0x000000ff)
texture_format = GL_RGB;
else
texture_format = GL_BGR;
}
else
{
MessageBox(NULL, _T("This is not a truecolor texture!"), _T("Error"), NULL);
SDL_Quit();
return 1;
}

glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, 4, surface->w, surface->h, 0, texture_format, GL_UNSIGNED_BYTE, surface->pixels);
}
else
{
MessageBox(NULL, _T("Image could not be loaded!"), _T("Error"), NULL);
SDL_Quit();
return 1;
}

if (surface)
SDL_FreeSurface(surface);


Basically, the end result I'd like to get from this is a completely transparent polygon I can put a texture with transparency on to get a sprite. Am I going about this the right way?

And forgive me if I sound like an idiot, but I don't know what a fixed-function pipeline is.

Dark Photon
11-09-2009, 05:54 AM
Strangely enough, using glDisable(GL_TEXTURE_2D) before I drew the second poly caused it to show up properly, with transparency (although the first poly still disappears.)
Likely because you have specified the first polygon's vertex color alpha values to be 1.0 = opaque (4th component of color), whereas with the second polygon you gave it a translucent value of 0.6.

As Alfonse said, you need to post your texture environment. That is "glTexEnv" calls, such as for example this, which is probably what you want:


glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );


glTexImage2D(GL_TEXTURE_2D, 0, 4, surface->w, surface->h, 0, texture_format, GL_UNSIGNED_BYTE, surface->pixels);
I'd be explicit about what internal format you want. For now, I assume you want GL_RGBA8.

Also, looks like you're trying to load RGB and RGBA data into an RGBA texture. Be advised that the RGB data will probably be assigned an opaque alpha in the conversion. That's probably what you expect.


And forgive me if I sound like an idiot, but I don't know what a fixed-function pipeline is.
If you're not using your own custom vertex and fragment shaders, then you're using what's termed the "fixed function pipeline" (FFP). The FFP is essentially "built-in shaders" you configure with the GL API. You tell it how you want to light, texture, etc., and it goes off and manages the shaders behind the scenes for you. Good training wheels for getting started while you're learning, but eventually you bump your head on the ceiling and desire to write more capable custom shaders instead.

DoomMuffins
11-09-2009, 07:37 PM
Turns out I never specified a texture environment... I'm such a noob at this.

That didn't solve the problem, but changing the internal format did. I now have transparency working how I want it.

Thanks for all the help.

Dark Photon
11-09-2009, 07:51 PM
Turns out I never specified a texture environment... I'm such a noob at this.
No prob, easy mistake.


That didn't solve the problem, but changing the internal format did. I now have transparency working how I want it.
Excellent.