开发者

How to Render images in shape/ polygon form?

Hey i'm working with SFML right now, and upon finishing the tutorials i still do not know how to give a shape a texture or image, and not just a solid color/outline.

The only thing i know can take an image is a sprite, but thats WAY to simple, as it only allows you to rend开发者_高级运维er rectangular images in a rectangluar way!

What are the tecniques for rendering images onto shapes, and ONLY inside the shape? It would be great if some of you could provide some resources or SFML-specific stuff!


SFML has been updated since this question was originally answered and you can now add textures to shapes easily. The shape class has the setTexture() and setTextureRect() methods. setTexture() takes a pointer to an sf::Texture. See the documentation.

Sf::Texture texture;
if (!texture.loadFromFile("mytexture.png"))
{
    std::cerr << "failed to load";
}
sf::RectangleShape myRect{ sf::Vector2f(width, height) };
myRect.setTexture(&texture);
myRect.setTextureRect(sf::IntRect( x, y, width, height ));


2d:

As far as I know, with SFML 1.6 it isn't possible to use a separate image as a mask. You can, however, use the alpha channel of an image to draw a "shape" from it. This is supported by default; just give your image an alpha channel.

This post on the SFML forms verifies that using a separate image as a mask is not supported.

The author of SFML, Laurent Gomila, has posted some code that will allow you to mask using shapes, sprites, and strings. That can be found here: Masking using Sprites, Shapes or Strings. To get this working, however, you need to modify and recompile some parts of SFML.

3d:

With regards to using an image as a texture and mapping it to an object in 3d space, I don't think SFML has too much to help you with this besides setting up a window for you. There is a project on Google Code called sf3d that could maybe get you in the right direction, though.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