实现opengl sphere示例代码

时间:2012-12-29 10:50:56

标签: c++ opengl

尝试实现一个球体示例代码I found here on stackoverflow我遇到了问题。请注意,我没有使用绘图代码,我使用此示例中的void SolidSphere函数来创建我的顶点数据。

这是我的代码,对我所提到的示例进行了一些细微的更改:

std::vector<GLfloat> vertices;
std::vector<GLfloat> normals;
std::vector<GLfloat> texcoords;
std::vector<GLushort> indices;

    Sphere(shared_ptr<GL::Shader> _shader, shared_ptr<Material> _material, float radius, unsigned int rings, unsigned int sectors)
{
    this->attachShader(_shader);
    this->attachMaterial(_material);

    /** Implementation from:
    https://stackoverflow.com/questions/5988686/how-do-i-create-a-3d-sphere-in-opengl-using-visual-c?lq=1
    **/

    float const R = 1./(float)(rings-1);
    float const S = 1./(float)(sectors-1);
    int r, s;

    vertices.resize(rings * sectors * 3);
    //normals.resize(rings * sectors * 3);
    texcoords.resize(rings * sectors * 2);
    std::vector<GLfloat>::iterator v = vertices.begin();
    //std::vector<GLfloat>::iterator n = sphere_normals.begin();
    std::vector<GLfloat>::iterator t = 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;
        }
    }



    indices.resize(rings * sectors * 4);
    std::vector<GLushort>::iterator i = 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;
        }
    }


           /************* THIS IS THE ADDED PART ********/
    for (unsigned int i = 0; i < vertices.size(); i += 3) {
        vertexCoords.push_back(glm::vec4(vertices[i], vertices[i+1], vertices[i+2], 1));
    }

    for (unsigned int i = 0; i < texcoords.size(); i += 2) {
        textureCoords.push_back(glm::vec2(texcoords[i], texcoords[i+1]));
    }
    indexElements = indices;

    cout << "Vertex vector size: " << vertexCoords.size() << endl;
    cout << "Texture vector size: " << textureCoords.size() << endl;
    cout << "Element index vector size: " << indexElements.size() << endl;
}

这里唯一的重大变化是使用M_PI / 2而不是M_PI_2以及两个将值推送到我的代码使用的格式的循环:

vector <glm::vec4> vertexCoords;
vector <glm::vec2> textureCoords;
vector <GLushort> indexElements;

这是我正在渲染的图片:

enter image description here

所以我看到球体顶部的最后一行......

我在这段代码中做错了可能会导致这种情况吗?

3 个答案:

答案 0 :(得分:2)

在这个循环中:

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

您正在访问r+1s+1,当r = rings - 1s = sectors - 1(即您的循环的最后一次迭代)将比您可用的数据高一时。

这将指向未初始化的内存区域。这可能包含任何内容,但在这种情况下,它似乎包含零,这将解释到原点的行。

在绘制“下一个”四边形时,您只需要在一次迭代中停止这些循环:

for(r = 0; r < rings-1; r++) {
    for(s = 0; s < sectors-1; s++) {

答案 1 :(得分:1)

您只有rings*sectors个顶点,但您的指数上升到(rings-1+1)*sectors+(sectors-1+1) =环*扇区+扇区。这是多少sectors个顶点数。

答案 2 :(得分:1)

问题是边界的输出索引。尝试修复这部分代码

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