OpenGL 版本问题

OpenGL version issues

我正在使用 OpenGL,但我使用的版本有一些问题。当我使用 glGetString(GL_VERSION) 检查我的版本时,我得到 "4.2.0 - Build 10.18.10.3574"。我有以下问题:

  1. 在一种方法中,我使用固定管道进行渲染,但我知道对固定管道的支持已从 OpenGL 3.1 中删除,那么我的代码如何与 4.2 配合使用?我正在使用 API,如 glTranslatef()glMatrixMode(GL_PROJECTION)。我认为这些都已弃用并且仅适用于 OpenGL 1.1。那么,当我将上下文字符串作为 OpenGL 4.2 获取时,代码如何工作?我想我一定是在使用 OpenGL 1.1,但为什么上下文版本是 4.2?

  2. 当我使用着色器时,我正在使用以下代码创建 3.1 或更高版本的上下文。我使用的着色器是与 OpenGL 2.0 兼容的旧着色器,具有 "attribute"、"varying" 等关键字,不应该与 OpenGL 3.1 或更高版本一起使用。我需要将我的着色器更新到 OpenGLSL 3.1,例如使用 "in"、"out" 等。还有一些已弃用的东西,例如 GL_LINE_SMOOTH_HINTglMatrixMode(GL_PROJECTION)。我认为这些东西已从 OpenGL 3.0 中弃用。那么这些东西如何与 GL context 3.1 及更高版本一起使用?

创建上下文的代码 3.1 或更高版本

if ( 0 == (m_tempContext = ::wglCreateContext( m_pDC->GetSafeHdc() ) ) )
{
    AfxMessageBox(_T("wglCreateContext failed."));
    return FALSE;
}
if ( FALSE == ::wglMakeCurrent( m_pDC->GetSafeHdc(), m_tempContext ) )
{
    AfxMessageBox(_T("wglMakeCurrent failed."));
    return FALSE;
}
int attribs[] =
{
    WGL_CONTEXT_MAJOR_VERSION_ARB, 3,
    WGL_CONTEXT_MINOR_VERSION_ARB, 1,
    WGL_CONTEXT_FLAGS_ARB, 0,
    0
};

if(wglewIsSupported("WGL_ARB_create_context") == 1)
{
m_hRC = wglCreateContextAttribsARB(m_pDC->GetSafeHdc() ,0, attribs);
wglMakeCurrent(NULL,NULL);
wglDeleteContext(m_tempContext);
wglMakeCurrent( m_pDC->GetSafeHdc(), m_hRC);
}

我正在使用的着色器之一

const GLchar* vertexShaderCode =                                        \
    "attribute vec4 a_Position;                                \n"  \
    "attribute vec2 a_TexCoordinate;                           \n"  \
    "uniform mat4 u_MVPMatrix;                                 \n"  \
    "varying vec2 v_TexCoordinate;                             \n"  \
                                                                    \
    "void main()                                               \n"  \
    "{                                                         \n"  \
    "    gl_Position = u_MVPMatrix * a_Position;               \n"  \
    "    v_TexCoordinate = a_TexCoordinate;                    \n"  \
    "}                                                         \n"; \
                                                                    \
const GLchar* fragmentShaderCode =                                      \
    "#extension GL_OES_standard_derivatives : enable            \n" \
    "#ifdef GL_ES                                               \n" \
    "precision highp float;                                     \n" \
    "#endif                                                     \n" \
    "varying vec2       v_TexCoordinate;                        \n" \
    "uniform sampler2D  u_Texture;                              \n" \
    "uniform vec4       u_Color;                                \n" \
    "uniform float      u_InsideCutoff;                         \n" \
    "uniform float      u_OutsideCutoff;                        \n" \
    "uniform float      u_TextureSize;                          \n" \
                                                                    \
    "void main()                                                \n" \
    "{                                                          \n" \
    "   vec4 distance = texture2D(u_Texture, v_TexCoordinate);  \n" \
    "   vec3 color;                                             \n" \
    "   float ic;                                               \n" \
    "   float oc;                                               \n" \
    "   float alpha;                                            \n" \
                                                                    \
    "   vec4  duvdxy  = vec4(dFdx(v_TexCoordinate), dFdy(v_TexCoordinate));              \n" \
    "   float pixSize = length(u_TextureSize*duvdxy);                                    \n" \
    "   ic = 0.5 + u_InsideCutoff*pixSize;                                               \n" \
    "   oc = 0.5 + u_OutsideCutoff*pixSize;                                              \n" \
    "   alpha = (clamp(distance.r,oc,ic) - oc)/(ic-oc);                                  \n" \
    "   alpha *= u_Color.a;                                                              \n" \
    "   color = u_Color.rgb;                                                             \n" \
    "   gl_FragColor = vec4(color, alpha);                                               \n" \
    "}                                                                                   \n";

基本上我需要更新我的着色器和其他代码以使用 OpenGL 3.1 和更高的上下文。我已经有一段时间没有接触 OpenGL 了。很抱歉,如果这个问题太天真了

OpenGL 3.1 确实删除了以前弃用的功能。但是,3.1 还引入了 ARB_compatibility 扩展,有效地将其全部恢复。如果您要求版本 3.1,您的 实现 将决定您是否也可以使用已删除的功能。

对于 3.2 或更高版本,OpenGL 被拆分为 core and compatibility profiles。如果您想要 wgl/glXCreateContextAttribsARB 的兼容性配置文件,您必须通过将 CONTEXT_CORE_PROFILE_BIT_ARB 设置为 CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB 来明确请求它。否则,当您对 3.2 或更高版本使用 ARB_create_context_attrib 时,将假定核心配置文件。

请注意,如果您使用 wglCreateContext,您将始终获得兼容性配置文件(或 core/compatibility 区别之前的 OpenGL 版本)。