moderngl-window
moderngl-window copied to clipboard
Examples have been flipped and have inverted color channels
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.
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"
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.
Swizzle seems like kinda a workaround, It would be much more elegant to just get the data the correct way the first time
I can tweak the pygame_simple.py
example a bit. Some more comments and embed the shader in the example as well.
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.
Updated the pygame example with a lot more information + emedded shaders and custom geometry. https://github.com/moderngl/moderngl-window/commit/32c83f0527760defc0049282e8c61e1065e8b2a3
Closing this for now. Please reopen if this didn't properly address the problem.