PDA

View Full Version : Value of alpha sampled from texture loaded in form of GL_RED



vincent911001
03-23-2015, 07:52 PM
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

Alfonse Reinheart
03-24-2015, 06:01 AM
Well, GL_RED is an unsigned, normalized integer (https://www.opengl.org/wiki/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.

arekkusu
03-24-2015, 09:19 AM
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.

vincent911001
03-24-2015, 06:22 PM
Well, GL_RED is an unsigned, normalized integer (https://www.opengl.org/wiki/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.



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.

vincent911001
03-24-2015, 06:32 PM
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.

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.

Alfonse Reinheart
03-24-2015, 07:19 PM
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.

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.

vincent911001
03-24-2015, 07:40 PM
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.

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

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

Thanks alot for those valuable clarification.

Alfonse Reinheart
03-24-2015, 07:51 PM
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 ;):(

I'm not sure what you mean by "possible". 95% of the point of a fragment shader (https://www.opengl.org/wiki/Fragment_Shader) is to set the output colors for that fragment (https://www.opengl.org/wiki/Fragment). So yes, it is possible for a fragment shader to set its output variables ;) Which, assuming that nothing else gets in the way (https://www.opengl.org/wiki/Per-Sample_Processing), 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 (https://www.opengl.org/wiki/Memory_Model#Framebuffer_objects).


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

I mean what I linked (https://www.opengl.org/wiki/Normalized_Integer) to in my first post.

vincent911001
03-24-2015, 08:19 PM
I'm not sure what you mean by "possible". 95% of the point of a fragment shader (https://www.opengl.org/wiki/Fragment_Shader) is to set the output colors for that fragment (https://www.opengl.org/wiki/Fragment). So yes, it is possible for a fragment shader to set its output variables ;) Which, assuming that nothing else gets in the way (https://www.opengl.org/wiki/Per-Sample_Processing), 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 (https://www.opengl.org/wiki/Memory_Model#Framebuffer_objects).



I mean what I linked (https://www.opengl.org/wiki/Normalized_Integer) to in my first post.

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::D

Alfonse Reinheart
03-24-2015, 10:39 PM
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??

*cough* "Now, you can't read that value from a later fragment shader."

vincent911001
03-25-2015, 12:05 AM
*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