PDA

View Full Version : Perspective distorsion using GL_QUAD



Runik
11-06-2007, 03:04 AM
Hi !

I have a question about something that bugs me since a long time : I'm rendering textured quads to the framebuffer. I don't have any z axis, so the texture is splitted in 2 triangles by the graphic card before rendering.
The problem is that the original quad, which is in the same plane, is transformed into 2 bound triangles which aren't in the same plane anymore, leading to graphical distorsion ugliness :(
3 screenshots :
- original texture :
http://runik.free.fr/OpenGL/texture_original.JPG
- texture rendered, triangles on different planes :
http://runik.free.fr/OpenGL/texture_rendered.JPG
- texture the way I want it to be :
http://runik.free.fr/OpenGL/texture_ok.JPG

So my question is : is there a way to have a better handling of quad textures without z axis, ie having the texture rendered like in the 3rd image ?

Thanks for reading me :)

ZbuffeR
11-06-2007, 05:23 AM
You need to emulate perpective correction, use the w texture coordinate for it.
There is a web page somewhere that deals with exactly your problem, but I can't find it at the moment.

Runik
11-06-2007, 06:00 AM
Is it this one (http://www.lysator.liu.se/~mikaelk/doc/perspectivetexture/) ?
EDIT: or this one (http://www-evasion.imag.fr/Membres/Antoine.Bouthors/teaching/opengl/opengl7.html) (in french) ?
Researching with correct keywords helps a lot :p
Thanks ;)

ZbuffeR
11-06-2007, 08:37 AM
Not really, these deal with perspective correction on 3D quads, and nowadays all GL implementations should do it with perspective correction by default.
It was a specific article on 2D trapezoidal quads.
Well anyway, either do it by rotating your quad, or tweak the last parameter to glTexCoord4 (q instead of w):
glTexCoord4f(x,y,0,1);
glTexCoord4f(x,y,0,0.5); // something like that

datsua
11-06-2007, 03:07 PM
Assuming you use a 3x3 matrix to do the 2D projection you can just use the "w" coordinate from that calculation.

A short intro to 3x3 projective matrix can be found in the OpenVG 1.0 spec.

Brolingstanz
11-06-2007, 03:57 PM
i want to know what that little girl is so happy about.

check out the stuff near the bottom of this page...

http://www.r3.nu/

ZbuffeR
11-06-2007, 04:44 PM
Thx for the link modus. /me added it to bookmarks.

Runik
11-07-2007, 01:00 AM
Thanks a bunch for the link, that's exactly what I needed :)
I'll give it a go later today.

The little girl texture is from a Sega Saturn game named Baku Baku Animals, she's happy because she's going to compete with someone in a puzzle game ;)

Runik
11-07-2007, 12:32 PM
I cannot edit my previous post ... weird :p

Anyway, after a bit of tweaking, I got everything right in my rendering : it works marvelously !
A little before / after pic :
http://runik.free.fr/OpenGL/qcoord_ok.JPG

Thanks again to everyone involved in this thread, you're great :cool:

Runik
11-21-2007, 04:36 PM
Hello again :)
I still need help ...
I'm trying to make this work in every possible configuration, and I still have trouble : it works great for trapezoids like these :
http://runik.free.fr/OpenGL/quads_1.JPG

but for regular quads like this one :
http://runik.free.fr/OpenGL/quads_2.JPG
it doesn't work well ...

For quad n1 for instance, I'm using the following code :


// farther side of the texture
fTextureSmallSide = (float)( max(XA,XB)-min(XA,XB) ) / (float)usXLengthMax;
// closer side of the texture
fTextureBigSide = (float)( max(XC,XD)-min(XC,XD) ) / (float)usXLengthMax;

// calculation of s,t & q for each dot, r is fixed to 0
fsA=0; ftA=0; fqA=fTextureSmallSide;
fsB=fTextureSmallSide; ftB=0; fqB=fTextureSmallSide;
fsC=fTextureBigSide; ftC=fTextureBigSide; fqC=fTextureBigSide;
fsD=0; ftD=fTextureBigSide; fqD=fTextureBigSide;

glBegin(GL_QUADS);

glTexCoord4f(fsA,ftA,0,fqA); glVertex2f(ConvertPixelX(XA),ConvertPixelY(YA));
glTexCoord4f(fsB,ftB,0,fqB); glVertex2f(ConvertPixelX(XB),ConvertPixelY(YB));
glTexCoord4f(fsC,ftC,0,fqC); glVertex2f(ConvertPixelX(XC),ConvertPixelY(YC));
glTexCoord4f(fsD,ftD,0,fqD); glVertex2f(ConvertPixelX(XD),ConvertPixelY(YD));

glEnd();


But for quad n5, I'm a bit confused about how the q coordinate is working, and how I should do ... I've tried a lot of things, but results aren't good at all :(
I tried calculating fsA and ftA to be relative to the position into the quad, results were a bit better, but far from what is expected ...

Any ideas on how to implement this ?

Thanks :)