在OpenGL中计算一个球体

5

我想计算所有需要的顶点并用直线连接它们,最终形成一个球体。有多少种方法可以实现这一目标?还有顶点之间的连线将是直线;如何使它们“弯曲”?我知道可以使用glutWireSphere(),但我对实际计算顶点感兴趣。我考虑的一种方法是手动将所有顶点放入数组中,但我猜这不是正确的方式。


4
请务必查看Andreas Kahler的著名精彩页面,网址为http://blog.andreaskahler.com/2009/06/creating-icosphere-mesh-in-code.html。该页面介绍了如何在代码中创建一个伊科球体网格。 - Fattie
1
你可以通过使用完全不同的范例——片段着色器中的距离函数,而非顶点,来绘制曲线基元。但这是严格的非初学者材料,所以请注意!=) - étale-cohomology
6个回答

16

我最初在使用Visual C++创建Opengl中的3D球问题中编写了一些代码,现在将其复制并粘贴。

class SolidSphere
{
protected
    std::vector<GLfloat> vertices;
    std::vector<GLfloat> normals;
    std::vector<GLfloat> texcoords;
    std::vector<GLushort> indices;

public:
    void SolidSphere(float radius, unsigned int rings, unsigned int sectors)
    {
        float const R = 1./(float)(rings-1);
        float const S = 1./(float)(sectors-1);
        int r, s;

        sphere_vertices.resize(rings * sectors * 3);
        sphere_normals.resize(rings * sectors * 3);
        sphere_texcoords.resize(rings * sectors * 2);
        std::vector<GLfloat>::iterator v = sphere_vertices.begin();
        std::vector<GLfloat>::iterator n = sphere_normals.begin();
        std::vector<GLfloat>::iterator t = sphere_texcoords.begin();
        for(r = 0; r < rings; r++) for(s = 0; s < sectors; s++) {
                float const y = sin( -M_PI_2 + M_PI * r * R );
                float const x = cos(2*M_PI * s * S) * sin( M_PI * r * R );
                float const z = sin(2*M_PI * s * S) * sin( M_PI * r * R );

                *t++ = s*S;
                *t++ = r*R;

                *v++ = x * radius;
                *v++ = y * radius;
                *v++ = z * radius;

                *n++ = x;
                *n++ = y;
                *n++ = z;
        }

        sphere_indices.resize(rings * sectors * 4);
        std:vector<GLushort>::iterator i = sphere_indices.begin();
        for(r = 0; r < rings; r++) for(s = 0; s < sectors; s++) {
                *i++ = r * sectors + s;
                *i++ = r * sectors + (s+1);
                *i++ = (r+1) * sectors + (s+1);
                *i++ = (r+1) * sectors + s;
        }
    }

    void draw(GLfloat x, GLfloat y, GLfloat z)
    {
        glMatrixMode(GL_MODELVIEW);
        glPushMatrix();
        glTranslatef(x,y,z);

        glEnableClientState(GL_VERTEX_ARRAY);
        glEnableClientState(GL_NORMAL_ARRAY);
        glEnableClientState(GL_TEXTURE_COORD_ARRAY);

        glVertexPointer(3, GL_FLOAT, 0, &sphere_vertices[0]);
        glNormalPointer(GL_FLOAT, 0, &sphere_normals[0]);
        glTexCoordPointer(2, GL_FLOAT, 0, &sphere_texcoords[0]);
        glDrawElements(GL_QUADS, sphere_indices.size()/4, GL_UNSIGNED_SHORT, sphere_indices);
        glPopMatrix();
    }
}

如何让它们“弯曲”?

无法做到。所有OpenGL基元都是“仿射”的,即平面或直线。通过以足够的分辨率绘制短直线段来模拟曲率。


1
你的代码生成的球体出问题了:UV 贴图错误,有些顶点也不对。我已经试过用三角带了。 - Michael IV
5
@MichaelIV说:球体不适用于三角带。 - datenwolf
1
你没有正确粘贴它:索引部分应该停止在 sector-1 和 rings-1。你的另一篇帖子是正确的。实际上,复制粘贴可能会更安全! - N0vember

4

Paul Bourke有一篇关于球体生成的详细介绍。至于曲线,在OpenGL中并不存在。你只能通过添加更多的中间连接点来使它们看起来弯曲。


