SFML community forums

Help => Graphics => Topic started by: Krechet on February 15, 2024, 07:06:29 pm

Title: Perspective distortion issue [Solved]
Post by: Krechet on February 15, 2024, 07:06:29 pm
Hi all. I'm trying to draw a distorted square to simulate perspective. I use rendering through an array of vertices. And as a result I get a strange picture. I tried using different vertices modes (PrimitiveType.Quads and PrimitiveType.Triangles), the result was the same.
I would be very grateful if anyone could suggest a solution on how to get the correct picture.
(Sample in attach (https://i.postimg.cc/wjzchMZs/Shapes-Sample.png))
Title: Re: Perspective distortion issue
Post by: fallahn on February 15, 2024, 07:36:49 pm
What you're getting is Affine Texture Mapping - caused by the vertex stage interpolating the UV coordinates in 2D space. (It's actually what the PS One did).

For Perspective Texture Mapping you'll need to supply some sort of depth value. This article explains more: https://mtrebi.github.io/2017/03/15/texture-mapping-affine-perspective.html as does wikipedia: https://en.wikipedia.org/wiki/Texture_mapping#Affine_texture_mapping
Title: Re: Perspective distortion issue
Post by: eXpl0it3r on February 16, 2024, 12:09:33 am
Waiting for Hapax to mention Elastic Sprite that "fixes" exactly this issue ;)

https://github.com/Hapaxia/SelbaWard/wiki/Elastic-Sprite
Title: Re: Perspective distortion issue
Post by: Krechet on February 16, 2024, 11:30:57 am
For Perspective Texture Mapping you'll need to supply some sort of depth value. This article explains more: https://mtrebi.github.io/2017/03/15/texture-mapping-affine-perspective.html
Magic! But its working)) Thanks a lot!
Title: Re: Perspective distortion issue [Solved]
Post by: Hapax on March 06, 2024, 05:55:04 pm
Just in case you hadn't seen the cause of the issue, it's because - in 2D - both of the triangles have no idea about the other triangles so one doesn't realise it's supposed to be a part of a pair to make a quad (and then have to share some of its texture with another one).

Waiting for Hapax to mention Elastic Sprite that "fixes" exactly this issue ;)

https://github.com/Hapaxia/SelbaWard/wiki/Elastic-Sprite
Haha! It exists because it solves the issue! :)

Elastic Sprite is entirely 2D and automatically 'solves' perspective (in the same way apps like Photoshop create perspective when given 2D corners).
It's also therefore compatible with SFML's 2D vertices (since z cannot be used with those).

I have long considered a more involved solution allowing multiple faces but if that much 3D is needed, it's likely that going raw OpenGL/Vulcan is the better option. Maybe in the future... ;D