Game Development Reference
In-Depth Information
Rendering directly to a texture
We have covered textures before. We even created an AssetManager that loads and
stores textures. However, the only thing that we have done with them so far has been to
place them on a sprite or a shape . As useful as that is, textures can also be used in dif-
ferent ways in graphics programming. That is typically done in shaders where we have a lot
of freedom to use every single texel (pixel of the texture) as we wish. We are going to talk
about using textures in shaders later on in the chapter. We will now explore a different way
of creating a texture—by directly rendering Drawable objects on it. In this case
RenderTexture provides the functionality that we need.
The RenderTexture class inherits from RenderTarget and implements all of its
drawing functionality using a framebuffer object (if it's available, otherwise it uses an al-
ternative method). A framebuffer object is an OpenGL object, which allows rendering to an
off-screen buffer. Using this technique, SFML is able to achieve seamless behavior so that
we are in fact rendering directly to a texture. The Texture object itself is held as a field
inside the RenderTexture class and we can get it by calling RenderTex-
ture::getTexture() .
In many cases it is useful to render on a texture and then do something with it. For ex-
ample, rendering the entire scene to a texture allows us to do post-processing effects with
shaders on it. In some cases we might want to create a more complex sprite texture for
some of our entities. Rendering multiple shapes or sprites per entity every frame can be-
come expensive, so RenderTexture allows us to improve performance by only render-
ing those once and using the resulting texture.
The way we can use a render texture is similar to RenderWindow . Firstly, we need to
create it with appropriate dimensions (width and height) and then draw on it with the famil-
iar routine:
Search WWH ::

Custom Search