Results 1 to 3 of 3

Thread: Can a shader produce a fully colored result given a GL_RED texture?

  1. #1
    Newbie Newbie
    Join Date
    May 2015
    Posts
    1

    Can a shader produce a fully colored result given a GL_RED texture?

    OpenGL beginner here. I was hoping I could do something like transmit a texture using GL_RED and R8, but then have my shader produce a fully colored result. At present even if my shader writes the green and blue colors, they are ignored and I get varying shades of red on the screen. However, if I use swizzle masks and tell it to pull green and blue from the red, I get a nice grayscale image. I want not grayscale, but to treat my unsigned byte as a color map... i.e., different values correspond to different colors. This would save a lot of space, but I can't get the idea to work. This is for 2D textures. Is this possible and I'm doing something wrong, or do I have to switch to an internal GL_RGB format in order to have my shader produce a colored texture?

  2. #2
    Senior Member OpenGL Lord
    Join Date
    Mar 2015
    Posts
    6,678
    Quote Originally Posted by TodOneD View Post
    At present even if my shader writes the green and blue colors, they are ignored and I get varying shades of red on the screen. However, if I use swizzle masks and tell it to pull green and blue from the red, I get a nice grayscale image.
    Well, that suggests that you're trying to write all of the components you fetch from the texture directly to the screen. Something that would look like this:

    Code :
    out_color = texture(sampler, uv);

    When you fetch a color from a color image format that doesn't provide all four channels, any unprovided channels will by default be 0. Except the alpha which will be 1. So a red texture will appear... red. Which is why it's called that

    If you want to broadcast the red component across all 4 components, you can just do that in your shader.

    Code :
    out_color = texture(sampler, uv).rrrr;

  3. #3
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    3,112
    Quote Originally Posted by TodOneD View Post
    I want not grayscale, but to treat my unsigned byte as a color map..
    You can certainly implement that in a shader, i.e. read a single value from a source texture then use that value to retrieve a colour from a palette texture.

    If the palette is a continuous gradient, you can use a normalised (e.g. GL_R source texture from which you retrieve a floating-point intensity value between 0.0 and 1.0 via a sampler2D uniform, which is then used as a texture coordinate into the palette texture. E.g.
    Code :
    uniform sampler2D u_texture;
    uniform sampler1D u_palette;
    in vec2 a_texcoord;
    ...
        float value = texture(u_texture, a_texcoord);
        gl_FragColor = texture(u_palette, value);

    If the palette is a discrete set of colours, you need to use integer colour indices, so the source texture should have a format of GL_RED_INTEGER, an internal format of e.g. GL_R8UI, and be accessed via a usampler2D uniform. The corresponding texel can be retrieved from the palette texture using texelFetch() (which takes integer array indices rather than floating-point coordinates). E.g.
    Code :
    uniform usampler2D u_texture;
    uniform sampler1D u_palette;
    in vec2 a_texcoord;
    ...
        uint value = texture(u_texture, a_texcoord);
        gl_FragColor = texelFetch(u_palette, int(value), 0);

Similar Threads

  1. Texture coordinates produce unexpected result
    By wenhaug in forum OpenGL: Basic Coding
    Replies: 6
    Last Post: 05-01-2018, 04:24 PM
  2. Replies: 0
    Last Post: 02-14-2017, 11:32 AM
  3. Value of alpha sampled from texture loaded in form of GL_RED
    By vincent911001 in forum OpenGL: Basic Coding
    Replies: 10
    Last Post: 03-25-2015, 01:05 AM
  4. Replies: 2
    Last Post: 07-27-2014, 12:46 AM
  5. Texture storage demands: GL_RED vs. GL_RGBA
    By alfoa in forum OpenGL: Basic Coding
    Replies: 7
    Last Post: 03-22-2012, 09:05 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean