将ARB汇编翻译成GLSL?

4

我正在尝试将一些旧版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目标的像素缓冲对象进行提供。

2个回答

0
  1. 我认为你可以只使用标准的 sampler2D 而不是 sampler2DRect(如果你没有真正需要它),因为引用 维基百科 的话,“从现代的角度来看,它们(矩形纹理)似乎基本上没什么用处。”。

然后,您可以将您的 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);
}

0
@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; 
}

看起来好像可以了。


很高兴它能正常工作。请注意,在OpenGL 3.3中,texture2D函数已被弃用,您应该使用texture函数代替(具有相同的签名)。 - cmourglia

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接