Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Still artifacts  (Read 7288 times)

0 Members and 1 Guest are viewing this topic.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Still artifacts
« on: June 30, 2009, 01:48:02 am »
Hi,

Recently I have read this and this thread and I think I've got a similar problem. I tried nitram_cero's SVN patch, but it didn't really help. I am not quite sure if I applied the patch right since my SVN version is a little bit newer. I just downloaded the newest SVN version some hours ago, adapted my code and tried it out. But the result remained the same, I still had ugly artifacts on my screen. Here you see an example:

The drawn sprites look like A or B. The source image is C, that's how it should look like. By the way, I don't use smooth images or zoomed/rotated views.

I tried a lot of things out, like aligning the view, the sprite's position or sprite's origin to integral coordinates. Some of the artifacts could be defeated, but some remain. Besides, my rounding seems to make the game less fluent.

It's quite strange, sometimes the sprites are drawn correctly. As far as I recognized this, the bigger a sprite's coordinates are, the less often it is drawn well (which would support nitram_cero's theory). But I am not very certain about this...

I don't know what to do, are there current workarounds or is this issue going to be fixed soon? I can also show my adaptation in RenderTarget.cpp if this helps (I didn't want to do it in this post since the code is quite long and maybe not even relevant)...
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Still artifacts
« Reply #1 on: June 30, 2009, 07:56:09 am »
What you can do is provide a minimal/complete example so that I can quickly test it.

You can also show your adaptation of RenderTarget.cpp, yes.
Laurent Gomila - SFML developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Still artifacts
« Reply #2 on: June 30, 2009, 06:35:30 pm »
Laurent, I sent you an e-mail with a complete example.

Concerning RenderTarget.cpp, I altered it like this (I tried to insert nitram_cero's changes, quite possibly I did everything wrong, my know-how about that stuff is limited). But I could imagine there should many other floats be changed to double, for example those of the viewport rectangle.
Code: [Select]
          // Setup the viewport
            const FloatRect& Viewport = myCurrentView->GetViewport();
            int Left   = static_cast<int>(0.5f + GetWidth()  * Viewport.Left);
            int Top    = static_cast<int>(0.5f + GetHeight() * (1.f - Viewport.Bottom));
            int Width  = static_cast<int>(0.5f + GetWidth()  * Viewport.GetSize().x);
            int Height = static_cast<int>(0.5f + GetHeight() * Viewport.GetSize().y);
            GLCheck(glViewport(Left, Top, Width, Height));
           
// =========================== CHANGES BEGIN ==========================================================

            //HACK: Use doubles to enhance the projection matrix.
            // This avoids artifacts and strange aspect ratio changes
            // due to some rounding errors in floats when resizing
            // the window.
            // Keeping the graphics with correct proportions.
            {
//The base to this code is taken from the View::RecomputeMatrix()

const Vector2f &ctr = myCurrentView->GetCenter();
const Vector2f &hs = myCurrentView->GetSize() / 2.f;

double Left   = (double)(ctr.x - hs.x);
double Top    = (double)(ctr.y - hs.y);
double Right  = (double)(ctr.x + hs.x);
double Bottom = (double)(ctr.y + hs.y);

double projMatrixHack[16]={
1.f, 0.f, 0.f, 0.f,
0.f, 1.f, 0.f, 0.f,
0.f, 0.f, 1.f, 0.f,
0.f, 0.f, 0.f, 1.f,
};
double &mat00=projMatrixHack[0 + 0*4];
double &mat11=projMatrixHack[1 + 1*4];
double &mat02=projMatrixHack[0 + 3*4];
double &mat12=projMatrixHack[1 + 3*4];
   
mat00 = 2.0 / (Right - Left);
mat11 = 2.0 / (Top - Bottom);
   
mat02 = (Left + Right) / (Left - Right);
mat12 = (Bottom + Top) / (Bottom - Top);

//USE DOUBLES!
           GLCheck(glMatrixMode(GL_PROJECTION)); GLCheck(glLoadMatrixd(projMatrixHack));
}

//removed // Setup the transform matrices
//removed GLCheck(glMatrixMode(GL_PROJECTION));            
//removed GLCheck(glLoadMatrixf(myCurrentView->GetMatrix().Get4x4Elements()));

// =========================== CHANGES END ==========================================================

            GLCheck(glMatrixMode(GL_MODELVIEW));
            GLCheck(glLoadIdentity());

            // Let the object draw itself
            Object.Draw(*this);
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Still artifacts
« Reply #3 on: June 30, 2009, 08:29:35 pm »
First test: I replaced every single float in SFML with a double, and changed the OpenGL calls accordingly (to use the functions that accept doubles as parameters).
This doesn't help at all, the results are exactly the same. Which doesn't really surprises me, as OpenGL internally uses floats anyway.

What surprises me is that nitram_cero got better results with the modification above ;)
Laurent Gomila - SFML developer

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Still artifacts
« Reply #4 on: June 30, 2009, 08:41:21 pm »
Apparently, the only way to get a perfect result is to enable the half-texel adjustment in sf::Image::GetTexCoords + using doubles. So they may not be completely useless after all ;)
Huge / non-integer coordinates do not even alter the rendering when the above combination is used.
I'll do more tests to see where exactly doubles are required.
Laurent Gomila - SFML developer

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Still artifacts
« Reply #5 on: July 04, 2009, 05:48:45 pm »
Finally, after many more tests, I found the solution here:
http://www.opengl.org/resources/faq/technical/transformations.htm#tran0030

