Search code examples
3dshaderwebgl

How to construct projection matrix with ortho-ish in y-axis and perspective in x-axis


How would I create a matrix that maps coordinates like so:

Near plane

x: [-3,3] -> [-1,1]
y: [-6,2] -> [-1,1]
z: 2 -> -1

Far plane

x: [-4,4] -> [-1,1]
y: [-4,4] -> [-1,1]
z: 0 -> 1

Illustration of frustum in xz-plane and yz-plane

I can do it in the vertex shader with special function but would like to do it with a projection matrix, if possible.

vec4 special_projection(vec4 p){
  float ty = 0.25 * p.z;
  float tx = 0.25 * p.x;
  float xz = (tx / 3.0) * (p.z / 2.0);
  float x = tx + xz;
  float y = 0.25 * p.y + ty;
  float z = -0.5 * p.z;
  return vec4(x,y,z,1);
}

Solution

  • What you desire is a not represantable with matrix multiplication in the 4d homogenous space you work in.

    To get the perspective divide right, you would need to set w_clip to some linear function of z_eye. Standard GL convention would be w_clip = -z_eye, and I'm going to use that here. To get y without the perspective distortion, you need to get y_clip multiplied by w_clip = - z_eye.

    You can of course write whatever formula into the shader you like, but be warned that by distorting the space in the way you intend to do, the persepctive correction for the interpolation will yield results you might not expect.