OpenGL版本问题

Pan*_*sal -1 c++ opengl glsl

我正在使用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兼容,并且具有"属性","变化"等关键字,不应与OpenGL 3.1或更高版本一起使用.我应该将我的着色器更新为OpenGLSL 3.1,比如使用"in","out"等.还有一些不赞成的东西,比如GL_LINE_SMOOTH_HINT,glMatrixMode(GL_PROJECTION).我认为这些东西已经从OpenGL 3.0中弃用了.那么这些东西如何与GL上下文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);
}
Run Code Online (Sandbox Code Playgroud)

我正在使用的着色器之一

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";
Run Code Online (Sandbox Code Playgroud)

基本上我需要更新我的着色器和其他代码以使用OpenGL 3.1和更高的上下文.我已经与OpenGL脱节了一段时间了.如果它太天真的问题那就很抱歉

Nic*_*las 6

OpenGL 3.1确实删除了以前弃用的功能.然而,3.1还引入了ARB_compatibility扩展,这有效地带回了它.如果您要求3.1版,您的实现将决定您是否也可以使用已删除的功能.

对于3.2或更高版本,OpenGL分为核心和兼容性配置文件.如果您需要兼容性配置文件wgl/glXCreateContextAttribsARB,则必须通过设置CONTEXT_CORE_PROFILE_BIT_ARB为明确要求CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB.否则,当您使用3.2或更高版本的ARB_create_context_attrib时,将假定核心配置文件.

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