Unhandled exception at 0x00000000 glGenBuffer

Started by
10 comments, last by Katie 12 years, 10 months ago
Hello all
Im begginer so bare with me here , i like to load 3ds model into simple opengl program , what i did is to use as refernce 2 links i found :
http://pastebin.com/CZ82VYWT and My linkhttp://www.donkerdump.nl/node/207 i take the code from the redbook the cube example and tryed to marage between the 2
but all the time i keep getting Unhandled exception at 0x00000000 glGenBuffer exception . in the CModel3DS::CreateVBO() method
here is my code :


// main.cpp
#include <stdlib.h>
#include "CModel3DS.h"

CModel3DS * monkey;
// Initialize some OpenGL settings
void initializeGL(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_SMOOTH);

// Enable lighting and set the position of the light
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
GLfloat pos[] = { 0.0, 4.0, 4.0 };
glLightfv(GL_LIGHT0, GL_POSITION, pos);

// Generate Vertex Buffer Objects
monkey->CreateVBO();
}

// Reset viewport and projection matrix after the window was resized
void resizeGL(int width, int height)
{
// Reset the viewport
glViewport(0, 0, width, height);
// Reset the projection and modelview matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// 10 x 10 x 10 viewing volume
glOrtho(-5.0, 5.0, -5.0, 5.0, -5.0, 5.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

// Do all the OpenGL rendering
void paintGL(void)
{
glClear(GL_COLOR_BUFFER_BIT);

// Draw our model
monkey->Draw();

// We don't need to swap the buffers, because QT does that automaticly for us
}

void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_FLAT);

}

void display(void)
{
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (1.0, 1.0, 1.0);
glLoadIdentity (); /* clear the matrix */
/* viewing transformation */
gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glScalef (1.0, 2.0, 1.0); /* modeling transformation */
//glutWireCube (1.0);
paintGL();
glFlush ();
}

void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);
glMatrixMode (GL_MODELVIEW);
}

void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 27:
exit(0);
break;
}
}




int main(int argc, char** argv)
{
glutInit(&argc, argv);
GLenum err = glewInit();
if (GLEW_OK != err)
{
/* Problem: glewInit failed, something is seriously wrong. */
fprintf(stderr, "Error: %s\n", glewGetErrorString(err));

}

try
{
monkey = new CModel3DS("monkey.3ds");
initializeGL();
}
catch(std::string error_str)
{
std::cerr << "Error: " << error_str << std::endl;


exit(1);
}
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}

//the CModel3DS.h

#ifndef CMODEL3DS_H
#define CMODEL3DS_H
#include <GL/glew.h>
#include <GL/glut.h>
#include <string>
#include <iostream>
#include <string>
#include <cstring>
#include <cassert>
#include <lib3ds/file.h>
#include <lib3ds/mesh.h>
// Our 3DS model class
class CModel3DS
{
public:
CModel3DS(std:: string filename);
virtual void Draw() const;
virtual void CreateVBO();
virtual ~CModel3DS();
protected:
void GetFaces();
unsigned int m_TotalFaces;
Lib3dsFile * m_model;
GLuint m_VertexVBO, m_NormalVBO;
};

and the cpp

#include "CModel3DS.h"





// Load 3DS model
CModel3DS::CModel3DS(std:: string filename)
{
m_TotalFaces = 0;

m_model = lib3ds_file_load(filename.c_str());
// If loading the model failed, we throw an exception
if(!m_model)
{
throw strcat("Unable to load ", filename.c_str());
}
}


// Count the total number of faces this model has
void CModel3DS::GetFaces()
{
assert(m_model != NULL);

m_TotalFaces = 0;
Lib3dsMesh * mesh;
// Loop through every mesh
for(mesh = m_model->meshes;mesh != NULL;mesh = mesh->next)
{
// Add the number of faces this mesh has to the total faces
m_TotalFaces += mesh->faces;
}
}


