绘制二维图像32位

时间:2013-07-17 07:57:26

标签: opengl

我正在尝试在屏幕上绘制像素缓冲区。当我绘制时,黑色被绘制为透明,这是完美的。但是,其他颜色也是透明的。我似乎无法弄明白为什么。

我使用:

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

绘制32位bmp。但是,除非我将其转换为24位并使用GL_BGR,否则位图不会显示。难道我做错了什么?黑色有透明的原因吗?

在抽签之前,我做:

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;
    }
}

在绘图之后,我做了:

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;
    }
}

然后我交换缓冲区。我还尝试使用纹理绘图:

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);
}

但它没有正确绘制位图。它绘制了一个32位的bmp,​​如: http://i.imgur.com/A4pE2Uw.png当它应该是完全白色的时候。如果我逐个绘制每个像素,它可以正常工作。我错过了什么吗?

绘制32位位图的最佳方法是什么?我必须使用glPixelStorei吗?有没有人有用于绘制位图的示例代码或有关如何的好解释?

1 个答案:

答案 0 :(得分:2)

你启用了混合吗?

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