Tyl*_*Cox 2 c++ opengl transparency texture-mapping
我想从包含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);
}
Run Code Online (Sandbox Code Playgroud)
侧面信息 我已经看到,加载纹理的方式也可能是问题的罪魁祸首.我也会把它包括在内.这是我从课堂上得到的代码,这就是我使用.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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
当您设置TEXTURE_ENV_MODE到DECAL,你得到的是纹理覆盖的颜色:
// This is not what you want
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
Run Code Online (Sandbox Code Playgroud)
你想要的MODULATE,这是默认的,所以只需删除该行.
请注意MODULATE,如果您希望纹理以全亮度显示,则将颜色乘以纹理,因此将颜色更改为白色:
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
Run Code Online (Sandbox Code Playgroud)