// Copy vertices and normals to the memory of the GPU
void CModel3DS::CreateVBO()
{
assert(m_model != NULL);

// Calculate the number of faces we have in total
GetFaces();

// Allocate memory for our vertices and normals
Lib3dsVector * vertices = new Lib3dsVector[m_TotalFaces * 3];
Lib3dsVector * normals = new Lib3dsVector[m_TotalFaces * 3];

Lib3dsMesh * mesh;
unsigned int FinishedFaces = 0;
// Loop through all the meshes
for(mesh = m_model->meshes;mesh != NULL;mesh = mesh->next)
{
lib3ds_mesh_calculate_normals(mesh, &normals[FinishedFaces*3]);
// Loop through every face
for(unsigned int cur_face = 0; cur_face < mesh->faces;cur_face++)
{
Lib3dsFace * face = &mesh->faceL[cur_face];
for(unsigned int i = 0;i < 3;i++)
{
memcpy(&vertices[FinishedFaces*3 + i], mesh->pointL[face->points[ i ]].pos, sizeof(Lib3dsVector));
}
FinishedFaces++;
}
}

// Generate a Vertex Buffer Object and store it with our vertices
glGenBuffers(1, &m_VertexVBO); <<<---------------------------------------------------------------------------------HERE IS MY EXCEPTION
glBindBuffer(GL_ARRAY_BUFFER, m_VertexVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(Lib3dsVector) * 3 * m_TotalFaces, vertices, GL_STATIC_DRAW);

// Generate another Vertex Buffer Object and store the normals in it
glGenBuffers(1, &m_NormalVBO);
glBindBuffer(GL_ARRAY_BUFFER, m_NormalVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(Lib3dsVector) * 3 * m_TotalFaces, normals, GL_STATIC_DRAW);

// Clean up our allocated memory
delete vertices;
delete normals;

// We no longer need lib3ds
lib3ds_file_free(m_model);
m_model = NULL;
}


// Render the model using Vertex Buffer Objects
void CModel3DS:: Draw() const
{
assert(m_TotalFaces != 0);

// Enable vertex and normal arrays
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);

// Bind the vbo with the normals
glBindBuffer(GL_ARRAY_BUFFER, m_NormalVBO);
// The pointer for the normals is NULL which means that OpenGL will use the currently bound vbo
glNormalPointer(GL_FLOAT, 0, NULL);

glBindBuffer(GL_ARRAY_BUFFER, m_VertexVBO);
glVertexPointer(3, GL_FLOAT, 0, NULL);

// Render the triangles
glDrawArrays(GL_TRIANGLES, 0, m_TotalFaces * 3);

glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
}


// Destructor
CModel3DS::~CModel3DS()
{
glDeleteBuffers(1, &m_VertexVBO);
glDeleteBuffers(1, &m_NormalVBO);

if(m_model != NULL)
{
lib3ds_file_free(m_model);
}
}
#endif



i know maybe i dont understand it fully but im working hard to understand
Thank s
Advertisement
glGenBuffer is OpenGL 1.5 and at least on Windows not part of core OpenGL. You are probably using an extension manager like GLEW or GLEE, otherwise this would not even compile. While I'm not familiar with GLEE, GLEW needs a call to glewInit() after you have set up an OpenGL context but before you make any call to extension functions. GLEE will need something similar.
You need to initialize GLEW efter creating your window. Without the window, you don't have an active rendering context, and without the rendering context, there are no functions that GLEW can load.
All that is covered in the Wiki
http://www.opengl.org/wiki/Getting_started

and more specifically
http://www.opengl.org/wiki/Getting_started#Accessing_OpenGL_functions
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
Thanks allot for your answers i did as you suggest and set the glewInit() to be the last after the window is created and the window does shown all right and i don't have any run-time error/exceptions
but i don't see the model in my window . i know there is a model ( somewhere in memory ) [font="arial, sans-serif"]Beacose[/font][font="arial, sans-serif"] [/font]i have its faces count and fopen does open the file .
but my window is totally black. maybe its have to do something with the camera / view ? how can i check this ?
UPDATE
i found out why only the window is showing , the glew code never executed beacose the glutMainLoop(); but it in a loop
when i put the glutMainLoop(); after the glewInit(); as the last line in the code i still getting the the old exception error with the buffers.
so what else i have to do ....

here is my new code :


#include <stdlib.h>
#include "CModel3DS.h"

CModel3DS * monkey;
// Initialize some OpenGL settings
void initializeGL(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_SMOOTH);

// Enable lighting and set the position of the light
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
GLfloat pos[] = { 0.0, 4.0, 4.0 };
glLightfv(GL_LIGHT0, GL_POSITION, pos);

// Generate Vertex Buffer Objects
monkey->CreateVBO();
}

