Fragment position in model space - Shader

I am trying to create a custom shader. In the fragment shader, I want to get the position of the fragment in model model space. I know that gl_FragCoord has the Coordinates in window space. But how can i convert to model space?

I’d suggest instead transferring the position from the vertex-shader. Something like this:

In the vertex shader:

// Other stuff here...

out vec3 vertexPos;

void main()
{
    // Other stuff here...

    vertexPos = p3d_Vertex.xyz;
}

In the fragment shader:

// Other stuff here...

in vec3 vertexPos;

void main()
{
    // The vertex-position should now be available here!
}

Many thanks @Thaumaturge. I somehow did not see your comment. This works for my requirements.

1 Like