OpenGL中用三角形绘制的圆柱体

3

这是使用堆栈和边绘制圆柱体的方法,但问题在于堆栈连接到同一点而不是一个新点。

也许图片会更好地说明:

Cylinder

以下是我如何渲染侧面的方式,因为圆盘是分开渲染的:

for (int i = 1; i <= height; ++i) {
        for (int j = 0; j < edges; ++j) {
            glBegin(GL_TRIANGLES); {
                // 0 bottom 
                glVertex3f(x + radius * cos(theta + interval), y , z + radius * sin(theta + interval));
                // 1 bottom
                glVertex3f(x + radius * cos(theta), y + y_value * i, z + radius * sin(theta));
                // 2 top
                glVertex3f(x + radius * cos(theta), y + y_value * i, z + radius * sin(theta));
                // 2 top
                glVertex3f(x + radius * cos(theta), y + y_value * i, z + radius * sin(theta));
                // 3 top
                glVertex3f(x + radius * cos(theta + interval), y + y_value * i, z + radius * sin(theta + interval));
                // 0 bottom 
                glVertex3f(x + radius * cos(theta + interval), y , z + radius * sin(theta + interval));
            } glEnd();
            theta += interval;
        }
        theta = 0.0;
    }

我已经尝试解决这个问题好几天了,但是我已经没有任何想法了。你知道我做错了什么吗?
更新: 我已经按照ybungalobill的建议使用四边形进行呈现。现在我正在努力进行UV映射。希望一旦解决了这个部分,将很容易转换为三角形。 这是我现在拥有的: enter image description here 这是我用于UV映射的代码:
u = 0.0,
v = 0.0,
u_inter = 1.0 / edges,
v_inter = 1.0 / y_value;  // (y_value = height / edges)

    for (int i = 1; i <= height; ++i) {
        for (int j = 0; j < edges; ++j) {
            glBegin(GL_QUAD_STRIP); {
                // 0 bottom 
                glTexCoord2f(u, v);
                // 1 bottom
                glTexCoord2f(u + u_inter, v);
                // 2 top
                glTexCoord2f(u + u_inter, v + v_inter);
                // 3 top
                glTexCoord2f(u, v + v_inter);
            } glEnd();
            theta += interval;
            u += u_inter;
        }
        v += v_inter;
        theta = 0.0;
    }
1个回答

2
float y0 = y + y_value * (i-1);
float y1 = y + y_value * i;
// 0 bottom 
glVertex3f(x + radius * cos(theta + interval), y0, z + radius * sin(theta + interval));
// 1 bottom
glVertex3f(x + radius * cos(theta), y0, z + radius * sin(theta));
// 2 top
glVertex3f(x + radius * cos(theta), y1, z + radius * sin(theta));
// 2 top
glVertex3f(x + radius * cos(theta), y1, z + radius * sin(theta));
// 3 top
glVertex3f(x + radius * cos(theta + interval), y1, z + radius * sin(theta + interval));
// 0 bottom 
glVertex3f(x + radius * cos(theta + interval), y0, z + radius * sin(theta + interval));

实际上,我不得不在 // 1 底部将 y0 更改为 y1,但我认为我理解你的意思了。 - matzar
很奇怪,因为你应该在顶部有三个顶点,在底部也应该有三个。 - Yakov Galka
是的,你说得对,它没有很好地映射。我已经将其更改为四边形并按照你所说的方式进行了映射,现在它可以工作了。现在我正在努力进行UV映射。 - matzar
我已经更新了,如果您想看一下,请查看。 - matzar
1
如果您之前的问题已经解决,请避免更改问题。相反,接受当前问题的答案,并用新问题提出一个新的问题。 - Yakov Galka
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接