开发者

OpenGL Texture Transparency

开发者 https://www.devze.com 2022-12-21 05:00 出处:网络
I\'m using C++ and OpenGL to make a basic 2D game, I have a png image with transparent areas for my player. It works perfectly on my laptop and lab computers, but on my desktop the entire image is mos

I'm using C++ and OpenGL to make a basic 2D game, I have a png image with transparent areas for my player. It works perfectly on my laptop and lab computers, but on my desktop the entire image is mostly see th开发者_JAVA百科rough, not just the areas that are meant to be. What could cause/fix this?

Here is the code I've used and is the same on all machines

glPushMatrix(); 
glEnable(GL_TEXTURE_2D);    
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBindTexture(GL_TEXTURE_2D, playerTex);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

glTranslatef(XPos, YPos, 0.0);
glRotatef(heading, 0,0,1);
    glBegin(GL_POLYGON);
        glTexCoord2f(0.0, 1.0); glVertex2f(-40,40);
        glTexCoord2f(0.0, 0.0); glVertex2f(-40,-40);
        glTexCoord2f(1.0, 0.0); glVertex2f(40,-40);
        glTexCoord2f(1.0, 1.0); glVertex2f(40,40);
    glEnd();

glDisable(GL_BLEND);
glDisable(GL_TEXTURE_2D);
glPopMatrix();


I found the problem, I changed

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);

to

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

and it works correctly, not sure why though.


Does setting glColor4f(1,1,1,1) help? (I can't remember if GL_REPLACE is affected by vertex color)

Check glGetError() at appropriate places to see if you're not doing anything really wrong.

Other generic tips:

  • try to lock down all loose ends of the render state.
  • make sure your PNG-read lib works correctly everywhere. (create texture data in code otherwise)
  • It might be hardware related, and then it helps if you list the OS:es and CPU types/drivers.
  • I'm assuming you're running the same executable on all computers?
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号