# Thread: rays from camera to fragment in eye space

1. ## rays from camera to fragment in eye space

Hi I was wondering: in a shader I'm writing I'm assuming that the 'ray' that I construct from the camera to to the fragment starts at 0,0,0 and goes trough the eyespace position of the fragment. Seems to work as I expected, but is this really correct?
I was starting to doubt when I remembered that you also can set up orthogonal projections. How does that work then? seems incorrect to assume that all rays originate at (0,0,0) then, because to me it seems the rays should be parallel to each other... or is the scene transformed in such a way that is still works? can't seem to wrap my head around it

I hope I make myself clear, or else just ask and I'll try to explain it more!

Thanks!

2. Originally Posted by STTrife
...orthogonal projections. How does that work then? seems incorrect to assume that all rays originate at (0,0,0) then
Right. Eyepoint is a perspective concept. With orthogonal, you still transform to eye-space as usual (an orthonormal space), but then instead of applying a projection which converges to a point in space (the eyepoint), your projection is a 3D box (more correctly, a rectangular parallelepiped). You clip to that box and smash it directly onto the screen. Your eye/camera is effectively a plane rather than a point in space.

This is the same as the difference between a point light source and a directional light source. You're just tracing rays in the opposite direction.

3. Originally Posted by Dark Photon
Right. Eyepoint is a perspective concept. With orthogonal, you still transform to eye-space as usual (an orthonormal space), but then instead of applying a projection which converges to a point in space (the eyepoint), your projection is a 3D box (more correctly, a rectangular parallelepiped). You clip to that box and smash it directly onto the screen. Your eye/camera is effectively a plane rather than a point in space.

This is the same as the difference between a point light source and a directional light source. You're just tracing rays in the opposite direction.
Thanks, this is very clear, now my next question would be, how would you find the point of origin of a camera ray in orthogonal projection? I assume you'd have to use the gl_FragCoord somehow???

4. From the fragment shader? You have to reverse-transform gl_FragCoord from window-space to clip-space. But since you're using an orthographic projection, this is much easier.

Basically, you follow these instructions; they work just as well for orthographic as perspective projections. However, you want to make two changes.

The first change is don't bother dividing by gl_FragCoord.w. That should be 1.0, so NDC space is the same as clip-space.

The second change is to modify your clip-space position before transforming it back into eye-space. Take clipPos.z and set it equal to -1.0. That should be the near-plane. Then continue on. So your shader code would look like:

Code :
```vec4 ndcPos;
ndcPos.xy = ((2.0 * gl_FragCoord.xy) - (2.0 * viewport.xy)) / (viewport.zw) - 1;
ndcPos.z = (2.0 * gl_FragCoord.z - gl_DepthRange.near - gl_DepthRange.far) /
(gl_DepthRange.far - gl_DepthRange.near);
ndcPos.w = 1.0;

vec4 clipPos = vec4(ndcPos, 1.0);
clipPos.z = -1.0;
vec4 eyePos = invPersMatrix * clipPos;```

5. Thanks, this is very helpfull!

#### Posting Permissions

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