Help! Error message when using buffer objects...

Hello,

I am writing a program in OpenGL, learning as I go. I have encountered an error which I cannot figure out. Can anyone help please? When I add these three lines to my init() function:

glGenBuffers(1,&vbo);
glBindBuffer(GL_ARRAY_BUFFER,vbo);
glBufferData(GL_ARRAY_BUFFER,sizeof(vertices),vertices,GL_DYNAMIC_DRAW);

I get these three errors:

error LNK2019: unresolved external symbol _glBufferData(at symbol)16 referenced in function …
error LNK2019: unresolved external symbol _glBindBuffer(at symbol)8 referenced in function …
error LNK2019: unresolved external symbol _glGenBuffers(at symbol)8 referenced in function …

Where it says “(at symbol)” it used to be an at-symbol. But this forum won’t let you post at-symbols for some reason.

Any thoughts?

Please read the Getting Started page on the wiki, specifically the “Writing an OpenGL Application”/“Getting Functions” section.

OK. I got the glew library installed. Those errors have gone away. Now I am getting two new errors:

First-chance exception at 0x00000000 in terraformer.exe: 0xC0000005: Access violation.
Unhandled exception at 0x00000000 in terraformer.exe: 0xC0000005: Access violation.

These the green debug arrow is resting on the line that says:

glGenBuffers(1,&vbo);

vbo is previously declared as:

unsigned int vbo;

Any ideas?

Are you calling glewInit() before calling any OpenGL function?

I did not call glewInit(). I called init(). And I am using glut. Don’t know if that matters.

These are the calls I made before init():

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(1000, 1000);
glutInitWindowPosition(900, 2);
glutCreateWindow(argv[0]);

The whole thing was working before I tried to use buffers. I was able to display arrays of vertices, normals, and textures. So I feel confident that the error is coming from glGenBuffers(1,&vbo). I am not positive I installed the library and linked everything correctly. But those first errors went away. So maybe I did?

OK. So I tried a few things. I called glewInit() before my init() and all the error messages went away. Except that the buffer array was not displayed on the screen. I tried to replace init() with glewInit() and I received more errors:

c:\users racy\workspace erraformer erraformer\start.cpp(342): error C2556: ‘void glewInit(void)’ : overloaded function differs only by return type from ‘GLenum glewInit(void)’
c:\program files (x86)\microsoft visual studio 10.0\vc\include\gl\glew.h(17094) : see declaration of ‘glewInit’
c:\users racy\workspace erraformer erraformer\start.cpp(342): error C2373: ‘glewInit’ : redefinition; different type modifiers
c:\program files (x86)\microsoft visual studio 10.0\vc\include\gl\glew.h(17094) : see declaration of ‘glewInit’
c:\users racy\workspace erraformer erraformer\start.cpp(342): error C2491: ‘glewInit’ : definition of dllimport function not allowed
c:\users racy\workspace erraformer erraformer\start.cpp(575): error C3861: ‘glewInit’: identifier not found

I tried changing my declaration of:

void glewInit(void){…}

to:

GLenum glewInit(void){…}

It seemed to get rid of the first few problems. Now I still have these errors:

c:\program files (x86)\microsoft visual studio 10.0\vc\include\gl\glew.h(17094) : see declaration of ‘glewInit’
c:\users racy\workspace erraformer erraformer\start.cpp(342): error C2491: ‘glewInit’ : definition of dllimport function not allowed
c:\users racy\workspace erraformer erraformer\start.cpp(575): error C3861: ‘glewInit’: identifier not found

But I have no idea where to go from here. Pulling my hair out now. Lol. Any help?

Please use [ code]/[ /code] tags around source snippets.

Hmm, I don’t know what init() does, I’m guessing it’s a function you’ve written? Please take a look at the glew website for info on how to initialize glew correctly, the example there uses glut as well. Also, note that using a library (often) involves more than just including a header, you also have to link against the lib.

If you don’t initialize glew you can still call those OpenGL functions that are exported by the systems opengl32.lib (which are the ones in OpenGL 1.2 IIRC), but it will crash on newer functionality (like glGenBuffers()) - because you are making a call through an uninitialized function pointer.

OK. So I’ve checked and Glew seems to be installed properly and running. I have initialized it with glewInit(). I am getting no errors, but the scene is not rendering. Any ideas? Here is the code I used to render it:


// create data types
unsigned int vbo;
GLfloat* vertices;

// fill array
for(int i=0;i<count;i++){
     vertices[i] = ...
}

// initialize glew
glewInit();

// initialize vbo
glGenBuffers(1,&vbo);
glBindBuffer(GL_ARRAY_BUFFER,vbo);
glBufferData(GL_ARRAY_BUFFER,sizeof(vertices),vertices,GL_DYNAMIC_DRAW);

// display
texture = load_texture("grass2");
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,texture);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glBindBuffer(GL_ARRAY_BUFFER,vbo);
glInterleavedArrays(GL_T2F_N3F_V3F,8*sizeof(GLfloat),0);
glDrawArrays(GL_TRIANGLES,0,count/3);
glBindBuffer(GL_ARRAY_BUFFER,0);
glDisable(GL_NORMAL_ARRAY);
glDisable(GL_TEXTURE_COORD_ARRAY);
glDisable(GL_VERTEX_ARRAY);
glDisable(GL_TEXTURE_2D);

Do I need to use glGenVertexArray? Some of the tutorials used it and some didn’t. Also, I have not included shaders. Don’t know if they’re necessary at this point or not.


glBufferData(GL_ARRAY_BUFFER,sizeof(vertices),vertices,GL_DYNAMIC_DRAW);

the sizeof(vertices) does not do what you think it does, it will return a number like 4 or 8 (depending on whether you are on a 32 or 64 bit machine). You will have to keep track of the array size yourself or better yet use a std::vector (assuming this is C++).

Whether you must use a vertex array object depends on if you are using a core or compatibility context same for whether shaders are a must. The wiki has links to tutorials for either, I would recommend following one for modern, i.e. core, OpenGL and stick with it until you are familiar with the basics how to draw an object. If you jump between different tutorials (that target different “generations” of OpenGL) early on you run the risk of getting confused about the best/right way to do things.

Yay! It works! Thank you! :smiley: I can use buffers now! Sooo appreciate the help!