Code: [Select]
glTranslatef(0.375f, 0.375f, 0.f)
Adding this simple call solved everything. No doubles where even necessary ;)
Laurent Gomila - SFML developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Still artifacts
« Reply #6 on: July 06, 2009, 04:37:30 pm »
Yeah, it works now! Great thanks for fixing this, Laurent! :)
I have been worrying about this issue a long time...

By the way, my code in RenderTarget.cpp now looks like this:
Code: [Select]
           // Setup the transform matrices
            GLCheck(glMatrixMode(GL_PROJECTION));
            GLCheck(glLoadMatrixf(myCurrentView->GetMatrix().Get4x4Elements()));
            GLCheck(glMatrixMode(GL_MODELVIEW));
            GLCheck(glLoadIdentity());
/* added */ GLCheck(glTranslatef(0.375f, 0.375f, 0.f));

            // Let the object draw itself
            Object.Draw(*this);
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Still artifacts
« Reply #7 on: July 06, 2009, 04:41:49 pm »
I've updated the trunk with this patch as well ;)

It's awesome that this tiny line of code is actually solving all the rendering issues :lol:
Laurent Gomila - SFML developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Still artifacts
« Reply #8 on: July 06, 2009, 04:45:45 pm »
Quote from: "Laurent"
I've updated the trunk with this patch as well ;)
Oh, I'm using the sfml2 branch. Last time I updated I didn't see the changes. And I think I'll wait some time until I recompile SFML again. ;)

Quote from: "Laurent"
It's awesome that this tiny line of code is actually solving all the rendering issues :lol:
True. I am amazed that this problem didn't occur earlier in SFML. Maybe the users just ignored it... :)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

dabo

  • Sr. Member
  • ****
  • Posts: 260
    • View Profile
    • http://www.dabostudios.net
Still artifacts
« Reply #9 on: July 06, 2009, 06:05:25 pm »
How do you make this problem occur? Would like to see it in action just for fun :D

nitram_cero

  • Full Member
  • ***
  • Posts: 166
    • View Profile
Still artifacts
« Reply #10 on: July 06, 2009, 11:05:10 pm »
Fucking awesome!

I'll try this soon

Maybe my ATI WinXP OpenGL drivers actually takes doubles? I don't know, but I swear it helped using them.

It could also be that the double to float cast is less lossy (precision wise) that the float division (2.f/(a-b)).