I want to write a GLSL shader which does NOT do perspective mapping for textures. I want to accomplish the vintage PS1 style graphics just because they look so damn cool.
I’m having trouble implementing this though, I hope anyone could help me. I need to know how to disable this perspecitve-correct sampling of the “texture2D(texture, uv)” process in the fragment shader.
That’s because you’re using uv to sample your texture. Try using gl_FragCoord.xy instead. It should give you screen-space coordinates in the [0, 1] range (actually, I’m not sure if it’s [-1, 1] range, I forget).
texture2D(texture, gl_FragCoord.xy);
Edit:
Actually, it gives you full pixel values, so to get a [0, 1] range, you’d have to divide by window.innerWidth & window.innerHeight, which you would pass to the shader as uniforms:
I tried your code, but what I got is a kind of flat texturing of my 3D object viewed from my camera (because of screen-space). I think what I need is the the modelView coordinates (normalized) so before the projectionmatrix is applied - and so without any perspective correction applied to the texture. What do you think?
To be honest, I’m not sure what kind of projection you’re trying to achieve, or even if this would work, but you have a few matrices to play with: modelMatrix, viewMatrix, modelViewMatrix, and projectionMatrix. Make sure you set your texture to repeat or mirror, in case you end up with a wild number range. I’d love to see what you end up with!
Edit:
Look into this tutorial to get a better understanding on how matrices work, maybe it can help you achieve the right transformation to your UVs.
although I managed to implement the non-perspective texture mapping by dividing by w. I get a lot of unwanted (although really cool) glitches. Could anyone explain to me why these glitches are present?
EDIT: it seems those glitches only occur when certain vertexes are not within the camera’s viewport. When moving away from the 3D plane object so that it fits entirely in the camera’s canvas the glitches are gone.