4

有不止一种方法可以实现这个目标:a) icosphere生成和b) UV sphere生成。可能还有其他方法可以实现。通过一些搜索,我找到了这篇关于icosphere生成的优秀文章。但是我没有找到UV sphere生成的方法。


1
没错,Kahler的网页http://blog.andreaskahler.com/2009/06/creating-icosphere-mesh-in-code.html是绝对的经典 - 真是太棒了。 - Fattie

0

datenwolf的回答很好,但是包含一些错误。当您使用vbo时,在启用后必须禁用客户端状态。

在绘制代码中添加三行

void draw(GLfloat x, GLfloat y, GLfloat z)
{
    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();
    glTranslatef(x,y,z);

    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_NORMAL_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);

    glVertexPointer(3, GL_FLOAT, 0, &sphere_vertices[0]);
    glNormalPointer(GL_FLOAT, 0, &sphere_normals[0]);
    glTexCoordPointer(2, GL_FLOAT, 0, &sphere_texcoords[0]);
    glDrawElements(GL_QUADS, sphere_indices.size()/4, GL_UNSIGNED_SHORT, sphere_indices);

    **glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_NORMAL_ARRAY);
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);**

    glPopMatrix();
}

0

在我的印象中,关于如何制作icospheres的教程非常丰富,但使用极坐标进行面片逼近的方法却不多。

这里有一个来自Richard S. Wright Jr.的《OpenGL Superbible 4th Edition》书籍的代码示例,经过了微小的编辑。

由于它是一个非常基本的固定功能管线使用(没有glDrawElements等...),我发现它很有用于教育目的。

堆栈被绘制成一系列三角形条带。显然不是最佳性能,但它可以工作!

// For best results, put this in a display list
// Draw a sphere at the origin
void RenderSphere(const float fRadius, const int iStacks, const int iSlices)
    {
        const auto PI = (float)M_PI;
        const auto PIx2 = (float)(M_PI * 2.0);
    
        GLfloat drho = PI / (GLfloat)iStacks;
        GLfloat dtheta = PIx2 / (GLfloat)iSlices;
        GLfloat ds = 1.0f / (GLfloat)iSlices;
        GLfloat dt = 1.0f / (GLfloat)iStacks;
        GLfloat t = 1.0f;
        GLfloat s = 0.0f;
    
        for (int i = 0; i < iStacks; i++)
        {
            const GLfloat rho = (GLfloat)i * drho;
            const GLfloat srho = (GLfloat)(std::sinf(rho));
            const GLfloat crho = (GLfloat)(std::cosf(rho));
            const GLfloat srhodrho = (GLfloat)(std::sinf(rho + drho));
            const GLfloat crhodrho = (GLfloat)(std::cosf(rho + drho));
    
            // Many sources of OpenGL sphere drawing code uses a triangle fan
            // for the caps of the sphere. This however introduces texturing
            // artifacts at the poles on some OpenGL implementations
            glBegin(GL_TRIANGLE_STRIP);
            s = 0.0f;
            for (int j = 0; j <= iSlices; j++)
            {
                const GLfloat theta = (j == iSlices) ? 0.0f : j * dtheta;
                const GLfloat stheta = (GLfloat)(-std::sinf(theta));
                const GLfloat ctheta = (GLfloat)(std::cosf(theta));
    
                GLfloat x = stheta * srho;
                GLfloat y = ctheta * srho;
                GLfloat z = crho;
    
                glTexCoord2f(s, t);
                glNormal3f(x, y, z);
                glVertex3f(x * fRadius, y * fRadius, z * fRadius);
    
                x = stheta * srhodrho;
                y = ctheta * srhodrho;
                z = crhodrho;
                glTexCoord2f(s, t - dt);
                s += ds;
                glNormal3f(x, y, z);
                glVertex3f(x * fRadius, y * fRadius, z * fRadius);
            }
            glEnd();
    
            t -= dt;
        }
    }

很遗憾,我无法找回这个源代码的原始在线仓库链接,它相当古老。如果您知道在哪里可以找到它,请随时发布!


0

一个图标球可能会有用。但是,要制作一个球形,您将不得不细分它的三角形。


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