四边形上的纹理映射不起作用
我正在尝试编写一些代码,将纹理渲染到一个简单的正方形上,但在让它工作时遇到了很多麻烦。我在此处下载了一些纹理映射示例源代码并对其进行了编译似乎在圆柱体上进行了精细的纹理映射。然后,我添加了一些代码,看看它是否会将纹理渲染到我的正方形上,但它只是使用纯色对其进行纹理处理,该颜色似乎介于红色和黑色之间,而不是像圆柱体上显示的棋盘格一样。这是我在第一遍调用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
示例代码启用自动纹理坐标生成,因此您的 glTexCoord2f 调用将被忽略。自动生成被投影到 Z 平面上。由于您的四边形是 Y 平面,因此它本质上只是在整个四边形上拉伸纹理的一个像素。
要使其正常工作,请在 glBegin 之前禁用自动纹理生成,然后在 glEnd 之后重新启用它。
即:
或者,您可以将自动纹理生成设置为在 Y 平面上投影,并省略 glTexCoord2f 调用,然后在完成后将其设置回 Z:
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.:
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: