简体   繁体   中英

All texture colors affected by colored rectangle - OpenGL

I'm currently trying to color some rectangles to represent collision boxes for my game character. The issue is the color I'm trying to apply ONLY to the rectangles are being applied to my character and background textures as well. So if I draw a rect using the following code:

void DrawRect(v2f MinPoint, v2f MaxPoint) 
    {
        glBegin(GL_QUADS);

        glColor3f(1.0f, 0.0f, 0.0f); //Red color
        glVertex2f(MaxPoint.x, MaxPoint.y);
        glColor3f(1.0f, 0.0f, 0.0f);
        glVertex2f(MinPoint.x, MaxPoint.y);
        glColor3f(1.0f, 0.0f, 0.0f);
        glVertex2f(MinPoint.x, MinPoint.y);
        glColor3f(1.0f, 0.0f, 0.0f);
        glVertex2f(MaxPoint.x, MinPoint.y);

        glEnd();
        glFlush();
    }

Then right after draw my background texture:

void DrawBackground(ui32 TextureID, Drawable_Rect BackgroundImage, v2f MinUV, v2f MaxUV)
    {
        glEnable(GL_TEXTURE_2D);
        glBindTexture(GL_TEXTURE_2D, TextureID);

        glBegin(GL_QUADS);
        glTexCoord2f(MinUV.x, MinUV.y);
        glVertex2f(BackgroundImage.BottomLeft.x, BackgroundImage.BottomLeft.y);

        glTexCoord2f(MaxUV.x, MinUV.y);
        glVertex2f(BackgroundImage.BottomRight.x, BackgroundImage.BottomRight.y);

        glTexCoord2f(MaxUV.x, MaxUV.y);
        glVertex2f(BackgroundImage.TopRight.x, BackgroundImage.TopRight.y);

        glTexCoord2f(MinUV.x, MaxUV.y);
        glVertex2f(BackgroundImage.TopLeft.x, BackgroundImage.TopLeft.y);

        glEnd();
        glFlush();

        glDisable(GL_TEXTURE_2D);
        glBindTexture(GL_TEXTURE_2D, 0);
    }

Then both my rectangle AND my background are red instead of just my rectangle. Why is the color being applied to both my rectangle and my texture?

If texturing is enabled, then by default the color of the texel is multiplied by the current color, because by default the texture environment mode ( GL_TEXTURE_ENV_MODE ) is GL_MODULATE . See glTexEnv .

This causes that the color of the texels of the texture is "mixed" by the last color which you have set by glColor3f :

Set a "white" color before you render the texture, to solve your issue:

glColor3f(1.0f, 1.0f, 1.0f); 

Likewise you can change the environment mode to GL_REPLACE , instead:

glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM