tags:

views:

82

answers:

2

After loading an image, I have the individual bytes for each channel loaded into an array of unsigned characters. It's passed to a function that projects it as a texture onto a quad. Everything seems to work properly other than the alpha channel, which shows up as the background color. I'm using OpenGL to draw the image. Would I benefit by adding a layering mechanism? Also, how can I achieve the transparent effect that I want?

Note: This is the code that I have a feeling needs changed:

void SetUpView()
{
    // Set color and depth clear value
    glClearDepth(1.f);
    glClearColor(1.f, 0.f, 0.f, 0.f);

    // Enable Z-buffer read and write
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_TEXTURE_2D);
    glDepthMask(GL_TRUE);

    glEnable (GL_BLEND); 
    glBlendFunc (GL_ONE, GL_ONE_MINUS_SRC_ALPHA);

    // Setup a perspective projection
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(90.f, 1.f, 1.f, 500.f);
};

Also, here's the code to render the quad.

void DrawTexturedRect(RectBounds *Verts, Image *Texture)
{
    glBindTexture(GL_TEXTURE_2D, GetTextureID(Texture));

    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    glPixelZoom(1, -1);

    glBegin(GL_QUADS);
    glTexCoord2f(0.0, 1.0);
    glVertex3f(Verts->corners[0].x, Verts->corners[0].y, Verts->corners[0].z);

    glTexCoord2f(1.0, 1.0);
    glVertex3f(Verts->corners[1].x, Verts->corners[1].y, Verts->corners[1].z);

    glTexCoord2f(1.0, 0.0);
    glVertex3f(Verts->corners[2].x, Verts->corners[2].y, Verts->corners[2].z);

    glTexCoord2f(0.0, 0.0);
    glVertex3f(Verts->corners[3].x, Verts->corners[3].y, Verts->corners[3].z);
    glEnd();
};

The Image class holds an array of unsigned chars, obtained using OpenIL.

Relevant code:

loaded = ilLoadImage(filename.c_str());
ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE);

unsigned char *bytes = ilGetData();

//NewImage is an instance of an Image. This is returned and passed to the above function.
NewImage->data = bytes;

Problem

Thanks

A: 

glClearColor(1.f, 0.f, 0.f, 0.f);

I'd assume that's at the RGBA default, and you're setting red to 1.0.

How about a better explanation of what you're trying to accomplish?

Arthur Kalliokoski
See the red box around the cursor? In Paint.Net, there's nothing there. It's supposed to be the alpha channel.
Jeff
A: 

Try:

glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

Edit: I know what that looks like. It looks like you are drawing the blue square (Which has a Z-order position placing it behind the cursor) AFTER the cursor. You have to draw things in the correct, back-to-front Z-Order when alpha blending or you see errors like you are seeing.

Goz
Thanks, but I'm still getting the same effect. Should that be called once, or every frame?
Jeff