I'm trying to render to a HWND window using a HGLRC OpenGL rendering context.
I have to initiate the glad but gladLoadGLLoader((GLADloadproc)wglGetProcAddress))
and the gladLoadGL()
seems to be failing.
glad OpenGL version : 4.0 core
I've been using the glfwGetProcAdress function until now.
How can I make the gladLoadGLLoader function work? Would it be that I have to indicate the version of the OpenGL to the wglGetProcAddress function or maybe I should use another loader?
Update 1 :
in the gladLoadGLLoader function the problem is (PFNGLGETSTRINGPROC)load("glGetString")
returns null
int gladLoadGLLoader(GLADloadproc load) {
GLVersion.major = 0; GLVersion.minor = 0;
glGetString = (PFNGLGETSTRINGPROC)load("glGetString");
if(glGetString == NULL) //Returns there (fails)
return 0;
if(glGetString(GL_VERSION) == NULL)
return 0;
find_coreGL();
load_GL_VERSION_1_0(load);
load_GL_VERSION_1_1(load);
load_GL_VERSION_1_2(load);
load_GL_VERSION_1_3(load);
load_GL_VERSION_1_4(load);
load_GL_VERSION_1_5(load);
load_GL_VERSION_2_0(load);
load_GL_VERSION_2_1(load);
load_GL_VERSION_3_0(load);
load_GL_VERSION_3_1(load);
load_GL_VERSION_3_2(load);
load_GL_VERSION_3_3(load);
if (!find_extensionsGL()) return 0;
return GLVersion.major != 0 || GLVersion.minor != 0;
}
Update 2 :
The problem seems to be that wglGetProcAddress
is being used to load core OpenGL functions. However, wglGetProcAddress
can only load specific WGL extensions and not core OpenGL stuff.
Update 3 :
As an additional note to the update 2, it seems that the GLFW library also uses wglGetProcAddress
for the glfwGetProcAddress
function
Solved :
void *GetAnyGLFuncAddress(const char *name)
{
void *p = (void *)wglGetProcAddress(name);
if(p == 0 ||
(p == (void*)0x1) || (p == (void*)0x2) || (p == (void*)0x3) ||
(p == (void*)-1) )
{
HMODULE module = LoadLibraryA("opengl32.dll");
p = (void *)GetProcAddress(module, name);
}
return p;
}
void Renderer::initGlad(){
//Init GLAD
if (!gladLoadGLLoader((GLADloadproc)GetAnyGLFuncAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
}
}
Replacing wglGetProcAddress
with the void *GetAnyGLFuncAddress(const char *name)
function solves the problem.