TL; DR:使用(py)sdl2,我试图绘制一个圆形轮廓,其内部是透明的,因此显示在其后面绘制的对象。基本上我需要找到一种方法来擦除实心圆的内部像素(或将它们绘制/设置为透明像素)。只要有可能,我想使用纹理而不是曲面。使用sdl2绘制轮廓圆
我想实现的东西看起来在概念上很简单,但我不能让它发生。
我想用python中的sdl2绘制圆形轮廓。当然,使用sdl2gfx函数circleRGBA()
可以非常简单地实现,但它只允许您绘制线宽为1px的轮廓线。画轮廓更粗的圆看起来不太可能。
我一直在使用透明色键(inspired by this method)做这样的事情之前pygame的面,我知道表面也可在SDL2,但告诫是,我想坚持到更快的纹理,它们不提供颜色键机制。
要更好一点视觉上解释的事情:我想要实现的是:
圆(环)大于1px的宽其内部是透明的,从而得出项目在圆圈后面将在内部可见。
过去我用过的一个技巧就是画出2个实心圆,第二个小圆与背景颜色相同。但是,这隐藏了圈内的所有东西(在这种情况下是白线)。下面是使用这种方法的函数的例子:
@to_texture
def draw_circle(self, x, y, r, color, opacity=1.0, fill=True, aa=False, penwidth=1):
# Make sure all spatial parameters are ints
x, y, r = int(x), int(y), int(r)
# convert color parameter to sdl2 understandable values
color = sdl2.ext.convert_to_color(color)
# convert possible opacity values to the right scale
opacity = self.opacity(opacity)
# Get background color set for this texture
bgcolor = self.__bgcolor
# Check for invalid penwidth values, and make sure the value is an int
if penwidth != 1:
if penwidth < 1:
raise ValueError("Penwidth cannot be smaller than 1")
if penwidth > 1:
penwidth = int(penwidth)
# if the circle needs to be filled, it's easy
if fill:
sdlgfx.filledCircleRGBA(self.sdl_renderer, x, y, r, color.r, color.g, color.b, opacity)
else:
# If penwidth is 1, simple use sdl2gfx own functions
if penwidth == 1:
if aa:
sdlgfx.aacircleRGBA(self.sdl_renderer, x, y, r, color.r, color.g, color.b, opacity)
else:
sdlgfx.circleRGBA(self.sdl_renderer, x, y, r, color.r, color.g, color.b, opacity)
else:
# If the penwidth is larger than 1, things become a bit more complex.
outer_r = int(r+penwidth*.5)
inner_r = int(r-penwidth*.5)
# Draw outer circle
sdlgfx.filledCircleRGBA(self.sdl_renderer, x, y,
outer_r, color.r, color.g, color.b, opacity)
# Draw inner circle
sdlgfx.filledCircleRGBA(self.sdl_renderer, x, y,
inner_r, bgcolor.r, bgcolor.g, bgcolor.b, 255)
return self
,导致:
我也试图与不同的混合模式玩耍,这是我能得到最好的结果:
@to_texture
def draw_circle(self, x, y, r, color, opacity=1.0, fill=True, aa=False, penwidth=1):
# ... omitted for brevity
else:
# If the penwidth is larger than 1, things become a bit more complex.
# To ensure that the interior of the circle is transparent, we will
# have to work with multiple textures and blending.
outer_r = int(r+penwidth*.5)
inner_r = int(r-penwidth*.5)
# Calculate the required dimensions of the separate texture we are
# going to draw the circle on. Add 1 pixel to account for division
# inaccuracies (i.e. dividing an odd number of pixels)
(c_width, c_height) = (outer_r*2+1, outer_r*2+1)
# Create the circle texture, and make sure it can be a rendering
# target by setting the correct access flag.
circle_texture = self.environment.texture_factory.create_sprite(
size=(c_width, c_height),
access=sdl2.SDL_TEXTUREACCESS_TARGET
)
# Set renderer target to the circle texture
if sdl2.SDL_SetRenderTarget(self.sdl_renderer, circle_texture.texture) != 0:
raise Exception("Could not set circle texture as rendering"
" target: {}".format(sdl2.SDL_GetError()))
# Draw the annulus:
# as the reference point is the circle center, outer_r
# can be used for the circles x and y coordinates.
sdlgfx.filledCircleRGBA(self.sdl_renderer, outer_r, outer_r,
outer_r, color.r, color.g, color.b, opacity)
# Draw the hole
sdlgfx.filledCircleRGBA(self.sdl_renderer, outer_r, outer_r,
inner_r, 0, 0, 0, 255)
# Set renderer target back to the FrameBuffer texture
if sdl2.SDL_SetRenderTarget(self.sdl_renderer, self.surface.texture) != 0:
raise Exception("Could not unset circle texture as rendering"
" target: {}".format(sdl2.SDL_GetError()))
# Use additive blending when blitting the circle texture on the main texture
sdl2.SDL_SetTextureBlendMode(circle_texture.texture, sdl2.SDL_BLENDMODE_ADD)
# Perform the blitting operation
self.renderer.copy(circle_texture, dstrect=(x-int(c_width/2),
y-int(c_height/2), c_width, c_height))
return self
导致:
关闭,但没有雪茄,作为线路现在看来是在圆的前而不是它的身后,而据我了解添加剂/屏相融,这是预期的行为。
我知道这里也有函数SDL_SetRenderDrawBlendMode,但sdl2gfx绘图函数似乎忽略了用这个函数设置的任何东西。
有没有人比我有更多的经验,他们之前做过这样的事情,谁能指导我如何应对这一挑战的正确方向?
谢谢!我认为你是对的,因为这是唯一的方法。你只是打败了我:我也以类似的方式解决了这个问题,并且也会在这里发布我的解决方案。尽管如此,你还是有一个更完整和全面的例子,值得被接受的答案。 –