【问题标题】:Cylinder drawn with triangles in OpenGL在OpenGL中用三角形绘制的圆柱体
【发布时间】:2016-11-22 02:52:09
【问题描述】:

它正在绘制一个带有堆栈和边缘的圆柱体,但问题是堆栈连接到一个点而不是一个新点。 也许一张图片会更好地显示它:

这是我渲染侧面的方式,因为磁盘是单独渲染的:

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 贴图而苦苦挣扎。希望一旦这部分得到解决,它就可以很容易地转换成三角形。 这就是我现在所拥有的:

这就是我用于 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;
    }

【问题讨论】:

    标签: opengl procedural-generation


    【解决方案1】:
    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 但我想我明白你在那里做了什么。
    • 这很奇怪,因为你应该在顶部和底部有三个顶点。
    • 是的,你是对的,它没有很好地映射。我已将其更改为四边形并按照您所说的方式对其进行映射,现在它正在工作。现在我正在为 UV 映射而苦苦挣扎。
    • 如果你想看看,我已经更新了。
    • 如果您之前的问题已经解决了,请避免换题。而是接受当前问题的答案并针对新问题提出新问题。
    猜你喜欢
    • 2014-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-08
    • 2015-05-26
    相关资源
    最近更新 更多