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 5 of 5

Thread: Use function insteead of projection matrix

  1. #1
    Junior Member Newbie
    Join Date
    Jun 2016
    Posts
    19

    Use function insteead of projection matrix

    Is it possible to use a custom function instead of the projection matrix to for example not project a vertex on a plane but a sphere ?
    It should return an X-/Y-/Z-value between 0 and 1 of course and whats about the W-value is it W = 1/Z ?

    Thanks in advance

  2. #2
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    2,406
    Quote Originally Posted by _NoName_ View Post
    Is it possible to use a custom function instead of the projection matrix to for example not project a vertex on a plane but a sphere ?
    It should return an X-/Y-/Z-value between 0 and 1 of course and whats about the W-value is it W = 1/Z ?
    When using a vertex shader, you can use any function. However, the resulting triangle will still lie in the plane defined by the three projected vertices, not on the surface of a sphere. Ultimately, the primitive is determined entirely by the outputs of the vertex shader (including gl_Position); how those values are computed doesn't matter.

    Regarding W: scaling all four components by the same factor leaves the position unchanged, but it will affect how attribute values are interpolated across the primitive (unless the variables have the noperspective qualifier).

  3. #3
    Junior Member Newbie
    Join Date
    Jun 2016
    Posts
    19
    I know its not really the topic anymore but I wrote this function to simulate a perspective projection in the shader and for me It seems mouch simpler then a matrix multiplication so my question is how fast it would be relative to a projection matrix.

    And whats about my change in the Z value calculation ?

    My function : (the model and view matrix need to be applied before)

    vec4 projection(vec4 coord)
    {
    vec4 result;
    float dist = length(coord.xyz);

    result.x = (coord.x/1)/(aspect*tan(fov*degree/2));
    result.y = (coord.y/1)/(tan(fov*degree/2));
    result.z = 1-(1/(max(coord.z, 0)+1));
    result.w = -coord.z;

    return vec4(result);
    }

  4. #4
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    2,406
    Quote Originally Posted by _NoName_ View Post
    I know its not really the topic anymore but I wrote this function to simulate a perspective projection in the shader and for me It seems mouch simpler then a matrix multiplication so my question is how fast it would be relative to a projection matrix.
    Probably quite a bit slower, as you're performing two calls to tan() for each vertex. It's also less general than a matrix transformation (e.g. the vanishing point is forced to be in the centre of the viewport). And the max() call in the Z calculation shouldn't be there.

    Even so, you probably aren't going to notice the overhead compared to that of a fragment shader.

  5. #5
    Junior Member Newbie
    Join Date
    Jun 2016
    Posts
    19
    Thanks for your reply, I moved the tan() thing to my CPU program and removed the max() thing (used if() instead ).
    Now my program is running 0.7 to 1.5 fps faster then the same program with a projection matrix.
    (Im using glutWireSierpinskiSponge() to have a many vertices )

Tags for this Thread

Posting Permissions

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