OpenGL 中具有宽度和高度的矩形纹理 POT
我正在尝试在 OpenGL 中使用矩形纹理。当纹理的高度等于宽度时,一切看起来都很好,但是,当高度与宽度不同时,纹理看起来会扭曲。
我的显示函数是(h
和 w
是存储图像高度和宽度的全局变量):
请注意,绘制图像的大小并不重要。无论实际多边形大小如何,它都会扭曲。
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
glBindTexture(GL_TEXTURE_2D, texName);
glTranslatef(-2.0f,-2.0f,0.0f);
glScalef(1.0f/128.0f,1.0f/128.0f,1.0f);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0); glVertex3f(0.0, 0.0, 0.0);
glTexCoord2f(0.0, 1.0); glVertex3f(0.0, w, 0.0);
glTexCoord2f(1.0, 1.0); glVertex3f(h, w, 0.0);
glTexCoord2f(1.0, 0.0); glVertex3f(h, 0.0, 0.0);
// Will be distorted also with the following:
/*glScalef(1.0f/128.0f,1.0f/128.0f,1.0f);
glTexCoord2f(0.0, 0.0); glVertex3f(0.0, 0.0, 0.0);
glTexCoord2f(0.0, 1.0); glVertex3f(0.0, h, 0.0);
glTexCoord2f(1.0, 1.0); glVertex3f(w, h, 0.0);
glTexCoord2f(1.0, 0.0); glVertex3f(w, 0.0, 0.0);*/
glEnd();
glFlush();
glDisable(GL_TEXTURE_2D);
}
我正在加载纹理:
void *data = LoadBMP("c:\\dev\\64x128_face.bmp");
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glGenTextures(1, &texName);
glBindTexture(GL_TEXTURE_2D, texName);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w,
h, 0, GL_RGBA, GL_UNSIGNED_BYTE,
data);
当我加载 64x64
方形纹理图像时,它看起来不错。然而,当我加载矩形纹理图像时,图像看起来扭曲。
OpenGL如何支持矩形POT纹理?我的代码有什么问题吗?
I'm trying to use rectangular texture with OpenGL. When the height is equal to the width of the texture, everything looks fine, however, when the height is different than the width the texture looks distorted.
My display function is (h
and w
are globals storing the height and the width of the image):
Please note that the size of the drawn image doesn't matter. It is distorted regardless of the actual polygon size.
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
glBindTexture(GL_TEXTURE_2D, texName);
glTranslatef(-2.0f,-2.0f,0.0f);
glScalef(1.0f/128.0f,1.0f/128.0f,1.0f);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0); glVertex3f(0.0, 0.0, 0.0);
glTexCoord2f(0.0, 1.0); glVertex3f(0.0, w, 0.0);
glTexCoord2f(1.0, 1.0); glVertex3f(h, w, 0.0);
glTexCoord2f(1.0, 0.0); glVertex3f(h, 0.0, 0.0);
// Will be distorted also with the following:
/*glScalef(1.0f/128.0f,1.0f/128.0f,1.0f);
glTexCoord2f(0.0, 0.0); glVertex3f(0.0, 0.0, 0.0);
glTexCoord2f(0.0, 1.0); glVertex3f(0.0, h, 0.0);
glTexCoord2f(1.0, 1.0); glVertex3f(w, h, 0.0);
glTexCoord2f(1.0, 0.0); glVertex3f(w, 0.0, 0.0);*/
glEnd();
glFlush();
glDisable(GL_TEXTURE_2D);
}
I'm loading the texture with:
void *data = LoadBMP("c:\\dev\\64x128_face.bmp");
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glGenTextures(1, &texName);
glBindTexture(GL_TEXTURE_2D, texName);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w,
h, 0, GL_RGBA, GL_UNSIGNED_BYTE,
data);
When I'm loading a 64x64
square texture image, it looks fine. However when I'm loading a rectangular texture image, the image looks distorted.
How does OpenGL support rectangular POT texture? What's wrong with my code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的矩形图像是 64x128,但您使用以下命令渲染它:
其中 h 是高度 (=128),w (=64) 是宽度。
但是您将高度放在 x(即宽度)上,将宽度放在 y(即高度)上。
也许可以尝试这个:
Your rect image is 64x128 but you render it with these commands:
where h is height (=128) and w (=64) is width.
But you placed height on x (which is width) and width on y (which is height).
Maybe try this instead:
在 中的 h 和 w 使用非二次幂值之前,您可能应该检查 GL_ARB_texture_non_power_of_two 扩展的支持情况glTexImage2D(),因为指定任意高度和宽度仅对该扩展有效。
You should probably check the support of the GL_ARB_texture_non_power_of_two extension before you use non-power-of-two values for h and w in glTexImage2D(), because specifying arbitrary heights and widths is only valid with this extension.