Reputation: 11506
Some time ago I started working on OpenGL(3.3) renderer for MFC program.Everything was fine till I decided to run some OpenGL debugging tools to see if I have some silent errors.I used gDEBugger .Running gDebug program analyzing tool I immediately started getting errors of the following type:
Debug String: Detected error: The debugged process asked for an extension function pointer (glGenBuffers) from one render context, but called this function pointer in another render context (context #2)
In fact every GLEW method gets this error.Now I started looking for a problem in forums and also on MSDN and I found some people mention that in Windows environment some GLEW methods pointers should be redefined.Also I stumbled upon this tutorial which shows how pretty every GLEW method is redefined using Windows OpenGL methods like these:
void CGLRenderer::InitAPI()
{
// Program
glCreateProgram = (PFNGLCREATEPROGRAMPROC)wglGetProcAddress("glCreateProgram");
glDeleteProgram = (PFNGLDELETEPROGRAMPROC)wglGetProcAddress("glDeleteProgram");
glUseProgram = (PFNGLUSEPROGRAMPROC)wglGetProcAddress("glUseProgram");
...
....
My OpenGL context set-up looks like this:
bool OpenGLMain::create30Context(HDC device_context){
//this->hwnd=hwnd;
hdc=device_context;//GetDC(hwnd);
hdcGlobal=&hdc;
PIXELFORMATDESCRIPTOR kPFD;
memset(&kPFD,0,sizeof(PIXELFORMATDESCRIPTOR));
kPFD.nSize=sizeof(PIXELFORMATDESCRIPTOR);
kPFD.nVersion=1;
kPFD.dwFlags=
PFD_DRAW_TO_WINDOW|
PFD_SUPPORT_OPENGL|
PFD_GENERIC_ACCELERATED|
PFD_DOUBLEBUFFER;
kPFD.iPixelType=PFD_TYPE_RGBA;
kPFD.cColorBits=32;
kPFD.cDepthBits=32;
kPFD.cStencilBits=8;
kPFD.iLayerType=PFD_MAIN_PLANE;
int iPixelFormat=ChoosePixelFormat(hdc,&kPFD);
if(iPixelFormat==0){
// ReleaseDC(window,gs_hWindowDC);
return false;
}
BOOL bSuccess=SetPixelFormat(hdc,iPixelFormat,&kPFD);
if(!bSuccess){
// ReleaseDC(window,gs_hWindowDC);
return false;
}
/////////init opengl context
HGLRC tempOpenGLContext=wglCreateContext(hdc);/////Openggl 2.1
wglMakeCurrent(hdc,tempOpenGLContext);////male openGL 2.1 context current and active
GLenum error =glewInit();
if(error!=GLEW_OK){
return false;
}
/////////context setup///////
int attributes[]={
WGL_CONTEXT_MAJOR_VERSION_ARB,3,
WGL_CONTEXT_MINOR_VERSION_ARB,2,
WGL_CONTEXT_FLAGS_ARB,WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB,0
};
if(wglewIsSupported("WGL_ARB_create_context")==1){
hrc=wglCreateContextAttribsARB(hdc,NULL,attributes);///create OpenGL 3x context based on the supplied attributes
wglMakeCurrent(NULL,NULL);//remove temp context
wglDeleteContext(tempOpenGLContext);
wglMakeCurrent(hdc,hrc);
}else{
hrc=tempOpenGLContext;////if no support for OGL 3x detected roll back to 2.0
}
/////////version check///////////////
int glVersion[2]={-1,-1};
glGetIntegerv(GL_MAJOR_VERSION,&glVersion[0]);
glGetIntegerv(GL_MINOR_VERSION,&glVersion[1]);
std::cout<<"Using openGL"<<glVersion[0]<<"."<<glVersion[1]<<
std::endl;
OutputDebugString(L"Using OPENGL version:"+glVersion[0]);
return true;
}
Now I am really confused at this point because in fact the program runs fine inside VisualStudio without redefining all these GLEW methods.But it shows the empty screen (no geometry) if I run the executable directly.Also in all the other examples and tutorials I have ever read it had never been mentioned that one has to reset pointers on GLEW API methods.So my question is if anybody can point out to the right way to integrate OpenGL 3.3 in Windows API because it seems that there are many ways doing it.
Upvotes: 3
Views: 2490
Reputation: 474436
The function pointers you get are based on the current context. That is, it is entirely possible for you to get different function pointers for different OpenGL contexts. gDEBugger is telling you that it has detected that you're using function pointers with a different context from the one used to acquire them. Which is not guaranteed to work.
That being said, it generally will work. It won't work if the two contexts aren't from the same vendor (and probably for the same GPU or SLI/CrossFire GPU setup). But if they are, it should be fine.
GLEW, as I understand it, doesn't have a way to accommodate two different contexts that don't use the same function pointers. You would have to call glewInit
every time you changed contexts.
Upvotes: 4