GL_INVALID_ENUM/GL_INVALID_OPERATION after OpenGL 3.1 context creation

时间:2015-11-12 10:51:55

标签: c++ opengl

I have another hopefully not THAT stupid question- I wanted to clean up some old code I updated from OpenGL1.3 to 3.1. So I changed now in a final step the context creation to something like this (own code below): https://www.opengl.org/wiki/Tutorial:_OpenGL_3.1_The_First_Triangle_%28C%2B%2B/Win%29

I am using GLEW 1.13.0 and if I do only glewInit additionally to the old context creation everything works fine, but after switching to a pure 3.1 context:

glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST); is giving me GL_INVALID_ENUM
glAlphaFunc( GL_GREATER, 0.5 ); is giving me GL_INVALID_OPERATION
glDisable( GL_ALPHA_TEST ); is giving me GL_INVALID_ENUM
glEnable(GL_NORMALIZE); is giving me GL_INVALID_ENUM

now from what I read none of these is deprecated in OpenGL3.1 - What did I miss?

It currently looks like this:

hWnd = (HWND)GetWindow();
hDC = GetDC( hWnd );

INT DesiredColorBits   = 32;
INT DesiredStencilBits = 8;
INT DesiredDepthBits   = 24;

PIXELFORMATDESCRIPTOR pfd;
memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = DesiredColorBits;
pfd.cDepthBits = DesiredDepthBits;
pfd.iLayerType = PFD_MAIN_PLANE;
pfd.cStencilBits = DesiredStencilBits;

INT nPixelFormat = ChoosePixelFormat( hDC, &pfd );
printf("Using pixel format %i", nPixelFormat);
if (!SetPixelFormat( hDC, nPixelFormat, &pfd ))
    return 0;

hRC = wglCreateContext( hDC );
wglMakeCurrent(hDC, hRC);

//init glew
glewExperimental = GL_TRUE;
GLenum err = glewInit();
if (GLEW_OK != err)
     return 0;

INT MajorVersion=3;
INT MinorVersion=1;

if (WGLEW_ARB_create_context && WGLEW_ARB_pixel_format)
{
    memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
    pfd.nSize= sizeof(PIXELFORMATDESCRIPTOR);
    pfd.nVersion   = 1;
    pfd.dwFlags    = PFD_DOUBLEBUFFER | PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW;
    pfd.iPixelType = PFD_TYPE_RGBA;
    pfd.cColorBits = 32;
    pfd.cDepthBits = 24;
    pfd.iLayerType = PFD_MAIN_PLANE;

    const INT iPixelFormatAttribList[] =
    {
        WGL_DRAW_TO_WINDOW_ARB, GL_TRUE,
        WGL_SUPPORT_OPENGL_ARB, GL_TRUE,
        WGL_DOUBLE_BUFFER_ARB, GL_TRUE,
        WGL_PIXEL_TYPE_ARB, WGL_TYPE_RGBA_ARB,
        WGL_COLOR_BITS_ARB, DesiredColorBits,
        WGL_DEPTH_BITS_ARB, DesiredDepthBits,
        WGL_STENCIL_BITS_ARB, DesiredStencilBits,
        0 // End of attributes list
    };
    INT iContextAttribs[] =
    {
        WGL_CONTEXT_MAJOR_VERSION_ARB, MajorVersion,
        WGL_CONTEXT_MINOR_VERSION_ARB, MinorVersion,
        WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB,
        0 // End of attributes list
    };

    INT iPixelFormat, iNumFormats;
    wglChoosePixelFormatARB(hDC, iPixelFormatAttribList, NULL, 1, &iPixelFormat, (UINT*)&iNumFormats);

    // pfd oldstyle crap...
    if (!SetPixelFormat(hDC, iPixelFormat, &pfd))
        return 0;

    hRC = wglCreateContextAttribsARB(hDC, 0, iContextAttribs);
}

if(hRC) 
{
    printf("GL_VENDOR     : %s", glGetString(GL_VENDOR));
    printf("GL_RENDERER   : %s", glGetString(GL_RENDERER));
    printf("GL_VERSION    : %s", glGetString(GL_VERSION));
    printf("GLEW Version  : %s", glewGetString(GLEW_VERSION));

    wglMakeCurrent(hDC, hRC);
}
else 
    return 0;

I stripped out the code, so sorry if I missed something, but the context is initialized and I am getting:

Using pixel format 9
GL_VENDOR     : NVIDIA Corporation
GL_RENDERER   : GeForce GT 740M/PCIe/SSE2
GL_VERSION    : 4.4.0
GLEW Version  : 1.13.0

The first part in the code initializes the old style context in order to get GLEW initialized and while I am a bit unsure about the PFD part in the 3.1 creation (although many examples show it like this). Nevertheless I tried a few different variants from different examples and tutorials and it always resulted in the GL_INVALID_OPERATION and GL_INVALID_ENUM when trying to set the states above.

1 个答案:

答案 0 :(得分:2)

GL_PERSPECTIVE_CORRECTION_HINT isn't in core anymore, and neither it was in 3.1. Refer to the OpenGL wiki for details, but allowed enums in 3.1 (and staying the same up to 4.5) are:

  • GL_LINE_SMOOTH_HINT
  • GL_POLYGON_SMOOTH_HINT
  • GL_TEXTURE_COMPRESSION_HINT
  • GL_FRAGMENT_SHADER_DERIVATIVE_HINT

That being said, don't bother with creating 3.1 contexts. If you can, go for 4.4/4.5, if you want to support previous generation, 3.3 is the reasonable minimum.