简体   繁体   中英

OpenGL Texture with alpha and glColor4f, glColor4f is affecting texture

I want to apply a texture from a .tga file that includes alpha transparency. When the texture is bound to the object it shows through to the solid color of the object itself. I want the color gone, or at least transparent. If I change the object color to be transparent, then the texture as a whole becomes more transparent, which is not what I want.

Is there a way to either turn off color, or how can I make the glColor4f not affect the texture?

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

Side Info I have seen that the way the texture is being loaded in might also be the culprit for the problem. I will include that as well. It is code I got from class, that is why I am using .tga because it was already usable. Here is what I figure is relevant, some functions are not included.

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

When you set the TEXTURE_ENV_MODE to DECAL , what you get is the texture overlaying the color:

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

You want MODULATE , which is the default, so just delete that line.

Note that MODULATE will multiply the color by the texture, so change your color to white, if you want the texture to show through at full brightness:

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

Change

glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);

to

glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,GL_REPLACE);

Just have to chime in here and tell you, that instead of ripping your hair with the fixed function texture environment you should simply abandon the whole fixed function mess and just use shaders. In a fragment shader you can exactly specify the way base color and texture are combined.

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