// Reset viewport and projection matrix after the window was resized
void resizeGL(int width, int height)
{
// Reset the viewport
glViewport(0, 0, width, height);
// Reset the projection and modelview matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// 10 x 10 x 10 viewing volume
glOrtho(-5.0, 5.0, -5.0, 5.0, -5.0, 5.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

// Do all the OpenGL rendering
void paintGL(void)
{
glClear(GL_COLOR_BUFFER_BIT);

// Draw our model
monkey->Draw();

// We don't need to swap the buffers, because QT does that automaticly for us
}

void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_FLAT);

}

void display(void)
{
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (1.0, 1.0, 1.0);
glLoadIdentity (); /* clear the matrix */
/* viewing transformation */
gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glScalef (1.0, 2.0, 1.0); /* modeling transformation */
//glutWireCube (1.0);

glFlush ();
}

void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);
glMatrixMode (GL_MODELVIEW);
}

void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 27:
exit(0);
break;
}
}




int main(int argc, char** argv)
{
glutInit(&argc, argv);

glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
GLenum err = glewInit(); <<<<<<<<<<<<<<<<<-------------HERE IS THE NEW PLACE TO INIT GLEW
if (GLEW_OK != err)
{
/* Problem: glewInit failed, something is seriously wrong. */
fprintf(stderr, "Error: %s\n", glewGetErrorString(err));

}

try
{
monkey = new CModel3DS("monkey.3ds");
initializeGL();
paintGL();
}
catch(std::string error_str)
{
std::cerr << "Error: " << error_str << std::endl;


exit(1);
}
return 0;
}

Try putting the glew initialization inside of your initializeGL function. I would also implement a check to see if the required extensions were loaded, and if not, fall back to a failsafe alternative using system memory. Additionally, use a debugger to find out the order in which your functions get called. Its also a good idea to test glError in your debug build, and perhaps store the intermediate value in a function scope variable for even easier overview of what happens when debugging
Actually, that is really bad code. You need to check the GL version before using a function. VBO itself appeared in GL 1.5.
Get the spec files from opengl.org and look them up. There are also the reference files for GL 2.1, 3.3 and 4.1.
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
It can be version of my OpenGL?
well what im using is : freeglut that is the free opengl for windows and glew as the extention implantation ( if i understand it right )
by the way even if i replace the freeglut with the glut-3.7.6 it gives me the same error

if i check versions in code this is what i get after :


cout<<"Using GLEW "<<glewGetString(GLEW_VERSION)<<endl;
cout<<"Vendor: "<<glGetString (GL_VENDOR)<<endl;
cout<<"Renderer: "<<glGetString (GL_RENDERER)<<endl;
cout<<"Version: "<<glGetString (GL_VERSION)<<endl;


here is the output :


Driver does not support OpenGL Shading Language
Using GLEW 1.6.0
Vendor: Intel
Renderer: Intel Broadwater G
Version: 1.4.0 - Build 6.14.10.4670

now here is the report from OpenGL extension viewer :

Renderer: Intel Broadwater G

Vendor: Intel
Memory: 256 MB
Version: 1.4.0 - Build 6.14.10.4670
Shading language version: N/A


Max texture size: 2048 x 2048
Max texture coordinates: 8
Max vertex texture image units: 0
Max texture image units: 16
Max geometry texture units: 0
Max anisotropic filtering value: 2
Max number of light sources: 16
Max viewport size: 2048 x 2048
Max uniform vertex components: 0
Max uniform fragment components: 0
Max geometry uniform components: 0
Max varying floats: 0
Max samples: 0
Max draw buffers: 0


Extensions: 57

