moderngl-window icon indicating copy to clipboard operation
moderngl-window copied to clipboard

Examples have been flipped and have inverted color channels

Open SoupySoups opened this issue 1 year ago • 1 comments

On the pygame examples in this repository the following method is used to convert from a pygame surface to a modernGL texture:

texture_data = self.pg_screen.get_view('1')

with this the outputted texture will be upside down and have red and blue color channels swapped. Use texture_data = pygame.image.tostring(self.pg_screen, "RGBA", True) to correctly copy the screen.

SoupySoups avatar Oct 01 '22 18:10 SoupySoups

What's the cost of image.tostring compared to just accessting the raw data? It's not really a problem that color channels are swapped or the image being upside down, but if image.tostring is equally fast it's worth swapping to.

(We can use texture swizzle to swap the channels in the sampler itself and tecture coordiantes can be flipped easily)

Swizzle

texture.swizzle = "BGRA"

einarf avatar Oct 01 '22 22:10 einarf

I mean, it wont kill the examples performance, but yes it is not as efficient. HOWEVER, that being said when writing my program, I used the example to figure out how to use moderngl with pygame. Rather than using slightly faster methods, in examples I think that we should always put the most correct and universal solution. It was very frustrating to find why my colors were not correct and if a newer programmer had come across this issue, I imagine it would be more so frustrating for them.

SoupySoups avatar Oct 15 '22 15:10 SoupySoups

Swizzle seems like kinda a workaround, It would be much more elegant to just get the data the correct way the first time

SoupySoups avatar Oct 15 '22 15:10 SoupySoups

I can tweak the pygame_simple.py example a bit. Some more comments and embed the shader in the example as well.

einarf avatar Oct 15 '22 15:10 einarf

Some quick profiling shows the cost of getting the surface data:

  • Calling pygame.image.tostring 100k times takes 11.2 seconds
  • Calling Surface.get_view 100k times takes 0.016 seconds

It means using get_view is around 700 times faster and this difference will increase the larger the surface is. This can be mentioned in the example.

By embedding the geometry and the shaders it should be much simpler to tinker with as well.

einarf avatar Oct 15 '22 16:10 einarf

Updated the pygame example with a lot more information + emedded shaders and custom geometry. https://github.com/moderngl/moderngl-window/commit/32c83f0527760defc0049282e8c61e1065e8b2a3

einarf avatar Oct 15 '22 18:10 einarf

Closing this for now. Please reopen if this didn't properly address the problem.

einarf avatar Oct 15 '22 18:10 einarf