PDA

View Full Version : OpenGL - 2D - Skewing/distorting texture vertically along a line



Draghi
30-12-2012, 01:21 PM
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.

https://dl.dropbox.com/u/15538749/Game/RPG/OpenGL help/TriangleSituation.png

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:
https://dl.dropbox.com/u/15538749/Game/RPG/OpenGL help/Result1.png

Here's one with a rough triangle overlay:
https://dl.dropbox.com/u/15538749/Game/RPG/OpenGL help/Result2.png

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):
https://dl.dropbox.com/u/15538749/Game/RPG/OpenGL help/TriangleSituation2.png

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.

Ñuño Martínez
30-12-2012, 05:47 PM
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.

User137
30-12-2012, 08:19 PM
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 (http://www.c-sharpcorner.com/UploadFile/jeradus/OpenGLBasics11172005014307AM/Images/OpenGL3.gif), where v0 is center of quad.

Draghi
30-12-2012, 10:08 PM
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?


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 (http://www.c-sharpcorner.com/UploadFile/jeradus/OpenGLBasics11172005014307AM/Images/OpenGL3.gif), 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:
https://dl.dropbox.com/u/15538749/Game/RPG/OpenGL help/issue1.png

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:
https://dl.dropbox.com/u/15538749/Game/RPG/OpenGL help/issue2.png

And here (in blue) is the path it follows, and (in yellow) the path it should follow:
https://dl.dropbox.com/u/15538749/Game/RPG/OpenGL help/issue3.png

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

Ñuño Martínez
03-01-2013, 06:04 PM
how do you change projections with OpenGL?
There are several ways. You can just define the matrix directly, but you should use use glOrtho (https://www.opengl.org/sdk/docs/man2/xhtml/glOrtho.xml) for orthographic and glFrustrum (https://www.opengl.org/sdk/docs/man2/xhtml/glFrustum.xml) for pyramidal as they're designed for it.