其他 PC 上的 OpenGL 白色纹理
我使用 SDL + OpenGL 制作了这个小游戏。该游戏在我的电脑上运行良好,但在朋友的电脑上,他只是看到白框和黑屏。
我认为这可能是一个问题,因为我的纹理的维度不是 2 的幂。我无法更改纹理尺寸,因此经过一番搜索,我发现使用 GL_ARB_texture_non_power_of_two 会以某种方式强制(?)npot 纹理。但是,令我惊讶的是,白框之类的东西出现在我的电脑上,而且它们甚至没有消失在我的朋友身上。我无法理解问题所在。任何帮助将不胜感激。
代码:
numColors = images[i]->format->BytesPerPixel;
if ( numColors == 4 )
{
if (images[i]->format->Rmask == 0x000000FF)
textureFormat = GL_RGBA;
else
textureFormat = GL_BGRA;
}
else if ( numColors == 3 )
{
if (images[i]->format->Rmask == 0x000000FF)
textureFormat = GL_RGBA;
else
textureFormat = GL_BGRA;
}
glPixelStorei(GL_UNPACK_ALIGNMENT,4);
glGenTextures( 1, &textures[i] );
glBindTexture( GL_ARB_texture_non_power_of_two, textures[i] );
glTexParameteri(GL_ARB_texture_non_power_of_two,GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_ARB_texture_non_power_of_two,GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_ARB_texture_non_power_of_two, 0, numColors, images[i]->w, images[i]->h, 0, textureFormat, GL_UNSIGNED_BYTE, images[i]->pixels);
I've made this small game using SDL + OpenGL. The game runs fine on my PC, but on a friend's PC, he just gets white boxes and blank screen.
I thought it might be an issue due to my textures being non power of 2 in dimensions. I cannot change the texture dimensions, so after some searching, I found that using GL_ARB_texture_non_power_of_two would somehow force(?) npot textures. But, to my surprise, the white boxes and stuff appear on my PC and they aren't even gone on my friends. I'm unable to understand what the problem is. Any help would be greatly appreciated.
Code:
numColors = images[i]->format->BytesPerPixel;
if ( numColors == 4 )
{
if (images[i]->format->Rmask == 0x000000FF)
textureFormat = GL_RGBA;
else
textureFormat = GL_BGRA;
}
else if ( numColors == 3 )
{
if (images[i]->format->Rmask == 0x000000FF)
textureFormat = GL_RGBA;
else
textureFormat = GL_BGRA;
}
glPixelStorei(GL_UNPACK_ALIGNMENT,4);
glGenTextures( 1, &textures[i] );
glBindTexture( GL_ARB_texture_non_power_of_two, textures[i] );
glTexParameteri(GL_ARB_texture_non_power_of_two,GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_ARB_texture_non_power_of_two,GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_ARB_texture_non_power_of_two, 0, numColors, images[i]->w, images[i]->h, 0, textureFormat, GL_UNSIGNED_BYTE, images[i]->pixels);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您朋友的显卡可能不支持两个纹理的非幂,因此尽管使用 GL_ARB_texture_non_power_of_two 扩展,输出仍然错误。
如果您的游戏依赖于特定的 OpenGL 扩展来正确显示,您应该在启动时检查这些扩展,并告诉用户,如果他的硬件缺乏这些功能,他将无法运行游戏。
Your friend's video card may not support non power of two textures, therefore the output is still wrong despite using the GL_ARB_texture_non_power_of_two extension.
If your game relies on specific OpenGL extensions to display correctly, you should check for those extensions at start up and tell the user he can't run the game if his hardware is lacking the features.
不要使用 GL_ARB_texture_non_power_of_two 而不是 GL_TEXTURE_2D。只需检查扩展是否受支持,然后使用 glTexImage(GL_TEXTURE_2D, w, h, ...) 发送 NPOT 纹理。
调用 glGetError() 查看是否出现错误。您应该这样做,因为 GL_ARB_...npot 在您使用时不是有效值。
GL_ARB_NPOT 也用于 1D 和 3D 纹理。
Don't use GL_ARB_texture_non_power_of_two instead of GL_TEXTURE_2D. Just check if the extension is supported then send NPOT textures using glTexImage(GL_TEXTURE_2D, w, h, ...).
Call glGetError() to see if you're getting error. You should, since GL_ARB_...npot is not a valid value as you use it.
GL_ARB_NPOT is also used for 1D and 3D textures.
除了 ARB_texture_non_power_of_two 之外,还有另一个扩展:GL_ARB_texture_rectangle;很老了,GPU 已经支持它很多年了。使用它,您的代码看起来像
顺便说一句: GL_ARB_texture_non_power_of_two 是扩展名称,而不是用作纹理目标的有效标记; OpenGL 应该发出 GL_INVALID_ENUM 错误。
Additionally to ARB_texture_non_power_of_two there's also another extension: GL_ARB_texture_rectangle; quite old, it's been supported by GPUs for ages. Using that your code would look like
BTW: GL_ARB_texture_non_power_of_two is a extension name, not a valid token to be used as texture target; OpenGL should have issued an GL_INVALID_ENUM error.