0

I am trying to draw a sphere but I keep getting a

Thread 1: EXC_BAD_ACCESS (code=1, address=0x0

error on this line:

glGenBuffers(1, &ballVbo);

I am not exactly sure what this means.

Here is my code below:

int main(int argc, char *argv[]) {
std::vector<GLfloat> ballVerts;
    GLuint ballVbo;

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[0], 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);
}
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
joeny
  • 11
  • 4
  • 2
    You need to use some extension loading library like [glew](http://glew.sourceforge.net/basic.html). Before initializing one of those libraries, many functions, including `glGenBuffers`, will be null pointers. – Brennan Vincent May 21 '19 at 23:49
  • Sorry this might be a very basic question but how do I do that? – joeny May 22 '19 at 00:10
  • I read online and added ```glewExperimental = GL_TRUE; glewInit();``` but I get the same error except on the ```glewInit(); ```line – joeny May 22 '19 at 00:37
  • @joeny: First, you're not trying to use instancing in the given code. Second, you're [still not using *shaders*](https://stackoverflow.com/q/56246855/734069), so even if you tried, you'd still fail. – Nicol Bolas May 22 '19 at 02:03
  • Sorry let me fix that, I am just trying to draw a sphere using a vbo in this! – joeny May 22 '19 at 03:25
  • see [simple complete GL+VAO/VBO+GLSL+shaders example in C++](https://stackoverflow.com/a/31913542/2521214) – Spektre May 22 '19 at 08:21

0 Answers0