其他分享
首页 > 其他分享> > c – OpenGL VBOs:绘制一个球体

c – OpenGL VBOs:绘制一个球体

作者:互联网

我在绘制一个球体时遇到了一些麻烦.

std::vector<GLfloat> ballVerts;

for(int i = 0; i <= 40; i++)
{
    double lat0 = M_PI * (-0.5 + (double) (i - 1) / 40);
    double z0  = sin(lat0);
    double zr0 =  cos(lat0);

    double lat1 = M_PI * (-0.5 + (double) i / 40);
    double z1 = sin(lat1);
    double zr1 = cos(lat1);

    for(int j = 0; j <= 40; j++)
    {
        double lng = 2 * M_PI * (double) (j - 1) / 40;
        double x = cos(lng);
        double y = sin(lng);

        ballVerts.push_back(x * zr0); //X
        ballVerts.push_back(y * zr0); //Y
        ballVerts.push_back(z0);      //Z

        ballVerts.push_back(0.0f); 
        ballVerts.push_back(1.0f); 
        ballVerts.push_back(0.0f); 
        ballVerts.push_back(1.0f); //R,G,B,A

        ballVerts.push_back(x * zr1); //X
        ballVerts.push_back(y * zr1); //Y
        ballVerts.push_back(z1);      //Z

        ballVerts.push_back(0.0f); 
        ballVerts.push_back(1.0f); 
        ballVerts.push_back(0.0f); 
        ballVerts.push_back(1.0f); //R,G,B,A
    }
}

glGenBuffers(1, &ballVbo);
glBindBuffer(GL_VERTEX_ARRAY, ballVbo);

GLuint sphereSize = 3200*7*4; //3200 vertixes * 7 floats
glBufferData(GL_VERTEX_ARRAY,sphereSize, &ballVerts, GL_STATIC_DRAW);



/*
    Draw a ball
*/
glBindBuffer(GL_VERTEX_ARRAY, ballVbo);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 7*4, 0);
glEnableClientState(GL_COLOR_ARRAY);
glColorPointer(4, GL_FLOAT, 7*4, (void*)(3*4));

glDrawArrays(GL_TRIANGLE_STRIP, 0, 3200);

glBindBuffer(GL_ARRAY_BUFFER, 0);

生成球体的代码在立即模式下工作正常,但是当我把它放在VBO中时,我不断从glDrawArrays中获得访问冲突异常.有什么建议?

解决方法:

glBufferData(GL_VERTEX_ARRAY,sphereSize, &ballVerts, GL_STATIC_DRAW);

ballVerts不是一个数组.它是一个std :: vector.获取std :: vector的地址不会获取向量中包含的数组的地址.你需要使用& ballVerts [0];

标签:c,opengl,geometry,vbo
来源: https://codeday.me/bug/20190729/1573314.html