1. Symptoms of the problem
In the VC + + environment, using the MFC Single document application SDI under the development of OpenGL program, when the call Glgenbuffersarb (1, &pbo) method compiled through but error, error code is as follows:
Unhandled exception at 0x00000000 in OpenGL program: 0xC0000005: Access violation when reading location 0x00000000
void Createvbo (gluint *vbo,int size) {glgenbuffers (1, VBO);//The line code is wrong, whining Glbindbuffer (Gl_array_buffer,*VBO); Glbufferdata (gl_array_buffer,size,0, Gl_dynamic_draw); Glbindbuffer (gl_array_buffer,0); CUT_CHECK_ERROR_GL ();}
is a code that creates a buffer function, the program compiles without errors, but when it runs to Glgenbuffers (1,VBO), a header error appears, as shown in:
2. Cause analysis
Some GL interfaces for buffers are only available from GL1.5, and the Windows-brought GL only supports version 1.1. But if your graphics card support GL1.5 above, Glew will be good to help you complete the extension work, since you use Glew, then you should use GL any interface first call Glewinit to initialize these extensions, otherwise those GL interface can not be used. add Glewinit to the beginning of your Init method.
3. Solutions
(1), WIN32 Engineering environment
First, initialize the code as follows
// setting up OpenGL environments void SETUPGL (intChar* * argv) { glutinit (&argc, argv) ;| Glut_rgb); Glutinitwindowsize (Window_width, window_height); Glutcreatewindow (programname); Glutdisplayfunc (display); Glutkeyboardfunc (keyboard); Glutreshapefunc (reshape); Glutidlefunc (idle); Glewinit ();//glew initialization}
The following code is then called to generate the buffer identity:
Glgenbuffers (1, VBO); //Generate buffer ID
(2), VC + + MFC Single Document application under SDI engineering environment
in the VC + + MFC Single Document application under the SDI engineering environment, you need to First call Wglmakecurrent (M_HDC, Tempcontext); method, and then call Glew initializes the code, and finally calls the code that generated the buffer identity. If the Glewinit method is not called after wglmakecurrent, it causes Glewinit to return 1 (0 successfully). This causes the preceding run-time issue to occur. The order of invocation is given below:
Crsquicklookview::oncreate--_>glinit ()--->getsafehdc ()--->choosepixelformat (m_hdc, &PFD)---> Setpixelformat (m_hDC, PixelFormat, &PFD)--->wglcreatecontext (M_HDC)--->wglmakecurrent (M_hDC, G_ P00RC)--->glewinit ()--->initglbuffers ()--->glgenbuffers (1,vbo). The Glinit () and Initglbuffers () methods are specifically defined as follows:
voidCrsquicklookview::glinit () {createtexturepixel ();//produces the measured pixel buffer data//M_HDC =:: GetDC (m_hwnd);cdc* CLIENTDC =NewCCLIENTDC ( This); m_hDC= clientdc->Getsafehdc (); if(m_hDC = =NULL)return; StaticPixelformatdescriptor PFD = { sizeof(pixelformatdescriptor),1, Pfd_draw_to_window| Pfd_support_opengl | Pfd_doublebuffer,//logoPfd_type_rgba,//Color Mode -,//number of color bits 0,0,0,0,0,0, 0,0,0,0,0,0,0, +,//depth of position 0, 0, Pfd_main_plane,0, 0,0,0 }; Gluint PixelFormat; if((PixelFormat = Choosepixelformat (m_hDC, &pfd)) = =0) return;//Select the appropriate pixel format if(! Setpixelformat (m_hDC, PixelFormat, &PFD)) return;//set the pixel format//if ((M_HRC = Wglcreatecontext (m_hdc)) = = NULL) if((G_P00RC = Wglcreatecontext (m_hdc)) = =NULL)return;//Creating a shaded description tableSetupcuda ();//set up Cuda environment, mainly to complete the selection and setting of the strongest computing ability graphics card//GLSETUPRC (); //if (!wglmakecurrent (M_HDC, M_HRC)) if(!wglmakecurrent (M_HDC, G_P00RC))return;//Connect a coloring description table to a Device description table intRe=Glewinit (); Initglbuffers ();}
voidcrsquicklookview::initglbuffers () {//Create pixel buffer object to store final image glgenbuffers(1, &PBO); Glbindbuffer (Gl_pixel_unpack_buffer, PBO); Glbufferdata (gl_pixel_unpack_buffer, Width* Height *sizeof(glubyte) *3, Ppixeldata, Gl_dynamic_draw);//Gl_stream_draw_arbGlbindbuffer (Gl_pixel_unpack_buffer,0); Checkcudaerrors (Cudaglregisterbufferobject (PBO)); //Create texture for display Glgentextures(1, &_texture); Glbindtexture (gl_texture_2d, _texture); Glteximage2d (gl_texture_2d,0, Gl_rgb, width, height,0, Gl_rgb, Gl_unsigned_byte, NULL); Gltexparameteri (gl_texture_2d, Gl_texture_mag_filter, gl_linear); Gltexparameteri (gl_texture_2d, Gl_texture_min_filter, gl_linear);}
Reference Links:
1. Glewinit initialization errors and Glewinit initialization errors
2, under MFC using OpenGL in the Vbo to draw, draw failed
3. The 0x00000000 in OpenGL program unhandled error: 0xC0000005: An access violation occurred while reading location 0x00000000
4. Glgenbuffersarb run-time access violation
5. Solution of unhandled exception related to Glew in OpenGL program
VC + + MFC Single Document application SDI downgrade with Glgenbuffersarb (1, &pbo) method compile pass but execution error cause analysis and resolution: Glewinit () initialization error