简体   繁体   English

OpenGL:wglGetProcAddress始终返回NULL

[英]OpenGL: wglGetProcAddress always returns NULL

I am trying to create an OpenGL program which uses shaders. 我正在尝试创建一个使用着色器的OpenGL程序。 I tried using the following code to load the shader functions, but wglGetProcAddress always returns 0 no matter what I do. 我尝试使用以下代码加载着色器功能,但是无论我做什么, wglGetProcAddress 始终返回0。 The rest of the program works as normal when not using the shader functions. 不使用着色器功能时,程序的其余部分正常运行。

HDC g_hdc;
HGLRC g_hrc;

PFNGLATTACHSHADERPROC   glpf_attachshader;
PFNGLCOMPILESHADERPROC  glpf_compileshader;
PFNGLCREATEPROGRAMPROC  glpf_createprogram;
PFNGLCREATESHADERPROC   glpf_createshader;
PFNGLDELETEPROGRAMPROC  glpf_deleteprogram;
PFNGLDELETESHADERPROC   glpf_deleteshader;
PFNGLDETACHSHADERPROC   glpf_detachshader;
PFNGLLINKPROGRAMPROC    glpf_linkprogram;
PFNGLSHADERSOURCEPROC   glpf_shadersource;
PFNGLUSEPROGRAMPROC     glpf_useprogram;

void GL_Init(HDC dc)
{
    //create pixel format
    PIXELFORMATDESCRIPTOR pfd = 
    {
        sizeof(PIXELFORMATDESCRIPTOR), 
        1,
        PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER, 
        PFD_TYPE_RGBA, 
        32,
        0, 0, 0, 0, 0, 0,
        0, 0, 0,
        0, 0, 0, 0,
        32, 0, 0,
        PFD_MAIN_PLANE,
        0, 0, 0, 0
    };

    //choose + set pixel format
    int pixfmt = ChoosePixelFormat(dc, &pfd);
    if (pixfmt && SetPixelFormat(dc, pixfmt, &pfd))
    {
        //create GL render context
        if (g_hrc = wglCreateContext(dc))
        {
            g_hdc = dc;

            //select GL render context
            wglMakeCurrent(dc, g_hrc);

            //get function pointers     
            glpf_attachshader   = (PFNGLATTACHSHADERPROC)   wglGetProcAddress("glAttachShader");
            glpf_compileshader  = (PFNGLCOMPILESHADERPROC)  wglGetProcAddress("glCompileShader");
            glpf_createprogram  = (PFNGLCREATEPROGRAMPROC)  wglGetProcAddress("glCreateProgram");
            glpf_createshader   = (PFNGLCREATESHADERPROC)   wglGetProcAddress("glCreateShader");
            glpf_deleteprogram  = (PFNGLDELETEPROGRAMPROC)  wglGetProcAddress("glDeleteProgram");
            glpf_deleteshader   = (PFNGLDELETESHADERPROC)   wglGetProcAddress("glDeleteShader");
            glpf_detachshader   = (PFNGLDETACHSHADERPROC)   wglGetProcAddress("glDetachShader");
            glpf_linkprogram    = (PFNGLLINKPROGRAMPROC)    wglGetProcAddress("glLinkProgram");
            glpf_shadersource   = (PFNGLSHADERSOURCEPROC)   wglGetProcAddress("glShaderSource");
            glpf_useprogram     = (PFNGLUSEPROGRAMPROC)     wglGetProcAddress("glUseProgram");
        }
    }
}

I know this may be a possible duplicate, but on most of the other posts the error was because of simple mistakes (like calling wglGetProcAddress before wglMakeCurrent ). 我知道这可能是重复的,但是在其他大多数帖子中,错误是由于简单的错误(例如在wglGetProcAddress之前调用wglMakeCurrent )引起的。 I'm in a bit of an unique situation - any help would be appreciated. 我处于一种特殊的情况-任何帮助将不胜感激。

You are requesting a 32-bit Z-Buffer in this code. 您正在此代码中请求32位Z缓冲区。 That will probably throw you onto the GDI software rasterizer (which implements exactly 0 extensions). 这可能会使您陷入GDI软件光栅化器(它实现了0个扩展)的问题。 You can use 32-bit depth buffers on modern hardware, but most of the time you cannot do it using the default framebuffer, you have to use FBOs. 您可以在现代硬件上使用32位深度缓冲区,但是大多数情况下 ,您无法使用默认帧缓冲区来做到这一点,您必须使用FBO。

I have seen some drivers accept 32-bit depth only to fallback to the closest matching 24-bit pixel format, while others simply refuse to give a hardware pixel format at all (this is probably your case). 我已经看到一些驱动程序接受32位深度只是为了回退到最接近的匹配24位像素格式,而其他驱动程序根本拒绝提供硬件像素格式(这可能是您的情况)。 If this is in fact your problem, a quick investigation of your GL strings ( GL_RENDERER , GL_VERSION , GL_VENDOR ) should make it obvious. 如果这实际上是您的问题,那么对您的GL字符串( GL_RENDERERGL_VERSIONGL_VENDOR )进行快速调查应该很明显。

24-bit Depth + 8-bit Stencil is pretty much universally supported, this is the first depth/stencil size you should try. 普遍支持24位深度+ 8位模板,这是您应该尝试的第一个深度/模板大小。

You could use glew to do that kind of work for you. 您可以使用glew为您完成这类工作。 Or maybe you don't want? 还是您不想要?

EDIT: You could maybe use glext.h then. 编辑:您也许可以使用glext.h。 Or at least look in it and copy paste what interest you. 或者至少查看一下并复制粘贴您感兴趣的内容。

I'm not an expert I just remember having that kind of problem and try to remind what was conected to it. 我不是专家,我只是记得曾经遇到过此类问题,并试图提醒您要解决的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM