Sign in to follow this  

Probably stupid Nehe code problem...

This topic is 2545 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This is probably a stupidly easy problem but for some reason I can't figure it out tonight.

Consider the following code:


class Model
{
public:
Vertex* VertexData;
Normal* NormalData;
Texcoord* TexcoordData;
Color* ColorData;

int NormalCount;
int TexcoordCount;
int ColorCount;
int FaceCount;

bool LoadObjFile(std::string FilePath);
bool IsVBOSupported();
bool BuildVBO();

int VertexCount;
int FaceCount;
GLuint VertexID;
GLuint NormalID;
GLuint TexcoordID;
GLuint ColorID;

Model(std::string Filepath);
~Model();
};

bool Model::BuildVBO()
{
// Generate And Bind The Vertex Buffer
glGenBuffersARB( 1, &VertexID ); // Get A Valid Name
glBindBufferARB( GL_ARRAY_BUFFER_ARB, VertexID ); // Bind The Buffer
glBufferDataARB( GL_ARRAY_BUFFER_ARB, VertexCount*3*sizeof(float), VertexData, GL_STATIC_DRAW_ARB );

// Generate And Bind The Texture Coordinate Buffer
glGenBuffersARB( 1, &TexcoordID ); // Get A Valid Name
glBindBufferARB( GL_ARRAY_BUFFER_ARB, TexcoordID ); // Bind The Buffer
// Load The Data
glBufferDataARB( GL_ARRAY_BUFFER_ARB, TexcoordCount*2*sizeof(float), TexcoordData, GL_STATIC_DRAW_ARB );

// Generate And Bind The Texture Coordinate Buffer
glGenBuffersARB( 1, &NormalID ); // Get A Valid Name
glBindBufferARB( GL_ARRAY_BUFFER_ARB, NormalID ); // Bind The Buffer
// Load The Data
glBufferDataARB( GL_ARRAY_BUFFER_ARB, NormalCount*2*sizeof(float), NormalData, GL_STATIC_DRAW_ARB );
// Our Copy Of The Data Is No Longer Necessary, It Is Safe In The Graphics Card

delete [] VertexData; VertexData = NULL;
delete [] TexcoordData; TexcoordData = NULL;
delete [] NormalData; NormalData = NULL;
return true;
}



Obviously there is a lot more code then that but this is where the problem lies. Everything compiles fine but when run it gets too:


glGenBuffersARB( 1, &VertexID );



And gives the following error:

Unhandled exception at 0x00000000 in Lesson6.exe: 0xC0000005: Access violation.

Again it's probably something stupid but I can't seem to figure it out. Probably need more caffeine...

HELP!

Share this post


Link to post
Share on other sites
Are you using glew or some such library to handle the opengl function pointers for you? I don't think the buffer functions are part of the version 1.1, so you need to get the extension pointer to it.

Either get GLEW, or read this if you don't know what I'm talking about:

http://support.microsoft.com/kb/328303

Share this post


Link to post
Share on other sites
I'm pretty much cannibalizing NeHes VBO code so far. His code (Lesson 45) works just fine for me. But my code (and keep in mind it compiles without errors) gives the error at that line when I run it.

Share this post


Link to post
Share on other sites
I'm pretty sure that something's not right with your function pointers.

glGenBuffers is a pointer to a function in your opengl driver, and if it's not initialized right it just points to 0x00000000, hence your error.

You have this in your code? Have you checked the return values from wglGetProcAddress? Again, or just use glew is the easiest solution.



#define GL_ARRAY_BUFFER_ARB 0x8892
#define GL_STATIC_DRAW_ARB 0x88E4
typedef void (APIENTRY * PFNGLBINDBUFFERARBPROC) (GLenum target, GLuint buffer);
typedef void (APIENTRY * PFNGLDELETEBUFFERSARBPROC) (GLsizei n, const GLuint *buffers);
typedef void (APIENTRY * PFNGLGENBUFFERSARBPROC) (GLsizei n, GLuint *buffers);
typedef void (APIENTRY * PFNGLBUFFERDATAARBPROC) (GLenum target, int size, const GLvoid *data, GLenum usage);

// VBO Extension Function Pointers
PFNGLGENBUFFERSARBPROC glGenBuffersARB = NULL; // VBO Name Generation Procedure
PFNGLBINDBUFFERARBPROC glBindBufferARB = NULL; // VBO Bind Procedure
PFNGLBUFFERDATAARBPROC glBufferDataARB = NULL; // VBO Data Loading Procedure
PFNGLDELETEBUFFERSARBPROC glDeleteBuffersARB = NULL; // VBO Deletion Procedure

glGenBuffersARB = (PFNGLGENBUFFERSARBPROC) wglGetProcAddress("glGenBuffersARB");
glBindBufferARB = (PFNGLBINDBUFFERARBPROC) wglGetProcAddress("glBindBufferARB");
glBufferDataARB = (PFNGLBUFFERDATAARBPROC) wglGetProcAddress("glBufferDataARB");
glDeleteBuffersARB = (PFNGLDELETEBUFFERSARBPROC) wglGetProcAddress("glDeleteBuffersARB");


Share this post


Link to post
Share on other sites
I came across a little problem with VBOs (involving glGenBuffers) a couple of days ago trying to come up with a system that would allow me to recreate them on a display resolution change.

The problem was that I was holding the instance of the class in charge of creating and destroying the VBO in a shared_ptr, and at the moment of re-creating the VBO, I was assigning a new instance of the class to the shared_ptr, and for some reason beyond my understanding, the older VBO was getting de-assigned (but not destroyed) in such a way that the new VBO would take the "name" or id of the earlier VBO and then OpenGL in an attempt to destroy the older one would actually destroy the recently created one.

I solved this by resetting the shared_ptr before creating the new one.

I dropped this just in case, and possibly to warn some other programmer about this kind of tricky behavior.

good luck!

Share this post


Link to post
Share on other sites
I didn't have this but neither did NeHes code...


glGenBuffersARB = (PFNGLGENBUFFERSARBPROC) wglGetProcAddress("glGenBuffersARB");
glBindBufferARB = (PFNGLBINDBUFFERARBPROC) wglGetProcAddress("glBindBufferARB");
glBufferDataARB = (PFNGLBUFFERDATAARBPROC) wglGetProcAddress("glBufferDataARB");
glDeleteBuffersARB = (PFNGLDELETEBUFFERSARBPROC) wglGetProcAddress("glDeleteBuffersARB");



But that gives me this:

error C3861: 'glGenBuffersARB': identifier not found
error C3861: 'glBindBufferARB': identifier not found
error C3861: 'glBufferDataARB': identifier not found

Share this post


Link to post
Share on other sites
Quote:
Original post by ahlywog
I didn't have this but neither did NeHes code...

*** Source Snippet Removed ***

But that gives me this:

error C3861: 'glGenBuffersARB': identifier not found
error C3861: 'glBindBufferARB': identifier not found
error C3861: 'glBufferDataARB': identifier not found


Read karwosts' post above (all of it)

Share this post


Link to post
Share on other sites
You were right. It was there verbatim. Now that I've gotten some rest and looked at it again I figured out what the problem was. It would appear that you have to have an active OpenGL window open before setting your function pointers. Learn something new every day.

Share this post


Link to post
Share on other sites

This topic is 2545 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this