gluCylinder with rotated texture

ぃ、小莉子 提交于 2019-12-13 04:55:49

问题


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

My draw code is the following:

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();

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.


回答1:


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);

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);

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);


来源:https://stackoverflow.com/questions/24556114/glucylinder-with-rotated-texture

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!