在OpenGL中绘制具有线性渐变纹理的圆柱体
如何在 OpenGL 中绘制一个圆柱体,该圆柱体具有线性渐变纹理,其方向类似于本示例图像中的纹理 (链接到图像)?
我使用 gluCylinder() 来绘制圆柱体。但我很难获得正确的纹理。
glEnable(GL_TEXTURE_1D);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_1D, texture);
GLubyte gradient[6] = { 255,0,0, 0,255,0 };
glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA8, 2, 0, GL_RGB, GL_UNSIGNED_BYTE, gradient);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
gluQuadricDrawStyle(quad,GLU_FILL);
gluQuadricTexture(quad,GL_TRUE);
gluCylinder(quad,1.0f,1.0f,3.0f,32,32);
这会产生一个渐变,但方向错误: screenshot
如何获得正确的方向?
How can I draw a cylinder in OpenGL which has a linear gradient texture that is oriented like the one in this example image (link to image)?
I use gluCylinder() to draw the cylinder. But I struggle to get the texture right.
glEnable(GL_TEXTURE_1D);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_1D, texture);
GLubyte gradient[6] = { 255,0,0, 0,255,0 };
glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA8, 2, 0, GL_RGB, GL_UNSIGNED_BYTE, gradient);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
gluQuadricDrawStyle(quad,GLU_FILL);
gluQuadricTexture(quad,GL_TRUE);
gluCylinder(quad,1.0f,1.0f,3.0f,32,32);
This produces a gradient, but it has the wrong orientation: screenshot
How can I get the correct orientation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要提供一些纹理坐标。我不记得 gluCylinder 生成哪个纹理坐标(如果有的话)。但在您的情况下,旧式 OpenGL 纹理坐标生成应该可以为您服务。 gluCylinder 沿着 Z 轴,因此我们需要纹理坐标映射 (0,0,0)→(0) 到 (0,0,heigt)→(1)。
OpenGL 最高版本 2 提供纹理坐标生成,因此我建议在调用 gluCylinder(而不是 gluQuadricTexture)之前添加以下内容。
You need to supply some texture coordinates. I don't remember which texture coordinates gluCylinder generates, if any at all. But in your case old style OpenGL texture coordinate generation should serve you. gluCylinder goes along the Z axis, so we need texture coordinates mapping (0,0,0)→(0) to (0,0,heigt)→(1).
OpenGL up to version 2 provides texture coordinate generation, so I suggest the following to add before calling gluCylinder, instead of gluQuadricTexture.