Brandon
Brandon

Reputation: 23510

Draw 2-Dimensional Image 32bit

I'm trying to draw a buffer of pixels to the screen. When I draw, black is drawn as transparent which is perfect. However, other colours are also drawn transparent. I cannot seem to figure out why.

I draw using:

glDrawPixels(width, height, GL_BGRA, GL_UNSIGNED_BYTE, buffer);

to draw a 32 bit bmp. However, the bitmap does not show unless I do convert it to 24 bit and use GL_BGR. Am I doing something wrong? Is there a reason black would be transparent?

Right before doing a draw, I do:

void Enable2DDrawing(bool &DrawingEnabled, bool &GLTexture2D, bool &GLRectangleTexture, bool &PointSmooth, float &PointSize)
{
    if (!DrawingEnabled)
    {
        GLTexture2D = glIsEnabled(GL_TEXTURE_2D);
        GLRectangleTexture = glIsEnabled(GL_TEXTURE_RECTANGLE);
        PointSmooth = glIsEnabled(GL_POINT_SMOOTH);
        glGetFloatv(GL_POINT_SIZE, &PointSize);

        glDisable(GL_TEXTURE_RECTANGLE);
        glDisable(GL_TEXTURE_2D);
        glEnable(GL_POINT_SMOOTH);
        glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);

        glPushMatrix();
        glLoadIdentity();
        DrawingEnabled = true;
    }
}

and right after drawing, I do:

void Disable2DDrawing(bool &DrawingEnabled, bool GLTexture2D, bool GLRectangleTexture, bool PointSmooth, float PointSize)
{
    if (DrawingEnabled)
    {
        glPopMatrix();
        if (GLTexture2D) glEnable(GL_TEXTURE_2D);
        if (GLRectangleTexture) glEnable(GL_TEXTURE_RECTANGLE);
        if (PointSmooth) glDisable(GL_POINT_SMOOTH);
        DrawingEnabled = false;
    }
}

Then I swap the buffer. I also tried drawing using textures by doing:

GLuint LoadTexture(void* Buffer, int width, int height, GLenum Target)
{
    GLuint ID = 0;
    glGenTextures(1, &ID);
    glBindTexture(Target, ID);
    glTexParameteri(Target, GL_TEXTURE_WRAP_S, Target == GL_TEXTURE_2D ? GL_REPEAT : GL_CLAMP_TO_EDGE);
    glTexParameteri(Target, GL_TEXTURE_WRAP_T, Target == GL_TEXTURE_2D ? GL_REPEAT : GL_CLAMP_TO_EDGE);
    glTexParameteri(Target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(Target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexImage2D(Target, 0, GL_RGBA, width, height, 0, GL_BGRA, GL_UNSIGNED_BYTE, Buffer);
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
    return ID;
}

void DrawTexture(std::uint32_t Target, std::uint32_t ID, float X1, float Y1, float X2, float Y2, int Width, int Height)
{
    Width = Target == GL_TEXTURE_RECTANGLE ? Width : 1;
    Height = Target == GL_TEXTURE_RECTANGLE ? Height : 1;

    glEnable(Target);
    glBindTexture(Target, ID);
        glBegin(GL_QUADS);
        glTexCoord2f(0, Height);
        glVertex2f(X1, Y1); //top left
        glTexCoord2f(0, 0); //bottom left
        glVertex2f(X1, Y2);
        glTexCoord2f(Width, 0);
        glVertex2f(X2, Y2);  //bottom right
        glTexCoord2f(Width, Height);
        glVertex2f(X2, Y1);  //top right
    glEnd();
    glDisable(Target);
}

But it does not draw the bitmap correctly. It draws a 32-bit bmp like: https://i.sstatic.net/XMW4k.png when it is supposed to be completely white. If I draw each pixel 1 by 1, it works fine. Am I missing something?

What is the best way to draw 32 bit bitmaps and do I have to use glPixelStorei? Does anyone have sample code for drawing a bitmap or a nice explanation on how?

Upvotes: 1

Views: 218

Answers (1)

Mark Ingram
Mark Ingram

Reputation: 73635

Have you enabled blending?

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

Upvotes: 2

Related Questions