四边形上的纹理映射不起作用

发布于 2024-12-23 07:29:21 字数 543 浏览 0 评论 0原文

我正在尝试编写一些代码,将纹理渲染到一个简单的正方形上,但在让它工作时遇到了很多麻烦。我在此处下载了一些纹理映射示例源代码并对其进行了编译似乎在圆柱体上进行了精细的纹理映射。然后,我添加了一些代码,看看它是否会将纹理渲染到我的正方形上,但它只是使用纯色对其进行纹理处理,该颜色似乎介于红色和黑色之间,而不是像圆柱体上显示的棋盘格一样。这是我在第一遍调用 gluCylinder 之后添加的代码:

glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex2f(-1, -1);
glTexCoord2f(1,0);
glVertex2f(1, -1);
glTexCoord2f(1, 1);
glVertex2f(1,  1);
glTexCoord2f(0,1);
glVertex2f(-1,  1);
glEnd();

是否有任何原因导致纹理在圆柱体上正常渲染而不是在我的四边形上渲染?

I'm trying to write some code that will render a texture onto a simple square, but have been having a lot of trouble getting it to work. I downloaded some example source code for texture mapping here and compiled it and it seems to do texture mapping fine onto their cylinder. Then, I added some of my code to see if it would render the texture onto my square, but it just textures it with a solid color that seems to be halfway between red and black instead of a checkerboard like it appears on the cylinder. Here is the code I added, right after the call to gluCylinder on the first pass:

glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex2f(-1, -1);
glTexCoord2f(1,0);
glVertex2f(1, -1);
glTexCoord2f(1, 1);
glVertex2f(1,  1);
glTexCoord2f(0,1);
glVertex2f(-1,  1);
glEnd();

Is there any reason why textures would render normally on the cylinder but not on my quad?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

您的好友蓝忘机已上羡 2024-12-30 07:29:21

示例代码启用自动纹理坐标生成,因此您的 glTexCoord2f 调用将被忽略。自动生成被投影到 Z 平面上。由于您的四边形是 Y 平面,因此它本质上只是在整个四边形上拉伸纹理的一个像素。

要使其正常工作,请在 glBegin 之前禁用自动纹理生成,然后在 glEnd 之后重新启用它。

即:

glDisable(GL_TEXTURE_GEN_S);
glDisable(GL_TEXTURE_GEN_T);

glBegin(GL_QUADS);
   glTexCoord2f(0, 0);
   glVertex2f(-1, -1);
   glTexCoord2f(1,0);
   glVertex2f(1, -1);
   glTexCoord2f(1, 1);
   glVertex2f(1,  1);
   glTexCoord2f(0,1);
   glVertex2f(-1,  1);
glEnd();

glEnable (GL_TEXTURE_GEN_S);
glEnable (GL_TEXTURE_GEN_T);

或者,您可以将自动纹理生成设置为在 Y 平面上投影,并省略 glTexCoord2f 调用,然后在完成后将其设置回 Z:

float zPlane[4] = {0., 0., 1., 0.};
float yPlane[4] = {0., 1., 0., 0.};
glTexGenfv (GL_T, GL_OBJECT_PLANE, yPlane);

glBegin(GL_QUADS);
   glVertex2f(-1, -1);
   glVertex2f(1, -1);
   glVertex2f(1,  1);
   glVertex2f(-1,  1);
glEnd();

glTexGenfv (GL_T, GL_OBJECT_PLANE, zPlane);

The example code is enabling automatic texture coordinate generation, so your glTexCoord2f calls are being ignored. The automatic generation is being projected onto a Z plane. Since your quad is a Y plane, it's essentially just stretching one pixel of your texture across the entire quad.

To get it to work, disable the automatic texture generation before your glBegin, and then re-enable it after the glEnd.

i.e.:

glDisable(GL_TEXTURE_GEN_S);
glDisable(GL_TEXTURE_GEN_T);

glBegin(GL_QUADS);
   glTexCoord2f(0, 0);
   glVertex2f(-1, -1);
   glTexCoord2f(1,0);
   glVertex2f(1, -1);
   glTexCoord2f(1, 1);
   glVertex2f(1,  1);
   glTexCoord2f(0,1);
   glVertex2f(-1,  1);
glEnd();

glEnable (GL_TEXTURE_GEN_S);
glEnable (GL_TEXTURE_GEN_T);

Alternatively, you could set the automatic texture generation to project on the Y plane, and omit the glTexCoord2f calls, and then set it back to Z when you're done:

float zPlane[4] = {0., 0., 1., 0.};
float yPlane[4] = {0., 1., 0., 0.};
glTexGenfv (GL_T, GL_OBJECT_PLANE, yPlane);

glBegin(GL_QUADS);
   glVertex2f(-1, -1);
   glVertex2f(1, -1);
   glVertex2f(1,  1);
   glVertex2f(-1,  1);
glEnd();

glTexGenfv (GL_T, GL_OBJECT_PLANE, zPlane);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文