c++ - 具有 alpha 和 glColor4f 的 OpenGL 纹理,glColor4f 影响纹理

标签 c++ opengl transparency texture-mapping

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

有没有办法关闭颜色,或者如何让 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 textures\n");
    _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);

关于c++ - 具有 alpha 和 glColor4f 的 OpenGL 纹理,glColor4f 影响纹理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29045871/

相关文章:

c# - 为什么 C# 不提供 C++ 风格的 'friend' 关键字?

c++ - OpenGL 动画似乎偶尔会丢失骨骼数据

css - HTML 中的透明镂空或镂空形状

css - 边框内有透明间隙的 Div

c++ - 重新打包异常,还是保持它们完好无损?

c++ - 如何防止 Cocoa 框架中的 C++ 名称重整

c++ - 没有用于调用未解析的重载函数类型的匹配函数

c++ - 是否可以使用 GL_TRIANGLE_FAN 或 GL_TRIANGLE_STRIP 绘制两个单独的四边形?

c++ - 为什么相机 View 矩阵不改变点的位置

objective-c - 透明 NSTableView