calculating a sphere in opengl

前端 未结 5 1348
时光说笑
时光说笑 2020-12-05 16:35

I want to calculate all the vertices needed and connect them with lines, so I eventually come up with a sphere. How many ways are there to do it? And also the lines between

相关标签:
5条回答
  • 2020-12-05 17:19

    Paul Bourke actually has a nice introduction to sphere generation. And as for curved lines, there is no such thing in OpenGL. You can only make them appear curved by adding more intermediate connected points.

    0 讨论(0)
  • 2020-12-05 17:19

    An iconosphere would do the trick . Still , to make a sphere with it , you will have to subdivide it's triangles.

    0 讨论(0)
  • 2020-12-05 17:27

    datenwolf's answer is great but contains some error. When you use vbo, client states must be disabled after enabled.

    Add Three lines to draw code

    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 讨论(0)
  • 2020-12-05 17:29

    Copy and Pasting some code I originally wrote in Creating a 3D sphere in Opengl using Visual C++

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

    how can I make them "curved"

    You can't. All OpenGL primitives are "affine", i.e. planar or straight. Curvature is emulated by drawing short, straight sections with sufficient resolution.

    0 讨论(0)
  • 2020-12-05 17:34

    There's more than one way to do this: a) icosphere generation and b)UV sphere generation. There may be more methods to do this. Some googling got me this excellent post on icosphere generation. I couldn't find UV sphere generation method though.

    0 讨论(0)
提交回复
热议问题