Variable data in fragment shader

varying vec3 vertex; // Vertex position in world

void main( void )
{
vec3 vvertex = normalize(vertex) + 1.0;

float x = vvertex.x;

if ( x < 1.0 )
x = 0.0;
else
x = 1.0;

gl_FragColor = vec4( x, 0.0, 0.0, 1.0 );
}

Both the code above and the one below draws a red pixel on the screen. What am I missing? (Hopefully you can tell I am frustrated about this)

varying vec3 vertex; // Vertex position in world

void main( void )
{
vec3 vvertex = normalize(vertex) + 1.0;

float x = vvertex.x;

if ( x >= 1.0 )
x = 0.0;
else
x = 1.0;

gl_FragColor = vec4( x, 0.0, 0.0, 1.0 );
}

So, what’s the problem? The code looks correct (although unnecessarily funky with the if/else dual use of x).

There are two fragment programs up there where one activates red is x is less than 1.0 and the other one activates red if x is greater than or equal to one. Given the same values of x (vvertex) with the two programs it always draws a red.

How can x be both greater than equal to 1 and less than 1 at the same time?

Because you probably normalize a null vector which produces a division by 0, and probably an undefined value or a NaN (not a number).
An undefined value is neither positive nor negative, just undefined.

Read the section that starts with
“A second pitfall to be aware of is that sometimes a variable may have an “undefined” value…” in the following reference:

http://www.davidcornette.com/glsl/glsl.html

oh wow that is exactly it.
so you are saying that vec3 vertex may have a length 0?

I’m just trying to draw the world coordinates of pixels on the screen as vectors represented by colors.

What I do is I just multiply the vertex coordinates by model matrix in the vertex program and have it interpolate over the surface.

First of all that will definitely lead into negative numbers and for that I just added 1000.0 to the vector for now. It is not the robust way but it makes sense as the models are at most around -100 in x,y,z. But it definitely should not lead into zeros.

Do you know of the correct way to draw the world coordinates like you would draw the normals of pixels?

Do you know of the correct way to draw the world coordinates like you would draw the normals of pixels?

Write it to a floating-point buffer? If you’re trying to do deferred rendering, that’s generally the way to go.

Yes definitely but I am trying to visualize the texture that I’ve drawn on to. How do I get rid of the negative values of the coordinate?

‘Normalize add 1 div by 2’
that’s what I was looking for. Thanks :slight_smile:

I have two quick questions:

  1. How do you visualize the world coordinates? What I have is this and it only outputs 0,0,0.

VERTEX PROGRAM

uniform mat4 modelMatrix;
varying vec3 vertex; // Vertex position in world
void main()
{
vec3 vertex = ( normalize( modelMatrix * gl_Vertex ) + 1.0 ) / 2.0;
gl_Position = ftransform();
}

FRAGMENT PROGRAM

varying vec3 vertex; // Vertex position in world
void main( void )
{
vertex = normalize(vertex);
gl_FragColor = vec4( vertex.x, vertex.y, vertex.z, 1.0 );}

2.How can you write the world coordinates to a floating point buffer as they are (with the negative values)?

>>2.How can you write the world coordinates to a floating point buffer as they are (with the negative values)?
I write in a FBO with 1-4 attached RGBA32f textures, when I debug.

dogdemir:


vec3 vertex = ( normalize( modelMatrix * gl_Vertex ) + 1.0 ) / 2.0;

You are redeclaring ‘vertex’ in local scope, so aren’t actually writing to the varying one.

That is definitely it :slight_smile:

So if I create an fbo and attach a texture this way to it:

glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA16, SCREEN_W, SCREEN_H, 0, GL_RGBA, GL_BYTE, NULL );

Can I simply output floating values ‘including the negative ones’ as pixel colors and in a different shader directly access them as they are? (Also the same for the normal, so that there would be no need for +1 /2) ?

You need a floating point texture to write arbitrary values into it. Use the internal format ending with ‘F’ (e.g. GL_RGBA16F, GL_RGBA32F, etc).

gl_Color requires 4 components