Do I need 4D texture coordinates to achieve perspective correct texture mapping in opengl?

StackOverflow https://stackoverflow.com/questions/20161810

  •  04-08-2022
  •  | 
  •  

Question

I'm running into the problem of affine transformed quads in a mesh and therefore wrong texture coordinate interpolations. I believe that in the fixed function pipeline you could just set the folloing glHint:

glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

But looking at the doc this seems to be deprecated. So I guess there is no more "simple" solution to do this and I need to specify all my texture coordinates as 4D vectors?

Additionally does this mean that if I want to load a mesh that only specifies 2D texture coordinates I need to manually detect quads and correct for this?

The image below displays my problem on a torus made out of 4 slices. Example from my program, a torus made out of 4 slices

I also tried to use interpolation qualifiers in my shaders as suggested by Nicol Bolas here.

Although I believe smooth is the default setting anyway. This had no effect on my output.

The question on how to do perspective correct texture mapping has come up multiple times here on SO. But I'm asking if there is no easier solution. My personal conclusion on why the fixed function pipeline was able to do this for you is because of GL_QUADS. And I have the feeling that specifying 4D texture coordinats is the only solution in the core profile.

Here are some links on the topic that explain how the 4D coordinates work:

Getting to know the Q texture coordinate...

perspective-correct-texturing-of-trapezoid-in-opengl-es-2-0

correct-glsl-affine-texture-mapping

Was it helpful?

Solution

I was misinformed about what glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); was used for in the fixed function pipeline. I was told that this hint should prevent the error that we're seeing above. I've since tested it myself in the fixed function pipeline and it turns out that the hint just controls the perspective correctness of the vertex transformations.

So to fix the problem above, there isn't any solution other than providing projective texture coordinates, which is the solution that most of the other SO questions I've linked to talk about.

I posted the same question to reddit and have recieved a satisfying explenation, source: http://www.reddit.com/r/opengl/comments/1ral2k/perspective_correct_texture_mapping/cdlhw04

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top