something I dont understand about coordinates and fragment's position

I understand the vertices are being transformted to clipspace coords then to ndc coords then to window coords

now ndc range is [-1,1] and window coords range is (0,0) (width, height) (exept for the z range)…

so I made a VA in my program

private final float vertexPositions[] = {
            0.75f,  0.75f, 0.0f, 1.0f,
            0.75f, -0.75f, 0.0f, 1.0f,
                      -0.75f, -0.75f, 0.0f, 1.0f};

and I made a program shader with vertex shader and fragment shader now the vertex shader transform those vertices to clip space, thats my vertex shader :

private final String strVertexShader = 
            "#version 330 
" +
            "
" +
            "layout(location = 0) in vec4 position;
" +
            "void main()
" +
            "{
" +
            "    gl_Position = position;
" +
            "}";

now in the book I read from its said that I dont need to transform the coords to ndc because they’re already ndc because every component of them is already on the range [-1,1] and the W component is the same in every vertex which is 1 (so they’re in the same clipspace so no need to transform them to ndc).

now in the book I read from its also said that to transform the coords from the ndc to the window coords so they’ll be on the range of (0,0) (width, height) I need to use the glViewport() method
so thats what I did after creating the display I call a reshape method that does that :

glViewport(0, 0, width, height);

and I also call this method whenever the window is being resized.

Now it is said in the openGL doc that the way ndc are being transformed into window coords is as follow :

xw = ( xnd + 1 ) width 2 + x yw = ( ynd + 1 ) height 2 + y

So according to this formula and according to my VA lets take for example the first vertex in my VA so this last vertex in window coord will be (1750, 1750) (just for the x,y) - which is some how bigger than the extent of the window clip because the window clip range is
(0,0) (500,500) (I initialized the width and height to 500 when I created the display). - Question #1 : Explain me how this is possible that the window coord (1750,1750) is bigger then the extent of the window clip which is (0,0) (500, 500) ?

Now the part in the book I’m at, the author is trying to teach the reader how to assign a variety of colors to a surface and not just a solid color so in his Fragment shader looks like this :

private final String strFragmentShader =
"#version 330
" +
"
" +
"out vec4 outputColor;
" +
"void main()
" +
"{
" +
" float lerpValue = gl_FragCoord.y / 500.0f;
" +
" outputColor = mix(vec4(1.0f, 1.0f, 1.0f, 1.0f), vec4(0.2f, 0.2f, 0.2f, 1.0f), lerpValue);
" +
“}”;

so he basically say that the gl_FragCoord.y gets us the y position of a vertex in window coord so we divide it by 500 to get this vertex y in the range of [0,1] and lets take the vertex coords from before (1750, 1750) and devide it by 500,
1750/500 = 3.5 and 3.5 isnt on the range of [0,1] - Question #2 : Same as question number 1 just things don’t make sense here …

Ok lets say we “ignore” this thing that I didnt understand now and continue… let’s take vertex number 3 and get it window coords according to the formula and we’ll get (250, 250) (just for the x,y), now this looks fine to me its an ndc coord which transformed into window coord and its on the range of (0,0) (500,500) .

But now pops up a new problem in the fragment shader letsa try to get the y coord in the range of [0,1] like we did before, so we divide by 500, 250/500 = 0.5 and 0.5 IS ON the range of [0,1] so far so good …

Now he uses the mix() method I hope you know what it is.

Ok so he explain that if lerpValue = 1 meaning the vertex is at the highest point on the screen we’ll color the fragment with our first vec4 color and if it was 0 we’ll color the fragment with the second vec4 color .
Now we would get the first vec4 color if one of the vertices Yndc was equal to 1 or the opposite we would get the second vec4 color if one of the vertices Yndc was equal to -1 but since none of our ndc coords is 1 or -1 we suppose to get a color between those 2 vec4 colors.

Well that’s what theoretically suppose to happen but it doesn’t happen for some reason in my program the highest Yndc which is 0.75 gets the first vec4 color and the lowest Yndc gets the second vec4 color, and how do I know it ? I made a test and took a picture look :

And according to what he did in the fragment shader (he used the mix function) the bigger the “lerpValue” variable (max = 1, min = 0) the darker the color and the opposite.
Question #3 : Why in 2 different cases the highest/lowest Yndc gets the t the same color ?

The viewport transformation is


xw = (xnd + 1) * (width/2) + x;
yw = (ynd + 1) * (height/2) + y;

With xnd, ynd in [-1,1] this produces values in [x, x+width] and [y, y+height] respectively, so the 1750 is highly suspicious.

[QUOTE=carsten neumann;1254986]The viewport transformation is


xw = (xnd + 1) * (width/2) + x;
yw = (ynd + 1) * (height/2) + y;

With xnd, ynd in [-1,1] this produces values in [x, x+width] and [y, y+height] respectively, so the 1750 is highly suspicious.[/QUOTE]

are u sure ? because in the openGL docs its said that the forumla is xw = (xnd+1) * widht * 2 + x … O_O

also what about my last question ? the fragment shader choose the color based on the fragment height, higher = darker but in the picture I show’d there’s no difference between the height for example it applies the darkest color to the highest fragment even if its coord was (0, 10, 0) it would give this fragment the darkest color because its the highest fragment but it doesnt suppose to be this way as far as I understood from what the author of the book wrote

The mix function performs a linear interpolation. It will return exactly the first parameter if the third parameter is 0, and it will return exactly the second parameter if the third parameter is 1. If the third parameter is between 0 and 1, it will return a value between the two other parameters, based on the third parameter.

No it doesn’t. It appears that you’re either reading a copy which has lost the formatting, or reading it using a program which cannot reproduce the formatting. The glViewport manual page has the formula which carsten gave (i.e. width/2, not width*2).

ok thanks got it but what about my last question can someone clear things out ?

someone please … ?
;o

They’re not. At least not in the screenshot you posted.

you suggest that im color blind ? :frowning:

Open up your favourite image editing software and check what the pixel’s color is! :slight_smile: They are different. haha

No, nothing so serious. I did what saucyio mentioned: I cheated and let software tell me what the color values were. 229 & 77. versus 255 & 52.

I’m not saying there isn’t a mystery still afoot. But the color values are definitely different.