3

我在画出一个球体时遇到了一些麻烦。

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 收到访问冲突异常。有什么建议么?

4

2 回答 2

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

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

于 2012-05-06T16:29:30.347 回答
0

更好的预调整向量并使用迭代器扫描它,如下所示:

 std::vector <GLfloat> vertices;
 vertices.resize(count * 3);
 ...
 std::vector <GLfloat>::iterator v = vertices.begin();

 for( U32 r = 0; r < m_prec/2; ++r ) //широта
    {
        float const theta1 = -F_PI_BY_TWO + pi2p * r;
        float const theta2 = theta1 +  pi2p;

        for( U32 s = 0; s < m_prec; ++s ) //долгота
        {
            float const theta3 =  s * pi2p;     

            float ex = cosf(theta3) *cosf(theta2);
            float ey = sinf(theta2);
            float ez = sinf(theta3) * cosf(theta2);
            *v++ = m_x + m_radius * ex;
            *v++ = m_y + m_radius * ey;
            *v++ = m_z + m_radius * ez;

等等

::glVertexPointer(3, GL_FLOAT, 0 , &vertices[0]);

会工作

于 2015-03-28T10:23:24.667 回答