gl_position.w = ???? How do I calculate the W coord directly without perspective mat

I am making a raytracer using GLSL.
I have it working but the zbuffer is not working correctly.
I believe it is because I dont know exaclty what is expected for gl_position.w.

I currently set it to :


attribute vec4 position;//vertex position into vertex shader

gl_position.w = -1.0/position.z;

What should it be?
I have read several transform tutorials, but they never explain how to calculate gl_position.w directly.
Please help!
thanks

How does your raytracer work that you’re using a vertex shader for anything other than just drawing a full-screen quad?

I dont use a perspective projection matrix.
I create a ray from the eye to the vertex.
I then do a ray intersect near plane to get gl_position.xyz

So I need to understand what the w component would be.
Is it raylength/farplane? (which doesnt work)

I need a explanation something like that. It has to be based on raylength somehow.

That didn’t answer my question. You’re in a vertex shader. The gl_Position output needs to be the position of a vertex. Unless you’re somehow making a vertex correspond to a ray intersection, your code doesn’t make sense. And if you are making a vertex be a ray intersection, I’m pretty sure that doesn’t work.

Not unless you’re subdividing the screen into pixel-sized quads or something. But even then, I don’t think that’s a good way to go about ray-tracing with shaders.

My point is that I’m very confused about how what you’re doing works as a ray tracer. Until I understand that, I can’t really explain what it is you’re looking for. What is the mesh you’re rendering?

Alfonse, thanks but your not helping.
If you had READ my post you would see that I am doing a ray intersect near plane.
I have been shader programming for over 8 years. Please be smarter than me if your going to post here. Otherwise your just making noise on my thread. Again thanks, but your obviously not reading the question.
I am hoping to find the one person on planet earth that knows how to calculate gl_position.w without a perspective matrix.

If you had READ my post you would see that I am doing a ray intersect near plane.

That doesn’t explain what you’re doing. Are you intersecting the ray with the near plane? Are you firing a ray from the near plane into some kind of scene?

And the most important question of all is what does the vertex represent? How does the point of intersection translate to a rasterized position of some primitive? Are you rendering a field of GL_POINTS, where each point is an intersection with a scene? Are you trying to ray trace a scene, such that the ray intersections are rendered as vertices of a triangle (with the lighting computations done in the fragment shader)?

The answers to these questions greatly affect what gl_Position should be.

In short, I need to understand what you’re doing more than “I am doing a ray intersect near plane”. Without the answers to these questions, I can’t help you.

Most GPU raytracing algorithms do the ray tracing in the fragment shader, where they have absolute control over everything. Yours is using vertex shaders, which is not exactly standard for GPU ray tracing. So it isn’t clear what your algorithm is.

Or let me put it another way. The answer I want to give is “make W 1.0”. But I can’t give that answer because I have no idea what it means to interpolate between ray intersections. I’ve never heard of a ray tracing algorithm that interpolates between intersections. And it is the interpolation that’s going to make your W do the wrong things.

I am hoping to find the one person on planet earth that knows how to calculate gl_position.w without a perspective matrix.

IDK if this is right, but try:
gl_position.w = -gl_Vertex.z

That’s what you get if you just multiple out the matrices.

Explanation from http://www.songho.ca/opengl/gl_projectionmatrix.html:

“Therefore, we can set the w-component of the clip coordinates as -z[SUB]e[/SUB]. And, the 4th of GL_PROJECTION matrix becomes (0, 0, -1, 0).”
[ATTACH=CONFIG]375[/ATTACH]