GL_3DFX_texture_compression_FXT1
GL_ARB_depth_texture
GL_ARB_fragment_program
GL_ARB_multitexture
GL_ARB_occlusion_query
GL_ARB_point_parameters
GL_ARB_shadow
GL_ARB_texture_border_clamp
GL_ARB_texture_compression
GL_ARB_texture_cube_map
GL_ARB_texture_env_add
GL_ARB_texture_env_combine
GL_ARB_texture_env_crossbar
GL_ARB_texture_env_dot3
GL_ARB_transpose_matrix
GL_ARB_vertex_buffer_object
GL_ARB_vertex_program
GL_ARB_window_pos
GL_EXT_abgr
GL_EXT_bgra
GL_EXT_blend_color
GL_EXT_blend_func_separate
GL_EXT_blend_minmax
GL_EXT_blend_subtract
GL_EXT_clip_volume_hint
GL_EXT_compiled_vertex_array
GL_EXT_cull_vertex
GL_EXT_draw_range_elements
GL_EXT_fog_coord
GL_EXT_multi_draw_arrays
GL_EXT_packed_pixels
GL_EXT_rescale_normal
GL_EXT_secondary_color
GL_EXT_separate_specular_color
GL_EXT_shadow_funcs
GL_EXT_stencil_two_side
GL_EXT_stencil_wrap
GL_EXT_texture3D
GL_EXT_texture_compression_s3tc
GL_EXT_texture_env_add
GL_EXT_texture_env_combine
GL_EXT_texture_filter_anisotropic
GL_EXT_texture_lod_bias
GL_IBM_texture_mirrored_repeat
GL_NV_blend_square
GL_NV_texgen_reflection
GL_SGIS_generate_mipmap
GL_SGIS_texture_edge_clamp
GL_SGIS_texture_lod
GL_WIN_swap_hint
WGL_ARB_buffer_region
WGL_ARB_extensions_string
WGL_ARB_make_current_read
WGL_ARB_pbuffer
WGL_ARB_pixel_format
WGL_EXT_extensions_string
WGL_EXT_swap_control

Core features
v1.1 (100 % - 7/7)
v1.2 (100 % - 8/8)
v1.3 (100 % - 9/9)
v1.4 (100 % - 15/15)
v1.5 (100 % - 3/3)
v2.0 (10 % - 1/10)
v2.1 (0 % - 0/3)
v3.0 (0 % - 0/23)
v3.1 (0 % - 0/8)
v3.2 (0 % - 0/9)
v3.3 (0 % - 0/9)
v4.0 (0 % - 0/13)
v4.1 (0 % - 0/7)

OpenGL driver version check (Current: 6.14.10.4670, Latest known: 6.14.10.4642):
Latest version of display drivers found
According the database, you are running the latest display drivers for your video card.

Compiled vertex array support
This feature improves OpenGL performance by using video memory to cache transformed vertices.

Multitexture support
This feature accelerates complex rendering such as lightmaps or environment mapping.

Secondary color support
This feature provides an alternate method of coloring specular highlights on polygons.

S3TC compression support
This feature improves texture mapping performance in some applications by using lossy compression.

Texture edge clamp support
This feature improves texturing quality by adding clamping control to edge texel filtering.

Vertex program support
This feature enables a wide variety of effects via flexible vertex programming (equivalent to DX8 Vertex Shader.)

Fragment program support
This feature enables a wide variety of effects via per pixel programming (equivalent to DX9 Pixel Shader.)

Texture anisotropic filtering support
This feature improves the quality of texture mapping on oblique surfaces.

Occlusion test support
This feature provides hardware accelerated culling for objects.

No OpenGL Shading Language support
This may break compatibility for applications using per pixel shading.

No Frame buffer object support
This may break compatibility for applications using render to texture functions.

Extension verification:
GL_ARB_debug_output was not found, but has the entry point glGetPointerv
GL_ARB_imaging was not found, but has the entry point glBlendEquation
GL_ARB_occlusion_query has been added to the extensions list of Intel Broadwater G

It seems your opengl renderer is only 1.4, but you have the arb extension. Basically, this means you should use glGenBuffersARB, and glBindBufferARB, etc, not glGenBuffers and glBindBuffer and the likes, since those functions require opengl version 1.5 or higher. So you need to create multiple paths, and decide during runtime which path to take depending on the supported gl version and extensions.

It seems your opengl renderer is only 1.4, but you have the arb extension. Basically, this means you should use glGenBuffersARB, and glBindBufferARB, etc, not glGenBuffers and glBindBuffer and the likes, since those functions require opengl version 1.5 or higher. So you need to create multiple paths, and decide during runtime which path to take depending on the supported gl version and extensions.



Hello and thanks for the replay ,
well now it start to make sens to me after reading this post :
[url="http://stackoverflow.com/questions/4317062/opengl-how-to-check-if-the-user-supports-glgenbuffers"]http://stackoverflow.com/questions/4317062/opengl-how-to-check-if-the-user-supports-glgenbuffers[/url]
and changing the to glGenBuffersARB it is working , so what you mean bassclly i have to use #IF 's in code after checking the opengl version , am i right ?
but from where the opengl version is defined? from the drivers ? or hardware ? should i upgrade drivers ?



This topic is closed to new replies.

Advertisement