0

I have a problem in openGL when using buffer object to store vertex array . when I run the code , a window appears that display message " access violation reading location 0x000000000 " and when I move the cursor to arrow beside glGenBuufers function I see description: this is the next statement to execute when this thread returns from current function.

IDE snapshot

Code:

int frame=0;
void display();
void datasource();

 GLuint vbo;

void datasource()
{
    GLfloat vertex1[]={-1.000000 ,0.500000 ,-0.700000 ,0.500000 ,-1.000000 ,0.800000 ,-0.700000 ,0.800000 ,-0.400000 ,0.500000 ,-0.100000 ,0.500000 ,-0.400000 ,0.800000 ,-0.100000 ,0.200000 ,0.500000 ,0.500000 ,0.500000 ,0.200000 ,0.800000 ,0.500000,0.800000 ,0.800000 ,0.800000 ,0.500000 ,1.100000 ,0.500000 ,0.800000 ,0.800000 ,1.100000,0.800000  };

    glGenBuffers(1,&vbo);
    glBindBuffer(GL_ARRAY_BUFFER,vbo);
    glBufferData(GL_ARRAY_BUFFER,sizeof(vertex1),vertex1,GL_STATIC_DRAW);
}


void init(int arc,char **arch)
{
    glutInit(&arc,arch);
    glutInitWindowSize(800,800);
    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
    glutCreateWindow("chess");
    glClearColor(0.0,0.0,0.0,0.0);
    glutDisplayFunc(display);
}

int main(int arc,char **arch)
{
    init(arc,arch);
    datasource();
    glutMainLoop();
    return 0;
}

void display()
{
    frame++;
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glDrawArrays(GL_TRIANGLE_STRIP,0,8);
    glutSwapBuffers();
    glutPostRedisplay();
}

What did I do wrong?

zoul
  • 102,279
  • 44
  • 260
  • 354
user1841718
  • 187
  • 1
  • 4
  • 15
  • 2
    SO is not for code review. Please ask a concrete technical question. – Jens Gustedt Dec 23 '12 at 09:04
  • 1
    I don't see where you initialise vbo before you pass its address to glGenBuffers – Lord Peter Dec 23 '12 at 09:13
  • Does he have to initialize it? The second argument to `glGenBuffers` is an array of `GLuint`s to store the created buffers. If there’s just one buffer, passing an address of a single `GLuint` should do? – zoul Dec 23 '12 at 09:20
  • 3
    He’s not asking for code review (“is this code good?”), he’s asking for help with a reasonably concrete problem (“why does this short code sample segfault?”). Seems fairly on-topic to me. – zoul Dec 23 '12 at 09:25
  • possible duplicate of [Assist me to understand OpenGL glGenBuffers](http://stackoverflow.com/questions/12102864/assist-me-to-understand-opengl-glgenbuffers) – Hans Passant Dec 23 '12 at 09:38
  • This question is perfectly valid. His program crashes, because he uses functions not available through the system default ABI and need to be post-loaded into the process after OpenGL initialization. *Could people who do know understand how OpenGL works please stop from trying to answer these questions – I got the feeling there's a trend ATM that people value on OpenGL questions only because they have a hunch.* Yes, the problem is a FAQ, yes it creeps up every week or so. – datenwolf Dec 23 '12 at 10:42

2 Answers2

5

glGenBuffers, glBindBuffer and glBufferData are functions of either the Vertex Buffer Object extension or of OpenGL-1.5. The default system ABIs only go to OpenGL-1.2 so you need to post-load extended OpenGL functionality before you can use it. Just including the headers is not sufficient, because those just provide you the uninitialized symbols.

Most easy way to go:

  1. Download and install GLEW from http://glew.sf.net
  2. Replace all occurances of #include <GL/gl.h> with #include <GL/glew.h>
  3. Call glewInit() right after glutCreateWindow and check the error code
  4. Check if are VBOs are available (i.e. at least GL_ARB_vertex_buffer_object or OpenGL version >= 1.5)
  5. Add the GLEW library to your linker settings
datenwolf
  • 159,371
  • 13
  • 185
  • 298
2

It’s possible that the function pointer itself (glGenBuffers) is NULL (source). This happens when the OpenGL system wasn’t properly initialized yet, and calling a NULL function pointer will obviously cause a segfault. Take a look at the address of glGenBuffers, is it NULL? If yes, then you’re making a mistake somewhere in the initialization code.

zoul
  • 102,279
  • 44
  • 260
  • 354
  • Problem is, OP didn't initialize extendend OpenGL functions at all. The standard system ABIs for OpenGL go no further than OpenGL-1.1 (Windows) or OpenGL-1.2 (GLX). – datenwolf Dec 23 '12 at 10:53
  • you right , glGenBuffer was null because I did not invoke the `glewInit` function , thank you – user1841718 Dec 23 '12 at 11:49