Forum Discussion

🚨 This forum is archived and read-only. To submit a forum post, please visit our new Developer Forum. 🚨
cheery's avatar
cheery
Explorer
10 years ago

How to retrieve eye rays for fragment shader?

I feel that I bother this part of forum for way too often. This is already something I feel like I would figure out soon on myself.

On fragment shaders, some nice effects require that you can cast a ray from the view space. I tried to do it myself like this in GLSL:


x = mix(leftTan, rightTan, step(ndc.x, 0.0)) * ndc.x;
y = mix(downTan, upTan, step(ndc.y, 0.0)) * ndc.y;
eyeOri = eye_matrix * vec3(0.0, 0.0, 0.0, 1.0);
eyeDir = mat3(eye_matrix) * vec3(x, y, -1.0);


I let that interpolate through vertex shader and normalize it in the fragment shader. The problem is the left and right eye doesn't converge when I do it like this, also the eyes on the view turn as if they were inverted. I double checked that I passed the right matrices for each eye. Am I doing some misconception here?

3 Replies

  • ndc coordinates are not in view-space. They're in either normalized device space or homogenous clip space, depending on whether they've been converted from vec4 to vec3 or not. See http://antongerdelan.net/opengl/raycasting.html

    To get from NDC back into view space, you would need the inverse projection matrix.
  • I thought perspective matrices don't invert well. But I can as well try it out.

    I probably have some problem in my code that produces the eye_matrix, since the views tilted opposite from how they should tilt.

    Before I try this out again, I will produce some utility code to make it easy to setup oculus for rendering.
  • "cheery" wrote:
    I thought perspective matrices don't invert well. But I can as well try it out.


    Well, you can always pass the eye space coordinates from the vertex shader to the fragment shader. Just create a new out value and populate it with the incoming vertex transformed only by the view matrix.