# Thread: OpenGL - 2D - Skewing/distorting texture vertically along a line

1. ## OpenGL - 2D - Skewing/distorting texture vertically along a line

Hello!

I've currently been wrestling with an issue involving texture coordinates.

Basically the issue I'm having is faking a projection (Orthagonal? Affine Tranformation? [I'm not sure]) with the walls.

My "walls" are defined as 4 points:
top-Left is point 0
top-right is point 1
bottom-left is point 2
bottom-right is point 3

The lines 0,1 and 2,3 are always parallel (and 0,2 and 1,3 as well)
The vertical difference between the lines is a constant called cGridsize
The horizontal length of the lines are always divisible by cGridsize
Also the texture's width and height is also cGridsize.
cGridsize is always a Power of Two.

I split the wall-quad into 2 tris:
tri1 is Point1 to Point0 to Point3
tri2 is Point2 to Point3 to Point0

What I want to do is distort a texture so that the bottom of the texture runs along the bottom/top of the wall like so:

Here's one with a rough triangle overlay:

I have gotten it to work when the one of the base lines go across one and up one or across two and up one but it doesn't work when I go across two and up two (the texture angle is slightly off) or if I go up two across one (It has the same angle as up one across one).

The current way I slope the texture is as follows (We'll just work with one triangle):

Here's the generic way I do it:
TexCoord[0].x := Tri[0].x/cGridsize;
TexCoord[0].y := Tri[0].y/cGridsize;

TexCoord[1].x := Tri[1].x/cGridsize;
TexCoord[1].y :=(Tri[0].y+(Tri[1].y-Tri[2].y))/cGridsize;

TexCoord[2].x := Tri[2].x/cGridsize;
TexCoord[2].y := Tri[2].y/cGridsize;

In the above example the triangle array index refernces the points the way they are ordered on the above image.

I've tried many other ways to do it but so far this one works the best, the other methods limited the angles of the line.
Does anybody know how to do it or what I'm doing wrong?

Rotation is not what I'm looking for, I don't want any distortion horizontally!

Also I'm using ZenGL for working with OpenGL, but it doesn't seem to mess with the co-ord system.

2. Actually I don't understand what's your problem. May be you can post an image showing what's not working.

I think you can get it by using 2D, discarding the "Z" axis. Better if you use orthogonal to prevent transformations.

3. I recently discovered on some quads, that texture isn't going smoothly along the whole image. I am unsure how this would look, but i would try by adding 1 vertex in the average middle point of quad. That way you would render it as 5 point GL_TRIANGLE_FAN, where v0 is center of quad.

4. Originally Posted by Ñuño Martínez
Actually I don't understand what's your problem. May be you can post an image showing what's not working.

I think you can get it by using 2D, discarding the "Z" axis. Better if you use orthogonal to prevent transformations.
Unfortunatly, I'm unsure what your saying XD
I am already rending in 2D, that's what zenGL does, although I'm unsure if it's in orthogonal mode, how do you change projections with OpenGL?

Originally Posted by User137
I recently discovered on some quads, that texture isn't going smoothly along the whole image. I am unsure how this would look, but i would try by adding 1 vertex in the average middle point of quad. That way you would render it as 5 point GL_TRIANGLE_FAN, where v0 is center of quad.
It's not the texture not matching up or not being smooth that's the issue.

I'll try and actually show the issue:

If you look at the slope the texture doesn't run along the line (The horizontal lines on the brick wall are meant to become parallel to the slope of the wall).
This is going across two, up two and as you can see from the images in the one above going one and and one across results in the correct distortion.
Here's one with a triangle overlay:

And here (in blue) is the path it follows, and (in yellow) the path it should follow:

Basically when the y-differnce is greater than cGridsize the texture is incorrectly distorted, making the horizontal lines on the texture not parallel to the top/bottom of the wall.

Edit: I just got it to work... and it was stupidly simple!

TexCoord[0].x := Tri[0].x/cGridsize;
TexCoord[0].y := Tri[0].y/cGridsize;

TexCoord[1].x := Tri[1].x/cGridsize;
TexCoord[1].y :=Tri[0].y/cGridsize+1;

TexCoord[2].x := Tri[3].x/cGridsize;
TexCoord[2].y := Tri[0].y/cGridsize+1;

All I have to do is make it so that it doesn't repeat along the y axis because apparently the texture was repeating vertically, when it shouldn't >.>

Sorry to have wasted your time, and thanks for trying to help me out! XD

5. Originally Posted by Draghi
how do you change projections with OpenGL?
There are several ways. You can just define the matrix directly, but you should use use glOrtho for orthographic and glFrustrum for pyramidal as they're designed for it.