Messin' around with the GL renderer
First of all, lil' status update. Things are nearly ready for the 0.9 release, we are mostly busy ironing out small issues here and there to ensure everything is good. Nobody would want something big like that 0.9 release to end up being a total flop.

Also, if you ever wondered why progress is slow: an emulator project is like a tree in that once you're done with the trunk, it branches off in a billion different directions. At this point, melonDS is too big to be a one-man show. There are a few people working on it now, but there's only so much we can do with our time and motivation.

Anyway, while Generic is busy polishing the JIT, I figured I'd go around and try fixing some of the issues on the issue tracker.

This one, Deformed floor textures in the Celestial Tower (Pokemon Black/White) (OpenGL only), is an interesting problem. The base issue is that, if you happen to remember, the DS can draw quads natively, while modern GPUs can't. Actually, it's even worse: clipping on the DS cuts through polygons but doesn't create more polygons, which means that you can end up with a maximum of 10 vertices per polygon. For example, a triangle that sticks out of the view volume can become a quad, a pentagon, or more.

The base issue here is faulty rendering of these polygons that have more than 3 vertices. The DS employs a scanline-based convex polygon renderer, so it doesn't care how many vertices your polygon has. Software renderers used in DS emulators use similar filling algorithms, so no problem there either. However, when you use OpenGL, it's another deal entirely -- modern OpenGL-compatible GPUs are very good at drawing triangles, but... that's about it.

So, what do you do when you need to render a quad? Easy, split it into two triangles!

Suppose the quad below.

All fine and dandy. Now we split this, like this:

You can notice that, compared to the original quad, it looks distorted now. Maybe if we try splitting it in the other direction?

... well, yeah. There's only so much we can do with this.

Enter Arisotura. I've always found this to be an interesting problem, and had a few ideas to alleviate it. There isn't going to be a perfect fix, short of implementing custom interpolation somehow, but we can try to reduce the distortion by dividing these polygons more. We can't overdo it either, lest it kill performance.

A simple way to do it is the following:

It's still not perfect, but somewhat better. Keep in mind that this is an extreme example to show the issue at hand. In real-world cases, polygons are going to be smaller, and the distortion generally more subtle.

Speaking of which:

The polygon right under the bridge is a quad and exhibits the issue we're talking about. For reference, here's how it looks with the software renderer:

So I tried implementing the polygon splitting algorithm shown above. The idea is simple: find the center of the polygon, calculate the color/texcoord/depth attributes at that point, and create triangles around it. In practice, it isn't always easy to get these things working.

Once the bugs were fixed, it started looking more promising.

Not perfect, but it looks much better this way.

Still got some testing to do, and considering whether I should add an option for this, but I'm confident this will make it in 0.9. We have been neglecting the OpenGL renderer since the day it was created, it's more than time to give it some love.
WaluigiWare64 says:
Aug 19th 2020
can't wait for 0.9!
Laka says:
Aug 19th 2020
Awesome mate, I'm really anxious for the update!
Anomalous says:
Aug 19th 2020
Instead of just making the error smaller and doubling the amount of polys, why don't you try something like this?
Arisotura says:
Aug 19th 2020
I saw that post; thing is, I don't think it can be adapted to arbitrary polygons, due to way typical GPUs work. one solution might be implementing custom interpolation in the fragment shaders, but they would need to know the attributes for all vertices (up to 10 vertices).
Foxergamer#1 says:
Aug 19th 2020
Please just add WiFi support
Foxergamer#1 says:
Aug 19th 2020
Nothing else is needed at al
Arisotura says:
Aug 19th 2020
wat. we've had wifi support for ages.
Mega Yoshi says:
Aug 19th 2020
Hello Man, you are doing an exellent work, im using now melonds (and i have deleted desmume), could you add a launch menĂº Like citra and yuzu emulators?, it would look cool (sorry for my bad english)
zedfireblast333 says:
Aug 20th 2020
very good news I'm waiting for news new options in the emulator. example:directX soport. and audio x2
Nixel says:
Aug 20th 2020
The issue of converting quads to triangles has always fascinated me. It's easy to explain, easy to understand, but seemingly impossible to fix. It's like the Fermat's Last Theorem of emulation.

Except this one hasn't been solved (yet?).
Nixel says:
Aug 20th 2020
Correction, I meant arbitrary polygons. Quads have apparently been solved, according to that post commented earlier.
GoodWall533 says:
Aug 20th 2020
Will the emulator have a feature where you can choose the path (Like having the roms, bios and saved games in different folders and choose the path?) Sorry if i can't clarify, english isn't my first language.
hckmn says:
Aug 20th 2020
this is a cool solution
... says:
Aug 21st 2020

beepbop says:
Aug 23rd 2020
Could you do this instead? At least it sounds like a similar issue, but I'm probably wrong since I'm just a rando
Post a comment