带旋转纹理的gluCylinder [英] gluCylinder with rotated texture

查看:314
本文介绍了带旋转纹理的gluCylinder的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用gluQuadric和gluCylinder绘制一个圆柱体. 该圆柱应为带纹理的.

I want to draw a cylinder using gluQuadric and gluCylinder. This cylinder shall be textured.

我的绘画代码如下:

pTexture->Enable();
pTexture->Bind();
glPushMatrix();
glRotatef(-90.0f, 1.0f, 0.0f, 0.0f);

gluQuadricOrientation(quadric, GLU_OUTSIDE);
gluQuadricNormals(quadric, true);
gluQuadricTexture(quadric, true);
gluCylinder(quadric, getRadius(), getRadius(), getHeight(), 16, 1);

glPopMatrix();
pTexture->Unbind();
pTexture->Disable();

我现在的问题是,纹理旋转了90度.如何旋转二次曲面的uv映射?

My problem with this is now, that the texture is rotated 90 degrees. How can I rotate the uv-mapping of the quadric?

纹理在其他地方使用,因此无法进行编辑.

The texture is used in other places and thus cannot be edited.

推荐答案

除了使用更广泛的GL_MODELVIEWGL_PROJECTION矩阵堆栈外,还有一个GL_TEXTURE矩阵堆栈可用于应用转换贴图坐标.例如,这会将纹理坐标旋转90度:

In addition to the more widely used GL_MODELVIEW and GL_PROJECTION matrix stacks, there is also a GL_TEXTURE matrix stack that can be used to apply transformations to texture coordinates. For example, this will rotate the texture coordinates by 90 degrees:

glMatrixMode(GL_TEXTURE);
glRotatef(90.0f, 0.0f, 0.0f, 1.0f);
glMatrixMode(GL_MODELVIEW);

由于纹理坐标通常为[0.0,1.0] x [0.0,1.0]单位正方形,因此仅使用它可能会产生不良的副作用.旋转是围绕原点逆时针旋转的,因此我们的单位正方形将被旋转为一个正方形,其范围为[-1.0,0.0] x [0.0,1.0].因此,除了旋转外,平方也偏移了(0.0,-1.0).如果换行模式为GL_REPEAT,这是无害的,但在使用像GL_CLAMP_TO_EDGE这样的换行模式时会很糟糕.我们可以通过在旋转后沿相反方向应用平移来对此进行更正(请记住,最后指定的转换是最先应用的转换):

Since texture coordinates are normally in a [0.0, 1.0] x [0.0, 1.0] unit square, using only this might have undesired side effects. The rotation is counter-clockwise around the origin, so our unit square will be rotated to a square with an extent of [-1.0, 0.0] x [0.0, 1.0]. So in addition to the rotation, the square was shifted by (0.0, -1.0). This is harmless if the wrap mode is GL_REPEAT, but would bad when using a wrap mode like GL_CLAMP_TO_EDGE. We can correct for this by applying a translation in the opposite direction after the rotation (remember that the transformation specified last is the one applied first):

glMatrixMode(GL_TEXTURE);
glTranslatef(1.0f, 0.0f, 0.0f);
glRotatef(90.0f, 0.0f, 0.0f, 1.0f);
glMatrixMode(GL_MODELVIEW);

此外,根据图像的朝向,您可能必须旋转-90度.在这种情况下,范围为[0.0,1.0] x [0.0,1.0]的单位正方形将旋转为范围为[0.0,1.0] x [-1.0,0.0]的正方形.在这种情况下应用相同类型的纠正翻译,我们最终得到:

Also, depending on what orientation your image is, you may have to rotate by -90 degrees instead. In this case, the unit square with extent [0.0, 1.0] x [0.0, 1.0] will be rotated into a square with extent [0.0, 1.0] x [-1.0, 0.0]. Applying the same type of corrective translation for this case, we end up with:

glMatrixMode(GL_TEXTURE);
glTranslatef(0.0f, 1.0f, 0.0f);
glRotatef(-90.0f, 0.0f, 0.0f, 1.0f);
glMatrixMode(GL_MODELVIEW);

这篇关于带旋转纹理的gluCylinder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