pythontexturessdl-2texture-mappingpysdl2

(Py)SDL2: Drawing a textured polygon


PySDL2 version: 0.9.3
SDL2 version: 2.0.3

I am trying to render this image as a texture on a polygon in PySDL2 using sdl_gfx

enter image description here

but its display is completely distorted, as can be seen at the bottom-right corner in the SDL window:

enter image description here

I have this python program in which I test all sdlgfx drawing functions that I implemented in a FrameBuffer class that takes care of the drawing and rendering. They all work well, except for an anti-aliased polygon (the middle green hexagon, but that is another question for another time) and the textured polygon.

To provide a more basic script, I followed these steps to draw a textured polygon:

# Initialize SDL2 and window
import sdl2
import sdl2.ext
import sdl2.sdlgfx
import ctypes
sdl2.ext.init()
window = sdl2.ext.Window(size=(800,600))
window.show()

# Create renderer and factories
renderer = sdl2.ext.Renderer(window)
renderer.clear(0)
renderer.present()
# Create sprite factory to create textures with later
texture_factory = sdl2.ext.SpriteFactory(renderer=renderer)
# Create sprite factory to create surfaces with later
surface_factory = sdl2.ext.SpriteFactory(sdl2.ext.SOFTWARE)

# Determine path to image to use as texture
RESOURCES = sdl2.ext.Resources(__file__, "LSD/resources")
image_path = RESOURCES.get_path("Memory.jpeg")

# set polygon coordinates
x = 100
row4 = 470
vx = [x, x+200, x+200, x]
vy = [row4-50, row4-50, row4+50, row4+50]

# Calculate the length of the vectors (which should be the same for x and y)
n = len(vx) 
# Make sure all coordinates are integers
vx = map(int,vx)
vy = map(int,vy)
# Cast the list to the appropriate ctypes vectors reabable by
# the sdlgfx polygon functions
vx = ctypes.cast((sdl2.Sint16*n)(*vx), ctypes.POINTER(sdl2.Sint16))
vy = ctypes.cast((sdl2.Sint16*n)(*vy), ctypes.POINTER(sdl2.Sint16))

# Load the image on an SoftwareSprite
# The underlying surface is available at SoftwareSprite.surface
texture = surface_factory.from_image(image_path)

## RENDER THE POLYGON WITH TEXTURE
sdl2.sdlgfx.texturedPolygon(renderer.renderer, vx, vy, n,\
texture.surface, 0, 0)

# Swap buffers
renderer.present()

# Handle window close events
processor = sdl2.ext.TestEventProcessor()
processor.run(window)

sdl2.ext.quit()

This above example script just outputs:

enter image description here

I find it all quite daunting to work with SDL2 with the ctype conversions and all, and I'm quite happy I got this far, but I don't seem to manage to solve this one on my own. Does anybody know at which step I am making a mistake or can anyone point me in the right direction?

Just as a sidenote, I know PySDL has factory functions for rendering images, and those work very well, but I really want to get the texturing option of polygons working too.


Solution

  • I found out that this is just a bug in the underlying sdl2_gfx library at C/DLL level. The homebrew version of sdl2_gfx is 1.0.0 while version 1.0.1 (jun 15 2014) is already released. I tested it on Windows and Ubuntu on which I had sdl2_gfx 1.0.1 available and the textured polygon is drawn correctly (although the usage of the offset parameters are still a bit shady to me).

    Bottom line: Don't use sdl2_gfx 1.0.0 if you want to use textured polygons, as it is just not functioning there. Try to get your hands on v1.0.1.