简体   繁体   English

OpenGL纹理透明度

[英]OpenGL Texture Transparency

I'm using C++ and OpenGL to make a basic 2D game, I have a png image with transparent areas for my player. 我正在使用C ++和OpenGL制作基本的2D游戏,我的播放器具有带透明区域的png图像。 It works perfectly on my laptop and lab computers, but on my desktop the entire image is mostly see through, 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? 设置glColor4f(1,1,1,1)有帮助吗? (I can't remember if GL_REPLACE is affected by vertex color) (我不记得GL_REPLACE是否受顶点颜色影响)

Check glGetError() at appropriate places to see if you're not doing anything really wrong. 在适当的地方检查glGetError(),看看您是否确实做错了什么。

Other generic tips: 其他通用提示:

  • try to lock down all loose ends of the render state. 尝试锁定渲染状态的所有松散末端。
  • make sure your PNG-read lib works correctly everywhere. 确保您的PNG读取的lib在任何地方都能正常工作。 (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. 它可能与硬件有关,如果您列出OS:es和CPU类型/驱动程序,那么它会有所帮助。
  • I'm assuming you're running the same executable on all computers? 我假设您在所有计算机上都运行相同的可执行文件?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM