Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Is it possible to remove "transparent" background of sprite?  (Read 4705 times)

0 Members and 1 Guest are viewing this topic.

sadin97

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Is it possible to remove "transparent" background of sprite?
« on: September 23, 2017, 10:25:46 pm »
If this is a sprite:



Blue color is transparent (I just marked it as color to help me explain question).

How can I than make only selectable this tree. Is it possible?
Is it possible to just select everything within red border of the sprite?

Because, I have some sprites that I'm drawing behind and in front of and I can only detect the whole rectangle of sprite. Is there any solution for this?

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Is it possible to remove "transparent" background of sprite?
« Reply #1 on: September 23, 2017, 10:51:29 pm »
One possibility is keeping the image and finding out which pixel was clicked and checking if it's transparent or not.
Back to C++ gamedev with SFML in May 2023

AlexAUT

  • Sr. Member
  • ****
  • Posts: 396
    • View Profile
Re: Is it possible to remove "transparent" background of sprite?
« Reply #2 on: September 23, 2017, 10:58:53 pm »
(FRex his solution is a lot simpler ;D)

This can be done by using the GPU (little hack) or with some polygon point checking algorithm.

1) On the GPU, this is called "picking". You simply draw the tree onto another rendertarget (e.g. renderTexture). When you click you check the color of this position, if it is transparent you missed, if it is a non transparent color you hit the tree. This is easy if you have one element you want to check.
If you have multiple object this is a bit tricky, because you cannot create a renderTexture for every object, you would run out of GPU VRAM, or the multiple getPixelColor will slow down your game (Transfer from GPU to CPU bottleneck). Howerver there is a trick, you can draw with a custom shader where you set a unique color for all your objects. So if the alpha of the sprite/texture fragment is > 0 draw this color otherwise dont fill the pixel. Then you can draw every object with a different color onto one renderTexture get the color and match it to one(clicked) object.

2) On the CPU, for this you have to model the non transparent parts of the image as a polygon. There is also the option to create it automatically (google it, but it may be compilcated). If you have the polygon you have to check if your point is inside this polygon, for an example for such an algorithm look here.
« Last Edit: September 23, 2017, 11:00:38 pm by AlexAUT »

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Is it possible to remove "transparent" background of sprite?
« Reply #3 on: September 23, 2017, 11:17:13 pm »
Jesus, AlexAUT, these ideas...  ;D ;D
The first is too complex and too much work and will be slower and the second one is too complex to do for such a simple task and would require that 'modeling' work or creating some auto modeller (that'd actually be super fun and simple with right lib or two.. but still!).
Back to C++ gamedev with SFML in May 2023

AlexAUT

  • Sr. Member
  • ****
  • Posts: 396
    • View Profile
Re: Is it possible to remove "transparent" background of sprite?
« Reply #4 on: September 23, 2017, 11:38:06 pm »
@Frex: The first idea can be quite fast for 2D, by using the depth buffer and depth test settings to your advantage, so you wont have an extra draw call ;).

AlexAUT

sadin97

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: Is it possible to remove "transparent" background of sprite?
« Reply #5 on: September 23, 2017, 11:46:48 pm »
FRex solution is indeed simpler. In that case how could I check which pixel is clicked and more important is that pixel transparent or not? ::)

Thank you both for replying!

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Is it possible to remove "transparent" background of sprite?
« Reply #6 on: September 24, 2017, 12:12:34 am »
AlexAUT, it's still too complex and definitely slower than just a transform and pixel picking.
sadin97, here is the example: https://gist.github.com/FRex/31971e02a9e6d342a575d7ee128d74bf

We use an inverse transform because that's what we need, mapping from world coords to sprite rect, the sprite's transform maps its int rect into world coords, so we take inverse of that. The mapping from int pixel in window to float coords is also done with linear algebra and transforms (aka matrices).

This also doesn't handle texture rects and texture repeating/stretching but that'd just be a few extra ops before texture lookup. Doing that might be a good exercise for the reader. ;)
« Last Edit: September 24, 2017, 12:49:46 am by FRex »
Back to C++ gamedev with SFML in May 2023

 

anything