简体   繁体   English

如何在OpenGL中为GL_TRIANGLE_STRIP创建的圆柱体做纹理映射以及如何使圆柱体变细

[英]How to do texture mapping for cylinder created by GL_TRIANGLE_STRIP in OpenGL and how to taper a cylinder

I am currently trying to create a model of squidward from spongebob squarepant's house.我目前正在尝试从海绵宝宝的房子中创建一个鱿鱼模型。

I have created a cyllinder out of GL_TRIANGLE_STRIPs as I am not allowed to use any predefined OpenGL models to create the shapes.我已经用 GL_TRIANGLE_STRIPs 创建了一个圆柱体,因为我不允许使用任何预定义的 OpenGL 模型来创建形状。

I am trying to do texture mapping for each triangle on the cylinder but the texture comes out stretched and not as it is supposed to be.我正在尝试为圆柱体上的每个三角形进行纹理映射,但纹理被拉伸而不是像它应该的那样。

Here is my code for the cylinder这是我的气缸代码

glPushMatrix();
    glTranslated(xPos, yPos, TABLETOP_Z - cubeLen);
    glScaled(cubeLen / 2, cubeLen / 2, 1.0);
        
    glBegin(GL_TRIANGLE_STRIP);
        glTexCoord3f(xPos, yPos, TABLETOP_Z);
        glTexCoord3f(xPos, yPos, TABLETOP_Z);

        for (int i = 0; i <= 32; i++) {
            double x_next = 1.0 * cos((i + 1) * 2.0 * PI/ (32 - 2.0));
            double y_next = 1.0 * sin((i + 1) * 2.0 * PI / (32 - 2.0));

            if (i % 2 == 0) {
                glTexCoord3f(x_next, y_next, TABLETOP_Z + cubeLen);
                glVertex3f(x_next, y_next, TABLETOP_Z + cubeLen);

            } else {
                glTexCoord3f(x_next, y_next, TABLETOP_Z);
                glVertex3f(x_next, y_next, TABLETOP_Z);
            }
        }

    glEnd();
    glPopMatrix();

And here is what the texture is supposed to look like:这是纹理应该是什么样子: 在此处输入图片说明

And here is what it looks like on the cylinder这是圆柱体上的样子在此处输入图片说明

The texture coordinates are 2-dimenional and have to be in range [0.0, 1.0]:纹理坐标是二维的,必须在 [0.0, 1.0] 范围内:

glBegin(GL_TRIANGLE_STRIP);
for (int i = 0; i <= 32; i++) {
    double x_next = 1.0 * cos((i + 1) * 2.0 * PI/ (32 - 2.0));
    double y_next = 1.0 * sin((i + 1) * 2.0 * PI / (32 - 2.0));

    if (i % 2 == 0) {
        glTexCoord3f((float)i / 32.0f, 1.0f);
        glVertex3f(x_next, y_next, TABLETOP_Z + cubeLen);

    } else {
        glTexCoord3f((float)i / 32.0f, 0.0f);
        glVertex3f(x_next, y_next, TABLETOP_Z);
    }
}
glEnd();

See How do opengl texture coordinates work?请参阅opengl 纹理坐标如何工作?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM