PDA

View Full Version : Use function insteead of projection matrix



_NoName_
09-07-2017, 06:07 AM
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

GClements
09-07-2017, 06:17 AM
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).

_NoName_
09-07-2017, 02:31 PM
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);
}

GClements
09-07-2017, 07:38 PM
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.

_NoName_
09-07-2017, 11:40 PM
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 )