OpenGL Texture with alpha 和 glColor4f,glColor4f 正在影响纹理

OpenGL Texture with alpha and glColor4f, glColor4f is affecting texture

本文关键字:glColor4f 影响 纹理 OpenGL with alpha Texture      更新时间:2023-10-16

我想应用包含 alpha 透明度的.tga文件中的纹理。当纹理绑定到对象时,它会显示到对象本身的纯色。我希望颜色消失,或者至少是透明的。如果我将对象颜色更改为透明,那么纹理作为一个整体变得更加透明,这不是我想要的。

有没有办法关闭颜色,或者如何使glColor4f不影响纹理?

void Sector::DrawPlanets(double gameTime){
  glEnable(GL_TEXTURE_2D);
  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  glEnable( GL_BLEND );
  glColor4f(0.0f, 0.0f, 1.0f,0.1f);
  for(std::vector<Planet *>::iterator it = Planets.begin(); it != Planets.end(); it++){
    double x,y,angle;
    (*it)->GetPosition(&x,&y,&angle,gameTime);
    int radius = (*it)->GetRadius();
    glPushMatrix();
    glTranslatef(x,y,0);
    GLUquadricObj * sphere = gluNewQuadric();
    gluQuadricDrawStyle(sphere, GLU_FILL);
    gluQuadricTexture(sphere, GL_TRUE);
    glBindTexture(GL_TEXTURE_2D, get_textures()[static_cast<int>((*it)->GetPlanetZone())]);
    gluSphere(sphere,radius,20,20);
    glPopMatrix();
  }
  glDisable(GL_TEXTURE_2D);
  }

侧面信息我已经看到纹理的加载方式也可能是问题的罪魁祸首。我也将包括这一点。这是我从课堂上得到的代码,这就是为什么我使用.tga因为它已经可用了。这是我认为相关的,不包括某些功能。

const int num_textures = 5;
static GLuint texName[num_textures];
void InitializeTextures(){
bool repeat[num_textures];
bool border[num_textures];
gliGenericImage *image[num_textures];
int n=0;
repeat[n] = false; border[n] = false;
image[n++] = readImage("ice.tga");
repeat[n] = false; border[n] = false;
image[n++] = readImage("jupiter.tga");
repeat[n] = false; border[n] = false;
image[n++] = readImage("world.tga");
repeat[n] = false; border[n] = false;
image[n++] = readImage("volcanic.tga");
repeat[n] = false; border[n] = false;
image[n++] = readImage("stars.tga");
if(n!=num_textures)
{
    printf("Error: Wrong number of texturesn");
    _getch();
    exit(1);;
}
glGenTextures(num_textures, texName);
for(int i=0; i<num_textures; i++)
{
    glBindTexture(GL_TEXTURE_2D, texName[i]);
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
    int repeats = repeat[i];
    int needs_border = border[i]; // Needed if clamping and not filling the whole polygon.
    if(repeats)
    {
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    }
    else
    {
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
    }
    if(needs_border)
    {
        // set a border.
        SetBorder(image[i]);
    }
    bool mipmaps = false;
    if(!PowerOf2(image[i]->height) || !PowerOf2(image[i]->width))
    {
        // WARNING: Images that do not have width and height as 
        // powers of 2 MUST use mipmaps.
        mipmaps = true; 
    }
    if (mipmaps) 
    {
        gluBuild2DMipmaps(GL_TEXTURE_2D, image[i]->components,
                image[i]->width, image[i]->height,
                image[i]->format, GL_UNSIGNED_BYTE, image[i]->pixels);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
                //GL_LINEAR_MIPMAP_LINEAR);
                GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, 
                //GL_LINEAR);
                GL_NEAREST);
    } 
    else 
    {
        glTexImage2D(GL_TEXTURE_2D, 0, image[i]->components,
                image[i]->width, image[i]->height, 0,
                image[i]->format, GL_UNSIGNED_BYTE, image[i]->pixels);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    }
}
}

当您将TEXTURE_ENV_MODE设置为 DECAL 时,您得到的是覆盖颜色的纹理:

// This is not what you want
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);

你想要 MODULATE ,这是默认值,所以只需删除该行。

请注意,MODULATE会将颜色乘以纹理,因此如果您希望纹理以全亮度显示,请将颜色更改为白色:

glColor4f(1.0f, 1.0f, 1.0f, 1.0f);

更改

glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);

glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,GL_REPLACE);

只需要在这里插话并告诉你,与其用固定函数纹理环境撕裂你的头发,不如简单地放弃整个固定函数混乱,只使用着色器。在片段着色器中,您可以精确指定基色和纹理的组合方式。