【问题标题】:Translate ARB assembly to GLSL?将 ARB 程序集翻译成 GLSL?
【发布时间】:2017-08-01 03:44:12
【问题描述】:

我正在尝试将一些旧的 OpenGL 代码转换为现代 OpenGL。此代码正在从纹理中读取数据并显示它。片段着色器当前是使用ARB_fragment_program 命令创建的:

static const char *gl_shader_code =
"!!ARBfp1.0\n"
"TEX result.color, fragment.texcoord, texture[0], RECT; \n"
"END";

GLuint program_id;
glGenProgramsARB(1, &program_id);
glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, program_id);
glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, (GLsizei) strlen(gl_shader_code ), (GLubyte *) gl_shader_code );

我只是想把它翻译成 GLSL 代码。我认为片段着色器应该是这样的:

    #version 430 core
    uniform sampler2DRect s;

    void main(void) 
    {
        gl_FragColor = texture2DRect(s, ivec2(gl_FragCoord.xy), 0);
    }

但我不确定一些细节:

  1. texture2DRect 的用法正确吗?
  2. gl_FragCoord 的用法正确吗?

正在使用GL_PIXEL_UNPACK_BUFFER 目标为纹理提供像素缓冲区对象。

【问题讨论】:

    标签: opengl glsl


    【解决方案1】:
    1. 我认为您可以只使用标准的sampler2D 而不是sampler2DRect(如果您没有真正需要它),因为引用the wiki,“从现代的角度来看,它们(矩形纹理)似乎基本上没用。”。

    然后您可以将texture2DRect(...) 更改为texture(...)texelFetch(...)(以模仿您的矩形抓取)。

    1. 由于您似乎使用的是 OpenGL 4,因此您不需要(不应该?)使用 gl_FragColor,而是声明一个 out 变量并写入它。

    你的片段着色器最终应该是这样的:

    #version 430 core
    uniform sampler2D s;
    out vec4 out_color;
    
    void main(void) 
    {
        out_color = texelFecth(s, vec2i(gl_FragCoord.xy), 0);
    }
    

    【讨论】:

      【解决方案2】:

      @Zouch,非常感谢您的回复。我接受了它并为此工作了一段时间。我的最终核心与您建议的非常相似。作为记录,我实现的最终顶点和片段着色器如下:

      顶点着色器:

      #version 330 core
      
      layout(location = 0) in vec3 vertexPosition_modelspace;
      layout(location = 1) in vec2 vertexUV;
      
      out vec2 UV;
      
      uniform mat4 MVP;
      
      void main()
      {
          gl_Position = MVP * vec4(vertexPosition_modelspace, 1);
          UV = vertexUV;
      }
      

      片段着色器:

      #version 330 core
      
      in vec2 UV;
      
      out vec3 color; 
      
      uniform sampler2D myTextureSampler;
      
      void main()
      {
          color = texture2D(myTextureSampler, UV).rgb; 
      }
      

      这似乎行得通。

      【讨论】:

      • 很高兴它有效。请注意 texture2D 函数在 OpenGL 3.3 中已弃用,您应该改用 texture 函数(相同的签名)
      猜你喜欢
      • 2014-05-31
      • 1970-01-01
      • 1970-01-01
      • 2017-11-21
      • 2016-02-10
      • 1970-01-01
      • 2022-07-05
      • 2016-01-05
      • 1970-01-01
      相关资源
      最近更新 更多