Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 7 of 7

Thread: Clip Space coordinate question

  1. #1
    Junior Member Regular Contributor
    Join Date
    May 2002
    Posts
    166

    Clip Space coordinate question

    In a vertex shader, after transforming by the MVP matrix - eg.
    gl_Position = gl_ProjectionMatrix * cameraModelViewMatrix * gl_ModelViewMatrix * gl_Vertex;

    (I have the camera transform separate from gl_ModelViewMatrix)

    The values of gl_Position are mapped to a unit cube, correct? e.g. -1 < x,y,z < 1

    I am trying to implement the effect of draping a texture over everything - basically like a projective texture or shadow map.

    I thought I could use the x and y coords of
    gl_lightPosition = lightProjectionMatrix * lightModelViewMatrix * gl_ModelViewMatrix * gl_Vertex

    as the s,t indices into the texture.

    But it seems to wrap the texture.


    Any feedback/advice is appreciated...

  2. #2
    Junior Member Regular Contributor
    Join Date
    May 2002
    Posts
    166

    Re: Clip Space coordinate question

    Ok, I almost got it.

    I need to divide by w, e.g.

    s = x/w
    t = y/w

    Why do I need to do this - can somemone help me understand "homogenous clips space"?

  3. #3
    Senior Member OpenGL Pro sqrt[-1]'s Avatar
    Join Date
    Jun 2002
    Location
    Australia
    Posts
    1,006

    Re: Clip Space coordinate question

    Quote Originally Posted by azcoder
    In a vertex shader, after transforming by the MVP matrix - eg.
    gl_Position = gl_ProjectionMatrix * cameraModelViewMatrix * gl_ModelViewMatrix * gl_Vertex;

    (I have the camera transform separate from gl_ModelViewMatrix)

    The values of gl_Position are mapped to a unit cube, correct? e.g. -1 < x,y,z < 1
    Wrong, they are mapped from -w < x,y,z < w .

    What it sounds like you need to do is supply a w component to your texture coordinates and do a texProj lookup in the fragment shader.

  4. #4
    Senior Member OpenGL Guru Humus's Avatar
    Join Date
    Mar 2000
    Location
    Stockholm, Sweden
    Posts
    2,444

    Re: Clip Space coordinate question

    Quote Originally Posted by azcoder
    Why do I need to do this - can somemone help me understand "homogenous clips space"?
    That's the perspective division, which is neccesary, well, because we need perspective correction. The value output from the vertex shader is (x, y, z, w). Then you divide by w and get the normalized coordinates (x / w, y / w, z / w). x / w and y / w will be in [-1 .. 1], as will z / w which is used in the depth test.

  5. #5
    Member Regular Contributor
    Join Date
    May 2001
    Posts
    349

    Re: Clip Space coordinate question

    Quote Originally Posted by Humus
    as will z / w which is used in the depth test.
    More precisely the depth test uses window space Z which is
    (z/w)*(f-n)/2 + (n+f)/2
    where n and f are the parameters do glDepthRange. One thing that D3D does better.

  6. #6
    Senior Member OpenGL Guru Relic's Avatar
    Join Date
    Apr 2000
    Posts
    2,527

    Re: Clip Space coordinate question

    Unrelated, but a performance tip you should keep in mind:
    Your simple line:
    gl_Position = gl_ProjectionMatrix * cameraModelViewMatrix * gl_ModelViewMatrix * gl_Vertex;
    will do two matrix * matrix and one matrix * vector multiplications which takes 16 + 16 + 4 instructions. Bad.

    If you bracket the code so that it resolves to only matrix * vector multiplications like this:
    gl_Position = gl_ProjectionMatrix * (cameraModelViewMatrix * (gl_ModelViewMatrix * gl_Vertex));
    it will only take 4 + 4 + 4 instructions!
    Saved 24 instructions in a single GLSL line. Nice, eh?

    And no, a compiler can not do that automatically because the multiplication operator associativity is from left to right.

  7. #7
    Member Regular Contributor
    Join Date
    May 2001
    Posts
    349

    Re: Clip Space coordinate question

    Better yet, calculate (gl_ProjectionMatrix * cameraModelViewMatrix * gl_ModelViewMatrix) on the CPU and pass the combined matrix as uniform to the shader.

Posting Permissions

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