Value of alpha sampled from texture loaded in form of GL_RED

Hi all,

I would like to ask for clarification of alpha value sampled from 3D texture which is loaded in form of GL_RED(glTexSubImage3D) from a stack of grayscale images. In my opinion, the sampled value should be 255 for all coordinates. Am i correct??

Thanks a lot

Well, GL_RED is an unsigned, normalized integer format, so the value you sample in your shader will be 1.0.

If you’re talking about a fixed-function GL process, then the value fetched will be the largest value for whatever internal bitdepth that GL_RED choose (since you didn’t specify a bitdepth). So it would only be 255 if the bitdepth the implementation choose was 8-bits. So it will be whatever would convert to 1.0 in unsigned, normalized integers of that bitdepth.

Also, it doesn’t really matter what format you used for TexSubImage. The internalformat set by TexImage (or TexStorage) is what dictates the alpha channel.

For example, if you TexImage RGBA8, then TexSubImage RED, those texels will be converted to RGBA8 with alpha 1.0, and that’s what you’ll sample. But there’s nothing stopping you from changing the alpha to other values after that. Whereas, if the internalformat was R8, then the alpha will always be 1.0.

Also the texture swizzle state can change all of this.

[QUOTE=Alfonse Reinheart;1265069]Well, GL_RED is an unsigned, normalized integer format, so the value you sample in your shader will be 1.0.

If you’re talking about a fixed-function GL process, then the value fetched will be the largest value for whatever internal bitdepth that GL_RED choose (since you didn’t specify a bitdepth). So it would only be 255 if the bitdepth the implementation choose was 8-bits. So it will be whatever would convert to 1.0 in unsigned, normalized integers of that bitdepth.[/QUOTE]


layout(location = 0) out vec4 vFragColor;	//fragment shader output
//There are some codes in between, i will post it if you want coz i thought it would be too messy
//FYI, the source code is from OpenGL Development Cookbook
float prev_alpha = sample - (sample * vFragColor.a);

Assume that the image loaded is a grayscale 8 bit image sequence into format of GL_RED (without specifying bitdepth) for volume rendering, so, what is the value produced by vFragColor.a as i cannot printf the value in glsl shader and also have no idea in using color to debug the code.
Thanks a lot.

[QUOTE=arekkusu;1265079]Also, it doesn’t really matter what format you used for TexSubImage. The internalformat set by TexImage (or TexStorage) is what dictates the alpha channel.

For example, if you TexImage RGBA8, then TexSubImage RED, those texels will be converted to RGBA8 with alpha 1.0, and that’s what you’ll sample. But there’s nothing stopping you from changing the alpha to other values after that. Whereas, if the internalformat was R8, then the alpha will always be 1.0.

Also the texture swizzle state can change all of this.[/QUOTE]

Hi,

Thanks for reply and explanation, but i still have few doubts in regards of handling of texture in OpenGL.

Do you mean TexImage by the texture i am loading into OpenGL for TexSubImage??
Besides, do texels still preserve the value of pixel of the image that is loaded in as texture?? In my understanding, if the image is loaded in as texture with format of GL_RED, the pixel value is located in the r channel of the texture, am i right??

Sorry for lot of questions.
Thanks a lot.

[QUOTE=vincent911001;1265093]


layout(location = 0) out vec4 vFragColor;	//fragment shader output
//There are some codes in between, i will post it if you want coz i thought it would be too messy
//FYI, the source code is from OpenGL Development Cookbook
float prev_alpha = sample - (sample * vFragColor.a);

Assume that the image loaded is a grayscale 8 bit image sequence into format of GL_RED (without specifying bitdepth) for volume rendering, so, what is the value produced by vFragColor.a as i cannot printf the value in glsl shader and also have no idea in using color to debug the code.
Thanks a lot.[/QUOTE]

It’s undefined because you cannot read the value from an output variable. That is undefined behavior (unless you’ve written to that variable first). Not unless you’re using OpenGL ES and an extension. In which case, you should let us know, because reading from an output variable is different from sampling from a texture.

Not, you know, a lot. But it’s good to have all the facts up front.

On the assumption that you are using both OpenGL ES and an extension that lets you read the framebuffer via a fragment shader output variable, and the current image in the framebuffer uses an internal format of GL_RED, and this represents an 8-bit colordepth, then the value you will read for the alpha ought to be 1.0. GL_RED is normalized, so it will be read like a normalized integer.

[QUOTE=Alfonse Reinheart;1265099]It’s undefined because you cannot read the value from an output variable. That is undefined behavior (unless you’ve written to that variable first). Not unless you’re using OpenGL ES and an extension. In which case, you should let us know, because reading from an output variable is different from sampling from a texture.

Not, you know, a lot. But it’s good to have all the facts up front.

On the assumption that you are using both OpenGL ES and an extension that lets you read the framebuffer via a fragment shader output variable, and the current image in the framebuffer uses an internal format of GL_RED, and this represents an 8-bit colordepth, then the value you will read for the alpha ought to be 1.0. GL_RED is normalized, so it will be read like a normalized integer.[/QUOTE]

Thanks,
I am using OpenGL, not the ES version, the code snippet is of the fragment shader used for raycasting for volume rendering. Pardon, btw, is it possible to set the value of vFragColor via another shader as below:

#version 330 core

layout(location = 0) out vec4 vFragColor;	//fragment shader output

void main()
{
	//return the constant white colour as shader output
	vFragColor = vec4(1,1,1,1);
}

If it is possible, then it would be utter stupidity of mine ;):frowning:

One last question, what do you mean by normalized value??

Thanks alot for those valuable clarification.

[QUOTE=vincent911001;1265100]Thanks,
I am using OpenGL, not the ES version, the code snippet is of the fragment shader used for raycasting for volume rendering. Pardon, btw, is it possible to set the value of vFragColor via another shader as below:

#version 330 core

layout(location = 0) out vec4 vFragColor;	//fragment shader output

void main()
{
	//return the constant white colour as shader output
	vFragColor = vec4(1,1,1,1);
}

If it is possible, then it would be utter stupidity of mine ;):([/quote]

I’m not sure what you mean by “possible”. 95% of the point of a fragment shader is to set the output colors for that fragment. So yes, it is possible for a fragment shader to set its output variables :wink: Which, assuming that nothing else gets in the way, will be what gets written to the framebuffer.

Now, you can’t read that value from a later fragment shader. Well, not without unbinding the framebuffer/image or some trickery.

I mean what I linked to in my first post.

[QUOTE=Alfonse Reinheart;1265102]I’m not sure what you mean by “possible”. 95% of the point of a fragment shader is to set the output colors for that fragment. So yes, it is possible for a fragment shader to set its output variables :wink: Which, assuming that nothing else gets in the way, will be what gets written to the framebuffer.

Now, you can’t read that value from a later fragment shader. Well, not without unbinding the framebuffer/image or some trickery.

I mean what I linked to in my first post.[/QUOTE]

Thanks a lot.

So, it is possible that the raycast shader get its vFragColor.a value from the value written in vFragColor = vec4(1,1,1,1); in another shader considering the location = 0 is the same for them. Am i right??

Sorry for taking much of your time for those clarification:angel::smiley:

[QUOTE=vincent911001;1265107]Thanks a lot.

So, it is possible that the raycast shader get its vFragColor.a value from the value written in vFragColor = vec4(1,1,1,1); in another shader considering the location = 0 is the same for them. Am i right??[/quote]

cough “Now, you can’t read that value from a later fragment shader.”

Thanks alot for your time, get well soon.
Really thanks for your clarification, will need to catch up with some extra reading