Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opengl perspective distortion

I'm having this weird issue and I'm hoping someone could clear this up for me so I can understand what's wrong and act accordingly. In OpenGL (fixed-function) I'm rendering a tube with inner faces in an orthographic projection.

The image below shows the result. It consists of 4 rings of vertices which are forming triangles using the index pattern shown at the left. I numbered the vertices on the tube for your convenience. At the right is the texture being used:

enter image description here

As you can see the texture is being heavily distorted. As I initially created the tube with only 2 rings of vertices, I thought raising the amount of rings would fix the distortion but no joy. Also glHint doesn't seem to affect this specific problem.

The texture coordinates seem to be alright. Also, the checker pattern seems to render correctly, but I guess the distortion is just not visible in that very specific pattern.

Please ignore the crossed lines as one of those is a non-existent edge; I rendered the wireframe through GL_LINE_LOOP.

like image 981
Will Kru Avatar asked Jan 25 '12 21:01

Will Kru


People also ask

What is perspective projection OpenGL?

Perspective Projection in OpenGLAll configurations with the same opening angle result in the same image (only with scaled x- and y-coordinates). In OpenGL, the size of the image plane is always chosen such that the resulting x- and y-coordinates are in the range [-1; 1].

What does GLM :: perspective do?

glm::perspective creates a 4x4 perspective projection matrix that is used in a shader (typically, vertex shader) to transform points. A few tips: Often, one sets up the projection transformation and does not change it during the course of a game, unless user changes the window.

What is gluOrtho2D in OpenGL?

The gluOrtho2D function sets up a two-dimensional orthographic viewing region. This is equivalent to calling glOrtho with zNear = -1 and zFar = 1.

What is gluPerspective OpenGL?

The gluPerspective function specifies a viewing frustum into the world coordinate system. In general, the aspect ratio in gluPerspective should match the aspect ratio of the associated viewport. For example, aspect = 2.0 means the viewer's angle of view is twice as wide in x as it is in y.


1 Answers

This particular effect is caused by the way texture coordinates are interpolated in a triangle. What happens is, that one direction becomes the major component, whereas the other is skewed. Your specimen happens to be very prone to this. This also happens to be a problem with perspective projections and textures on floors or walls. What you need is so called "perspective correct texturing" PCT. There's a glHint for this but I guess, you already tried that.

Frankly the only way to avoid this is by subdividing and applying the perspective correction as well. But fortunately this is easy enough for quadrilateral based geoemtry (like yours). When subdividing the edges interpolate the texture coordinates at the subdivision centers along all 4 edges and use the mean value of a 4 of them. Interpolating the texture coordinate only along one edge is exactly what you want to avoid.

If you want to keep your geometry data untouched you can implement PCT in the fragment shader.

like image 79
datenwolf Avatar answered Oct 25 '22 18:10

datenwolf