PDA

View Full Version : Problem with gl_FragColor()



SagoO
04-04-2011, 11:05 PM
Hi,

I have few questions:

1. How gl_FragColor() output to frame buffer?
Eg: gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
How will the frame buffer output looks like if I use this command.

2. Can I set every pixels on the frame buffer to 1 or 0 in GLubyte using gl_FragColor or other functions?

3. I already disable all the modes such as:
/*Disable modes*/
glDisable(GL_LIGHTING);
glDisable(GL_LIGHT0);
glDisable(GL_BLEND);
glDisable(GL_DEPTH_TEST);

So, if I just want to output the color value onto the frame buffer without using the texture value, is it possible if using gl_FragColor()? What I means here is actually set the pixels on the frame buffer myself without calculation using the texture data.

Because the texture data is just to be used for comparison and decide which color to output to the frame buffer in my case.

4. The format of the texture will directly/indirectly affect the format of the frame buffer? For example, I use GL_LUMINANCE for my texture. So, the frame buffer will be in GL_LUMINANCE also? Or I can change the format of the frame buffer as well?

Sorry if I am asking weird questions again. But those questions are very important for me to understand more to complete my project. Thanks for you reply.

mobeen
04-04-2011, 11:18 PM
Ans 1) The output color will be red.
Ans 2) The gl_FragColor is a vec4 type of float values in the range 0 to 1. So the answer to whether u can use GLubyte is no.
Ans 3) Nobody is stopping u from outputting any color u like from gl_FragColor whether u get it from a texture or from a calculation is up to u. The question is unclear to me what exactly do u want to do?
Ans 4) The format of the texture is independent of the format of the framebuffer.

SagoO
04-04-2011, 11:42 PM
Ans 1) The output color will be red.
Ans 2) The gl_FragColor is a vec4 type of float values in the range 0 to 1. So the answer to whether u can use GLubyte is no.
Ans 3) Nobody is stopping u from outputting any color u like from gl_FragColor whether u get it from a texture or from a calculation is up to u. The question is unclear to me what exactly do u want to do?
Ans 4) The format of the texture is independent of the format of the framebuffer.

Thank for your answer. Much clearer for me right now. It means that even though I use GL_LUMINANCE for my texture but I still can have a RGBA format on the frame buffer.

Here is what I am doing:

1. I have a texture with GL_LUMINANCE format. So, each pixels only contains one byte of value. (Done)
2. I create a shader to read out the value in the texture. Compare and decide to output what color to the pixels on frame buffer. I decide to use red component only. So, if match, I set Red component to 1.0 else set to 0.0.(Done. But not yet test)
3. I want to read the red component of every pixels from the frame buffer. (Not yet done due to some errors)


Questions:

1. By the way, do I need to set the format of the frame buffer? Or I will according to the format I use when I use glReadPixels()?

2. How to read the red component only from a RGBA format?

Alfonse Reinheart
04-04-2011, 11:43 PM
So, if I just want to output the color value onto the frame buffer without using the texture value, is it possible if using gl_FragColor()?

You're really overthinking this. You're thinking like someone using the fixed-function pipeline, not like someone using a shader.

Your fragment shader outputs exactly what you specify. If you happen to use a texture or two (or ten) to compute that value, it doesn't matter. What matters is the value that the fragment shader outputs.

Shaders are like function calls. You give them input parameters and some globals (uniforms), and they return output values. Exactly what goes on in there is up to you. You can return a constant value, you can do some texture lookups, you can run a noise function for procedural texturing which gets piped into normal offset computations for bump mapping that uses a texture representing a Gaussian specular distribution. It's all up to you.

Textures are nothing more than fancy array accesses. They're array lookups with an optional filtering engine attached to them. The effect they have on the fragment shader output is exactly and only the effect that your fragment shader specifies that they do.


The gl_FragColor is a vec4 type of float values in the range 0 to 1. So the answer to whether u can use GLubyte is no.

This is not true. gl_FragColor is just a vec4. If there is clamping to the [0, 1] range, it will happen after the fragment shader outputs its values.

SagoO
04-04-2011, 11:50 PM
Thank. I understand much more now.

Actually I plan to use GL_RED format only if possible. But gl_FragColor() is a vec4. So, I have to choose RGBA format in glReadPixels to read out from the frame buffer. But I just want to read out the RED component from the frame buffer only. Is it possible? How?

In short, I hope that there are only 2 values in frame buffer at the end which are 0 and 1.

Alfonse Reinheart
04-05-2011, 12:12 AM
So, I have to choose RGBA format in glReadPixels to read out from the frame buffer.

Why? glReadPixels does not read gl_FragCoord. It reads the values in the framebuffer. And if your framebuffer is of the format GL_RED8, then it will have only a red component, regardless of how much extra data gl_FragCoord contained.

The OpenGL specification covers this post-fragment shader processing in detail.

SagoO
04-05-2011, 12:36 AM
Yup. I just want to read the values from frame buffer but not the gl_FragCoord.

Because I am confused here. If I use gl_FragColor(), the frame buffer should be in RGBA format right since gl_FragColor() is a vec4 to store rgba value.

If it is correct, then why need to set the frame buffer since the gl_FragColor() is in RGBA format. So, it will output the value to the frame buffer in RGBA format too?

How to set the frame buffer format?