3
votes

I'm trying to translate some old OpenGL code to modern OpenGL. This code is reading data from a texture and displaying it. The fragment shader is currently created using ARB_fragment_program commands:

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 );

I'd simply like to translate this into GLSL code. I think the fragment shader should look something like this:

    #version 430 core
    uniform sampler2DRect s;

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

But I'm not sure of a couple of details:

  1. Is this the right usage of texture2DRect?
  2. Is this the right usage of gl_FragCoord?

The texture is being fed with a pixel buffer object using GL_PIXEL_UNPACK_BUFFER target.

2

2 Answers

0
votes
  1. I think you can just use the standard sampler2D instead of sampler2DRect (if you do not have a real need for it) since, quoting the wiki, "From a modern perspective, they (rectangle textures) seem essentially useless.".

You can then change your texture2DRect(...) to texture(...) or texelFetch(...) (to mimic your rectangle fetching).

  1. Since you seem to be using OpenGL 4, you do not need to (should not ?) use gl_FragColor but instead declare an out variable and write to it.

Your fragment shader should look something like this in the end:

#version 430 core
uniform sampler2D s;
out vec4 out_color;

void main(void) 
{
    out_color = texelFecth(s, vec2i(gl_FragCoord.xy), 0);
}
0
votes

@Zouch, thank you very much for your response. I took it and worked on this for a bit. My final cores were very similar to what you suggested. For the record the final vertex and fragment shaders I implemented were as follows:

Vertex Shader:

#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;
}

Fragment Shader:

#version 330 core

in vec2 UV;

out vec3 color; 

uniform sampler2D myTextureSampler;

void main()
{
    color = texture2D(myTextureSampler, UV).rgb; 
}

That seemed to work.