Part of the Khronos Group

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 10 of 10

Thread: something I dont understand about coordinates and fragment's position

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Junior Member Newbie
    Join Date
    Jul 2013

    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
    Code :
    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 :
    Code :
    private final String strVertexShader = 
                "#version 330 \n" +
                "\n" +
                "layout(location = 0) in vec4 position;\n" +
                "void main()\n" +
                "{\n" +
                "    gl_Position = position;\n" +

    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 :
    Code :
    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\n" +
    "\n" +
    "out vec4 outputColor;\n" +
    "void main()\n" +
    "{\n" +
    " float lerpValue = gl_FragCoord.y / 500.0f;\n" +
    " outputColor = mix(vec4(1.0f, 1.0f, 1.0f, 1.0f), vec4(0.2f, 0.2f, 0.2f, 1.0f), lerpValue);\n" +

    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 ?
    Last edited by Dark Photon; 09-21-2013 at 10:58 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts